KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > discountcode > factories > DiscountCodeFactory


1 package com.dotmarketing.portlets.discountcode.factories;
2
3 import java.util.Date JavaDoc;
4 import java.util.List JavaDoc;
5
6 import com.dotmarketing.db.DotHibernate;
7 import com.dotmarketing.factories.InodeFactory;
8 import com.dotmarketing.util.Config;
9 import com.dotmarketing.util.Logger;
10 import com.dotmarketing.util.UtilMethods;
11 import com.dotmarketing.util.WebKeys;
12 import com.dotmarketing.cms.product.model.Holder;
13 import com.dotmarketing.portlets.discountcode.model.DiscountCode;
14 import com.dotmarketing.portlets.order_manager.model.OrderItem;
15 import com.dotmarketing.portlets.order_manager.struts.OrderItemForm;
16 import com.dotmarketing.portlets.product.factories.ProductFormatFactory;
17 import com.dotmarketing.portlets.product.model.ProductFormat;
18 import com.dotmarketing.portlets.product.model.ProductPrice;
19 /**
20  *
21  * @author david
22  */

23 public class DiscountCodeFactory {
24
25     public static DiscountCode newInstance() {
26         DiscountCode dc = new DiscountCode();
27         return dc;
28     }
29
30     public static void saveDiscountCode(DiscountCode dc) {
31         InodeFactory.saveInode(dc);
32     }
33
34     public static void deleteDiscountCode(DiscountCode dc) {
35         InodeFactory.deleteInode(dc);
36     }
37
38     public static DiscountCode getDiscountCode(String JavaDoc inode) {
39         return (DiscountCode) InodeFactory.getInode(inode,DiscountCode.class);
40     }
41
42     public static DiscountCode getDiscountCode(long inode) {
43         return (DiscountCode) InodeFactory.getInode(inode,DiscountCode.class);
44     }
45
46     public static DiscountCode getDiscountCodeById(String JavaDoc id) {
47         return (DiscountCode) InodeFactory.getInodeOfClassByCondition(DiscountCode.class, "code_id = '" + id + "'");
48     }
49     
50     public static List JavaDoc searchDiscountCode(int discountType, Date JavaDoc startDate, Date JavaDoc endDate, String JavaDoc codeId, String JavaDoc desc, String JavaDoc orderBy, String JavaDoc direction, int limit) {
51         
52         String JavaDoc sql = "from inode in class com.dotmarketing.portlets.discountcode.model.DiscountCode where 1 = 1 ";
53         if (UtilMethods.isSet(startDate))
54             sql += "and start_date >= ? ";
55         if (UtilMethods.isSet(endDate))
56             sql += "and end_date <= ? ";
57         if (UtilMethods.isSet(codeId))
58         {
59             codeId = "%"+codeId+"%";
60             sql += "and code_id like ? ";
61         }
62         if (UtilMethods.isSet(desc))
63         {
64             desc = "%"+desc+"%";
65             sql += "and code_description like ? ";
66         }
67         if (discountType != 0)
68             sql += "and discount_type = ? ";
69         if (UtilMethods.isSet(orderBy))
70             sql += "order by " + orderBy;
71         else
72             sql += "order by start_date";
73         if (UtilMethods.isSet(direction))
74             sql += " " + direction;
75
76         DotHibernate dh = new DotHibernate ();
77         dh.setQuery(sql);
78         if (UtilMethods.isSet(startDate))
79             dh.setParam(startDate);
80         if (UtilMethods.isSet(endDate))
81             dh.setParam(endDate);
82         if (UtilMethods.isSet(codeId))
83             dh.setParam(codeId);
84         if (UtilMethods.isSet(desc))
85             dh.setParam(desc);
86         if (discountType != 0)
87             dh.setParam(discountType);
88         if (limit > 0)
89             dh.setMaxResults(limit);
90         return dh.list();
91     }
92     
93     public static float getTotalApplicableDiscount(List JavaDoc<Holder> holders,List JavaDoc<DiscountCode> discounts,boolean partner)
94     {
95         float totalDiscount = 0;
96         try
97         {
98         float[] amountDiscounts = getApplicableDiscount(holders,discounts,partner);
99         for(int i = 0;i < amountDiscounts.length;i++)
100         {
101             totalDiscount += amountDiscounts[i];
102         }
103         totalDiscount = ((float) Math.round(totalDiscount * 100)) / ((float) 100);
104         }
105         catch(Exception JavaDoc ex)
106         {
107             Logger.debug(DiscountCodeFactory.class,ex.toString());
108         }
109         return totalDiscount;
110     }
111     
112     public static float[] getApplicableDiscount(List JavaDoc<Holder> holders,List JavaDoc<DiscountCode> discounts,boolean partner)
113     {
114         Date JavaDoc now = new Date JavaDoc();
115         float[] amountDiscounts = new float[discounts.size()];
116         
117         for(int i = 0;i < discounts.size();i++)
118         {
119             int totalQuantity = 0;
120             float totalPrice = 0;
121             DiscountCode discount = discounts.get(i);
122             for(int j = 0;j < holders.size();j++)
123             {
124                 Holder holder = holders.get(j);
125                 if(_potentialDiscount(holder,discount))
126                 {
127                     int quantity = holder.getQuantity();
128                     ProductFormat format = holder.getFormat();
129                     
130                     totalQuantity += quantity;
131                     
132                     ProductPrice productPrice = null;
133                     
134                     if (discount.getNoBulkDisc())
135                     {
136                         productPrice = format.getQuantityPrice(1);
137                     }
138                     else
139                     {
140                         productPrice = format.getQuantityPrice(quantity);
141                     }
142                     float price = (partner ? productPrice.getPartnerPrice() : productPrice.getRetailPrice());
143                     
144                     if(holder.getInode() != 0)
145                     {
146                         price = holder.getPrice();
147                     }
148                     totalPrice += quantity * price;
149                 }
150             }
151             if(_applyDiscount(totalQuantity,discount,now))
152             {
153                 amountDiscounts[i] = _calculateDiscount(totalPrice,discount,holders);
154             }
155         }
156         return amountDiscounts;
157     }
158     
159     public static boolean _potentialDiscount(Holder holder,DiscountCode discount)
160     {
161         boolean potential = false;
162         ProductFormat format = holder.getFormat();
163         List JavaDoc<ProductFormat> applicableFormat = discount.getProductFormatApplicable();
164         if (!(applicableFormat.size() == 0))
165         {
166             for(ProductFormat auxFormat : applicableFormat)
167             {
168                 if (auxFormat.getInode() == format.getInode())
169                 {
170                     potential = true;
171                     break;
172                 }
173             }
174         }else
175         {
176             potential = true;
177         }
178         return potential;
179     }
180     
181     public static boolean _potentialDiscount(ProductFormat format,DiscountCode discount)
182     {
183         boolean potential = false;
184         List JavaDoc<ProductFormat> applicableFormat = discount.getProductFormatApplicable();
185         if (!(applicableFormat.size() == 0))
186         {
187             for(ProductFormat auxFormat : applicableFormat)
188             {
189                 if (auxFormat.getInode() == format.getInode())
190                 {
191                     potential = true;
192                     break;
193                 }
194             }
195         }else
196         {
197             potential = true;
198         }
199         return potential;
200     }
201     public static boolean _potentialDiscount(OrderItem orderItem,DiscountCode discount)
202     {
203         boolean potential = false;
204         ProductFormat format = ProductFormatFactory.getProductFormat(orderItem.getProductInode());
205         List JavaDoc<ProductFormat> applicableFormat = discount.getProductFormatApplicable();
206         if (!(applicableFormat.size() == 0))
207         {
208             for(ProductFormat auxFormat : applicableFormat)
209             {
210                 if (auxFormat.getInode() == format.getInode())
211                 {
212                     potential = true;
213                     break;
214                 }
215             }
216         }else
217         {
218             potential = true;
219         }
220         return potential;
221     }
222     
223     public static boolean _potentialDiscount(OrderItemForm orderItemForm,DiscountCode discount)
224     {
225         boolean potential = false;
226         ProductFormat format = ProductFormatFactory.getProductFormat(orderItemForm.getProductInode());
227         List JavaDoc<ProductFormat> applicableFormat = discount.getProductFormatApplicable();
228         if (!(applicableFormat.size() == 0))
229         {
230             for(ProductFormat auxFormat : applicableFormat)
231             {
232                 if (auxFormat.getInode() == format.getInode())
233                 {
234                     potential = true;
235                     break;
236                 }
237             }
238         }else
239         {
240             potential = true;
241         }
242         return potential;
243     }
244     
245     private static int _totalPotentialDiscount(Holder holder,DiscountCode discount)
246     {
247         int potential = 0;
248         ProductFormat format = holder.getFormat();
249         List JavaDoc<ProductFormat> applicableFormat = discount.getProductFormatApplicable();
250         if (!(applicableFormat.size() == 0))
251         {
252             for(ProductFormat auxFormat : applicableFormat)
253             {
254                 if (auxFormat.getInode() == format.getInode())
255                 {
256                     potential = holder.getQuantity();
257                     break;
258                 }
259             }
260         }
261         return potential;
262     }
263     
264     public static boolean _applyDiscount(int quantity,DiscountCode discount,Date JavaDoc now)
265     {
266         boolean apply = true;
267         if ((discount.getStartDate() != null && now.compareTo(discount.getStartDate()) < 0) ||
268             (discount.getEndDate() != null && now.compareTo(discount.getEndDate()) > 0))
269         {
270             apply = false;
271         }
272         if(quantity < discount.getMinOrder())
273         {
274             apply = false;
275         }
276         return apply;
277     }
278     
279     private static float _calculateDiscount(float totalPrice,DiscountCode discount,List JavaDoc<Holder> holders)
280     {
281         float discountAmount = 0;
282         if (discount.getDiscountType() == Integer.parseInt(WebKeys.DISCOUNTCODE_DISCOUNT))
283         {
284             int totalItems = 0;
285             boolean discountByItem = false;
286             try
287             {
288                 discountByItem = Config.getBooleanProperty("DISCOUNT_BY_ITEM");
289             }
290             catch(Exception JavaDoc ex)
291             {
292             }
293             if (discountByItem)
294             {
295                 for(Holder holder : holders)
296                 {
297                     totalItems += _totalPotentialDiscount(holder,discount);
298                 }
299             }
300             else
301             {
302                 totalItems = 1;
303             }
304             discountAmount = totalItems * discount.getDiscountAmount();
305         }
306         else if(discount.getDiscountType() == Integer.parseInt(WebKeys.DISCOUNTCODE_PERCENTAGE))
307         {
308             discountAmount = (totalPrice) * discount.getDiscountAmount() / 100F;
309         }
310         return discountAmount;
311     }
312 }
313
Popular Tags