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.util.CharPool;
019 import com.liferay.portal.kernel.util.PwdGenerator;
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
041 import java.util.ArrayList;
042 import java.util.Date;
043 import java.util.List;
044
045
049 public class ShoppingCouponLocalServiceImpl
050 extends ShoppingCouponLocalServiceBaseImpl {
051
052 @Override
053 public ShoppingCoupon addCoupon(
054 long userId, String code, boolean autoCode, String name,
055 String description, int startDateMonth, int startDateDay,
056 int startDateYear, int startDateHour, int startDateMinute,
057 int endDateMonth, int endDateDay, int endDateYear, int endDateHour,
058 int endDateMinute, boolean neverExpire, boolean active,
059 String limitCategories, String limitSkus, double minOrder,
060 double discount, String discountType, ServiceContext serviceContext)
061 throws PortalException {
062
063 User user = userPersistence.findByPrimaryKey(userId);
064 long groupId = serviceContext.getScopeGroupId();
065
066 code = StringUtil.toUpperCase(code.trim());
067
068 if (autoCode) {
069 code = getCode();
070 }
071
072 Date startDate = PortalUtil.getDate(
073 startDateMonth, startDateDay, startDateYear, startDateHour,
074 startDateMinute, user.getTimeZone(),
075 CouponStartDateException.class);
076
077 Date endDate = null;
078
079 if (!neverExpire) {
080 endDate = PortalUtil.getDate(
081 endDateMonth, endDateDay, endDateYear, endDateHour,
082 endDateMinute, user.getTimeZone(),
083 CouponEndDateException.class);
084 }
085
086 if ((endDate != null) && startDate.after(endDate)) {
087 throw new CouponDateException();
088 }
089
090 validate(
091 user.getCompanyId(), groupId, code, autoCode, name, description,
092 limitCategories, limitSkus, minOrder, discount);
093
094 long couponId = counterLocalService.increment();
095
096 ShoppingCoupon coupon = shoppingCouponPersistence.create(couponId);
097
098 coupon.setGroupId(groupId);
099 coupon.setCompanyId(user.getCompanyId());
100 coupon.setUserId(user.getUserId());
101 coupon.setUserName(user.getFullName());
102 coupon.setCode(code);
103 coupon.setName(name);
104 coupon.setDescription(description);
105 coupon.setStartDate(startDate);
106 coupon.setEndDate(endDate);
107 coupon.setActive(active);
108 coupon.setLimitCategories(limitCategories);
109 coupon.setLimitSkus(limitSkus);
110 coupon.setMinOrder(minOrder);
111 coupon.setDiscount(discount);
112 coupon.setDiscountType(discountType);
113
114 shoppingCouponPersistence.update(coupon);
115
116 return coupon;
117 }
118
119 @Override
120 public void deleteCoupon(long couponId) throws PortalException {
121 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
122 couponId);
123
124 deleteCoupon(coupon);
125 }
126
127 @Override
128 public void deleteCoupon(ShoppingCoupon coupon) {
129 shoppingCouponPersistence.remove(coupon);
130 }
131
132 @Override
133 public void deleteCoupons(long groupId) {
134 List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
135 groupId);
136
137 for (ShoppingCoupon coupon : coupons) {
138 deleteCoupon(coupon);
139 }
140 }
141
142 @Override
143 public ShoppingCoupon getCoupon(long couponId) throws PortalException {
144 return shoppingCouponPersistence.findByPrimaryKey(couponId);
145 }
146
147 @Override
148 public ShoppingCoupon getCoupon(String code) throws PortalException {
149 code = StringUtil.toUpperCase(code.trim());
150
151 return shoppingCouponPersistence.findByCode(code);
152 }
153
154 @Override
155 public List<ShoppingCoupon> search(
156 long groupId, long companyId, String code, boolean active,
157 String discountType, boolean andOperator, int start, int end) {
158
159 return shoppingCouponFinder.findByG_C_C_A_DT(
160 groupId, companyId, code, active, discountType, andOperator, start,
161 end);
162 }
163
164 @Override
165 public int searchCount(
166 long groupId, long companyId, String code, boolean active,
167 String discountType, boolean andOperator) {
168
169 return shoppingCouponFinder.countByG_C_C_A_DT(
170 groupId, companyId, code, active, discountType, andOperator);
171 }
172
173 @Override
174 public ShoppingCoupon updateCoupon(
175 long userId, long couponId, String name, String description,
176 int startDateMonth, int startDateDay, int startDateYear,
177 int startDateHour, int startDateMinute, int endDateMonth,
178 int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
179 boolean neverExpire, boolean active, String limitCategories,
180 String limitSkus, double minOrder, double discount,
181 String discountType, ServiceContext serviceContext)
182 throws PortalException {
183
184 User user = userPersistence.findByPrimaryKey(userId);
185
186 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
187 couponId);
188
189 Date startDate = PortalUtil.getDate(
190 startDateMonth, startDateDay, startDateYear, startDateHour,
191 startDateMinute, user.getTimeZone(),
192 CouponStartDateException.class);
193
194 Date endDate = null;
195
196 if (!neverExpire) {
197 endDate = PortalUtil.getDate(
198 endDateMonth, endDateDay, endDateYear, endDateHour,
199 endDateMinute, user.getTimeZone(),
200 CouponEndDateException.class);
201 }
202
203 if ((endDate != null) && startDate.after(endDate)) {
204 throw new CouponDateException();
205 }
206
207 validate(
208 coupon.getCompanyId(), coupon.getGroupId(), name, description,
209 limitCategories, limitSkus, minOrder, discount);
210
211 coupon.setName(name);
212 coupon.setDescription(description);
213 coupon.setStartDate(startDate);
214 coupon.setEndDate(endDate);
215 coupon.setActive(active);
216 coupon.setLimitCategories(limitCategories);
217 coupon.setLimitSkus(limitSkus);
218 coupon.setMinOrder(minOrder);
219 coupon.setDiscount(discount);
220 coupon.setDiscountType(discountType);
221
222 shoppingCouponPersistence.update(coupon);
223
224 return coupon;
225 }
226
227 protected String getCode() {
228 String code = PwdGenerator.getPassword(
229 8, PwdGenerator.KEY1, PwdGenerator.KEY2);
230
231 ShoppingCoupon coupon = shoppingCouponPersistence.fetchByCode(code);
232
233 if (coupon != null) {
234 return coupon.getCode();
235 }
236
237 return code;
238 }
239
240 protected void validate(
241 long companyId, long groupId, String code, boolean autoCode,
242 String name, String description, String limitCategories,
243 String limitSkus, double minOrder, double discount)
244 throws PortalException {
245
246 if (!autoCode) {
247 if (Validator.isNull(code) || Validator.isNumber(code) ||
248 (code.indexOf(CharPool.COMMA) != -1) ||
249 (code.indexOf(CharPool.SPACE) != -1)) {
250
251 throw new CouponCodeException();
252 }
253
254 if (shoppingCouponPersistence.fetchByCode(code) != null) {
255 throw new DuplicateCouponCodeException("{code=" + code + "}");
256 }
257 }
258
259 validate(
260 companyId, groupId, name, description, limitCategories, limitSkus,
261 minOrder, discount);
262 }
263
264 protected void validate(
265 long companyId, long groupId, String name, String description,
266 String limitCategories, String limitSkus, double minOrder,
267 double discount)
268 throws PortalException {
269
270 if (Validator.isNull(name)) {
271 throw new CouponNameException();
272 }
273 else if (Validator.isNull(description)) {
274 throw new CouponDescriptionException();
275 }
276
277
278
279 List<Long> categoryIds = new ArrayList<>();
280
281 String[] categoryNames = StringUtil.split(limitCategories);
282
283 for (String categoryName : categoryNames) {
284 ShoppingCategory category = shoppingCategoryPersistence.fetchByG_N(
285 groupId, categoryName);
286
287 categoryIds.add(category.getCategoryId());
288 }
289
290 List<Long> invalidCategoryIds = new ArrayList<>();
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.isEmpty()) {
302 CouponLimitCategoriesException clce =
303 new CouponLimitCategoriesException();
304
305 clce.setCategoryIds(invalidCategoryIds);
306
307 throw clce;
308 }
309
310
311
312 String[] skus = StringUtil.split(limitSkus);
313
314 List<String> invalidSkus = new ArrayList<>();
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.isEmpty()) {
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 }