| 557 |
chandransh |
1 |
'''
|
|
|
2 |
Created on 10-May-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from elixir import *
|
| 6921 |
anupam.sin |
7 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
8 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
9 |
from shop2020.clients.PromotionClient import PromotionClient
|
|
|
10 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
11 |
from shop2020.model.v1 import user
|
|
|
12 |
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, \
|
|
|
13 |
Discount, InsuranceDetails
|
|
|
14 |
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
|
|
|
15 |
DeliveryType
|
| 9299 |
kshitij.so |
16 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, InsurerType
|
| 6921 |
anupam.sin |
17 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction, \
|
|
|
18 |
TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem, \
|
| 7504 |
rajveer |
19 |
OrderStatus, OrderSource
|
| 6921 |
anupam.sin |
20 |
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
|
| 11592 |
amit.gupta |
21 |
ShoppingCartException, PromotionException, CartPlus
|
| 6921 |
anupam.sin |
22 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
| 557 |
chandransh |
23 |
import datetime
|
| 11592 |
amit.gupta |
24 |
from shop2020.model.v1.user.impl.Converters import to_t_cart
|
| 557 |
chandransh |
25 |
|
|
|
26 |
|
| 576 |
chandransh |
27 |
|
|
|
28 |
|
| 5326 |
rajveer |
29 |
def get_cart(userId):
|
|
|
30 |
user = User.get_by(id=userId)
|
|
|
31 |
return user.active_cart
|
| 557 |
chandransh |
32 |
|
|
|
33 |
def get_cart_by_id(id):
|
|
|
34 |
cart = Cart.get_by(id=id)
|
|
|
35 |
return cart
|
|
|
36 |
|
| 5326 |
rajveer |
37 |
def create_cart():
|
|
|
38 |
cart = Cart()
|
|
|
39 |
cart.created_on = datetime.datetime.now()
|
|
|
40 |
cart.updated_on = datetime.datetime.now()
|
|
|
41 |
cart.cart_status = CartStatus.ACTIVE
|
| 557 |
chandransh |
42 |
return cart
|
|
|
43 |
|
|
|
44 |
def get_carts_between(start_time, end_time, status):
|
|
|
45 |
init_time = to_py_date(start_time)
|
|
|
46 |
finish_time = to_py_date(end_time)
|
|
|
47 |
|
|
|
48 |
query = Cart.query
|
|
|
49 |
if status:
|
|
|
50 |
query = query.filter(Cart.cart_status==status)
|
|
|
51 |
if init_time:
|
|
|
52 |
query = query.filter(Cart.created_on >= init_time)
|
|
|
53 |
if finish_time:
|
|
|
54 |
query = query.filter(Cart.created_on <= finish_time)
|
|
|
55 |
|
|
|
56 |
carts = query.all()
|
|
|
57 |
return carts
|
|
|
58 |
|
| 643 |
chandransh |
59 |
def get_line(item_id, cart_id, status, single):
|
| 557 |
chandransh |
60 |
#get cart first
|
|
|
61 |
try:
|
|
|
62 |
found_cart = Cart.get_by(id=cart_id)
|
|
|
63 |
except:
|
|
|
64 |
raise ShoppingCartException(101, "cart not found ")
|
| 643 |
chandransh |
65 |
query = Line.query.filter_by(cart = found_cart, item_id = item_id)
|
| 557 |
chandransh |
66 |
|
|
|
67 |
if status:
|
|
|
68 |
query = query.filter_by(line_status = status)
|
|
|
69 |
else:
|
|
|
70 |
query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
|
|
|
71 |
try:
|
|
|
72 |
if single:
|
|
|
73 |
return query.one()
|
|
|
74 |
else:
|
|
|
75 |
return query.all()
|
|
|
76 |
except:
|
|
|
77 |
return None
|
|
|
78 |
|
| 3557 |
rajveer |
79 |
def add_item_to_cart(cart_id, item_id, quantity, sourceId):
|
| 557 |
chandransh |
80 |
if not item_id:
|
|
|
81 |
raise ShoppingCartException(101, "item_id cannot be null")
|
|
|
82 |
|
|
|
83 |
if not quantity:
|
|
|
84 |
raise ShoppingCartException(101, "quantity cannot be null")
|
| 643 |
chandransh |
85 |
|
| 2983 |
chandransh |
86 |
cart = Cart.get_by(id = cart_id)
|
| 557 |
chandransh |
87 |
if not cart:
|
| 6502 |
rajveer |
88 |
raise ShoppingCartException(101, "no cart attached to this id" + str(cart_id))
|
| 2983 |
chandransh |
89 |
retval = ""
|
| 6550 |
rajveer |
90 |
catalog_client = CatalogClient().get_client()
|
|
|
91 |
item = catalog_client.getItemForSource(item_id, sourceId)
|
| 9299 |
kshitij.so |
92 |
dataProtectionInsurer = catalog_client.getPrefferedInsurerForItem(item_id,InsurerType._NAMES_TO_VALUES.get("DATA"))
|
| 6550 |
rajveer |
93 |
item_shipping_info = catalog_client.isActive(item_id)
|
| 2983 |
chandransh |
94 |
if not item_shipping_info.isActive:
|
| 6550 |
rajveer |
95 |
return catalog_client.getItemStatusDescription(item_id)
|
| 557 |
chandransh |
96 |
|
| 643 |
chandransh |
97 |
current_time = datetime.datetime.now()
|
|
|
98 |
cart.updated_on = current_time
|
|
|
99 |
line = get_line(item_id, cart_id, None,True)
|
|
|
100 |
if line:
|
|
|
101 |
#change the quantity only
|
| 6903 |
anupam.sin |
102 |
line.insuranceAmount = (line.insuranceAmount/line.quantity) * quantity
|
| 643 |
chandransh |
103 |
line.quantity = quantity
|
| 685 |
chandransh |
104 |
line.updated_on = current_time
|
| 9299 |
kshitij.so |
105 |
line.dataProtectionAmount = (line.dataProtectionAmount/line.quantity) * quantity
|
| 643 |
chandransh |
106 |
else:
|
|
|
107 |
line = Line()
|
|
|
108 |
line.cart = cart
|
|
|
109 |
line.item_id = item_id
|
|
|
110 |
line.quantity = quantity
|
|
|
111 |
line.created_on = current_time
|
|
|
112 |
line.updated_on = current_time
|
| 3557 |
rajveer |
113 |
line.actual_price = item.sellingPrice
|
| 643 |
chandransh |
114 |
line.line_status = LineStatus.LINE_ACTIVE
|
| 6903 |
anupam.sin |
115 |
line.insurer = 0
|
|
|
116 |
line.insuranceAmount = 0
|
| 9299 |
kshitij.so |
117 |
#DATA INSURER IS SET UPON ADD TO CART
|
|
|
118 |
line.dataProtectionInsurer = dataProtectionInsurer
|
| 557 |
chandransh |
119 |
session.commit()
|
| 2983 |
chandransh |
120 |
return retval
|
| 557 |
chandransh |
121 |
|
|
|
122 |
def delete_item_from_cart(cart_id, item_id):
|
|
|
123 |
if not item_id:
|
|
|
124 |
raise ShoppingCartException(101, "item_id cannot be null")
|
| 685 |
chandransh |
125 |
cart = Cart.get_by(id = cart_id)
|
|
|
126 |
if not cart:
|
|
|
127 |
raise ShoppingCartException(101, "no cart attached to this id")
|
| 643 |
chandransh |
128 |
item = get_line(item_id, cart_id, None, True)
|
| 3554 |
varun.gupt |
129 |
count_deleted_discounts = delete_discounts_for_line(item)
|
| 557 |
chandransh |
130 |
item.delete()
|
| 685 |
chandransh |
131 |
current_time = datetime.datetime.now()
|
|
|
132 |
cart.updated_on = current_time
|
| 557 |
chandransh |
133 |
session.commit()
|
|
|
134 |
|
| 3554 |
varun.gupt |
135 |
def delete_discounts_for_line(item_line):
|
|
|
136 |
count_deleted = Discount.query.filter_by(line = item_line).delete()
|
|
|
137 |
session.commit()
|
|
|
138 |
return count_deleted
|
|
|
139 |
|
|
|
140 |
def delete_discounts_from_cart(cart_id, cart = None):
|
|
|
141 |
if cart is None:
|
|
|
142 |
if cart_id is None:
|
|
|
143 |
raise ShoppingCartException(101, 'cart_id and cart, both cannot be null')
|
|
|
144 |
else:
|
|
|
145 |
cart = Cart.get_by(id = cart_id)
|
|
|
146 |
|
|
|
147 |
if cart.lines:
|
|
|
148 |
for line in cart.lines:
|
|
|
149 |
delete_discounts_for_line(line)
|
|
|
150 |
|
|
|
151 |
def save_discounts(discounts):
|
|
|
152 |
if not discounts:
|
|
|
153 |
raise ShoppingCartException(101, 'discounts be null')
|
|
|
154 |
|
|
|
155 |
if len(discounts) > 0:
|
|
|
156 |
cart = Cart.get_by(id = discounts[0].cart_id)
|
|
|
157 |
|
|
|
158 |
for t_discount in discounts:
|
|
|
159 |
line = Line.query.filter_by(cart = cart, item_id = t_discount.item_id).first()
|
|
|
160 |
if line is not None:
|
|
|
161 |
discount = Discount()
|
|
|
162 |
discount.line = line
|
|
|
163 |
discount.discount = t_discount.discount
|
|
|
164 |
discount.quantity = t_discount.quantity
|
|
|
165 |
session.commit()
|
|
|
166 |
|
| 557 |
chandransh |
167 |
def add_address_to_cart(cart_id, address_id):
|
|
|
168 |
if not cart_id:
|
|
|
169 |
raise ShoppingCartException(101, "cart id cannot be made null")
|
|
|
170 |
|
|
|
171 |
if not address_id:
|
|
|
172 |
raise ShoppingCartException(101, "address id cannot be made null")
|
|
|
173 |
|
|
|
174 |
cart = get_cart_by_id(cart_id)
|
|
|
175 |
if not cart:
|
|
|
176 |
raise ShoppingCartException(101, "no cart for this id")
|
| 576 |
chandransh |
177 |
|
|
|
178 |
address = Address.get_by(id=address_id)
|
|
|
179 |
if not address:
|
|
|
180 |
raise ShoppingCartException(101, "No address for this id")
|
|
|
181 |
|
| 557 |
chandransh |
182 |
cart.address_id = address_id
|
| 685 |
chandransh |
183 |
current_time = datetime.datetime.now()
|
| 716 |
rajveer |
184 |
#cart.updated_on = current_time
|
| 557 |
chandransh |
185 |
session.commit()
|
| 5555 |
rajveer |
186 |
|
|
|
187 |
def add_store_to_cart(cartId, storeId):
|
|
|
188 |
if not cartId:
|
|
|
189 |
raise ShoppingCartException(101, "cart id cannot be made null")
|
| 557 |
chandransh |
190 |
|
| 5555 |
rajveer |
191 |
cart = get_cart_by_id(cartId)
|
|
|
192 |
if not cart:
|
|
|
193 |
raise ShoppingCartException(101, "no cart for this id")
|
|
|
194 |
|
|
|
195 |
if storeId:
|
|
|
196 |
cart.pickupStoreId = storeId
|
|
|
197 |
else:
|
|
|
198 |
cart.pickupStoreId = None
|
|
|
199 |
|
|
|
200 |
session.commit()
|
|
|
201 |
|
| 6922 |
anupam.sin |
202 |
def apply_coupon_to_cart(t_cart, coupon_code):
|
|
|
203 |
cart = get_cart_by_id(t_cart.id)
|
| 1976 |
varun.gupt |
204 |
if not cart:
|
|
|
205 |
raise ShoppingCartException(101, "no cart attached to this id")
|
| 6922 |
anupam.sin |
206 |
pc = PromotionClient().get_client()
|
| 11853 |
amit.gupta |
207 |
for t_line in t_cart.lines:
|
|
|
208 |
line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
|
|
|
209 |
if not pc.isGiftVoucher(coupon_code):
|
| 6922 |
anupam.sin |
210 |
line.discounted_price = t_line.discountedPrice
|
| 11853 |
amit.gupta |
211 |
line.dealText = t_line.dealText
|
|
|
212 |
line.freebieId = t_line.freebieId
|
| 6922 |
anupam.sin |
213 |
|
|
|
214 |
cart.total_price = t_cart.totalPrice
|
|
|
215 |
cart.discounted_price = t_cart.discountedPrice
|
| 1976 |
varun.gupt |
216 |
cart.coupon_code = coupon_code
|
|
|
217 |
session.commit()
|
|
|
218 |
|
|
|
219 |
def remove_coupon(cart_id):
|
|
|
220 |
cart = get_cart_by_id(cart_id)
|
|
|
221 |
if not cart:
|
|
|
222 |
raise ShoppingCartException(101, "no cart attached to this id")
|
| 2261 |
varun.gupt |
223 |
|
|
|
224 |
#Resetting discounted price of each line in cart to Null
|
|
|
225 |
for line in cart.lines:
|
|
|
226 |
line.discounted_price = None
|
| 11653 |
amit.gupta |
227 |
line.dealText = None
|
|
|
228 |
line.freebieId = None
|
| 2261 |
varun.gupt |
229 |
|
| 3619 |
chandransh |
230 |
delete_discounts_from_cart(cart.id, cart=cart)
|
| 1976 |
varun.gupt |
231 |
cart.discounted_price = None
|
|
|
232 |
cart.coupon_code = None
|
|
|
233 |
session.commit()
|
|
|
234 |
|
| 11526 |
amit.gupta |
235 |
def commit_cart(cart_id, sessionSource, sessionTime, firstSource, firstSourceTime, userId, schemeId, orderSource):
|
| 690 |
chandransh |
236 |
cart = get_cart_by_id(cart_id)
|
| 557 |
chandransh |
237 |
#now we have a cart. Need to create a transaction with it
|
| 576 |
chandransh |
238 |
txn = TTransaction()
|
|
|
239 |
txn.shoppingCartid = cart_id
|
| 5326 |
rajveer |
240 |
txn.customer_id = userId
|
| 576 |
chandransh |
241 |
txn.createdOn = to_java_date(datetime.datetime.now())
|
|
|
242 |
txn.transactionStatus = TTransactionStatus.INIT
|
|
|
243 |
txn.statusDescription = "New Order"
|
| 2219 |
varun.gupt |
244 |
txn.coupon_code = cart.coupon_code
|
| 2815 |
vikas |
245 |
txn.sessionSource = sessionSource
|
|
|
246 |
txn.sessionStartTime = sessionTime
|
| 3858 |
vikas |
247 |
txn.firstSource = firstSource
|
|
|
248 |
txn.firstSourceTime = firstSourceTime
|
| 6389 |
rajveer |
249 |
txn.emiSchemeId = schemeId
|
| 11526 |
amit.gupta |
250 |
txn.orders = create_orders(cart, userId, orderSource)
|
| 576 |
chandransh |
251 |
|
|
|
252 |
transaction_client = TransactionClient().get_client()
|
|
|
253 |
txn_id = transaction_client.createTransaction(txn)
|
|
|
254 |
session.commit()
|
|
|
255 |
|
|
|
256 |
return txn_id
|
|
|
257 |
|
| 11526 |
amit.gupta |
258 |
def create_orders(cart, userId, orderSource):
|
| 557 |
chandransh |
259 |
cart_lines = cart.lines
|
| 576 |
chandransh |
260 |
orders = []
|
| 6318 |
rajveer |
261 |
isGv = False
|
|
|
262 |
if cart.coupon_code:
|
|
|
263 |
try:
|
|
|
264 |
pc = PromotionClient().get_client()
|
|
|
265 |
isGv = pc.isGiftVoucher(cart.coupon_code)
|
|
|
266 |
except:
|
|
|
267 |
isGv = False
|
|
|
268 |
|
| 6903 |
anupam.sin |
269 |
insuranceDetails = InsuranceDetails.get_by(addressId = cart.address_id)
|
|
|
270 |
|
| 557 |
chandransh |
271 |
for line in cart_lines:
|
| 576 |
chandransh |
272 |
if line.line_status == LineStatus.LINE_ACTIVE:
|
| 3554 |
varun.gupt |
273 |
quantity_remaining_for_order = line.quantity
|
|
|
274 |
|
|
|
275 |
for discount in line.discounts:
|
|
|
276 |
i = 0
|
|
|
277 |
while i < discount.quantity:
|
| 11915 |
amit.gupta |
278 |
t_line_item = create_line_item(line, line.actual_price if isGv else (line.actual_price - discount.discount))
|
| 11669 |
amit.gupta |
279 |
t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, discount.discount if isGv else 0, line.insurer, (line.insuranceAmount/line.quantity), insuranceDetails,line.dataProtectionInsurer,(line.dataProtectionAmount)/line.quantity, orderSource, line.freebieId)
|
| 3554 |
varun.gupt |
280 |
orders.append(t_order)
|
|
|
281 |
i += 1
|
|
|
282 |
quantity_remaining_for_order -= discount.quantity
|
|
|
283 |
|
| 576 |
chandransh |
284 |
i = 0
|
| 3554 |
varun.gupt |
285 |
while i < quantity_remaining_for_order:
|
| 11915 |
amit.gupta |
286 |
t_line_item = create_line_item(line, line.actual_price)
|
| 11669 |
amit.gupta |
287 |
t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, 0, line.insurer, (line.insuranceAmount/line.quantity), insuranceDetails, line.dataProtectionInsurer,(line.dataProtectionAmount)/line.quantity, orderSource, line.freebieId)
|
| 576 |
chandransh |
288 |
orders.append(t_order)
|
|
|
289 |
i += 1
|
|
|
290 |
return orders
|
|
|
291 |
|
| 11669 |
amit.gupta |
292 |
def create_order(userId, address_id, t_line_item, pickupStoreId, gvAmount, insurer, insuranceAmount, insuranceDetails, dataProtectionInsurer, dataProtectionAmount, orderSource, freebieId):
|
| 5326 |
rajveer |
293 |
user = User.get_by(id=userId)
|
| 576 |
chandransh |
294 |
address = Address.get_by(id=address_id)
|
|
|
295 |
t_order = TOrder()
|
| 557 |
chandransh |
296 |
|
| 576 |
chandransh |
297 |
t_order.customer_id = user.id
|
|
|
298 |
t_order.customer_email = user.email
|
|
|
299 |
|
| 910 |
rajveer |
300 |
t_order.customer_name = address.name
|
| 576 |
chandransh |
301 |
t_order.customer_pincode = address.pin
|
| 738 |
chandransh |
302 |
t_order.customer_address1 = address.line_1
|
|
|
303 |
t_order.customer_address2 = address.line_2
|
| 669 |
chandransh |
304 |
t_order.customer_city = address.city
|
|
|
305 |
t_order.customer_state = address.state
|
| 576 |
chandransh |
306 |
t_order.customer_mobilenumber = address.phone
|
|
|
307 |
|
| 9299 |
kshitij.so |
308 |
t_order.total_amount = t_line_item.total_price + insuranceAmount + dataProtectionAmount
|
| 6318 |
rajveer |
309 |
t_order.gvAmount = gvAmount
|
| 1976 |
varun.gupt |
310 |
|
| 576 |
chandransh |
311 |
t_order.total_weight = t_line_item.total_weight
|
|
|
312 |
t_order.lineitems = [t_line_item]
|
|
|
313 |
|
| 690 |
chandransh |
314 |
t_order.status = OrderStatus.PAYMENT_PENDING
|
| 970 |
chandransh |
315 |
t_order.statusDescription = "Payment Pending"
|
| 576 |
chandransh |
316 |
t_order.created_timestamp = to_java_date(datetime.datetime.now())
|
|
|
317 |
|
| 5555 |
rajveer |
318 |
t_order.pickupStoreId = pickupStoreId
|
| 6903 |
anupam.sin |
319 |
t_order.insuranceAmount = insuranceAmount
|
|
|
320 |
t_order.insurer = insurer
|
| 6935 |
anupam.sin |
321 |
if insuranceDetails:
|
|
|
322 |
t_order.dob = insuranceDetails.dob
|
|
|
323 |
t_order.guardianName = insuranceDetails.guardianName
|
| 7190 |
amar.kumar |
324 |
|
|
|
325 |
catalog_client = CatalogClient().get_client()
|
| 11669 |
amit.gupta |
326 |
|
|
|
327 |
if freebieId is None:
|
|
|
328 |
freebie_item_id = catalog_client.getFreebieForItem(t_line_item.item_id)
|
|
|
329 |
if freebie_item_id:
|
|
|
330 |
t_order.freebieItemId = freebie_item_id
|
|
|
331 |
else:
|
|
|
332 |
freebie_item_id = None if freebieId == 0 else freebieId
|
| 11526 |
amit.gupta |
333 |
t_order.source = orderSource
|
| 9299 |
kshitij.so |
334 |
t_order.dataProtectionInsurer = dataProtectionInsurer
|
|
|
335 |
t_order.dataProtectionAmount = dataProtectionAmount
|
| 576 |
chandransh |
336 |
return t_order
|
|
|
337 |
|
| 11915 |
amit.gupta |
338 |
def create_line_item(line, final_price, quantity=1):
|
| 3133 |
rajveer |
339 |
inventory_client = CatalogClient().get_client()
|
| 11915 |
amit.gupta |
340 |
item = inventory_client.getItem(line.item_id)
|
| 576 |
chandransh |
341 |
t_line_item = TLineItem()
|
| 963 |
chandransh |
342 |
t_line_item.productGroup = item.productGroup
|
|
|
343 |
t_line_item.brand = item.brand
|
| 636 |
rajveer |
344 |
t_line_item.model_number = item.modelNumber
|
| 669 |
chandransh |
345 |
if item.color is None or item.color == "NA":
|
|
|
346 |
t_line_item.color = ""
|
|
|
347 |
else:
|
| 917 |
chandransh |
348 |
t_line_item.color = item.color
|
| 963 |
chandransh |
349 |
t_line_item.model_name = item.modelName
|
| 636 |
rajveer |
350 |
t_line_item.extra_info = item.featureDescription
|
| 702 |
chandransh |
351 |
t_line_item.item_id = item.id
|
| 3768 |
vikas |
352 |
t_line_item.quantity = quantity
|
| 1983 |
varun.gupt |
353 |
|
| 3554 |
varun.gupt |
354 |
t_line_item.unit_price = final_price
|
| 3768 |
vikas |
355 |
t_line_item.total_price = final_price * quantity
|
| 1983 |
varun.gupt |
356 |
|
| 636 |
rajveer |
357 |
t_line_item.unit_weight = item.weight
|
| 4172 |
rajveer |
358 |
t_line_item.total_weight = item.weight if item.weight is None else item.weight * quantity
|
| 11915 |
amit.gupta |
359 |
if line.dealText is None:
|
|
|
360 |
t_line_item.dealText = item.bestDealText
|
|
|
361 |
elif line.dealText == '':
|
|
|
362 |
t_line_item.dealText = None
|
|
|
363 |
else:
|
|
|
364 |
t_line_item.dealText = line.dealText
|
|
|
365 |
|
| 4312 |
rajveer |
366 |
if item.warrantyPeriod:
|
|
|
367 |
#Computing Manufacturer Warranty expiry date
|
|
|
368 |
today = datetime.date.today()
|
|
|
369 |
expiry_year = today.year + int((today.month + item.warrantyPeriod) / 12)
|
|
|
370 |
expiry_month = (today.month + item.warrantyPeriod) % 12
|
|
|
371 |
|
| 4295 |
varun.gupt |
372 |
try:
|
| 4312 |
rajveer |
373 |
expiry_date = datetime.datetime(expiry_year, expiry_month, today.day, 23, 59, 59, 999999)
|
| 4295 |
varun.gupt |
374 |
except ValueError:
|
|
|
375 |
try:
|
| 4312 |
rajveer |
376 |
expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 1), 23, 59, 59, 999999)
|
| 4295 |
varun.gupt |
377 |
except ValueError:
|
| 4312 |
rajveer |
378 |
try:
|
|
|
379 |
expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 2), 23, 59, 59, 999999)
|
|
|
380 |
except ValueError:
|
|
|
381 |
expiry_date = datetime.date(expiry_year, expiry_month, (today.day - 3), 23, 59, 59, 999999)
|
|
|
382 |
|
|
|
383 |
t_line_item.warrantry_expiry_timestamp = to_java_date(expiry_date)
|
|
|
384 |
|
| 576 |
chandransh |
385 |
return t_line_item
|
|
|
386 |
|
| 3557 |
rajveer |
387 |
def validate_cart(cartId, sourceId):
|
| 3133 |
rajveer |
388 |
inventory_client = CatalogClient().get_client()
|
| 576 |
chandransh |
389 |
logistics_client = LogisticsClient().get_client()
|
| 1976 |
varun.gupt |
390 |
promotion_client = PromotionClient().get_client()
|
| 1466 |
ankur.sing |
391 |
retval = ""
|
| 6736 |
amit.gupta |
392 |
emival = ""
|
| 557 |
chandransh |
393 |
# No need to validate duplicate items since there are only two ways
|
|
|
394 |
# to add items to a cart and both of them check whether the item being
|
|
|
395 |
# added is a duplicate of an already existing item.
|
| 563 |
chandransh |
396 |
cart = Cart.get_by(id=cartId)
|
|
|
397 |
cart_lines = cart.lines
|
| 776 |
rajveer |
398 |
customer_pincode = None
|
| 690 |
chandransh |
399 |
current_time = datetime.datetime.now()
|
| 5929 |
anupam.sin |
400 |
if cart.pickupStoreId :
|
| 5782 |
rajveer |
401 |
store = logistics_client.getPickupStore(cart.pickupStoreId)
|
|
|
402 |
customer_pincode = store.pin
|
| 5572 |
anupam.sin |
403 |
if cart.address_id != None and customer_pincode == None:
|
| 576 |
chandransh |
404 |
address = Address.get_by(id=cart.address_id)
|
|
|
405 |
customer_pincode = address.pin
|
| 776 |
rajveer |
406 |
if not customer_pincode:
|
| 785 |
rajveer |
407 |
user = User.get_by(active_cart_id = cartId)
|
|
|
408 |
default_address_id = user.default_address_id
|
| 776 |
rajveer |
409 |
if default_address_id:
|
|
|
410 |
address = Address.get_by(id = default_address_id)
|
|
|
411 |
customer_pincode = address.pin
|
|
|
412 |
if not customer_pincode:
|
|
|
413 |
#FIXME should not be hard coded. May be we can pick from config server.
|
|
|
414 |
customer_pincode = "110001"
|
| 1976 |
varun.gupt |
415 |
cart.total_price = 0
|
| 563 |
chandransh |
416 |
for line in cart_lines:
|
| 612 |
chandransh |
417 |
old_estimate = line.estimate
|
| 636 |
rajveer |
418 |
item_id = line.item_id
|
| 3557 |
rajveer |
419 |
item = inventory_client.getItemForSource(item_id, sourceId)
|
| 2983 |
chandransh |
420 |
item_shipping_info = inventory_client.isActive(item_id)
|
|
|
421 |
if item_shipping_info.isActive:
|
|
|
422 |
if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
|
|
|
423 |
line.quantity = 1
|
|
|
424 |
retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
|
|
|
425 |
|
| 2139 |
chandransh |
426 |
line.actual_price = item.sellingPrice
|
| 6921 |
anupam.sin |
427 |
cart.total_price = cart.total_price + (line.actual_price * line.quantity)
|
| 776 |
rajveer |
428 |
try:
|
| 4642 |
mandeep.dh |
429 |
item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
|
| 844 |
chandransh |
430 |
except LogisticsServiceException:
|
|
|
431 |
item_delivery_estimate = -1
|
|
|
432 |
#TODO Use the exception clause to set the retval appropriately
|
|
|
433 |
except :
|
|
|
434 |
item_delivery_estimate = -1
|
| 776 |
rajveer |
435 |
if old_estimate != item_delivery_estimate:
|
|
|
436 |
line.estimate = item_delivery_estimate
|
|
|
437 |
cart.updated_on = current_time
|
| 576 |
chandransh |
438 |
else:
|
| 11877 |
amit.gupta |
439 |
Discount.query.filter(Discount.line==line).delete()
|
| 563 |
chandransh |
440 |
line.delete()
|
| 716 |
rajveer |
441 |
if cart.checked_out_on is not None:
|
|
|
442 |
if cart.updated_on > cart.checked_out_on:
|
| 844 |
chandransh |
443 |
cart.checked_out_on = None
|
| 612 |
chandransh |
444 |
session.commit()
|
| 1976 |
varun.gupt |
445 |
|
| 6921 |
anupam.sin |
446 |
cart = Cart.get_by(id=cartId)
|
|
|
447 |
cart_lines = cart.lines
|
|
|
448 |
for line in cart_lines :
|
|
|
449 |
if line.insurer > 0 :
|
| 9299 |
kshitij.so |
450 |
insure_item(line.item_id, cartId, True, InsurerType._NAMES_TO_VALUES.get("DEVICE"))
|
|
|
451 |
if line.dataProtectionInsurer > 0:
|
|
|
452 |
insure_item(line.item_id, cartId, True, InsurerType._NAMES_TO_VALUES.get("DATA"))
|
| 1976 |
varun.gupt |
453 |
if cart.coupon_code is not None:
|
| 2026 |
varun.gupt |
454 |
try:
|
|
|
455 |
updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
|
| 6922 |
anupam.sin |
456 |
# totalInsuranceAmt = 0
|
|
|
457 |
# for t_line in updated_cart.lines:
|
|
|
458 |
# #Find the line in the database which corresponds to this line
|
|
|
459 |
# line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
|
|
|
460 |
# #Update its discounted price.
|
|
|
461 |
# line.discounted_price = t_line.discountedPrice
|
|
|
462 |
# #If discounted price of line is set and this coupon is not a gift voucher that means
|
|
|
463 |
# # we will need to correct the insurance price accordingly.
|
|
|
464 |
## if line.insurer > 0 and line.discounted_price and not promotion_client.isGiftVoucher(cart.coupon_code) :
|
|
|
465 |
## cc = CatalogClient().get_client()
|
|
|
466 |
## insuranceAmt = cc.getInsuranceAmount(line.item_id, line.discounted_price, line.insurer, line.quantity)
|
|
|
467 |
## line.insuranceAmount = insuranceAmt
|
|
|
468 |
## totalInsuranceAmt += insuranceAmt
|
|
|
469 |
# cart.total_price = updated_cart.totalPrice
|
|
|
470 |
# cart.discounted_price = updated_cart.discountedPrice
|
|
|
471 |
# session.commit()
|
| 6740 |
amit.gupta |
472 |
if updated_cart.message is not None:
|
| 6739 |
amit.gupta |
473 |
emival = updated_cart.message
|
| 2026 |
varun.gupt |
474 |
except PromotionException as ex:
|
|
|
475 |
remove_coupon(cart.id)
|
|
|
476 |
retval = ex.message
|
| 11860 |
amit.gupta |
477 |
session.close()
|
| 6736 |
amit.gupta |
478 |
return [retval, emival]
|
| 576 |
chandransh |
479 |
|
| 557 |
chandransh |
480 |
def merge_cart(fromCartId, toCartId):
|
|
|
481 |
fromCart = Cart.get_by(id=fromCartId)
|
|
|
482 |
toCart = Cart.get_by(id=toCartId)
|
|
|
483 |
|
|
|
484 |
old_lines = fromCart.lines
|
|
|
485 |
new_lines = toCart.lines
|
|
|
486 |
|
|
|
487 |
for line in old_lines:
|
| 5345 |
rajveer |
488 |
for discount in line.discounts:
|
|
|
489 |
discount.delete()
|
|
|
490 |
session.commit()
|
|
|
491 |
|
|
|
492 |
for line in old_lines:
|
| 557 |
chandransh |
493 |
flag = True
|
|
|
494 |
for new_line in new_lines:
|
|
|
495 |
if line.item_id == new_line.item_id:
|
|
|
496 |
flag = False
|
| 5345 |
rajveer |
497 |
|
| 576 |
chandransh |
498 |
if flag:
|
| 5345 |
rajveer |
499 |
line.cart_id = toCartId
|
|
|
500 |
else:
|
|
|
501 |
line.delete()
|
|
|
502 |
|
| 2019 |
varun.gupt |
503 |
if toCart.coupon_code is None:
|
|
|
504 |
toCart.coupon_code = fromCart.coupon_code
|
|
|
505 |
|
|
|
506 |
toCart.updated_on = datetime.datetime.now()
|
| 557 |
chandransh |
507 |
fromCart.expired_on = datetime.datetime.now()
|
|
|
508 |
fromCart.cart_status = CartStatus.INACTIVE
|
| 643 |
chandransh |
509 |
session.commit()
|
| 691 |
chandransh |
510 |
|
|
|
511 |
def check_out(cartId):
|
|
|
512 |
if cartId is None:
|
|
|
513 |
raise ShoppingCartException(101, "Cart id not specified")
|
| 716 |
rajveer |
514 |
cart = Cart.get_by(id = cartId)
|
| 691 |
chandransh |
515 |
if cart is None:
|
|
|
516 |
raise ShoppingCartException(102, "The specified cart couldn't be found")
|
|
|
517 |
cart.checked_out_on = datetime.datetime.now()
|
|
|
518 |
session.commit()
|
|
|
519 |
return True
|
|
|
520 |
|
|
|
521 |
def reset_cart(cartId, items):
|
|
|
522 |
if cartId is None:
|
|
|
523 |
raise ShoppingCartException(101, "Cart id not specified")
|
|
|
524 |
for item_id, quantity in items.iteritems():
|
|
|
525 |
line = Line.query.filter_by(cart_id=cartId, item_id=item_id).one()
|
|
|
526 |
if line is not None:
|
| 3566 |
rajveer |
527 |
delete_discounts_for_line(line)
|
| 2261 |
varun.gupt |
528 |
line.discounted_price = None
|
| 691 |
chandransh |
529 |
line.quantity = line.quantity - quantity
|
|
|
530 |
if line.quantity == 0:
|
|
|
531 |
line.delete()
|
| 717 |
rajveer |
532 |
cart = Cart.get_by(id=cartId)
|
| 691 |
chandransh |
533 |
cart.updated_on = datetime.datetime.now()
|
| 1894 |
vikas |
534 |
cart.checked_out_on = None
|
| 1976 |
varun.gupt |
535 |
|
|
|
536 |
# Removing Coupon
|
|
|
537 |
cart.total_price = None
|
|
|
538 |
cart.discounted_price = None
|
|
|
539 |
cart.coupon_code = None
|
|
|
540 |
|
| 691 |
chandransh |
541 |
session.commit()
|
| 766 |
rajveer |
542 |
return True
|
|
|
543 |
|
| 3386 |
varun.gupt |
544 |
def get_carts_with_coupon_count(coupon_code):
|
|
|
545 |
return Cart.query.filter_by(coupon_code = coupon_code).count()
|
| 4668 |
varun.gupt |
546 |
|
|
|
547 |
def show_cod_option(cartId, sourceId, pincode):
|
|
|
548 |
cart = Cart.get_by(id = cartId)
|
|
|
549 |
cod_option = True
|
|
|
550 |
logistics_client = LogisticsClient().get_client()
|
|
|
551 |
if cart:
|
| 6355 |
amit.gupta |
552 |
if cart.coupon_code:
|
|
|
553 |
promotion_client = PromotionClient().get_client()
|
| 11819 |
amit.gupta |
554 |
cod_option = promotion_client.isCodApplicable(to_t_cart(cart))
|
| 5351 |
varun.gupt |
555 |
|
| 6355 |
amit.gupta |
556 |
if cod_option and cart.lines:
|
| 4668 |
varun.gupt |
557 |
for line in cart.lines:
|
| 4866 |
rajveer |
558 |
logistics_info = logistics_client.getLogisticsEstimation(line.item_id, pincode, DeliveryType.PREPAID)
|
| 4871 |
rajveer |
559 |
if not logistics_info.codAllowed:
|
| 4668 |
varun.gupt |
560 |
cod_option = False
|
|
|
561 |
break
|
| 7492 |
rajveer |
562 |
if cart.total_price > 60000 or cart.total_price <= 250:
|
| 5430 |
rajveer |
563 |
cod_option = False
|
| 4668 |
varun.gupt |
564 |
return cod_option
|
| 6821 |
amar.kumar |
565 |
|
| 9791 |
rajveer |
566 |
def get_products_added_to_cart(startDate, endDate):
|
|
|
567 |
lines = session.query(Line.item_id).filter(Line.created_on > to_py_date(startDate)).filter(Line.created_on < to_py_date(endDate)).all()
|
| 9804 |
rajveer |
568 |
datas = []
|
|
|
569 |
for line in lines:
|
|
|
570 |
datas.append(line[0])
|
|
|
571 |
return datas
|
| 6903 |
anupam.sin |
572 |
|
| 11655 |
amit.gupta |
573 |
def insure_item(itemId, cartId, toInsure, insurerType):
|
|
|
574 |
cart = Cart.get_by(id = cartId)
|
|
|
575 |
line = None
|
|
|
576 |
for cartLine in cart.lines:
|
|
|
577 |
if(cartLine.item_id == itemId):
|
|
|
578 |
line = cartLine
|
|
|
579 |
break
|
|
|
580 |
|
|
|
581 |
if not line:
|
|
|
582 |
print("Error : No line found for cartId : " + cartId + " and itemId : " + itemId)
|
|
|
583 |
return False
|
|
|
584 |
|
| 6903 |
anupam.sin |
585 |
try:
|
|
|
586 |
if toInsure:
|
| 6921 |
anupam.sin |
587 |
csc = CatalogClient().get_client()
|
|
|
588 |
item = csc.getItem(itemId)
|
| 9299 |
kshitij.so |
589 |
insurerId = csc.getPrefferedInsurerForItem(itemId,insurerType)
|
|
|
590 |
insuranceAmount = csc.getInsuranceAmount(itemId, line.discounted_price if line.discounted_price else line.actual_price, insurerId, line.quantity)
|
|
|
591 |
if InsurerType._VALUES_TO_NAMES.get(insurerType)=='DEVICE':
|
|
|
592 |
if cart.discounted_price:
|
|
|
593 |
cart.discounted_price = cart.discounted_price - line.insuranceAmount + insuranceAmount
|
|
|
594 |
line.insurer = insurerId
|
|
|
595 |
line.insuranceAmount = insuranceAmount
|
|
|
596 |
if InsurerType._VALUES_TO_NAMES.get(insurerType)=='DATA':
|
|
|
597 |
if cart.discounted_price:
|
|
|
598 |
cart.discounted_price = cart.discounted_price - line.dataProtectionAmount + insuranceAmount
|
|
|
599 |
line.dataProtectionInsurer = insurerId
|
|
|
600 |
line.dataProtectionAmount = insuranceAmount
|
| 6903 |
anupam.sin |
601 |
cart.total_price = cart.total_price + insuranceAmount
|
|
|
602 |
else:
|
| 9299 |
kshitij.so |
603 |
if InsurerType._VALUES_TO_NAMES.get(insurerType)=='DEVICE':
|
|
|
604 |
cart.total_price = cart.total_price - line.insuranceAmount
|
|
|
605 |
if cart.discounted_price:
|
|
|
606 |
cart.discounted_price = cart.discounted_price - line.insuranceAmount
|
|
|
607 |
line.insurer = 0
|
|
|
608 |
line.insuranceAmount = 0
|
|
|
609 |
if InsurerType._VALUES_TO_NAMES.get(insurerType)=='DATA':
|
|
|
610 |
cart.total_price = cart.total_price - line.dataProtectionAmount
|
|
|
611 |
if cart.discounted_price:
|
|
|
612 |
cart.discounted_price = cart.discounted_price - line.dataProtectionAmount
|
|
|
613 |
line.dataProtectionInsurer = 0
|
|
|
614 |
line.dataProtectionAmount = 0
|
| 6903 |
anupam.sin |
615 |
line.updated_on = datetime.datetime.now()
|
|
|
616 |
cart.updated_on = datetime.datetime.now()
|
|
|
617 |
session.commit()
|
|
|
618 |
except:
|
|
|
619 |
print("Error : Unable to insure")
|
| 9299 |
kshitij.so |
620 |
print("insurerId : " + str(insurerId) + " ItemId : " + str(itemId) + " CartId : " + str(cartId))
|
| 6903 |
anupam.sin |
621 |
return False
|
|
|
622 |
|
|
|
623 |
return True
|
|
|
624 |
|
|
|
625 |
def cancel_insurance(cartId):
|
|
|
626 |
try:
|
|
|
627 |
cart = Cart.get_by(id = cartId)
|
|
|
628 |
for cartLine in cart.lines:
|
|
|
629 |
cart.total_price = cart.total_price - cartLine.insuranceAmount
|
|
|
630 |
if cart.discounted_price:
|
|
|
631 |
cart.discounted_price = cart.discounted_price - cartLine.insuranceAmount
|
|
|
632 |
cartLine.insurer = 0
|
|
|
633 |
cartLine.insuranceAmount = 0
|
|
|
634 |
cartLine.updated_on = datetime.datetime.now()
|
|
|
635 |
cart.updated_on = datetime.datetime.now()
|
|
|
636 |
session.commit()
|
|
|
637 |
except:
|
|
|
638 |
print("Error : Unable to cancel insurance for cartId :" + str(cartId))
|
|
|
639 |
return False
|
|
|
640 |
|
|
|
641 |
return True
|
|
|
642 |
|
|
|
643 |
def store_insurance_specific_details(addressId, dob, guardianName):
|
|
|
644 |
try:
|
|
|
645 |
insuranceDetails = InsuranceDetails.get_by(addressId = addressId);
|
|
|
646 |
if insuranceDetails is None :
|
|
|
647 |
insuranceDetails = InsuranceDetails()
|
|
|
648 |
insuranceDetails.addressId = addressId
|
|
|
649 |
insuranceDetails.dob = dob
|
|
|
650 |
insuranceDetails.guardianName = guardianName
|
|
|
651 |
session.commit()
|
|
|
652 |
except:
|
|
|
653 |
print("Error : Unable to store insurance details for addressId : " + str(addressId))
|
|
|
654 |
return False
|
|
|
655 |
return True
|
|
|
656 |
|
|
|
657 |
def is_insurance_detail_present(addressId):
|
|
|
658 |
try:
|
|
|
659 |
insuranceDetails = InsuranceDetails.get_by(addressId = addressId);
|
|
|
660 |
if insuranceDetails is None :
|
|
|
661 |
return False
|
|
|
662 |
except:
|
|
|
663 |
print("Error : Unable to get insurance details for addressId : " + str(addressId))
|
|
|
664 |
return False
|
|
|
665 |
return True
|
|
|
666 |
|
| 11655 |
amit.gupta |
667 |
def validate_cart_plus(cart_id, source_id):
|
|
|
668 |
try:
|
|
|
669 |
cart_messages = validate_cart(cart_id, source_id)
|
|
|
670 |
found_cart = Cart.get_by(id=cart_id)
|
| 11592 |
amit.gupta |
671 |
pincode = "110001"
|
| 11599 |
amit.gupta |
672 |
default_address_id = User.get_by(active_cart_id = cart_id).default_address_id
|
| 11598 |
amit.gupta |
673 |
|
| 11592 |
amit.gupta |
674 |
default_address = None
|
|
|
675 |
if found_cart.address_id is not None and found_cart.address_id > 0:
|
|
|
676 |
pincode = Address.get_by(id=found_cart.address_id).pin
|
| 11600 |
amit.gupta |
677 |
elif default_address_id is not None:
|
| 11592 |
amit.gupta |
678 |
default_address = Address.get_by(id = default_address_id)
|
|
|
679 |
pincode = default_address.pin
|
|
|
680 |
|
| 11612 |
amit.gupta |
681 |
needInuranceInfo = False
|
| 11600 |
amit.gupta |
682 |
if default_address_id is not None:
|
|
|
683 |
for line in found_cart.lines:
|
|
|
684 |
if line.insurer > 0:
|
| 11611 |
amit.gupta |
685 |
needInuranceInfo = not is_insurance_detail_present(default_address_id)
|
| 11600 |
amit.gupta |
686 |
break
|
| 11592 |
amit.gupta |
687 |
cartPlus = CartPlus()
|
|
|
688 |
cartPlus.cart = to_t_cart(found_cart)
|
|
|
689 |
cartPlus.pinCode = pincode
|
|
|
690 |
cartPlus.validateCartMessages = cart_messages
|
|
|
691 |
cartPlus.needInsuranceInfo = needInuranceInfo
|
|
|
692 |
return cartPlus
|
|
|
693 |
finally:
|
|
|
694 |
close_session()
|
|
|
695 |
|
| 766 |
rajveer |
696 |
def close_session():
|
|
|
697 |
if session.is_active:
|
|
|
698 |
print "session is active. closing it."
|
| 6903 |
anupam.sin |
699 |
session.close()
|