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.service.ServiceContext;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portlet.shopping.CouponCodeException;
34 import com.liferay.portlet.shopping.CouponDateException;
35 import com.liferay.portlet.shopping.CouponDescriptionException;
36 import com.liferay.portlet.shopping.CouponDiscountException;
37 import com.liferay.portlet.shopping.CouponEndDateException;
38 import com.liferay.portlet.shopping.CouponLimitCategoriesException;
39 import com.liferay.portlet.shopping.CouponLimitSKUsException;
40 import com.liferay.portlet.shopping.CouponMinimumOrderException;
41 import com.liferay.portlet.shopping.CouponNameException;
42 import com.liferay.portlet.shopping.CouponStartDateException;
43 import com.liferay.portlet.shopping.DuplicateCouponCodeException;
44 import com.liferay.portlet.shopping.NoSuchCouponException;
45 import com.liferay.portlet.shopping.model.ShoppingCategory;
46 import com.liferay.portlet.shopping.model.ShoppingCoupon;
47 import com.liferay.portlet.shopping.model.ShoppingItem;
48 import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
49 import com.liferay.util.PwdGenerator;
50
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.List;
54
55
63 public class ShoppingCouponLocalServiceImpl
64 extends ShoppingCouponLocalServiceBaseImpl {
65
66 public ShoppingCoupon addCoupon(
67 long userId, String code, boolean autoCode, String name,
68 String description, int startDateMonth, int startDateDay,
69 int startDateYear, int startDateHour, int startDateMinute,
70 int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
71 int endDateMinute, boolean neverExpire, boolean active,
72 String limitCategories, String limitSkus, double minOrder,
73 double discount, String discountType, ServiceContext serviceContext)
74 throws PortalException, SystemException {
75
76
78 User user = userPersistence.findByPrimaryKey(userId);
79 long groupId = serviceContext.getScopeGroupId();
80
81 code = code.trim().toUpperCase();
82
83 if (autoCode) {
84 code = getCode();
85 }
86
87 Date startDate = PortalUtil.getDate(
88 startDateMonth, startDateDay, startDateYear, startDateHour,
89 startDateMinute, user.getTimeZone(),
90 new CouponStartDateException());
91
92 Date endDate = null;
93
94 if (!neverExpire) {
95 endDate = PortalUtil.getDate(
96 endDateMonth, endDateDay, endDateYear, endDateHour,
97 endDateMinute, user.getTimeZone(),
98 new CouponEndDateException());
99 }
100
101 if ((endDate != null) && (startDate.after(endDate))) {
102 throw new CouponDateException();
103 }
104
105 Date now = new Date();
106
107 validate(
108 user.getCompanyId(), groupId, code, autoCode, name, description,
109 limitCategories, limitSkus, minOrder, discount);
110
111 long couponId = counterLocalService.increment();
112
113 ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
114
115 coupon.setGroupId(groupId);
116 coupon.setCompanyId(user.getCompanyId());
117 coupon.setUserId(user.getUserId());
118 coupon.setUserName(user.getFullName());
119 coupon.setCreateDate(now);
120 coupon.setModifiedDate(now);
121 coupon.setCode(code);
122 coupon.setName(name);
123 coupon.setDescription(description);
124 coupon.setStartDate(startDate);
125 coupon.setEndDate(endDate);
126 coupon.setActive(active);
127 coupon.setLimitCategories(limitCategories);
128 coupon.setLimitSkus(limitSkus);
129 coupon.setMinOrder(minOrder);
130 coupon.setDiscount(discount);
131 coupon.setDiscountType(discountType);
132
133 shoppingCouponPersistence.update(coupon, false);
134
135 return coupon;
136 }
137
138 public void deleteCoupon(long couponId)
139 throws PortalException, SystemException {
140
141 shoppingCouponPersistence.remove(couponId);
142 }
143
144 public void deleteCoupons(long groupId) throws SystemException {
145 shoppingCouponPersistence.removeByGroupId(groupId);
146 }
147
148 public ShoppingCoupon getCoupon(long couponId)
149 throws PortalException, SystemException {
150
151 return shoppingCouponPersistence.findByPrimaryKey(couponId);
152 }
153
154 public ShoppingCoupon getCoupon(String code)
155 throws PortalException, SystemException {
156
157 code = code.trim().toUpperCase();
158
159 return shoppingCouponPersistence.findByCode(code);
160 }
161
162 public List<ShoppingCoupon> search(
163 long groupId, long companyId, String code, boolean active,
164 String discountType, boolean andOperator, int start, int end)
165 throws SystemException {
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, ServiceContext serviceContext)
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 }