001
014
015 package com.liferay.portlet.shopping.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.CharPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.model.User;
023 import com.liferay.portal.service.ServiceContext;
024 import com.liferay.portal.util.PortalUtil;
025 import com.liferay.portlet.shopping.CouponCodeException;
026 import com.liferay.portlet.shopping.CouponDateException;
027 import com.liferay.portlet.shopping.CouponDescriptionException;
028 import com.liferay.portlet.shopping.CouponDiscountException;
029 import com.liferay.portlet.shopping.CouponEndDateException;
030 import com.liferay.portlet.shopping.CouponLimitCategoriesException;
031 import com.liferay.portlet.shopping.CouponLimitSKUsException;
032 import com.liferay.portlet.shopping.CouponMinimumOrderException;
033 import com.liferay.portlet.shopping.CouponNameException;
034 import com.liferay.portlet.shopping.CouponStartDateException;
035 import com.liferay.portlet.shopping.DuplicateCouponCodeException;
036 import com.liferay.portlet.shopping.model.ShoppingCategory;
037 import com.liferay.portlet.shopping.model.ShoppingCoupon;
038 import com.liferay.portlet.shopping.model.ShoppingItem;
039 import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
040 import com.liferay.util.PwdGenerator;
041
042 import java.util.ArrayList;
043 import java.util.Date;
044 import java.util.List;
045
046
050 public class ShoppingCouponLocalServiceImpl
051 extends ShoppingCouponLocalServiceBaseImpl {
052
053 @Override
054 public ShoppingCoupon addCoupon(
055 long userId, String code, boolean autoCode, String name,
056 String description, int startDateMonth, int startDateDay,
057 int startDateYear, int startDateHour, int startDateMinute,
058 int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
059 int endDateMinute, boolean neverExpire, boolean active,
060 String limitCategories, String limitSkus, double minOrder,
061 double discount, String discountType, ServiceContext serviceContext)
062 throws PortalException, SystemException {
063
064 User user = userPersistence.findByPrimaryKey(userId);
065 long groupId = serviceContext.getScopeGroupId();
066
067 code = StringUtil.toUpperCase(code.trim());
068
069 if (autoCode) {
070 code = getCode();
071 }
072
073 Date startDate = PortalUtil.getDate(
074 startDateMonth, startDateDay, startDateYear, startDateHour,
075 startDateMinute, user.getTimeZone(),
076 CouponStartDateException.class);
077
078 Date endDate = null;
079
080 if (!neverExpire) {
081 endDate = PortalUtil.getDate(
082 endDateMonth, endDateDay, endDateYear, endDateHour,
083 endDateMinute, user.getTimeZone(),
084 CouponEndDateException.class);
085 }
086
087 if ((endDate != null) && startDate.after(endDate)) {
088 throw new CouponDateException();
089 }
090
091 Date now = new Date();
092
093 validate(
094 user.getCompanyId(), groupId, code, autoCode, name, description,
095 limitCategories, limitSkus, minOrder, discount);
096
097 long couponId = counterLocalService.increment();
098
099 ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
100
101 coupon.setGroupId(groupId);
102 coupon.setCompanyId(user.getCompanyId());
103 coupon.setUserId(user.getUserId());
104 coupon.setUserName(user.getFullName());
105 coupon.setCreateDate(now);
106 coupon.setModifiedDate(now);
107 coupon.setCode(code);
108 coupon.setName(name);
109 coupon.setDescription(description);
110 coupon.setStartDate(startDate);
111 coupon.setEndDate(endDate);
112 coupon.setActive(active);
113 coupon.setLimitCategories(limitCategories);
114 coupon.setLimitSkus(limitSkus);
115 coupon.setMinOrder(minOrder);
116 coupon.setDiscount(discount);
117 coupon.setDiscountType(discountType);
118
119 shoppingCouponPersistence.update(coupon);
120
121 return coupon;
122 }
123
124 @Override
125 public void deleteCoupon(long couponId)
126 throws PortalException, SystemException {
127
128 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
129 couponId);
130
131 deleteCoupon(coupon);
132 }
133
134 @Override
135 public void deleteCoupon(ShoppingCoupon coupon) throws SystemException {
136 shoppingCouponPersistence.remove(coupon);
137 }
138
139 @Override
140 public void deleteCoupons(long groupId) throws SystemException {
141 List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
142 groupId);
143
144 for (ShoppingCoupon coupon : coupons) {
145 deleteCoupon(coupon);
146 }
147 }
148
149 @Override
150 public ShoppingCoupon getCoupon(long couponId)
151 throws PortalException, SystemException {
152
153 return shoppingCouponPersistence.findByPrimaryKey(couponId);
154 }
155
156 @Override
157 public ShoppingCoupon getCoupon(String code)
158 throws PortalException, SystemException {
159
160 code = StringUtil.toUpperCase(code.trim());
161
162 return shoppingCouponPersistence.findByCode(code);
163 }
164
165 @Override
166 public List<ShoppingCoupon> search(
167 long groupId, long companyId, String code, boolean active,
168 String discountType, boolean andOperator, int start, int end)
169 throws SystemException {
170
171 return shoppingCouponFinder.findByG_C_C_A_DT(
172 groupId, companyId, code, active, discountType, andOperator, start,
173 end);
174 }
175
176 @Override
177 public int searchCount(
178 long groupId, long companyId, String code, boolean active,
179 String discountType, boolean andOperator)
180 throws SystemException {
181
182 return shoppingCouponFinder.countByG_C_C_A_DT(
183 groupId, companyId, code, active, discountType, andOperator);
184 }
185
186 @Override
187 public ShoppingCoupon updateCoupon(
188 long userId, long couponId, String name, String description,
189 int startDateMonth, int startDateDay, int startDateYear,
190 int startDateHour, int startDateMinute, int endDateMonth,
191 int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
192 boolean neverExpire, boolean active, String limitCategories,
193 String limitSkus, double minOrder, double discount,
194 String discountType, ServiceContext serviceContext)
195 throws PortalException, SystemException {
196
197 User user = userPersistence.findByPrimaryKey(userId);
198
199 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
200 couponId);
201
202 Date startDate = PortalUtil.getDate(
203 startDateMonth, startDateDay, startDateYear, startDateHour,
204 startDateMinute, user.getTimeZone(),
205 CouponStartDateException.class);
206
207 Date endDate = null;
208
209 if (!neverExpire) {
210 endDate = PortalUtil.getDate(
211 endDateMonth, endDateDay, endDateYear, endDateHour,
212 endDateMinute, user.getTimeZone(),
213 CouponEndDateException.class);
214 }
215
216 if ((endDate != null) && startDate.after(endDate)) {
217 throw new CouponDateException();
218 }
219
220 validate(
221 coupon.getCompanyId(), coupon.getGroupId(), name, description,
222 limitCategories, limitSkus, minOrder, discount);
223
224 coupon.setModifiedDate(new Date());
225 coupon.setName(name);
226 coupon.setDescription(description);
227 coupon.setStartDate(startDate);
228 coupon.setEndDate(endDate);
229 coupon.setActive(active);
230 coupon.setLimitCategories(limitCategories);
231 coupon.setLimitSkus(limitSkus);
232 coupon.setMinOrder(minOrder);
233 coupon.setDiscount(discount);
234 coupon.setDiscountType(discountType);
235
236 shoppingCouponPersistence.update(coupon);
237
238 return coupon;
239 }
240
241 protected String getCode() throws SystemException {
242 String code = PwdGenerator.getPassword(
243 8, PwdGenerator.KEY1, PwdGenerator.KEY2);
244
245 ShoppingCoupon coupon = shoppingCouponPersistence.fetchByCode(code);
246
247 if (coupon != null) {
248 return coupon.getCode();
249 }
250
251 return code;
252 }
253
254 protected void validate(
255 long companyId, long groupId, String code, boolean autoCode,
256 String name, String description, String limitCategories,
257 String limitSkus, double minOrder, double discount)
258 throws PortalException, SystemException {
259
260 if (!autoCode) {
261 if (Validator.isNull(code) || Validator.isNumber(code) ||
262 (code.indexOf(CharPool.COMMA) != -1) ||
263 (code.indexOf(CharPool.SPACE) != -1)) {
264
265 throw new CouponCodeException();
266 }
267
268 if (shoppingCouponPersistence.fetchByCode(code) != null) {
269 throw new DuplicateCouponCodeException("{code=" + code + "}");
270 }
271 }
272
273 validate(
274 companyId, groupId, name, description, limitCategories, limitSkus,
275 minOrder, discount);
276 }
277
278 protected void validate(
279 long companyId, long groupId, String name, String description,
280 String limitCategories, String limitSkus, double minOrder,
281 double discount)
282 throws PortalException, SystemException {
283
284 if (Validator.isNull(name)) {
285 throw new CouponNameException();
286 }
287 else if (Validator.isNull(description)) {
288 throw new CouponDescriptionException();
289 }
290
291
292
293 List<Long> categoryIds = new ArrayList<Long>();
294
295 String[] categoryNames = StringUtil.split(limitCategories);
296
297 for (String categoryName : categoryNames) {
298 ShoppingCategory category = shoppingCategoryPersistence.fetchByG_N(
299 groupId, categoryName);
300
301 categoryIds.add(category.getCategoryId());
302 }
303
304 List<Long> invalidCategoryIds = new ArrayList<Long>();
305
306 for (long categoryId : categoryIds) {
307 ShoppingCategory category =
308 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
309
310 if ((category == null) || (category.getGroupId() != groupId)) {
311 invalidCategoryIds.add(categoryId);
312 }
313 }
314
315 if (invalidCategoryIds.size() > 0) {
316 CouponLimitCategoriesException clce =
317 new CouponLimitCategoriesException();
318
319 clce.setCategoryIds(invalidCategoryIds);
320
321 throw clce;
322 }
323
324
325
326 String[] skus = StringUtil.split(limitSkus);
327
328 List<String> invalidSkus = new ArrayList<String>();
329
330 for (String sku : skus) {
331 ShoppingItem item = shoppingItemPersistence.fetchByC_S(
332 companyId, sku);
333
334 if (item != null) {
335 ShoppingCategory category = item.getCategory();
336
337 if (category.getGroupId() != groupId) {
338 invalidSkus.add(sku);
339 }
340 }
341 else {
342 invalidSkus.add(sku);
343 }
344 }
345
346 if (invalidSkus.size() > 0) {
347 CouponLimitSKUsException clskue = new CouponLimitSKUsException();
348
349 clskue.setSkus(invalidSkus);
350
351 throw clskue;
352 }
353
354 if (minOrder < 0) {
355 throw new CouponMinimumOrderException();
356 }
357
358 if (discount < 0) {
359 throw new CouponDiscountException();
360 }
361 }
362
363 }