Subversion Repositories SmartDukaan

Rev

Rev 6821 | Rev 6921 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6821 Rev 6903
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, Address, User, Discount
7
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, Discount, InsuranceDetails
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
    PromotionException
9
    PromotionException
10
import datetime
10
import datetime
11
from shop2020.utils.Utils import to_py_date, to_java_date
11
from shop2020.utils.Utils import to_py_date, to_java_date
12
 
12
 
Line 91... Line 91...
91
    current_time = datetime.datetime.now()
91
    current_time = datetime.datetime.now()
92
    cart.updated_on = current_time
92
    cart.updated_on = current_time
93
    line = get_line(item_id, cart_id, None,True)
93
    line = get_line(item_id, cart_id, None,True)
94
    if line:
94
    if line:
95
        #change the quantity only
95
        #change the quantity only
-
 
96
        line.insuranceAmount = (line.insuranceAmount/line.quantity) * quantity
96
        line.quantity = quantity
97
        line.quantity = quantity
97
        line.updated_on = current_time
98
        line.updated_on = current_time
98
    else:
99
    else:
99
        line = Line()
100
        line = Line()
100
        line.cart = cart
101
        line.cart = cart
Line 102... Line 103...
102
        line.quantity = quantity
103
        line.quantity = quantity
103
        line.created_on = current_time
104
        line.created_on = current_time
104
        line.updated_on = current_time
105
        line.updated_on = current_time
105
        line.actual_price = item.sellingPrice
106
        line.actual_price = item.sellingPrice
106
        line.line_status = LineStatus.LINE_ACTIVE
107
        line.line_status = LineStatus.LINE_ACTIVE
-
 
108
        line.insurer = 0
-
 
109
        line.insuranceAmount = 0
107
    session.commit()
110
    session.commit()
108
    return retval
111
    return retval
109
 
112
 
110
def delete_item_from_cart(cart_id, item_id):
113
def delete_item_from_cart(cart_id, item_id):
111
    if not item_id:
114
    if not item_id:
Line 242... Line 245...
242
            pc = PromotionClient().get_client()
245
            pc = PromotionClient().get_client()
243
            isGv = pc.isGiftVoucher(cart.coupon_code)
246
            isGv = pc.isGiftVoucher(cart.coupon_code)
244
        except:
247
        except:
245
            isGv = False
248
            isGv = False
246
        
249
        
-
 
250
    insuranceDetails = InsuranceDetails.get_by(addressId = cart.address_id)
-
 
251
    
247
    for line in cart_lines:
252
    for line in cart_lines:
248
        if line.line_status == LineStatus.LINE_ACTIVE:
253
        if line.line_status == LineStatus.LINE_ACTIVE:
249
            quantity_remaining_for_order = line.quantity
254
            quantity_remaining_for_order = line.quantity
250
        
255
        
251
            for discount in line.discounts:
256
            for discount in line.discounts:
252
                i = 0
257
                i = 0
253
                while i < discount.quantity:
258
                while i < discount.quantity:
254
                    t_line_item = create_line_item(line.item_id, line.actual_price if isGv else (line.actual_price - discount.discount))
259
                    t_line_item = create_line_item(line.item_id, line.actual_price if isGv else (line.actual_price - discount.discount))
255
                    t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, discount.discount if isGv else 0)
260
                    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)
256
                    orders.append(t_order)
261
                    orders.append(t_order)
257
                    i += 1
262
                    i += 1
258
                quantity_remaining_for_order -= discount.quantity
263
                quantity_remaining_for_order -= discount.quantity
259
            
264
            
260
            i = 0
265
            i = 0
261
            while i < quantity_remaining_for_order:
266
            while i < quantity_remaining_for_order:
262
                t_line_item = create_line_item(line.item_id, line.actual_price)
267
                t_line_item = create_line_item(line.item_id, line.actual_price)
263
                t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, 0)
268
                t_order = create_order(userId, cart.address_id, t_line_item, cart.pickupStoreId, 0, line.insurer, (line.insuranceAmount/line.quantity), insuranceDetails)
264
                orders.append(t_order)
269
                orders.append(t_order)
265
                i += 1
270
                i += 1
266
    return orders
271
    return orders
267
 
272
 
268
def create_order(userId, address_id, t_line_item, pickupStoreId, gvAmount):
273
def create_order(userId, address_id, t_line_item, pickupStoreId, gvAmount, insurer, insuranceAmount, insuranceDetails):
269
    user = User.get_by(id=userId)
274
    user = User.get_by(id=userId)
270
    address = Address.get_by(id=address_id)
