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 = code.trim().toUpperCase();
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 = code.trim().toUpperCase();
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.SPACE) != -1)) {
263
264 throw new CouponCodeException();
265 }
266
267 if (shoppingCouponPersistence.fetchByCode(code) != null) {
268 throw new DuplicateCouponCodeException();
269 }
270 }
271
272 validate(
273 companyId, groupId, name, description, limitCategories, limitSkus,
274 minOrder, discount);
275 }
276
277 protected void validate(
278 long companyId, long groupId, String name, String description,
279 String limitCategories, String limitSkus, double minOrder,
280 double discount)
281 throws PortalException, SystemException {
282
283 if (Validator.isNull(name)) {
284 throw new CouponNameException();
285 }
286 else if (Validator.isNull(description)) {
287 throw new CouponDescriptionException();
288 }
289
290
291
292 long[] categoryIds = StringUtil.split(limitCategories, 0L);
293
294 List<Long> invalidCategoryIds = new ArrayList<Long>();
295
296 for (long categoryId : categoryIds) {
297 ShoppingCategory category =
298 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
299
300 if ((category == null) || (category.getGroupId() != groupId)) {
301 invalidCategoryIds.add(categoryId);
302 }
303 }
304
305 if (invalidCategoryIds.size() > 0) {
306 CouponLimitCategoriesException clce =
307 new CouponLimitCategoriesException();
308
309 clce.setCategoryIds(invalidCategoryIds);
310
311 throw clce;
312 }
313
314
315
316 String[] skus = StringUtil.split(limitSkus);
317
318 List<String> invalidSkus = new ArrayList<String>();
319
320 for (String sku : skus) {
321 ShoppingItem item = shoppingItemPersistence.fetchByC_S(
322 companyId, sku);
323
324 if (item != null) {
325 ShoppingCategory category = item.getCategory();
326
327 if (category.getGroupId() != groupId) {
328 invalidSkus.add(sku);
329 }
330 }
331 else {
332 invalidSkus.add(sku);
333 }
334 }
335
336 if (invalidSkus.size() > 0) {
337 CouponLimitSKUsException clskue = new CouponLimitSKUsException();
338
339 clskue.setSkus(invalidSkus);
340
341 throw clskue;
342 }
343
344 if (minOrder < 0) {
345 throw new CouponMinimumOrderException();
346 }
347
348 if (discount < 0) {
349 throw new CouponDiscountException();
350 }
351 }
352
353 }