001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.util.PropsUtil;
023    import com.liferay.portlet.shopping.CartMinQuantityException;
024    import com.liferay.portlet.shopping.CouponActiveException;
025    import com.liferay.portlet.shopping.CouponEndDateException;
026    import com.liferay.portlet.shopping.CouponStartDateException;
027    import com.liferay.portlet.shopping.NoSuchCouponException;
028    import com.liferay.portlet.shopping.model.ShoppingCart;
029    import com.liferay.portlet.shopping.model.ShoppingCartItem;
030    import com.liferay.portlet.shopping.model.ShoppingCategory;
031    import com.liferay.portlet.shopping.model.ShoppingCoupon;
032    import com.liferay.portlet.shopping.model.ShoppingItem;
033    import com.liferay.portlet.shopping.model.impl.ShoppingCartItemImpl;
034    import com.liferay.portlet.shopping.service.base.ShoppingCartLocalServiceBaseImpl;
035    import com.liferay.portlet.shopping.util.ShoppingUtil;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    import java.util.Map;
040    import java.util.TreeMap;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ShoppingCartLocalServiceImpl
046            extends ShoppingCartLocalServiceBaseImpl {
047    
048            @Override
049            public void deleteGroupCarts(long groupId) {
050                    List<ShoppingCart> carts = shoppingCartPersistence.findByGroupId(
051                            groupId);
052    
053                    for (ShoppingCart cart : carts) {
054                            deleteShoppingCart(cart);
055                    }
056            }
057    
058            @Override
059            public void deleteUserCarts(long userId) {
060                    List<ShoppingCart> shoppingCarts = shoppingCartPersistence.findByUserId(
061                            userId);
062    
063                    for (ShoppingCart shoppingCart : shoppingCarts) {
064                            deleteShoppingCart(shoppingCart);
065                    }
066            }
067    
068            @Override
069            public ShoppingCart getCart(long userId, long groupId)
070                    throws PortalException {
071    
072                    return shoppingCartPersistence.findByG_U(groupId, userId);
073            }
074    
075            @Override
076            public Map<ShoppingCartItem, Integer> getItems(
077                    long groupId, String itemIds) {
078    
079                    Map<ShoppingCartItem, Integer> items = new TreeMap<>();
080    
081                    String[] itemIdsArray = StringUtil.split(itemIds);
082    
083                    for (int i = 0; i < itemIdsArray.length; i++) {
084                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
085                            String fields = ShoppingUtil.getItemFields(itemIdsArray[i]);
086    
087                            ShoppingItem item = shoppingItemPersistence.fetchByPrimaryKey(
088                                    itemId);
089    
090                            if (item != null) {
091                                    ShoppingCategory category = item.getCategory();
092    
093                                    if (category.getGroupId() == groupId) {
094                                            ShoppingCartItem cartItem = new ShoppingCartItemImpl(
095                                                    item, fields);
096    
097                                            Integer count = items.get(cartItem);
098    
099                                            if (count == null) {
100                                                    count = new Integer(1);
101                                            }
102                                            else {
103                                                    count = new Integer(count.intValue() + 1);
104                                            }
105    
106                                            items.put(cartItem, count);
107                                    }
108                            }
109                    }
110    
111                    return items;
112            }
113    
114            @Override
115            public ShoppingCart updateCart(
116                            long userId, long groupId, String itemIds, String couponCodes,
117                            int altShipping, boolean insure)
118                    throws PortalException {
119    
120                    List<Long> badItemIds = new ArrayList<>();
121    
122                    Map<ShoppingCartItem, Integer> items = getItems(groupId, itemIds);
123    
124                    boolean minQtyMultiple = GetterUtil.getBoolean(
125                            PropsUtil.get(PropsKeys.SHOPPING_CART_MIN_QTY_MULTIPLE));
126    
127                    for (Map.Entry<ShoppingCartItem, Integer> entry : items.entrySet()) {
128                            ShoppingCartItem cartItem = entry.getKey();
129                            Integer count = entry.getValue();
130    
131                            ShoppingItem item = cartItem.getItem();
132    
133                            int minQuantity = ShoppingUtil.getMinQuantity(item);
134    
135                            if (minQuantity <= 0) {
136                                    continue;
137                            }
138    
139                            if (minQtyMultiple) {
140                                    if ((count.intValue() % minQuantity) > 0) {
141                                            badItemIds.add(item.getItemId());
142                                    }
143                            }
144                            else {
145                                    if (count.intValue() < minQuantity) {
146                                            badItemIds.add(item.getItemId());
147                                    }
148                            }
149                    }
150    
151                    if (!badItemIds.isEmpty()) {
152                            throw new CartMinQuantityException(
153                                    StringUtil.merge(
154                                            badItemIds.toArray(new Long[badItemIds.size()])));
155                    }
156    
157                    String[] couponCodesArray = StringUtil.split(couponCodes);
158    
159                    for (int i = 0; i < couponCodesArray.length; i++) {
160                            try {
161                                    ShoppingCoupon coupon = shoppingCouponPersistence.findByCode(
162                                            couponCodesArray[i]);
163    
164                                    if (coupon.getGroupId() != groupId) {
165                                            throw new NoSuchCouponException(couponCodesArray[i]);
166                                    }
167                                    else if (!coupon.isActive()) {
168                                            throw new CouponActiveException(couponCodesArray[i]);
169                                    }
170                                    else if (!coupon.hasValidStartDate()) {
171                                            throw new CouponStartDateException(couponCodesArray[i]);
172                                    }
173                                    else if (!coupon.hasValidEndDate()) {
174                                            throw new CouponEndDateException(couponCodesArray[i]);
175                                    }
176                            }
177                            catch (NoSuchCouponException nsce) {
178                                    throw new NoSuchCouponException(couponCodesArray[i]);
179                            }
180    
181                            // Temporarily disable stacking of coupon codes
182    
183                            break;
184                    }
185    
186                    User user = userPersistence.findByPrimaryKey(userId);
187    
188                    ShoppingCart cart = null;
189    
190                    if (user.isDefaultUser()) {
191                            cart = shoppingCartPersistence.create(0);
192    
193                            cart.setGroupId(groupId);
194                            cart.setCompanyId(user.getCompanyId());
195                            cart.setUserId(userId);
196                            cart.setUserName(user.getFullName());
197                    }
198                    else {
199                            cart = shoppingCartPersistence.fetchByG_U(groupId, userId);
200    
201                            if (cart == null) {
202                                    long cartId = counterLocalService.increment();
203    
204                                    cart = shoppingCartPersistence.create(cartId);
205    
206                                    cart.setGroupId(groupId);
207                                    cart.setCompanyId(user.getCompanyId());
208                                    cart.setUserId(userId);
209                                    cart.setUserName(user.getFullName());
210                            }
211                    }
212    
213                    cart.setItemIds(checkItemIds(groupId, itemIds));
214                    cart.setCouponCodes(couponCodes);
215                    cart.setAltShipping(altShipping);
216                    cart.setInsure(insure);
217    
218                    if (!user.isDefaultUser()) {
219                            shoppingCartPersistence.update(cart);
220                    }
221    
222                    return cart;
223            }
224    
225            protected String checkItemIds(long groupId, String itemIds) {
226                    String[] itemIdsArray = StringUtil.split(itemIds);
227    
228                    for (int i = 0; i < itemIdsArray.length; i++) {
229                            long itemId = ShoppingUtil.getItemId(itemIdsArray[i]);
230    
231                            ShoppingItem item = null;
232    
233                            try {
234                                    item = shoppingItemPersistence.findByPrimaryKey(itemId);
235    
236                                    ShoppingCategory category = item.getCategory();
237    
238                                    if (category.getGroupId() != groupId) {
239                                            item = null;
240                                    }
241                            }
242                            catch (Exception e) {
243                            }
244    
245                            if (item == null) {
246                                    itemIds = StringUtil.removeFromList(itemIds, itemIdsArray[i]);
247                            }
248                    }
249    
250                    return itemIds;
251            }
252    
253    }