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