| 5432 |
amar.kumar |
1 |
package in.shop2020.user.handler;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Date;
|
|
|
5 |
import java.util.HashSet;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Map;
|
|
|
8 |
import java.util.Set;
|
|
|
9 |
|
|
|
10 |
import org.apache.commons.logging.Log;
|
|
|
11 |
import org.apache.commons.logging.LogFactory;
|
|
|
12 |
import org.apache.thrift.TException;
|
|
|
13 |
import org.apache.thrift.transport.TTransportException;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
15 |
import org.springframework.stereotype.Service;
|
|
|
16 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
17 |
|
|
|
18 |
import in.shop2020.logistics.DeliveryType;
|
|
|
19 |
import in.shop2020.logistics.LogisticsInfo;
|
|
|
20 |
import in.shop2020.logistics.LogisticsService;
|
|
|
21 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
22 |
import in.shop2020.model.v1.catalog.InventoryService;
|
|
|
23 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
24 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
25 |
import in.shop2020.model.v1.catalog.ItemShippingInfo;
|
|
|
26 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
27 |
import in.shop2020.model.v1.order.Order;
|
|
|
28 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
29 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
30 |
import in.shop2020.model.v1.order.TransactionService;
|
|
|
31 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
32 |
import in.shop2020.model.v1.order.TransactionStatus;
|
|
|
33 |
import in.shop2020.model.v1.user.CartStatus;
|
|
|
34 |
import in.shop2020.model.v1.user.Discount;
|
|
|
35 |
import in.shop2020.model.v1.user.LineStatus;
|
|
|
36 |
import in.shop2020.model.v1.user.PromotionException;
|
|
|
37 |
import in.shop2020.model.v1.user.PromotionService;
|
|
|
38 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
39 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
40 |
import in.shop2020.thrift.clients.LogisticsClient;
|
|
|
41 |
import in.shop2020.thrift.clients.PromotionClient;
|
|
|
42 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
43 |
import in.shop2020.user.domain.Address;
|
|
|
44 |
import in.shop2020.user.domain.Cart;
|
|
|
45 |
import in.shop2020.user.domain.Line;
|
|
|
46 |
import in.shop2020.user.persistence.CartMapper;
|
|
|
47 |
import in.shop2020.user.util.Converter;
|
|
|
48 |
|
|
|
49 |
@Service
|
|
|
50 |
public class CartHandler {
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private CartMapper cartMapper;
|
|
|
54 |
@Autowired
|
|
|
55 |
private AddressHandler addressHandler;
|
|
|
56 |
@Autowired
|
|
|
57 |
private UserHandler userHandler;
|
|
|
58 |
|
|
|
59 |
private static final Log log = LogFactory.getLog(CartHandler.class);
|
|
|
60 |
|
|
|
61 |
public Cart createCart() throws TException{
|
|
|
62 |
log.info("Creating new cart");
|
|
|
63 |
Cart cart = new Cart();
|
|
|
64 |
cart.setCart_status(CartStatus.ACTIVE.getValue());
|
|
|
65 |
cart.setCreated_on(new Date());
|
|
|
66 |
cart.setUpdated_on(new Date());
|
|
|
67 |
cartMapper.createCart(cart);
|
|
|
68 |
return cart;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public in.shop2020.model.v1.user.Cart getCart(long id) throws TException{
|
|
|
72 |
return Converter.toThriftCart(cartMapper.getCart(id));
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public in.shop2020.model.v1.user.Cart getCartsForUser(long userId,
|
|
|
76 |
CartStatus status) throws ShoppingCartException, TException{
|
|
|
77 |
if(userId == 0 || userId == -1) {
|
|
|
78 |
log.error("UserId : "+userId+"incorrect to get cart");
|
|
|
79 |
throw new ShoppingCartException(101, "user_id is not provided");
|
|
|
80 |
}
|
|
|
81 |
in.shop2020.model.v1.user.User user = userHandler.getUserById(userId);
|
|
|
82 |
return Converter.toThriftCart(cartMapper.getCart(user.getActiveCartId()));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
public void changeCartStatus(long cartId, CartStatus status) throws TException{
|
|
|
86 |
cartMapper.changeCartStatus(cartId, status.getValue());
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public List<in.shop2020.model.v1.user.Cart> getCartsByStatus(
|
|
|
90 |
CartStatus status) throws TException{
|
|
|
91 |
List<Cart> carts = cartMapper.getCartsByStatus(status.getValue());
|
|
|
92 |
if(carts!=null) {
|
|
|
93 |
List<in.shop2020.model.v1.user.Cart> tCarts = new ArrayList<in.shop2020.model.v1.user.Cart>();
|
|
|
94 |
for(Cart cart : carts) {
|
|
|
95 |
tCarts.add(Converter.toThriftCart(cart));
|
|
|
96 |
}
|
|
|
97 |
return tCarts;
|
|
|
98 |
}
|
|
|
99 |
log.warn("No carts found for cartStatus : "+status);
|
|
|
100 |
return null;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public List<in.shop2020.model.v1.user.Cart> getCartsByTime(long from_time,
|
|
|
104 |
long to_time, CartStatus status) throws TException{
|
|
|
105 |
List<Cart> carts = cartMapper.getCartsByTime(from_time, to_time, status.getValue());
|
|
|
106 |
if(carts!=null) {
|
|
|
107 |
List<in.shop2020.model.v1.user.Cart> tCarts = new ArrayList<in.shop2020.model.v1.user.Cart>();
|
|
|
108 |
for(Cart cart : carts) {
|
|
|
109 |
tCarts.add(Converter.toThriftCart(cart));
|
|
|
110 |
}
|
|
|
111 |
return tCarts;
|
|
|
112 |
}
|
|
|
113 |
log.warn("Couldn't get carts in specified time ");
|
|
|
114 |
return null;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public void deleteItemFromCart(long cartId, long itemId)
|
|
|
118 |
throws ShoppingCartException, TException{
|
|
|
119 |
try {
|
|
|
120 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
121 |
if(cart == null) {
|
|
|
122 |
log.error("No cart found with Id : "+cartId);
|
|
|
123 |
throw new ShoppingCartException(101, "no cart attached to this id");
|
|
|
124 |
} else {
|
|
|
125 |
Line lineItem = cartMapper.getLine(itemId, cartId, -1);
|
|
|
126 |
if(lineItem!=null) {
|
|
|
127 |
cartMapper.deleteDiscountForLine(cartId, itemId);
|
|
|
128 |
cartMapper.deleteLine(cartId, itemId);
|
|
|
129 |
cartMapper.updateCartUpdatedTime(new Date(), cartId);
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
} catch(Exception e) {
|
|
|
133 |
log.error(e);
|
|
|
134 |
throw new TException(e.getMessage(), e);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public long getCartsWithCouponCount(String couponCode) throws TException{
|
|
|
139 |
return cartMapper.getCartsWithCouponCount(couponCode);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public void changeItemStatus(long cartId, long itemId, LineStatus status)
|
|
|
143 |
throws ShoppingCartException, TException{
|
|
|
144 |
try {
|
|
|
145 |
if(status == null) {
|
|
|
146 |
log.error("LineStatus cannot be null for Item : " + itemId +
|
|
|
147 |
"cart : "+cartId);
|
|
|
148 |
throw new ShoppingCartException(101, "Status cannot be made null");
|
|
|
149 |
}
|
|
|
150 |
Line line = cartMapper.getLine(itemId, cartId, -1);
|
|
|
151 |
if(line!=null) {
|
|
|
152 |
line.setLine_status(status.getValue());
|
|
|
153 |
line.setUpdated_on(new Date());
|
|
|
154 |
cartMapper.updateLine(line);
|
|
|
155 |
cartMapper.updateCartUpdatedTime(new Date(), cartId);
|
|
|
156 |
} else {
|
|
|
157 |
log.error("Unable to probe the line you desired");
|
|
|
158 |
throw new ShoppingCartException(101, "Unable to probe the line you desired");
|
|
|
159 |
}
|
|
|
160 |
} catch(Exception e) {
|
|
|
161 |
log.error(e);
|
|
|
162 |
throw new TException(e.getMessage(), e);
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public void addAddressToCart(long cartId, long addressId)
|
|
|
167 |
throws ShoppingCartException, TException{
|
|
|
168 |
try {
|
|
|
169 |
if(cartId == 0 ) {
|
|
|
170 |
log.error("CartId cannot be 0");
|
|
|
171 |
throw new ShoppingCartException(101, "cart id cannot be empty");
|
|
|
172 |
}
|
|
|
173 |
if(addressId == 0) {
|
|
|
174 |
log.error("AddressId cannot be 0");
|
|
|
175 |
throw new ShoppingCartException(101, "Address id cannot be empty");
|
|
|
176 |
}
|
|
|
177 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
178 |
if(cart == null) {
|
|
|
179 |
log.error("No cart found for Id : " + cartId);
|
|
|
180 |
throw new ShoppingCartException(101, "no cart for this id");
|
|
|
181 |
}
|
|
|
182 |
in.shop2020.user.domain.Address address = addressHandler.getAddress(addressId);
|
|
|
183 |
if(address == null) {
|
|
|
184 |
log.error("No address found for Id : " + addressId);
|
|
|
185 |
throw new ShoppingCartException(101, "No address for this id");
|
|
|
186 |
}
|
|
|
187 |
cartMapper.addAddressToCart(cartId, addressId);
|
|
|
188 |
} catch(Exception e) {
|
|
|
189 |
log.error(e);
|
|
|
190 |
throw new TException(e.getMessage(), e);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public boolean checkOut(long cartId) throws ShoppingCartException, TException{
|
|
|
195 |
try {
|
|
|
196 |
if(cartId == 0){
|
|
|
197 |
log.error("CartId cannot be 0");
|
|
|
198 |
throw new ShoppingCartException(101, "Cart id not specified");
|
|
|
199 |
}
|
|
|
200 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
201 |
if(cart == null) {
|
|
|
202 |
log.error("No cart found for Id : " + cartId);
|
|
|
203 |
throw new ShoppingCartException(102, "The specified cart couldn't be found");
|
|
|
204 |
}
|
|
|
205 |
cartMapper.updateCartUpdatedTime(new Date(), cartId);
|
|
|
206 |
return true;
|
|
|
207 |
} catch(Exception e) {
|
|
|
208 |
log.error(e);
|
|
|
209 |
throw new TException(e.getMessage(), e);
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
public void saveDiscounts(List<Discount> discounts)
|
|
|
214 |
throws ShoppingCartException, TException{
|
|
|
215 |
try {
|
| 5456 |
amar.kumar |
216 |
if(discounts == null||discounts.size() == 0) {
|
| 5432 |
amar.kumar |
217 |
log.error("Discounts cannot be empty");
|
|
|
218 |
throw new ShoppingCartException(101, "discounts cannot be null");
|
|
|
219 |
}
|
|
|
220 |
if(discounts.size()>0) {
|
|
|
221 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(discounts.get(0).getCart_id());
|
|
|
222 |
for(Discount tDiscount : discounts) {
|
|
|
223 |
in.shop2020.user.domain.Line line = cartMapper.getLine(tDiscount.getItem_id(), cart.getId(), -1);
|
|
|
224 |
if(line!=null) {
|
|
|
225 |
in.shop2020.user.domain.Discount discount = new in.shop2020.user.domain.Discount();
|
|
|
226 |
discount.setLine_cart_id(line.getCart_id());
|
|
|
227 |
discount.setLine_item_id(tDiscount.getItem_id());
|
|
|
228 |
discount.setDiscount(tDiscount.getDiscount());
|
|
|
229 |
discount.setQuantity(tDiscount.getQuantity());
|
|
|
230 |
cartMapper.insertDiscount(discount);
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
} catch(Exception e) {
|
|
|
235 |
log.error(e);
|
|
|
236 |
throw new TException(e.getMessage(), e);
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
public void applyCouponToCart(long cartId, String couponCode,
|
|
|
241 |
double totalPrice, double discountedPrice) throws ShoppingCartException, TException{
|
|
|
242 |
try {
|
|
|
243 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
244 |
if(cart == null) {
|
|
|
245 |
log.error("No cart found for Id : " + cartId);
|
|
|
246 |
throw new ShoppingCartException(101, "no cart attached to this id");
|
|
|
247 |
}
|
|
|
248 |
cart.setTotal_price(totalPrice);
|
|
|
249 |
cart.setDiscounted_price(discountedPrice);
|
|
|
250 |
cart.setCoupon_code(couponCode);
|
|
|
251 |
cartMapper.updateCart(cart);
|
|
|
252 |
} catch(Exception e) {
|
|
|
253 |
log.error(e);
|
|
|
254 |
throw new TException(e.getMessage(), e);
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
public void removeCoupon(long cartId) throws ShoppingCartException, TException{
|
|
|
259 |
try {
|
|
|
260 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
261 |
if(cart == null) {
|
|
|
262 |
log.error("No cart found for Id : " + cartId);
|
|
|
263 |
throw new ShoppingCartException(101, "no cart attached to this id");
|
|
|
264 |
}
|
|
|
265 |
for(in.shop2020.user.domain.Line line : cart.getLines()) {
|
| 5464 |
amar.kumar |
266 |
cartMapper.removeLineDiscount(cart.getId(), line.getItem_id());
|
| 5432 |
amar.kumar |
267 |
}
|
|
|
268 |
|
|
|
269 |
deleteDiscountsFromCart(cart.getId());
|
|
|
270 |
cart.setDiscounted_price(0);
|
|
|
271 |
cart.setCoupon_code(null);
|
|
|
272 |
cartMapper.updateCart(cart);
|
|
|
273 |
} catch(Exception e) {
|
|
|
274 |
log.error(e);
|
|
|
275 |
throw new TException(e.getMessage(), e);
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
public String addItemToCart(long cartId, long itemId, long quantity,
|
|
|
280 |
long sourceId) throws ShoppingCartException, TException {
|
|
|
281 |
try {
|
|
|
282 |
if(itemId == 0) {
|
|
|
283 |
log.error("ItemId cannot be 0");
|
|
|
284 |
throw new ShoppingCartException(101, "item_id cannot be 0");
|
|
|
285 |
}
|
|
|
286 |
if(quantity == 0) {
|
|
|
287 |
log.error("quantity cannot be 0");
|
|
|
288 |
throw new ShoppingCartException(101, "quantity cannot be null");
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
Cart cart = cartMapper.getCart(cartId);
|
|
|
292 |
if(cart == null) {
|
|
|
293 |
log.error("No cart found for Id : " + cartId);
|
|
|
294 |
throw new ShoppingCartException(101, "no cart attached to this id");
|
|
|
295 |
}
|
|
|
296 |
Item item;
|
|
|
297 |
try {
|
|
|
298 |
InventoryService.Client catalogClient = new CatalogClient().getClient();
|
|
|
299 |
item = catalogClient.getItemForSource(itemId, sourceId);
|
|
|
300 |
ItemShippingInfo itemShippingInfo = catalogClient.isActive(itemId);
|
|
|
301 |
if(!itemShippingInfo.isIsActive()) {
|
|
|
302 |
return catalogClient.getItemStatusDescription(itemId);
|
|
|
303 |
}
|
|
|
304 |
} catch(InventoryServiceException ex) {
|
|
|
305 |
log.error("InventoryServiceException while getting item ");
|
|
|
306 |
try {
|
|
|
307 |
InventoryService.Client catalogClient = new CatalogClient().getClient();
|
|
|
308 |
item = catalogClient.getItemForSource(itemId, sourceId);
|
|
|
309 |
ItemShippingInfo itemShippingInfo = catalogClient.isActive(itemId);
|
|
|
310 |
if(!itemShippingInfo.isIsActive()) {
|
|
|
311 |
return catalogClient.getItemStatusDescription(itemId);
|
|
|
312 |
}
|
|
|
313 |
} catch(InventoryServiceException e) {
|
|
|
314 |
log.error("InventoryServiceException while getting item ");
|
|
|
315 |
throw new TException("InventoryServiceException caught in adding Item to Cart"+e);
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
cartMapper.updateCartUpdatedTime(new Date(), cartId);
|
|
|
320 |
Line line = cartMapper.getLine(itemId, cartId, -1);
|
|
|
321 |
if(line==null) {
|
|
|
322 |
line = new Line();
|
|
|
323 |
line.setCart_id(cartId);
|
|
|
324 |
line.setItem_id(itemId);
|
|
|
325 |
line.setQuantity(quantity);
|
|
|
326 |
line.setCreated_on(new Date());
|
|
|
327 |
line.setUpdated_on(new Date());
|
|
|
328 |
line.setActual_price(item.getSellingPrice());
|
|
|
329 |
line.setLine_status(LineStatus.LINE_ACTIVE.getValue());
|
|
|
330 |
cartMapper.insertLine(line);
|
|
|
331 |
} else {
|
|
|
332 |
cartMapper.updateLineQuantity(quantity, cartId, itemId, new Date());
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
return "";
|
|
|
336 |
} catch(Exception e) {
|
|
|
337 |
log.error(e);
|
|
|
338 |
throw new TException(e.getMessage(), e);
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
public boolean showCODOption(long cartId, long sourceId, String pincode) throws TException {
|
|
|
343 |
try {
|
|
|
344 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
345 |
boolean codOption = true;
|
|
|
346 |
try{
|
|
|
347 |
if(cart!=null) {
|
| 5446 |
rajveer |
348 |
if(cart.getCoupon_code() != null && cart.getCoupon_code().toLowerCase().equals("searlybird")) {
|
| 5432 |
amar.kumar |
349 |
codOption = false;
|
|
|
350 |
} else if(cart.getLines()!=null){
|
|
|
351 |
for(Line line : cart.getLines()) {
|
|
|
352 |
LogisticsService.Client logisticsClient = new LogisticsClient().getClient();
|
|
|
353 |
LogisticsInfo logisticsInfo = logisticsClient.
|
|
|
354 |
getLogisticsEstimation(line.getItem_id(), pincode, DeliveryType.PREPAID);
|
|
|
355 |
if(!logisticsInfo.isCodAllowed()){
|
|
|
356 |
codOption = false;
|
|
|
357 |
break;
|
|
|
358 |
}
|
|
|
359 |
}
|
| 5446 |
rajveer |
360 |
if(cart.getTotal_price() > 20000 || cart.getTotal_price() <= 250)
|
|
|
361 |
codOption = false;
|
| 5432 |
amar.kumar |
362 |
}
|
|
|
363 |
}
|
|
|
364 |
}catch(LogisticsServiceException ex) {
|
|
|
365 |
log.error("Error in getting COD option");
|
|
|
366 |
codOption = false;
|
|
|
367 |
}
|
|
|
368 |
return codOption;
|
|
|
369 |
} catch(Exception e) {
|
|
|
370 |
log.error(e);
|
|
|
371 |
throw new TException(e.getMessage(), e);
|
|
|
372 |
}
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
public void deleteDiscountsFromCart(long cartId) throws ShoppingCartException, TException{
|
|
|
376 |
try {
|
|
|
377 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
378 |
if(cart == null) {
|
|
|
379 |
log.error("No cart found for Id : "+cartId);
|
|
|
380 |
throw new ShoppingCartException(101, "No cart found for Id : "+cartId);
|
|
|
381 |
}
|
|
|
382 |
if(cart.getLines()!=null && !cart.getLines().isEmpty()) {
|
|
|
383 |
for(in.shop2020.user.domain.Line line : cart.getLines()) {
|
| 5464 |
amar.kumar |
384 |
deleteDiscountsForLine(cart.getId(), line.getItem_id());
|
| 5432 |
amar.kumar |
385 |
}
|
|
|
386 |
} else {
|
|
|
387 |
log.info("No lines found for cart : " + cartId);
|
|
|
388 |
}
|
|
|
389 |
} catch(Exception e) {
|
|
|
390 |
log.error(e);
|
|
|
391 |
throw new TException(e.getMessage(), e);
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
private void deleteDiscountsForLine(long cartId, long itemId) throws TException{
|
|
|
396 |
cartMapper.deleteDiscountForLine(cartId, itemId);
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
@Transactional
|
|
|
400 |
public boolean resetCart(long cartId, Map<Long, Double> items) throws TException{
|
|
|
401 |
try {
|
|
|
402 |
Set<Long> itemIds = new HashSet<Long>();
|
|
|
403 |
itemIds = items.keySet();
|
|
|
404 |
for(Long itemId : itemIds) {
|
|
|
405 |
in.shop2020.user.domain.Line line = cartMapper.getLine(itemId, cartId, 0);
|
|
|
406 |
if(line!=null) {
|
|
|
407 |
deleteDiscountsForLine(cartId, itemId);
|
|
|
408 |
if((line.getQuantity() - items.get(itemId)) == 0) {
|
|
|
409 |
cartMapper.deleteDiscountForLine(line.getCart_id(), line.getItem_id());
|
|
|
410 |
cartMapper.deleteLine(line.getCart_id(), line.getItem_id());
|
|
|
411 |
} else {
|
|
|
412 |
line.setQuantity(line.getQuantity() - items.get(itemId));
|
|
|
413 |
line.setDiscounted_price(0);
|
|
|
414 |
cartMapper.updateLine(line);
|
|
|
415 |
}
|
|
|
416 |
}
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
420 |
cart.setUpdated_on(new Date());
|
|
|
421 |
cart.setChecked_out_on(null);
|
|
|
422 |
cart.setTotal_price(0);
|
|
|
423 |
cart.setDiscounted_price(0);
|
|
|
424 |
cart.setCoupon_code(null);
|
|
|
425 |
cartMapper.updateCart(cart);
|
|
|
426 |
return true;
|
|
|
427 |
} catch(Exception e) {
|
|
|
428 |
log.error(e);
|
|
|
429 |
throw new TException(e.getMessage(), e);
|
|
|
430 |
}
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
@Transactional
|
|
|
434 |
public void mergeCart(long fromCartId, long toCartId) throws TException{
|
|
|
435 |
try {
|
|
|
436 |
in.shop2020.user.domain.Cart fromCart = cartMapper.getCart(fromCartId);
|
|
|
437 |
in.shop2020.user.domain.Cart toCart = cartMapper.getCart(toCartId);
|
|
|
438 |
|
|
|
439 |
List<in.shop2020.user.domain.Line> fromLines = new ArrayList<in.shop2020.user.domain.Line>();
|
|
|
440 |
List<in.shop2020.user.domain.Line> toLines = new ArrayList<in.shop2020.user.domain.Line>();
|
|
|
441 |
fromLines = fromCart.getLines();
|
|
|
442 |
toLines = toCart.getLines();
|
|
|
443 |
|
|
|
444 |
for (Line line : fromLines) {
|
|
|
445 |
for(in.shop2020.user.domain.Discount discount : line.getDiscounts()) {
|
|
|
446 |
cartMapper.deleteDiscountForLine(discount.getLine_cart_id(), discount.getLine_item_id());
|
|
|
447 |
}
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
for(Line line : fromLines) {
|
|
|
451 |
boolean flag = true;
|
|
|
452 |
for(Line toLine : toLines) {
|
|
|
453 |
if(line.getItem_id() == toLine.getItem_id()) {
|
|
|
454 |
flag = false;
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
if(flag) {
|
|
|
458 |
line.setCart_id(toCartId);
|
|
|
459 |
cartMapper.changeCartIdForLine(fromCartId, line.getItem_id(), toCartId);
|
|
|
460 |
} else {
|
|
|
461 |
cartMapper.deleteDiscountForLine(fromCartId, line.getItem_id());
|
|
|
462 |
cartMapper.deleteLine(fromCartId,line.getItem_id());
|
|
|
463 |
}
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
if(toCart.getCoupon_code()==null) {
|
|
|
467 |
toCart.setCoupon_code(fromCart.getCoupon_code());
|
|
|
468 |
}
|
|
|
469 |
toCart.setUpdated_on(new Date());
|
|
|
470 |
cartMapper.updateCart(toCart);
|
|
|
471 |
cartMapper.deActivateCart(fromCart.getId());
|
|
|
472 |
} catch(Exception e) {
|
|
|
473 |
log.error(e);
|
|
|
474 |
throw new TException(e.getMessage(), e);
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
public String validateCart(long cartId, long sourceId) throws TException, ShoppingCartException {
|
|
|
479 |
try {
|
|
|
480 |
String retval = "";
|
|
|
481 |
|
|
|
482 |
/* No need to validate duplicate items since there are only two ways
|
|
|
483 |
to add items to a cart and both of them check whether the item being
|
|
|
484 |
added is a duplicate of an already existing item.*/
|
|
|
485 |
|
|
|
486 |
Cart cart = cartMapper.getCart(cartId);
|
|
|
487 |
List<Line> lines = cart.getLines();
|
|
|
488 |
String pincode = null;
|
|
|
489 |
Date currentTime = new Date();
|
|
|
490 |
if(cart.getAddress_id()!=0) {
|
|
|
491 |
Address address = addressHandler.getAddress(cart.getAddress_id());
|
|
|
492 |
pincode = address.getPin();
|
|
|
493 |
}
|
|
|
494 |
if(null == pincode) {
|
|
|
495 |
in.shop2020.model.v1.user.User user = userHandler.getUserByCartId(cartId);
|
|
|
496 |
long addressId = user.getDefaultAddressId();
|
|
|
497 |
pincode = addressHandler.getPincodeForAddress(addressId);
|
|
|
498 |
}
|
|
|
499 |
if(pincode == null) {
|
|
|
500 |
pincode = "110001";
|
|
|
501 |
}
|
|
|
502 |
cart.setTotal_price(0);
|
|
|
503 |
for(Line line : lines) {
|
|
|
504 |
int oldEstimate = line.getEstimate();
|
|
|
505 |
long itemId = line.getItem_id();
|
|
|
506 |
line.setCart_id(cartId);
|
|
|
507 |
Item item;
|
|
|
508 |
ItemShippingInfo itemShippingInfo;
|
|
|
509 |
try {
|
|
|
510 |
InventoryService.Client inventoryClient = new CatalogClient().getClient();
|
|
|
511 |
item = inventoryClient.getItemForSource(itemId, sourceId);
|
|
|
512 |
itemShippingInfo = inventoryClient.isActive(itemId);
|
|
|
513 |
} catch(InventoryServiceException invEx) {
|
|
|
514 |
log.error("Exception in getting item and itemShippingInfo" +
|
|
|
515 |
"inventory Service");
|
|
|
516 |
throw new ShoppingCartException(101, invEx.getMessage());
|
|
|
517 |
}
|
|
|
518 |
if(itemShippingInfo.isIsActive()) {
|
|
|
519 |
long itemDeliveryEstimate;
|
|
|
520 |
if(itemShippingInfo.isIsRisky() && itemShippingInfo.getQuantity()<line.getQuantity()) {
|
|
|
521 |
line.setQuantity(1);
|
|
|
522 |
retval = "Try adding a smaller quantity of " + item.getBrand() +
|
|
|
523 |
" " + item.getModelNumber() + " (" + item.getColor() + ")";
|
|
|
524 |
}
|
|
|
525 |
line.setActual_price(item.getSellingPrice());
|
|
|
526 |
cart.setTotal_price(cart.getTotal_price() + (line.getActual_price()*line.getQuantity()));
|
|
|
527 |
try {
|
|
|
528 |
LogisticsService.Client logisticsClient = new LogisticsClient().getClient();
|
|
|
529 |
itemDeliveryEstimate = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID).getDeliveryTime();
|
|
|
530 |
} catch(LogisticsServiceException ex) {
|
|
|
531 |
log.error("Exception while delivery estimates for item:" +
|
|
|
532 |
""+itemId+" & pincode:"+pincode +"\n"+ex);
|
|
|
533 |
itemDeliveryEstimate = -1;
|
|
|
534 |
} catch(Exception e) {
|
|
|
535 |
log.error("Exception while delivery estimates for item:" +
|
|
|
536 |
""+itemId+" & pincode:"+pincode +"\n"+e);
|
|
|
537 |
itemDeliveryEstimate = -1;
|
|
|
538 |
}
|
|
|
539 |
if(oldEstimate!=itemDeliveryEstimate) {
|
|
|
540 |
line.setEstimate((int)itemDeliveryEstimate);
|
|
|
541 |
cart.setUpdated_on(new Date());
|
|
|
542 |
if(oldEstimate!=-1||oldEstimate!=0) {
|
|
|
543 |
retval = "Delivery Estimates updated.";
|
|
|
544 |
}
|
|
|
545 |
}
|
|
|
546 |
} else {
|
|
|
547 |
cartMapper.deleteDiscountForLine(line.getCart_id(), line.getItem_id());
|
|
|
548 |
cartMapper.deleteLine(line.getCart_id(), line.getItem_id());
|
|
|
549 |
retval = "Some items have been removed from the cart.";
|
|
|
550 |
}
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
if(cart.getChecked_out_on()!=null && cart.getUpdated_on()!=null) {
|
|
|
554 |
if(cart.getUpdated_on().getTime()>cart.getChecked_out_on().getTime()) {
|
|
|
555 |
cart.setChecked_out_on(null);
|
|
|
556 |
if(retval.equals("")) {
|
|
|
557 |
retval = "Your cart has been updated after the last checkout.";
|
|
|
558 |
}
|
|
|
559 |
}
|
|
|
560 |
}
|
|
|
561 |
cartMapper.updateCart(cart);
|
|
|
562 |
for(Line line : lines) {
|
|
|
563 |
line.setCart_id(cartId);
|
|
|
564 |
cartMapper.updateLine(line);
|
|
|
565 |
}
|
|
|
566 |
if(cart.getCoupon_code()!=null) {
|
|
|
567 |
try {
|
|
|
568 |
PromotionService.Client promotionClient = new PromotionClient().getClient();
|
|
|
569 |
in.shop2020.model.v1.user.Cart tCart = promotionClient.applyCoupon(cart.getCoupon_code(), cart.getId());
|
|
|
570 |
for(in.shop2020.model.v1.user.Line tLine : tCart.getLines()) {
|
|
|
571 |
Line line = cartMapper.getLine(tLine.getItemId(), tCart.getId(), -1);
|
|
|
572 |
line.setDiscounted_price(tLine.getDiscountedPrice());
|
|
|
573 |
}
|
|
|
574 |
cart.setDiscounted_price(tCart.getDiscountedPrice());
|
|
|
575 |
|
|
|
576 |
cartMapper.updateCart(cart);
|
|
|
577 |
for(Line line : lines) {
|
|
|
578 |
cartMapper.updateLine(line);
|
|
|
579 |
}
|
|
|
580 |
} catch (PromotionException ex) {
|
|
|
581 |
log.error("PromotionException while applying coupon "+cart.getCoupon_code()+ex);
|
|
|
582 |
removeCoupon(cart.getId());
|
|
|
583 |
retval = ex.getMessage();
|
|
|
584 |
}
|
|
|
585 |
}
|
|
|
586 |
return retval;
|
|
|
587 |
} catch(Exception e) {
|
|
|
588 |
log.error(e);
|
|
|
589 |
throw new TException(e.getMessage(), e);
|
|
|
590 |
}
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
public long commitCart(long cartId, String sessionSource,
|
|
|
594 |
long sessionStartTime, String firstSource, long firstSourceTime,
|
|
|
595 |
long userId) throws TException, ShoppingCartException {
|
|
|
596 |
try {
|
|
|
597 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
598 |
|
|
|
599 |
Transaction transaction = new Transaction();
|
|
|
600 |
transaction.setShoppingCartid(cartId);
|
|
|
601 |
transaction.setCustomer_id(userId);
|
|
|
602 |
transaction.setCreatedOn(new Date().getTime());
|
|
|
603 |
transaction.setTransactionStatus(TransactionStatus.INIT);
|
|
|
604 |
transaction.setStatusDescription("New Order");
|
|
|
605 |
transaction.setCoupon_code(cart.getCoupon_code());
|
|
|
606 |
transaction.setSessionSource(sessionSource);
|
|
|
607 |
transaction.setSessionStartTime(sessionStartTime);
|
|
|
608 |
transaction.setFirstSource(firstSource);
|
|
|
609 |
transaction.setFirstSourceTime(firstSourceTime);
|
|
|
610 |
transaction.setOrders(createOrders(cart,userId));
|
|
|
611 |
try{
|
|
|
612 |
TransactionService.Client transactionClient = new TransactionClient().getClient();
|
|
|
613 |
return transactionClient.createTransaction(transaction);
|
|
|
614 |
} catch(TransactionServiceException txnEx) {
|
|
|
615 |
log.error("TransactionServiceException while creating Transa caction ");
|
|
|
616 |
try {
|
|
|
617 |
TransactionService.Client transactionClient = new TransactionClient().getClient();
|
|
|
618 |
return transactionClient.createTransaction(transaction);
|
|
|
619 |
} catch(TransactionServiceException txnE) {
|
|
|
620 |
log.error("TransactionServiceException while creating Transa caction ");
|
|
|
621 |
throw new ShoppingCartException(101, txnE.getMessage());
|
|
|
622 |
}
|
|
|
623 |
}
|
|
|
624 |
} catch(Exception e) {
|
|
|
625 |
log.error(e);
|
|
|
626 |
throw new TException(e.getMessage(), e);
|
|
|
627 |
}
|
|
|
628 |
}
|
|
|
629 |
|
|
|
630 |
private List<Order> createOrders(Cart cart, long userId) throws TException {
|
|
|
631 |
try {
|
|
|
632 |
List<Line> cartLine = cart.getLines();
|
|
|
633 |
List<Order> orders = new ArrayList<Order>();
|
|
|
634 |
double quantityRemainingForOrder;
|
|
|
635 |
|
|
|
636 |
for(Line line : cartLine) {
|
|
|
637 |
if(line.getLine_status() == LineStatus.LINE_ACTIVE.getValue()) {
|
|
|
638 |
quantityRemainingForOrder = line.getQuantity();
|
|
|
639 |
List<in.shop2020.user.domain.Discount> discounts = line.getDiscounts();
|
|
|
640 |
if(discounts!=null) {
|
|
|
641 |
log.info("Discount not null");
|
|
|
642 |
for(in.shop2020.user.domain.Discount discount : discounts) {
|
|
|
643 |
log.info(discount.getLine_item_id());
|
|
|
644 |
int i = 0;
|
|
|
645 |
while(i<discount.getQuantity()) {
|
|
|
646 |
LineItem tLineItem = createLineItem(line.getItem_id(), line.getActual_price()-discount.getDiscount());
|
|
|
647 |
Order order = createOrder(userId, cart.getAddress_id(), tLineItem);
|
|
|
648 |
orders.add(order);
|
|
|
649 |
i++;
|
|
|
650 |
}
|
|
|
651 |
quantityRemainingForOrder = quantityRemainingForOrder - discount.getQuantity();
|
|
|
652 |
}
|
|
|
653 |
}
|
|
|
654 |
int i=0;
|
|
|
655 |
while(i<quantityRemainingForOrder) {
|
|
|
656 |
LineItem tLineItem = createLineItem(line.getItem_id(), line.getActual_price());
|
|
|
657 |
Order order = createOrder(userId, cart.getAddress_id(), tLineItem);
|
|
|
658 |
orders.add(order);
|
|
|
659 |
i += 1;
|
|
|
660 |
}
|
|
|
661 |
}
|
|
|
662 |
}
|
|
|
663 |
return orders;
|
|
|
664 |
} catch(Exception e) {
|
|
|
665 |
log.error(e);
|
|
|
666 |
throw new TException(e.getMessage(), e);
|
|
|
667 |
}
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
private Order createOrder(long userId, long addressId, LineItem tLine) throws TException{
|
|
|
671 |
try {
|
|
|
672 |
in.shop2020.model.v1.user.User user = userHandler.getUserById(userId);
|
|
|
673 |
Address address = addressHandler.getAddress(addressId);
|
|
|
674 |
Order order = new Order();
|
|
|
675 |
order.setCustomer_id(user.getUserId());
|
|
|
676 |
order.setCustomer_email(user.getEmail());
|
|
|
677 |
order.setCustomer_name(address.getName());
|
|
|
678 |
order.setCustomer_pincode(address.getPin());
|
|
|
679 |
order.setCustomer_address1(address.getLine_1());
|
|
|
680 |
order.setCustomer_address2(address.getLine_2());
|
|
|
681 |
order.setCustomer_city(address.getCity());
|
|
|
682 |
order.setCustomer_state(address.getState());
|
|
|
683 |
order.setCustomer_mobilenumber(address.getPhone());
|
|
|
684 |
order.setTotal_amount(tLine.getTotal_price());
|
|
|
685 |
order.setTotal_weight(tLine.getTotal_weight());
|
|
|
686 |
List<LineItem> lineItems = new ArrayList<LineItem>();
|
|
|
687 |
lineItems.add(tLine);
|
|
|
688 |
order.setLineitems(lineItems);
|
|
|
689 |
order.setStatus(OrderStatus.PAYMENT_PENDING);
|
|
|
690 |
order.setStatusDescription("Payment Pending");
|
|
|
691 |
order.setCreated_timestamp(new Date().getTime());
|
|
|
692 |
return order;
|
|
|
693 |
} catch(Exception e) {
|
|
|
694 |
log.error(e);
|
|
|
695 |
throw new TException(e.getMessage(), e);
|
|
|
696 |
}
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
private LineItem createLineItem(long item_id, double finalPrice) throws TException {
|
|
|
700 |
double quantity=1;
|
|
|
701 |
try {
|
|
|
702 |
InventoryService.Client inventoryClient;
|
|
|
703 |
Item item = null;
|
|
|
704 |
try {
|
|
|
705 |
inventoryClient = new CatalogClient().getClient();
|
|
|
706 |
item = inventoryClient.getItem(item_id);
|
|
|
707 |
} catch (TTransportException e) {
|
|
|
708 |
log.warn("Retrying to create Line Item, Failed first time"+e);
|
|
|
709 |
try {
|
|
|
710 |
inventoryClient = new CatalogClient().getClient();
|
|
|
711 |
item = inventoryClient.getItem(item_id);
|
|
|
712 |
} catch(TTransportException ex) {
|
|
|
713 |
log.error("Error while getting item from inventory");
|
|
|
714 |
throw new TException("Exception while creating line item"+ex);
|
|
|
715 |
} catch (InventoryServiceException exx) {
|
|
|
716 |
log.error("Error while getting item from inventory"+exx);
|
|
|
717 |
throw new TException("Exception while creating line item"+exx);
|
|
|
718 |
}
|
|
|
719 |
} catch(InventoryServiceException invEx) {
|
|
|
720 |
log.error("Error while creating line item "+invEx);
|
|
|
721 |
throw new TException("Exception while creating line item"+invEx);
|
|
|
722 |
}
|
|
|
723 |
LineItem tLineItem = new LineItem();
|
|
|
724 |
tLineItem.setProductGroup(item.getProductGroup());
|
|
|
725 |
tLineItem.setBrand(item.getBrand());
|
|
|
726 |
tLineItem.setModel_number(item.getModelNumber());
|
|
|
727 |
if(item.getColor()==null || item.getColor().equals("N/A")) {
|
|
|
728 |
tLineItem.setColor("");
|
|
|
729 |
} else {
|
|
|
730 |
tLineItem.setColor(item.getColor());
|
|
|
731 |
}
|
|
|
732 |
tLineItem.setModel_name(item.getModelName());
|
|
|
733 |
tLineItem.setExtra_info(item.getFeatureDescription());
|
|
|
734 |
tLineItem.setItem_id(item.getId());
|
|
|
735 |
tLineItem.setQuantity(quantity);//TODO Quantity always 1
|
|
|
736 |
tLineItem.setUnit_price(finalPrice);
|
|
|
737 |
tLineItem.setTotal_price(finalPrice*quantity);
|
|
|
738 |
tLineItem.setUnit_weight(item.getWeight());
|
|
|
739 |
tLineItem.setTotal_weight(item.getWeight()*quantity);
|
|
|
740 |
tLineItem.setDealText(item.getBestDealText());
|
|
|
741 |
|
|
|
742 |
if(item.getWarrantyPeriod()!=0) {
|
|
|
743 |
Date today = new Date();
|
|
|
744 |
Date expiryDate;
|
|
|
745 |
int expiryYear = today.getYear() + (int)((today.getMonth() + item.getWarrantyPeriod())/12);
|
|
|
746 |
int expiryMonth = (today.getMonth() + item.getWarrantyPeriod()) % 12;
|
|
|
747 |
try {
|
|
|
748 |
expiryDate = new Date(expiryYear, expiryMonth, today.getDate(), 23, 59, 59); //Milliseconds
|
|
|
749 |
} catch (Exception e) {
|
|
|
750 |
try {
|
|
|
751 |
expiryDate = new Date(expiryYear, expiryMonth, (today.getDate() - 1), 23, 59, 59);
|
|
|
752 |
} catch (Exception ex) {
|
|
|
753 |
try {
|
|
|
754 |
expiryDate = new Date(expiryYear, expiryMonth, (today.getDate() - 2), 23, 59, 59);
|
|
|
755 |
} catch (Exception exc) {
|
|
|
756 |
expiryDate = new Date(expiryYear, expiryMonth, (today.getDate() - 3), 23, 59, 59);
|
|
|
757 |
}
|
|
|
758 |
}
|
|
|
759 |
}
|
|
|
760 |
tLineItem.setWarrantry_expiry_timestamp(expiryDate.getTime());
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
return tLineItem;
|
|
|
764 |
} catch(Exception e) {
|
|
|
765 |
log.error(e);
|
|
|
766 |
throw new TException(e.getMessage(), e);
|
|
|
767 |
}
|
|
|
768 |
}
|
| 5560 |
rajveer |
769 |
|
|
|
770 |
|
|
|
771 |
void addStoreToCart(long cartId, long storeId) throws TException{
|
|
|
772 |
try {
|
|
|
773 |
in.shop2020.user.domain.Cart cart = cartMapper.getCart(cartId);
|
|
|
774 |
if(cart == null) {
|
|
|
775 |
log.error("No cart found for Id : "+cartId);
|
|
|
776 |
throw new ShoppingCartException(101, "No cart found for Id : "+cartId);
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
if(storeId != 0){
|
|
|
780 |
cart.setPickupStoreId(storeId);
|
|
|
781 |
}else{
|
|
|
782 |
cart.setPickupStoreId(0);
|
|
|
783 |
}
|
|
|
784 |
cartMapper.updateCart(cart);
|
|
|
785 |
} catch(Exception e) {
|
|
|
786 |
log.error(e);
|
|
|
787 |
throw new TException(e.getMessage(), e);
|
|
|
788 |
}
|
|
|
789 |
|
|
|
790 |
}
|
| 5432 |
amar.kumar |
791 |
}
|