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