1
14
15 package com.liferay.portlet.shopping.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.util.StringPool;
20 import com.liferay.portal.kernel.util.StringUtil;
21 import com.liferay.portal.kernel.util.Validator;
22 import com.liferay.portal.model.User;
23 import com.liferay.portal.service.ServiceContext;
24 import com.liferay.portal.util.PortalUtil;
25 import com.liferay.portlet.shopping.CouponCodeException;
26 import com.liferay.portlet.shopping.CouponDateException;
27 import com.liferay.portlet.shopping.CouponDescriptionException;
28 import com.liferay.portlet.shopping.CouponDiscountException;
29 import com.liferay.portlet.shopping.CouponEndDateException;
30 import com.liferay.portlet.shopping.CouponLimitCategoriesException;
31 import com.liferay.portlet.shopping.CouponLimitSKUsException;
32 import com.liferay.portlet.shopping.CouponMinimumOrderException;
33 import com.liferay.portlet.shopping.CouponNameException;
34 import com.liferay.portlet.shopping.CouponStartDateException;
35 import com.liferay.portlet.shopping.DuplicateCouponCodeException;
36 import com.liferay.portlet.shopping.NoSuchCouponException;
37 import com.liferay.portlet.shopping.model.ShoppingCategory;
38 import com.liferay.portlet.shopping.model.ShoppingCoupon;
39 import com.liferay.portlet.shopping.model.ShoppingItem;
40 import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
41 import com.liferay.util.PwdGenerator;
42
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.List;
46
47
54 public class ShoppingCouponLocalServiceImpl
55 extends ShoppingCouponLocalServiceBaseImpl {
56
57 public ShoppingCoupon addCoupon(
58 long userId, String code, boolean autoCode, String name,
59 String description, int startDateMonth, int startDateDay,
60 int startDateYear, int startDateHour, int startDateMinute,
61 int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
62 int endDateMinute, boolean neverExpire, boolean active,
63 String limitCategories, String limitSkus, double minOrder,
64 double discount, String discountType, ServiceContext serviceContext)
65 throws PortalException, SystemException {
66
67 User user = userPersistence.findByPrimaryKey(userId);
68 long groupId = serviceContext.getScopeGroupId();
69
70 code = code.trim().toUpperCase();
71
72 if (autoCode) {
73 code = getCode();
74 }
75
76 Date startDate = PortalUtil.getDate(
77 startDateMonth, startDateDay, startDateYear, startDateHour,
78 startDateMinute, user.getTimeZone(),
79 new CouponStartDateException());
80
81 Date endDate = null;
82
83 if (!neverExpire) {
84 endDate = PortalUtil.getDate(
85 endDateMonth, endDateDay, endDateYear, endDateHour,
86 endDateMinute, user.getTimeZone(),
87 new CouponEndDateException());
88 }
89
90 if ((endDate != null) && (startDate.after(endDate))) {
91 throw new CouponDateException();
92 }
93
94 Date now = new Date();
95
96 validate(
97 user.getCompanyId(), groupId, code, autoCode, name, description,
98 limitCategories, limitSkus, minOrder, discount);
99
100 long couponId = counterLocalService.increment();
101
102 ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
103
104 coupon.setGroupId(groupId);
105 coupon.setCompanyId(user.getCompanyId());
106 coupon.setUserId(user.getUserId());
107 coupon.setUserName(user.getFullName());
108 coupon.setCreateDate(now);
109 coupon.setModifiedDate(now);
110 coupon.setCode(code);
111 coupon.setName(name);
112 coupon.setDescription(description);
113 coupon.setStartDate(startDate);
114 coupon.setEndDate(endDate);
115 coupon.setActive(active);
116 coupon.setLimitCategories(limitCategories);
117 coupon.setLimitSkus(limitSkus);
118 coupon.setMinOrder(minOrder);
119 coupon.setDiscount(discount);
120 coupon.setDiscountType(discountType);
121
122 shoppingCouponPersistence.update(coupon, false);
123
124 return coupon;
125 }
126
127 public void deleteCoupon(long couponId)
128 throws PortalException, SystemException {
129
130 shoppingCouponPersistence.remove(couponId);
131 }
132
133 public void deleteCoupons(long groupId) throws SystemException {
134 shoppingCouponPersistence.removeByGroupId(groupId);
135 }
136
137 public ShoppingCoupon getCoupon(long couponId)
138 throws PortalException, SystemException {
139
140 return shoppingCouponPersistence.findByPrimaryKey(couponId);
141 }
142
143 public ShoppingCoupon getCoupon(String code)
144 throws PortalException, SystemException {
145
146 code = code.trim().toUpperCase();
147
148 return shoppingCouponPersistence.findByCode(code);
149 }
150
151 public List<ShoppingCoupon> search(
152 long groupId, long companyId, String code, boolean active,
153 String discountType, boolean andOperator, int start, int end)
154 throws SystemException {
155
156 return shoppingCouponFinder.findByG_C_C_A_DT(
157 groupId, companyId, code, active, discountType, andOperator,
158 start, end);
159 }
160
161 public int searchCount(
162 long groupId, long companyId, String code, boolean active,
163 String discountType, boolean andOperator)
164 throws SystemException {
165
166 return shoppingCouponFinder.countByG_C_C_A_DT(
167 groupId, companyId, code, active, discountType, andOperator);
168 }
169
170 public ShoppingCoupon updateCoupon(
171 long userId, long couponId, String name, String description,
172 int startDateMonth, int startDateDay, int startDateYear,
173 int startDateHour, int startDateMinute, int endDateMonth,
174 int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
175 boolean neverExpire, boolean active, String limitCategories,
176 String limitSkus, double minOrder, double discount,
177 String discountType, ServiceContext serviceContext)
178 throws PortalException, SystemException {
179
180 User user = userPersistence.findByPrimaryKey(userId);
181
182 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
183 couponId);
184
185 Date startDate = PortalUtil.getDate(
186 startDateMonth, startDateDay, startDateYear, startDateHour,
187 startDateMinute, user.getTimeZone(),
188 new CouponStartDateException());
189
190 Date endDate = null;
191
192 if (!neverExpire) {
193 endDate = PortalUtil.getDate(
194 endDateMonth, endDateDay, endDateYear, endDateHour,
195 endDateMinute, user.getTimeZone(),
196 new CouponEndDateException());
197 }
198
199 if ((endDate != null) && (startDate.after(endDate))) {
200 throw new CouponDateException();
201 }
202
203 validate(
204 coupon.getCompanyId(), coupon.getGroupId(), name, description,
205 limitCategories, limitSkus, minOrder, discount);
206
207 coupon.setModifiedDate(new Date());
208 coupon.setName(name);
209 coupon.setDescription(description);
210 coupon.setStartDate(startDate);
211 coupon.setEndDate(endDate);
212 coupon.setActive(active);
213 coupon.setLimitCategories(limitCategories);
214 coupon.setLimitSkus(limitSkus);
215 coupon.setMinOrder(minOrder);
216 coupon.setDiscount(discount);
217 coupon.setDiscountType(discountType);
218
219 shoppingCouponPersistence.update(coupon, false);
220
221 return coupon;
222 }
223
224 protected String getCode() throws SystemException {
225 String code =
226 PwdGenerator.getPassword(PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
227
228 try {
229 shoppingCouponPersistence.findByCode(code);
230
231 return getCode();
232 }
233 catch (NoSuchCouponException nsce) {
234 return code;
235 }
236 }
237
238 protected void validate(
239 long companyId, long groupId, String code, boolean autoCode,
240 String name, String description, String limitCategories,
241 String limitSkus, double minOrder, double discount)
242 throws PortalException, SystemException {
243
244 if (!autoCode) {
245 if ((Validator.isNull(code)) ||
246 (Validator.isNumber(code)) ||
247 (code.indexOf(StringPool.SPACE) != -1)) {
248
249 throw new CouponCodeException();
250 }
251
252 if (shoppingCouponPersistence.fetchByCode(code) != null) {
253 throw new DuplicateCouponCodeException();
254 }
255 }
256
257 validate(
258 companyId, groupId, name, description, limitCategories, limitSkus,
259 minOrder, discount);
260 }
261
262 protected void validate(
263 long companyId, long groupId, String name, String description,
264 String limitCategories, String limitSkus, double minOrder,
265 double discount)
266 throws PortalException, SystemException {
267
268 if (Validator.isNull(name)) {
269 throw new CouponNameException();
270 }
271 else if (Validator.isNull(description)) {
272 throw new CouponDescriptionException();
273 }
274
275
277 long[] categoryIds = StringUtil.split(limitCategories, 0L);
278
279 List<Long> invalidCategoryIds = new ArrayList<Long>();
280
281 for (long categoryId : categoryIds) {
282 ShoppingCategory category =
283 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
284
285 if ((category == null) || (category.getGroupId() != groupId)) {
286 invalidCategoryIds.add(categoryId);
287 }
288 }
289
290 if (invalidCategoryIds.size() > 0) {
291 CouponLimitCategoriesException clce =
292 new CouponLimitCategoriesException();
293
294 clce.setCategoryIds(invalidCategoryIds);
295
296 throw clce;
297 }
298
299
301 String[] skus = StringUtil.split(limitSkus);
302
303 List<String> invalidSkus = new ArrayList<String>();
304
305 for (String sku : skus) {
306 ShoppingItem item = shoppingItemPersistence.fetchByC_S(
307 companyId, sku);
308
309 if (item != null) {
310 ShoppingCategory category = item.getCategory();
311
312 if (category.getGroupId() != groupId) {
313 invalidSkus.add(sku);
314 }
315 }
316 else {
317 invalidSkus.add(sku);
318 }
319 }
320
321 if (invalidSkus.size() > 0) {
322 CouponLimitSKUsException clskue = new CouponLimitSKUsException();
323
324 clskue.setSkus(invalidSkus);
325
326 throw clskue;
327 }
328
329 if (minOrder < 0) {
330 throw new CouponMinimumOrderException();
331 }
332
333 if (discount < 0) {
334 throw new CouponDiscountException();
335 }
336 }
337
338 }