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.NoSuchCouponException;
037 import com.liferay.portlet.shopping.model.ShoppingCategory;
038 import com.liferay.portlet.shopping.model.ShoppingCoupon;
039 import com.liferay.portlet.shopping.model.ShoppingItem;
040 import com.liferay.portlet.shopping.service.base.ShoppingCouponLocalServiceBaseImpl;
041 import com.liferay.util.PwdGenerator;
042
043 import java.util.ArrayList;
044 import java.util.Date;
045 import java.util.List;
046
047
051 public class ShoppingCouponLocalServiceImpl
052 extends ShoppingCouponLocalServiceBaseImpl {
053
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 new CouponStartDateException());
077
078 Date endDate = null;
079
080 if (!neverExpire) {
081 endDate = PortalUtil.getDate(
082 endDateMonth, endDateDay, endDateYear, endDateHour,
083 endDateMinute, user.getTimeZone(),
084 new CouponEndDateException());
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, false);
120
121 return coupon;
122 }
123
124 public void deleteCoupon(long couponId)
125 throws PortalException, SystemException {
126
127 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
128 couponId);
129
130 deleteCoupon(coupon);
131 }
132
133 public void deleteCoupon(ShoppingCoupon coupon) throws SystemException {
134 shoppingCouponPersistence.remove(coupon);
135 }
136
137 public void deleteCoupons(long groupId) throws SystemException {
138 List<ShoppingCoupon> coupons = shoppingCouponPersistence.findByGroupId(
139 groupId);
140
141 for (ShoppingCoupon coupon : coupons) {
142 deleteCoupon(coupon);
143 }
144 }
145
146 public ShoppingCoupon getCoupon(long couponId)
147 throws PortalException, SystemException {
148
149 return shoppingCouponPersistence.findByPrimaryKey(couponId);
150 }
151
152 public ShoppingCoupon getCoupon(String code)
153 throws PortalException, SystemException {
154
155 code = code.trim().toUpperCase();
156
157 return shoppingCouponPersistence.findByCode(code);
158 }
159
160 public List<ShoppingCoupon> search(
161 long groupId, long companyId, String code, boolean active,
162 String discountType, boolean andOperator, int start, int end)
163 throws SystemException {
164
165 return shoppingCouponFinder.findByG_C_C_A_DT(
166 groupId, companyId, code, active, discountType, andOperator, start,
167 end);
168 }
169
170 public int searchCount(
171 long groupId, long companyId, String code, boolean active,
172 String discountType, boolean andOperator)
173 throws SystemException {
174
175 return shoppingCouponFinder.countByG_C_C_A_DT(
176 groupId, companyId, code, active, discountType, andOperator);
177 }
178
179 public ShoppingCoupon updateCoupon(
180 long userId, long couponId, String name, String description,
181 int startDateMonth, int startDateDay, int startDateYear,
182 int startDateHour, int startDateMinute, int endDateMonth,
183 int endDateDay, int endDateYear, int endDateHour, int endDateMinute,
184 boolean neverExpire, boolean active, String limitCategories,
185 String limitSkus, double minOrder, double discount,
186 String discountType, ServiceContext serviceContext)
187 throws PortalException, SystemException {
188
189 User user = userPersistence.findByPrimaryKey(userId);
190
191 ShoppingCoupon coupon = shoppingCouponPersistence.findByPrimaryKey(
192 couponId);
193
194 Date startDate = PortalUtil.getDate(
195 startDateMonth, startDateDay, startDateYear, startDateHour,
196 startDateMinute, user.getTimeZone(),
197 new CouponStartDateException());
198
199 Date endDate = null;
200
201 if (!neverExpire) {
202 endDate = PortalUtil.getDate(
203 endDateMonth, endDateDay, endDateYear, endDateHour,
204 endDateMinute, user.getTimeZone(),
205 new CouponEndDateException());
206 }
207
208 if ((endDate != null) && (startDate.after(endDate))) {
209 throw new CouponDateException();
210 }
211
212 validate(
213 coupon.getCompanyId(), coupon.getGroupId(), name, description,
214 limitCategories, limitSkus, minOrder, discount);
215
216 coupon.setModifiedDate(new Date());
217 coupon.setName(name);
218 coupon.setDescription(description);
219 coupon.setStartDate(startDate);
220 coupon.setEndDate(endDate);
221 coupon.setActive(active);
222 coupon.setLimitCategories(limitCategories);
223 coupon.setLimitSkus(limitSkus);
224 coupon.setMinOrder(minOrder);
225 coupon.setDiscount(discount);
226 coupon.setDiscountType(discountType);
227
228 shoppingCouponPersistence.update(coupon, false);
229
230 return coupon;
231 }
232
233 protected String getCode() throws SystemException {
234 String code = PwdGenerator.getPassword(
235 PwdGenerator.KEY1 + PwdGenerator.KEY2, 8);
236
237 try {
238 shoppingCouponPersistence.findByCode(code);
239
240 return getCode();
241 }
242 catch (NoSuchCouponException nsce) {
243 return code;
244 }
245 }
246
247 protected void validate(
248 long companyId, long groupId, String code, boolean autoCode,
249 String name, String description, String limitCategories,
250 String limitSkus, double minOrder, double discount)
251 throws PortalException, SystemException {
252
253 if (!autoCode) {
254 if ((Validator.isNull(code)) ||
255 (Validator.isNumber(code)) ||
256 (code.indexOf(CharPool.SPACE) != -1)) {
257
258 throw new CouponCodeException();
259 }
260
261 if (shoppingCouponPersistence.fetchByCode(code) != null) {
262 throw new DuplicateCouponCodeException();
263 }
264 }
265
266 validate(
267 companyId, groupId, name, description, limitCategories, limitSkus,
268 minOrder, discount);
269 }
270
271 protected void validate(
272 long companyId, long groupId, String name, String description,
273 String limitCategories, String limitSkus, double minOrder,
274 double discount)
275 throws PortalException, SystemException {
276
277 if (Validator.isNull(name)) {
278 throw new CouponNameException();
279 }
280 else if (Validator.isNull(description)) {
281 throw new CouponDescriptionException();
282 }
283
284
285
286 long[] categoryIds = StringUtil.split(limitCategories, 0L);
287
288 List<Long> invalidCategoryIds = new ArrayList<Long>();
289
290 for (long categoryId : categoryIds) {
291 ShoppingCategory category =
292 shoppingCategoryPersistence.fetchByPrimaryKey(categoryId);
293
294 if ((category == null) || (category.getGroupId() != groupId)) {
295 invalidCategoryIds.add(categoryId);
296 }
297 }
298
299 if (invalidCategoryIds.size() > 0) {
300 CouponLimitCategoriesException clce =
301 new CouponLimitCategoriesException();
302
303 clce.setCategoryIds(invalidCategoryIds);
304
305 throw clce;
306 }
307
308
309
310 String[] skus = StringUtil.split(limitSkus);
311
312 List<String> invalidSkus = new ArrayList<String>();
313
314 for (String sku : skus) {
315 ShoppingItem item = shoppingItemPersistence.fetchByC_S(
316 companyId, sku);
317
318 if (item != null) {
319 ShoppingCategory category = item.getCategory();
320
321 if (category.getGroupId() != groupId) {
322 invalidSkus.add(sku);
323 }
324 }
325 else {
326 invalidSkus.add(sku);
327 }
328 }
329
330 if (invalidSkus.size() > 0) {
331 CouponLimitSKUsException clskue = new CouponLimitSKUsException();
332
333 clskue.setSkus(invalidSkus);
334
335 throw clskue;
336 }
337
338 if (minOrder < 0) {
339 throw new CouponMinimumOrderException();
340 }
341
342 if (discount < 0) {
343 throw new CouponDiscountException();
344 }
345 }
346
347 }