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