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