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