| 557 |
chandransh |
1 |
'''
|
|
|
2 |
Created on 10-May-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from elixir import *
|
| 576 |
chandransh |
7 |
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User
|
| 557 |
chandransh |
8 |
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
|
|
|
9 |
import datetime
|
| 576 |
chandransh |
10 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
| 557 |
chandransh |
11 |
|
| 576 |
chandransh |
12 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction,\
|
|
|
13 |
TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem,\
|
|
|
14 |
PaymentInfo as TPaymentInfo, OrderStatus
|
| 557 |
chandransh |
15 |
|
| 576 |
chandransh |
16 |
from shop2020.thriftpy.model.v1.catalog.ttypes import Item as TItem
|
|
|
17 |
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo as TLogisticsInfo
|
|
|
18 |
|
|
|
19 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
20 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
21 |
from shop2020.clients.LogisticsClient import LogisticsClient
|
|
|
22 |
from shop2020.model.v1 import user
|
|
|
23 |
|
| 557 |
chandransh |
24 |
def get_cart(user_id):
|
|
|
25 |
query = Cart.query.filter_by(user_id=user_id)
|
|
|
26 |
query = query.filter_by(cart_status = CartStatus.ACTIVE)
|
|
|
27 |
try:
|
|
|
28 |
cart = query.one()
|
|
|
29 |
cart.accessed_on = datetime.datetime.now()
|
|
|
30 |
session.commit()
|
|
|
31 |
except:
|
|
|
32 |
return None
|
|
|
33 |
# raise ShoppingCartException(101, "Cart does not exist")
|
|
|
34 |
return cart
|
|
|
35 |
|
|
|
36 |
def get_cart_by_id(id):
|
|
|
37 |
cart = Cart.get_by(id=id)
|
|
|
38 |
if cart:
|
|
|
39 |
cart.accessed_on = datetime.datetime.now()
|
|
|
40 |
session.commit()
|
|
|
41 |
return cart
|
|
|
42 |
|
|
|
43 |
def create_cart(user_id):
|
|
|
44 |
cart = get_cart(user_id)
|
|
|
45 |
if not cart:
|
|
|
46 |
cart = Cart()
|
|
|
47 |
cart.user_id = user_id
|
|
|
48 |
cart.created_on = datetime.datetime.now()
|
|
|
49 |
cart.updated_on = datetime.datetime.now()
|
|
|
50 |
cart.cart_status = CartStatus.ACTIVE
|
|
|
51 |
session.commit()
|
|
|
52 |
|
|
|
53 |
return cart
|
|
|
54 |
|
|
|
55 |
def get_cart_by_user_id_and_status(user_id, status):
|
|
|
56 |
|
|
|
57 |
query = Cart.query.filter_by(user_id=user_id)
|
|
|
58 |
if status:
|
|
|
59 |
query = query.filter_by(cart_status=status)
|
|
|
60 |
carts = query.all()
|
|
|
61 |
return carts
|
|
|
62 |
|
|
|
63 |
def get_carts_by_status(status):
|
|
|
64 |
return Cart.query.filter_by(cart_status=status).all()
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
def get_carts_between(start_time, end_time, status):
|
|
|
68 |
init_time = to_py_date(start_time)
|
|
|
69 |
finish_time = to_py_date(end_time)
|
|
|
70 |
|
|
|
71 |
query = Cart.query
|
|
|
72 |
if status:
|
|
|
73 |
query = query.filter(Cart.cart_status==status)
|
|
|
74 |
if init_time:
|
|
|
75 |
query = query.filter(Cart.created_on >= init_time)
|
|
|
76 |
if finish_time:
|
|
|
77 |
query = query.filter(Cart.created_on <= finish_time)
|
|
|
78 |
|
|
|
79 |
carts = query.all()
|
|
|
80 |
return carts
|
|
|
81 |
|
|
|
82 |
def get_item(item_id, cart_id, status, single):
|
|
|
83 |
|
|
|
84 |
#get cart first
|
|
|
85 |
try:
|
|
|
86 |
found_cart = Cart.get_by(id=cart_id)
|
|
|
87 |
|
|
|
88 |
except:
|
|
|
89 |
raise ShoppingCartException(101, "cart not found ")
|
|
|
90 |
query = Line.query.filter(Line.cart.has(id = cart_id))
|
|
|
91 |
query = query.filter_by(item_id = item_id)
|
|
|
92 |
|
|
|
93 |
# query = LineItem.query.filter_by(item_id = item_id)
|
|
|
94 |
# query = query.filter(LineItem.cart.id == cart_id)
|
|
|
95 |
if status:
|
|
|
96 |
query = query.filter_by(line_status = status)
|
|
|
97 |
else:
|
|
|
98 |
query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
|
|
|
99 |
try:
|
|
|
100 |
if single:
|
|
|
101 |
return query.one()
|
|
|
102 |
else:
|
|
|
103 |
return query.all()
|
|
|
104 |
except:
|
|
|
105 |
return None
|
|
|
106 |
|
|
|
107 |
def get_item_by_id(line_id):
|
|
|
108 |
return Line.get_by(id=line_id)
|
|
|
109 |
|
|
|
110 |
def change_cart_status(cart_id, status):
|
|
|
111 |
cart = get_cart_by_id(id)
|
|
|
112 |
if not cart:
|
|
|
113 |
raise ShoppingCartException(101, "no cart attached to this id")
|
|
|
114 |
if not status:
|
|
|
115 |
raise ShoppingCartException(101, "invalid status")
|
|
|
116 |
|
|
|
117 |
cart.cart_status = status
|
|
|
118 |
|
|
|
119 |
session.commit()
|
|
|
120 |
|
|
|
121 |
def add_item_to_cart(cart_id, item_id, quantity):
|
|
|
122 |
if not item_id:
|
|
|
123 |
raise ShoppingCartException(101, "item_id cannot be null")
|
|
|
124 |
|
|
|
125 |
if not quantity:
|
|
|
126 |
raise ShoppingCartException(101, "quantity cannot be null")
|
|
|
127 |
|
|
|
128 |
item = get_item(item_id, cart_id, None,True)
|
|
|
129 |
if item:
|
|
|
130 |
#change the quantity only
|
|
|
131 |
item.quantity = quantity
|
|
|
132 |
session.commit()
|
|
|
133 |
return item.id
|
|
|
134 |
|
|
|
135 |
cart = get_cart_by_id(cart_id)
|
|
|
136 |
if not cart:
|
|
|
137 |
raise ShoppingCartException(101, "no cart attached to this id")
|
|
|
138 |
|
|
|
139 |
line = Line()
|
|
|
140 |
line.item_id = item_id
|
|
|
141 |
line.quantity = quantity
|
|
|
142 |
line.created_on = datetime.datetime.now()
|
|
|
143 |
line.updated_on = datetime.datetime.now()
|
|
|
144 |
line.line_status = LineStatus.LINE_ACTIVE
|
|
|
145 |
|
|
|
146 |
cart.lines.append(line)
|
|
|
147 |
session.commit()
|
|
|
148 |
return line.id
|
|
|
149 |
|
|
|
150 |
def delete_item_from_cart(cart_id, item_id):
|
|
|
151 |
if not item_id:
|
|
|
152 |
raise ShoppingCartException(101, "item_id cannot be null")
|
|
|
153 |
item = get_item(item_id, cart_id, None, True)
|
|
|
154 |
item.delete()
|
|
|
155 |
session.commit()
|
|
|
156 |
return
|
|
|
157 |
|
|
|
158 |
def change_item_quantity(cart_id, item_id, quantity):
|
|
|
159 |
return add_item_to_cart(cart_id, item_id, quantity)
|
|
|
160 |
|
|
|
161 |
def change_item_status(cart_id, item_id, status):
|
|
|
162 |
if not status:
|
|
|
163 |
raise ShoppingCartException(101, "Status cannot be made null")
|
|
|
164 |
item = get_item(item_id, cart_id, None, True)
|
|
|
165 |
if item:
|
|
|
166 |
item.line_status = status
|
|
|
167 |
session.commit()
|
|
|
168 |
else:
|
|
|
169 |
raise ShoppingCartException(101, "Unable to probe the item you desired")
|
|
|
170 |
|
|
|
171 |
def add_address_to_cart(cart_id, address_id):
|
|
|
172 |
if not cart_id:
|
|
|
173 |
raise ShoppingCartException(101, "cart id cannot be made null")
|
|
|
174 |
|
|
|
175 |
if not address_id:
|
|
|
176 |
raise ShoppingCartException(101, "address id cannot be made null")
|
|
|
177 |
|
|
|
178 |
cart = get_cart_by_id(cart_id)
|
|
|
179 |
if not cart:
|
|
|
180 |
raise ShoppingCartException(101, "no cart for this id")
|
| 576 |
chandransh |
181 |
|
|
|
182 |
address = Address.get_by(id=address_id)
|
|
|
183 |
if not address:
|
|
|
184 |
raise ShoppingCartException(101, "No address for this id")
|
|
|
185 |
|
| 557 |
chandransh |
186 |
cart.address_id = address_id
|
|
|
187 |
session.commit()
|
|
|
188 |
|
| 576 |
chandransh |
189 |
def commit_cart(cart_id):
|
| 557 |
chandransh |
190 |
cart = get_cart_by_id(cart_id)
|
|
|
191 |
cart.cart_status = CartStatus.COMMITTED
|
|
|
192 |
cart.committed_on = datetime.datetime.now()
|
| 576 |
chandransh |
193 |
|
| 557 |
chandransh |
194 |
#now we have a cart. Need to create a transaction with it
|
| 576 |
chandransh |
195 |
txn = TTransaction()
|
|
|
196 |
txn.paymentInfo = TPaymentInfo()
|
|
|
197 |
txn.shoppingCartid = cart_id
|
|
|
198 |
txn.customer_id = cart.user_id
|
|
|
199 |
txn.createdOn = to_java_date(datetime.datetime.now())
|
|
|
200 |
txn.transactionStatus = TTransactionStatus.INIT
|
|
|
201 |
txn.statusDescription = "New Order"
|
| 557 |
chandransh |
202 |
|
| 576 |
chandransh |
203 |
txn.orders = create_orders(cart)
|
|
|
204 |
|
|
|
205 |
transaction_client = TransactionClient().get_client()
|
|
|
206 |
txn_id = transaction_client.createTransaction(txn)
|
|
|
207 |
session.commit()
|
|
|
208 |
|
|
|
209 |
new_cart = create_cart(cart.user_id)
|
|
|
210 |
user = User.get_by(id=cart.user_id)
|
|
|
211 |
user.active_cart_id = new_cart.id
|
|
|
212 |
session.commit()
|
|
|
213 |
return txn_id
|
|
|
214 |
|
|
|
215 |
def create_orders(cart):
|
| 557 |
chandransh |
216 |
cart_lines = cart.lines
|
| 576 |
chandransh |
217 |
orders = []
|
|
|
218 |
|
| 557 |
chandransh |
219 |
for line in cart_lines:
|
| 576 |
chandransh |
220 |
if line.line_status == LineStatus.LINE_ACTIVE:
|
|
|
221 |
i = 0
|
|
|
222 |
while i< line.quantity:
|
|
|
223 |
t_line_item = create_line_item(line.item_id)
|
|
|
224 |
t_order = create_order(cart.user_id, cart.address_id, t_line_item)
|
|
|
225 |
orders.append(t_order)
|
|
|
226 |
i += 1
|
|
|
227 |
return orders
|
|
|
228 |
|
|
|
229 |
def create_order(user_id, address_id, t_line_item):
|
|
|
230 |
user = User.get_by(id=user_id)
|
|
|
231 |
address = Address.get_by(id=address_id)
|
|
|
232 |
t_order = TOrder()
|
| 557 |
chandransh |
233 |
|
| 576 |
chandransh |
234 |
t_order.customer_id = user.id
|
|
|
235 |
t_order.customer_name = user.name
|
|
|
236 |
t_order.customer_email = user.email
|
|
|
237 |
|
|
|
238 |
t_order.customer_pincode = address.pin
|
|
|
239 |
t_order.customer_address = get_address_string(address)
|
|
|
240 |
t_order.customer_mobilenumber = address.phone
|
|
|
241 |
|
|
|
242 |
t_order.total_amount = t_line_item.total_price
|
|
|
243 |
t_order.total_weight = t_line_item.total_weight
|
|
|
244 |
t_order.lineitems = [t_line_item]
|
|
|
245 |
|
|
|
246 |
t_order.status = OrderStatus.SUBMITTED_FOR_PROCESSING
|
|
|
247 |
t_order.statusDescription = "Submitted to warehouse"
|
|
|
248 |
t_order.created_timestamp = to_java_date(datetime.datetime.now())
|
|
|
249 |
|
|
|
250 |
logistics_client = LogisticsClient().get_client()
|
|
|
251 |
logistics_info = logistics_client.getLogisticsInfo(t_order.customer_pincode, t_line_item.sku_id)
|
|
|
252 |
|
|
|
253 |
t_order.logistics_provider_id = logistics_info.provider_id
|
|
|
254 |
t_order.airwaybill_no = logistics_info.airway_billno
|
|
|
255 |
t_order.tracking_id = t_order.airwaybill_no
|
|
|
256 |
t_order.expected_delivery_time = to_java_date(datetime.datetime.now()) + 60*60*1000*logistics_info.delivery_estimate
|
|
|
257 |
t_order.warehouse_id = logistics_info.warehouse_id
|
|
|
258 |
|
|
|
259 |
return t_order
|
|
|
260 |
|
| 636 |
rajveer |
261 |
def create_line_item(item_id):
|
| 576 |
chandransh |
262 |
inventory_client = InventoryClient().get_client()
|
| 636 |
rajveer |
263 |
item = inventory_client.getItem(item_id)
|
| 576 |
chandransh |
264 |
t_line_item = TLineItem()
|
| 636 |
rajveer |
265 |
t_line_item.model_name = item.modelName
|
|
|
266 |
t_line_item.model_number = item.modelNumber
|
|
|
267 |
t_line_item.extra_info = item.featureDescription
|
|
|
268 |
t_line_item.brand = item.manufacturerName
|
|
|
269 |
t_line_item.sku_id = item.vendorItemId
|
| 576 |
chandransh |
270 |
t_line_item.quantity = 1
|
| 636 |
rajveer |
271 |
t_line_item.unit_price = item.sellingPrice
|
|
|
272 |
t_line_item.unit_weight = item.weight
|
|
|
273 |
t_line_item.total_price = item.sellingPrice
|
|
|
274 |
t_line_item.total_weight = item.weight
|
| 576 |
chandransh |
275 |
return t_line_item
|
|
|
276 |
|
|
|
277 |
def get_address_string(address):
|
|
|
278 |
return address.line_1 + "\n" + address.line_2 + "\nLandmark: " + address.landmark + "\n" + address.city + "\n" + address.state + "\n" + address.country
|
|
|
279 |
|
|
|
280 |
def validate_cart(cartId):
|
|
|
281 |
inventory_client = InventoryClient().get_client()
|
|
|
282 |
logistics_client = LogisticsClient().get_client()
|
| 563 |
chandransh |
283 |
retval = True
|
| 557 |
chandransh |
284 |
# No need to validate duplicate items since there are only two ways
|
|
|
285 |
# to add items to a cart and both of them check whether the item being
|
|
|
286 |
# added is a duplicate of an already existing item.
|
| 563 |
chandransh |
287 |
cart = Cart.get_by(id=cartId)
|
|
|
288 |
cart_lines = cart.lines
|
| 576 |
chandransh |
289 |
customer_pincode = ""
|
|
|
290 |
if cart.address_id:
|
|
|
291 |
address = Address.get_by(id=cart.address_id)
|
|
|
292 |
customer_pincode = address.pin
|
| 563 |
chandransh |
293 |
for line in cart_lines:
|
| 612 |
chandransh |
294 |
old_estimate = line.estimate
|
| 636 |
rajveer |
295 |
item_id = line.item_id
|
|
|
296 |
if inventory_client.isActive(item_id):
|
| 576 |
chandransh |
297 |
if customer_pincode:
|
|
|
298 |
#FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
|
| 612 |
chandransh |
299 |
try:
|
| 636 |
rajveer |
300 |
item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, 1).shippingTime
|
| 612 |
chandransh |
301 |
except:
|
|
|
302 |
item_delivery_estimate = 72
|
|
|
303 |
if old_estimate != item_delivery_estimate:
|
|
|
304 |
line.estimate = item_delivery_estimate
|
|
|
305 |
retval = False
|
| 576 |
chandransh |
306 |
else:
|
| 563 |
chandransh |
307 |
line.delete()
|
|
|
308 |
retval = False
|
| 612 |
chandransh |
309 |
|
|
|
310 |
session.commit()
|
| 563 |
chandransh |
311 |
return retval
|
| 576 |
chandransh |
312 |
|
| 557 |
chandransh |
313 |
def merge_cart(fromCartId, toCartId):
|
|
|
314 |
fromCart = Cart.get_by(id=fromCartId)
|
|
|
315 |
toCart = Cart.get_by(id=toCartId)
|
|
|
316 |
|
|
|
317 |
old_lines = fromCart.lines
|
|
|
318 |
new_lines = toCart.lines
|
|
|
319 |
|
|
|
320 |
for line in old_lines:
|
|
|
321 |
flag = True
|
|
|
322 |
for new_line in new_lines:
|
|
|
323 |
if line.item_id == new_line.item_id:
|
|
|
324 |
flag = False
|
| 576 |
chandransh |
325 |
if flag:
|
| 557 |
chandransh |
326 |
toCart.lines.append(line)
|
|
|
327 |
|
|
|
328 |
toCart.updatedOn = datetime.datetime.now()
|
|
|
329 |
|
|
|
330 |
fromCart.expired_on = datetime.datetime.now()
|
|
|
331 |
fromCart.cart_status = CartStatus.INACTIVE
|
| 576 |
chandransh |
332 |
session.commit()
|