275
    address = Address.get_by(id=address_id)
271
    t_order = TOrder()
276
    t_order = TOrder()
272
    
277
    
273
    t_order.customer_id = user.id
278
    t_order.customer_id = user.id
Line 279... Line 284...
279
    t_order.customer_address2 = address.line_2
284
    t_order.customer_address2 = address.line_2
280
    t_order.customer_city = address.city
285
    t_order.customer_city = address.city
281
    t_order.customer_state = address.state
286
    t_order.customer_state = address.state
282
    t_order.customer_mobilenumber = address.phone
287
    t_order.customer_mobilenumber = address.phone
283
    
288
    
284
    t_order.total_amount = t_line_item.total_price
289
    t_order.total_amount = t_line_item.total_price + insuranceAmount
285
    t_order.gvAmount = gvAmount
290
    t_order.gvAmount = gvAmount
286
    
291
    
287
    t_order.total_weight = t_line_item.total_weight
292
    t_order.total_weight = t_line_item.total_weight
288
    t_order.lineitems = [t_line_item]
293
    t_order.lineitems = [t_line_item]
289
    
294
    
290
    t_order.status = OrderStatus.PAYMENT_PENDING
295
    t_order.status = OrderStatus.PAYMENT_PENDING
291
    t_order.statusDescription = "Payment Pending"
296
    t_order.statusDescription = "Payment Pending"
292
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
297
    t_order.created_timestamp = to_java_date(datetime.datetime.now())
293
    
298
    
294
    t_order.pickupStoreId = pickupStoreId 
299
    t_order.pickupStoreId = pickupStoreId 
-
 
300
    t_order.insuranceAmount = insuranceAmount 
-
 
301
    t_order.insurer = insurer
-
 
302
    t_order.dob = insuranceDetails.dob
-
 
303
    t_order.guardianName = insuranceDetails.guardianName
295
    return t_order
304
    return t_order
296
        
305
        
297
def create_line_item(item_id, final_price, quantity=1):
306
def create_line_item(item_id, final_price, quantity=1):
298
    inventory_client = CatalogClient().get_client()
307
    inventory_client = CatalogClient().get_client()
299
    item = inventory_client.getItem(item_id)
308
    item = inventory_client.getItem(item_id)
Line 376... Line 385...
376
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
385
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
377
                line.quantity = 1
386
                line.quantity = 1
378
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
387
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
379
            
388
            
380
            line.actual_price = item.sellingPrice 
389
            line.actual_price = item.sellingPrice 
381
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
390
            cart.total_price = cart.total_price + (line.actual_price * line.quantity) + line.insuranceAmount
382
            try:
391
            try:
383
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
392
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
384
            except LogisticsServiceException:
393
            except LogisticsServiceException:
385
                item_delivery_estimate = -1
394
                item_delivery_estimate = -1
386
                #TODO Use the exception clause to set the retval appropriately
395
                #TODO Use the exception clause to set the retval appropriately
Line 397... Line 406...
397
    session.commit()
406
    session.commit()
398
    
407
    
399
    if cart.coupon_code is not None:
408
    if cart.coupon_code is not None:
400
        try:
409
        try:
401
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
410
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
-
 
411
            totalPrice = 0
402
            for t_line in updated_cart.lines:
412
            for t_line in updated_cart.lines:
403
            #Find the line in the database which corresponds to this line
413
            #Find the line in the database which corresponds to this line
404
                line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
414
                line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
405
            #Update its discounted price.
415
            #Update its discounted price.
406
                line.discounted_price = t_line.discountedPrice
416
                line.discounted_price = t_line.discountedPrice
-
 
417
            #If discounted price of line is set and this coupon is not a gift voucher that means
-
 
418
            # we will need to correct the insurance price accordingly.
-
 
419
                if line.discounted_price and not promotion_client.isGiftVoucher(cart.coupon_code) :
-
 
420
                    line.insuranceAmount = round((line.insuranceAmount/line.actual_price) * line.discounted_price)
-
 
421
                    totalPrice = totalPrice + line.actual_price * line.quantity + line.insuranceAmount
-
 
422
                    cart.total_price = totalPrice
407
            cart.discounted_price = updated_cart.discountedPrice
423
            cart.discounted_price = updated_cart.discountedPrice
408
            session.commit()
424
            session.commit()
409
            if updated_cart.message is not None:
425
            if updated_cart.message is not None:
410
                emival = updated_cart.message
426
                emival = updated_cart.message
411
        except PromotionException as ex:
427
        except PromotionException as ex:
