| Line 2... |
Line 2... |
| 2 |
Created on 10-May-2010
|
2 |
Created on 10-May-2010
|
| 3 |
|
3 |
|
| 4 |
@author: ashish
|
4 |
@author: ashish
|
| 5 |
'''
|
5 |
'''
|
| 6 |
from elixir import *
|
6 |
from elixir import *
|
| 7 |
from shop2020.model.v1.user.impl.Dataservice import Cart, Line
|
7 |
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User
|
| 8 |
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
|
8 |
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
|
| 9 |
import datetime
|
9 |
import datetime
|
| 10 |
from shop2020.utils.Utils import to_py_date
|
10 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
| 11 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction,\
|
- |
|
| 12 |
TransactionStatus
|
- |
|
| 13 |
|
11 |
|
| - |
|
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
|
| - |
|
15 |
|
| - |
|
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
|
| 14 |
|
23 |
|
| 15 |
def get_cart(user_id):
|
24 |
def get_cart(user_id):
|
| 16 |
query = Cart.query.filter_by(user_id=user_id)
|
25 |
query = Cart.query.filter_by(user_id=user_id)
|
| 17 |
query = query.filter_by(cart_status = CartStatus.ACTIVE)
|
26 |
query = query.filter_by(cart_status = CartStatus.ACTIVE)
|
| 18 |
try:
|
27 |
try:
|
| Line 167... |
Line 176... |
| 167 |
raise ShoppingCartException(101, "address id cannot be made null")
|
176 |
raise ShoppingCartException(101, "address id cannot be made null")
|
| 168 |
|
177 |
|
| 169 |
cart = get_cart_by_id(cart_id)
|
178 |
cart = get_cart_by_id(cart_id)
|
| 170 |
if not cart:
|
179 |
if not cart:
|
| 171 |
raise ShoppingCartException(101, "no cart for this id")
|
180 |
raise ShoppingCartException(101, "no cart for this id")
|
| - |
|
181 |
|
| - |
|
182 |
address = Address.get_by(id=address_id)
|
| - |
|
183 |
if not address:
|
| - |
|
184 |
raise ShoppingCartException(101, "No address for this id")
|
| - |
|
185 |
|
| 172 |
cart.address_id = address_id
|
186 |
cart.address_id = address_id
|
| 173 |
session.commit()
|
187 |
session.commit()
|
| 174 |
|
188 |
|
| 175 |
def commit_cart(cart_id, transaction_client):
|
189 |
def commit_cart(cart_id):
|
| 176 |
|
- |
|
| 177 |
cart = get_cart_by_id(cart_id)
|
190 |
cart = get_cart_by_id(cart_id)
|
| 178 |
cart.cart_status = CartStatus.COMMITTED
|
191 |
cart.cart_status = CartStatus.COMMITTED
|
| 179 |
cart.committed_on = datetime.datetime.now()
|
192 |
cart.committed_on = datetime.datetime.now()
|
| 180 |
session.commit()
|
193 |
|
| 181 |
#now we have a cart. Need to create a transaction with it
|
194 |
#now we have a cart. Need to create a transaction with it
|
| 182 |
#transaction = transaction_client.create_transaction(cart)
|
195 |
txn = TTransaction()
|
| 183 |
return True
|
196 |
txn.paymentInfo = TPaymentInfo()
|
| 184 |
"""
|
- |
|
| 185 |
transaction.shoppingCartid = cart_id
|
197 |
txn.shoppingCartid = cart_id
|
| 186 |
transaction.customer_id = cart.user_id
|
198 |
txn.customer_id = cart.user_id
|
| 187 |
transaction.transactionStatus = TransactionStatus.INIT
|
199 |
txn.createdOn = to_java_date(datetime.datetime.now())
|
| 188 |
transaction.statusDescription = "Transaction submitted to warehouse"
|
200 |
txn.transactionStatus = TTransactionStatus.INIT
|
| 189 |
transaction.created_on = datetime.datetime.now()
|
201 |
txn.statusDescription = "New Order"
|
| 190 |
transaction.warehouse_id = 1;
|
- |
|
| 191 |
|
202 |
|
| - |
|
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):
|
| 192 |
cart_lines = cart.lines
|
216 |
cart_lines = cart.lines
|
| 193 |
for line in cart_lines:
|
- |
|
| 194 |
if line.status == LineStatus.LINE_ACTIVE:
|
- |
|
| 195 |
#line is active
|
- |
|
| 196 |
line_item = get_line_item(cartLine.getItemId());
|
- |
|
| 197 |
// create orders equivalent to quantity. Create one order per item.
|
- |
|
| 198 |
for(int i= 0; i<cartLine.getQuantity(); i++){
|
- |
|
| 199 |
// set order
|
- |
|
| 200 |
Order order = getOrder(c.getAddressId(), lineItem);
|
- |
|
| 201 |
order.addToLineitems(lineItem);
|
- |
|
| 202 |
orders.add(order);
|
217 |
orders = []
|
| 203 |
"""
|
- |
|
| 204 |
|
218 |
|
| - |
|
219 |
for line in cart_lines:
|
| - |
|
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()
|
| - |
|
233 |
|
| - |
|
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 |
|
| - |
|
261 |
def create_line_item(catalog_item_id):
|
| - |
|
262 |
inventory_client = InventoryClient().get_client()
|
| - |
|
263 |
catalog_item = inventory_client.getItemByCatalogId(catalog_item_id)
|
| - |
|
264 |
t_line_item = TLineItem()
|
| - |
|
265 |
t_line_item.model_name = catalog_item.modelName
|
| - |
|
266 |
t_line_item.model_number = catalog_item.modelNumber
|
| - |
|
267 |
t_line_item.extra_info = catalog_item.featureDescription
|
| - |
|
268 |
t_line_item.brand = catalog_item.manufacturerName
|
| - |
|
269 |
t_line_item.sku_id = catalog_item.vendorItemId
|
| - |
|
270 |
t_line_item.quantity = 1
|
| - |
|
271 |
t_line_item.unit_price = catalog_item.sellingPrice
|
| - |
|
272 |
t_line_item.unit_weight = catalog_item.weight
|
| - |
|
273 |
t_line_item.total_price = catalog_item.sellingPrice
|
| - |
|
274 |
t_line_item.total_weight = catalog_item.weight
|
| - |
|
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 |
|
| 205 |
def validate_cart(cartId, inventory_client):
|
280 |
def validate_cart(cartId):
|
| - |
|
281 |
inventory_client = InventoryClient().get_client()
|
| - |
|
282 |
logistics_client = LogisticsClient().get_client()
|
| 206 |
retval = True
|
283 |
retval = True
|
| 207 |
# No need to validate duplicate items since there are only two ways
|
284 |
# No need to validate duplicate items since there are only two ways
|
| 208 |
# to add items to a cart and both of them check whether the item being
|
285 |
# to add items to a cart and both of them check whether the item being
|
| 209 |
# added is a duplicate of an already existing item.
|
286 |
# added is a duplicate of an already existing item.
|
| 210 |
cart = Cart.get_by(id=cartId)
|
287 |
cart = Cart.get_by(id=cartId)
|
| - |
|
288 |
old_estimate = cart.estimate
|
| - |
|
289 |
new_estimate = 0
|
| 211 |
cart_lines = cart.lines
|
290 |
cart_lines = cart.lines
|
| - |
|
291 |
customer_pincode = ""
|
| - |
|
292 |
if cart.address_id:
|
| - |
|
293 |
address = Address.get_by(id=cart.address_id)
|
| - |
|
294 |
customer_pincode = address.pin
|
| 212 |
for line in cart_lines:
|
295 |
for line in cart_lines:
|
| 213 |
item_id = line.item_id
|
296 |
catalog_item_id = line.item_id
|
| 214 |
if not inventory_client.is_active(item_id):
|
297 |
if inventory_client.isActive(catalog_item_id):
|
| - |
|
298 |
if customer_pincode:
|
| - |
|
299 |
#FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
|
| - |
|
300 |
item_delivery_estimate = logistics_client.getLogisticsEstimation(catalog_item_id, customer_pincode, 1).shippingTime
|
| - |
|
301 |
if new_estimate < item_delivery_estimate:
|
| - |
|
302 |
new_estimate = item_delivery_estimate
|
| - |
|
303 |
else:
|
| 215 |
line.delete()
|
304 |
line.delete()
|
| 216 |
retval = False
|
305 |
retval = False
|
| - |
|
306 |
|
| - |
|
307 |
if new_estimate != old_estimate:
|
| - |
|
308 |
cart.estimate = new_estimate
|
| - |
|
309 |
session.commit()
|
| - |
|
310 |
retval = False
|
| 217 |
return retval
|
311 |
return retval
|
| 218 |
|
312 |
|
| 219 |
def merge_cart(fromCartId, toCartId):
|
313 |
def merge_cart(fromCartId, toCartId):
|
| 220 |
fromCart = Cart.get_by(id=fromCartId)
|
314 |
fromCart = Cart.get_by(id=fromCartId)
|
| 221 |
toCart = Cart.get_by(id=toCartId)
|
315 |
toCart = Cart.get_by(id=toCartId)
|
| 222 |
|
316 |
|
| 223 |
old_lines = fromCart.lines
|
317 |
old_lines = fromCart.lines
|
| Line 226... |
Line 320... |
| 226 |
for line in old_lines:
|
320 |
for line in old_lines:
|
| 227 |
flag = True
|
321 |
flag = True
|
| 228 |
for new_line in new_lines:
|
322 |
for new_line in new_lines:
|
| 229 |
if line.item_id == new_line.item_id:
|
323 |
if line.item_id == new_line.item_id:
|
| 230 |
flag = False
|
324 |
flag = False
|
| 231 |
if flag:
|
325 |
if flag:
|
| 232 |
toCart.lines.append(line)
|
326 |
toCart.lines.append(line)
|
| 233 |
|
327 |
|
| 234 |
toCart.updatedOn = datetime.datetime.now()
|
328 |
toCart.updatedOn = datetime.datetime.now()
|
| 235 |
|
329 |
|
| 236 |
fromCart.expired_on = datetime.datetime.now()
|
330 |
fromCart.expired_on = datetime.datetime.now()
|
| 237 |
fromCart.cart_status = CartStatus.INACTIVE
|
331 |
fromCart.cart_status = CartStatus.INACTIVE
|
| 238 |
session.commit()
|
- |
|
| 239 |
"""
|
- |
|
| 240 |
t_line.id = line.id
|
- |
|
| 241 |
t_line.itemId = line.item_id
|
- |
|
| 242 |
t_line.quantity = line.quantity
|
- |
|
| 243 |
t_line.createdOn = to_java_date(line.created_on)
|
- |
|
| 244 |
t_line.updatedOn = to_java_date(line.updated_on)
|
- |
|
| 245 |
t_line.lineStatus = line.line_status
|
- |
|
| 246 |
"""
|
- |
|
| 247 |
|
332 |
session.commit()
|
| - |
|
333 |
|
| 248 |
|
334 |
|