Line 505... Line 521...
505
    '''line = Line.query.filter_by(item_id = itemId, created_on > to_py_date(startDate), created_on < to_py_date(endDate)).first()'''
521
    '''line = Line.query.filter_by(item_id = itemId, created_on > to_py_date(startDate), created_on < to_py_date(endDate)).first()'''
506
    if line is not None:
522
    if line is not None:
507
        return True
523
        return True
508
    else:
524
    else:
509
        return False
525
        return False
-
 
526
 
-
 
527
def insure_item(itemId, cartId, toInsure):
-
 
528
    cart = Cart.get_by(id = cartId)
-
 
529
    line = None
-
 
530
    for cartLine in cart.lines:
-
 
531
        if(cartLine.item_id == itemId):
-
 
532
            line = cartLine
-
 
533
            break
-
 
534
        
-
 
535
    if not line:
-
 
536
        print("Error : No line found for cartId : " + cartId + " and itemId : " + itemId)
-
 
537
        return False
-
 
538
    
-
 
539
    try:
-
 
540
        csc = CatalogClient().get_client()
-
 
541
        item = csc.getItem(itemId)
-
 
542
        insuranceAmount = csc.getInsuranceAmount(itemId, item.preferredInsurer, line.quantity)
-
 
543
        if toInsure:
-
 
544
            line.insurer = item.preferredInsurer
-
 
545
            line.insuranceAmount = insuranceAmount
-
 
546
            cart.total_price = cart.total_price + insuranceAmount
-
 
547
            if cart.discounted_price:
-
 
548
                cart.discounted_price = cart.discounted_price + insuranceAmount
-
 
549
        else:
-
 
550
            cart.total_price = cart.total_price - insuranceAmount
-
 
551
            if cart.discounted_price:
-
 
552
                cart.discounted_price = cart.discounted_price - insuranceAmount
-
 
553
            line.insurer = 0
-
 
554
            line.insuranceAmount = 0
-
 
555
        line.updated_on = datetime.datetime.now()
-
 
556
        cart.updated_on = datetime.datetime.now()
-
 
557
        session.commit()
-
 
558
    except:
-
 
559
        print("Error : Unable to insure")
-
 
560
        print("insurerId : " + str(item.preferredInsurer) + " ItemId : " + str(itemId) + " CartId : " + str(cartId))
-
 
561
        return False
-
 
562
    
-
 
563
    return True
-
 
564
 
-
 
565
def cancel_insurance(cartId):
-
 
566
    try:
-
 
567
        cart = Cart.get_by(id = cartId)
-
 
568
        for cartLine in cart.lines:
-
 
569
            cart.total_price = cart.total_price - cartLine.insuranceAmount
-
 
570
            if cart.discounted_price:
-
 
571
                cart.discounted_price = cart.discounted_price - cartLine.insuranceAmount
-
 
572
            cartLine.insurer = 0
-
 
573
            cartLine.insuranceAmount = 0
-
 
574
            cartLine.updated_on = datetime.datetime.now()
-
 
575
        cart.updated_on = datetime.datetime.now()
-
 
576
        session.commit()
-
 
577
    except:
-
 
578
        print("Error : Unable to cancel insurance for cartId :" + str(cartId))
-
 
579
        return False
510
    
580
    
-
 
581
    return True
-
 
582
 
-
 
583
def store_insurance_specific_details(addressId, dob, guardianName):
-
 
584
    try:
-
 
585
        insuranceDetails = InsuranceDetails.get_by(addressId = addressId);
-
 
586
        if insuranceDetails is None :
-
 
587
            insuranceDetails = InsuranceDetails()
-
 
588
        insuranceDetails.addressId = addressId
-
 
589
        insuranceDetails.dob = dob
-
 
590
        insuranceDetails.guardianName = guardianName
-
 
591
        session.commit()
-
 
592
    except:
-
 
593
        print("Error : Unable to store insurance details for addressId : " + str(addressId))
-
 
594
        return False
-
 
595
    return True
-
 
596
 
-
 
597
def is_insurance_detail_present(addressId):
-
 
598
    try:
-
 
599
        insuranceDetails = InsuranceDetails.get_by(addressId = addressId);
-
 
600
        if insuranceDetails is None :
-
 
601
            return False
-
 
602
    except:
-
 
603
        print("Error : Unable to get insurance details for addressId : " + str(addressId))
-
 
604
        return False
-
 
605
    return True
-
 
606
 
511
def close_session():
607
def close_session():
512
    if session.is_active:
608
    if session.is_active:
513
        print "session is active. closing it."
609
        print "session is active. closing it."
514
        session.close()
610
        session.close()
515
611