Subversion Repositories SmartDukaan

Rev

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

Rev 6903 Rev 6921
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.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
7
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, Discount, InsuranceDetails
12
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, \
-
 
13
    Discount, InsuranceDetails
8
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException,\
14
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
9
    PromotionException
15
    DeliveryType
-
 
16
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
-
 
17
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction, \
-
 
18
    TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem, \
10
import datetime
19
    OrderStatus
-
 
20
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
-
 
21
    ShoppingCartException, PromotionException
11
from shop2020.utils.Utils import to_py_date, to_java_date
22
from shop2020.utils.Utils import to_py_date, to_java_date
-
 
23
import datetime
12
 
24
 
13
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as TTransaction,\
-
 
14
    TransactionStatus as TTransactionStatus, Order as TOrder, LineItem as TLineItem, OrderStatus 
-
 
15
 
25
 
16
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, DeliveryType
-
 
17
 
26
 
18
from shop2020.clients.TransactionClient import TransactionClient
-
 
19
from shop2020.clients.CatalogClient import CatalogClient
-
 
20
from shop2020.clients.LogisticsClient import LogisticsClient
-
 
21
from shop2020.model.v1 import user
-
 
22
from shop2020.clients.PromotionClient import PromotionClient
-
 
23
 
27
 
24
def get_cart(userId):
28
def get_cart(userId):
25
    user = User.get_by(id=userId)
29
    user = User.get_by(id=userId)
26
    return user.active_cart
30
    return user.active_cart
27
 
31
 
Line 385... Line 389...
385
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
389
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
386
                line.quantity = 1
390
                line.quantity = 1
387
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
391
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
388
            
392
            
389
            line.actual_price = item.sellingPrice 
393
            line.actual_price = item.sellingPrice 
390
            cart.total_price = cart.total_price + (line.actual_price * line.quantity) + line.insuranceAmount
394
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
391
            try:
395
            try:
392
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
396
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
393
            except LogisticsServiceException:
397
            except LogisticsServiceException:
394
                item_delivery_estimate = -1
398
                item_delivery_estimate = -1
395
                #TODO Use the exception clause to set the retval appropriately
399
                #TODO Use the exception clause to set the retval appropriately
Line 403... Line 407...
403
    if cart.checked_out_on is not None:
407
    if cart.checked_out_on is not None:
404
        if cart.updated_on > cart.checked_out_on:
408
        if cart.updated_on > cart.checked_out_on:
405
            cart.checked_out_on = None
409
            cart.checked_out_on = None
406
    session.commit()
410
    session.commit()
407
    
411
    
-
 
412
    cart = Cart.get_by(id=cartId)
-
 
413
    cart_lines = cart.lines
-
 
414
    for line in cart_lines :
-
 
415
        if line.insurer > 0 :
-
 
416
            insure_item(line.item_id, cartId, True)
-
 
417
    
408
    if cart.coupon_code is not None:
418
    if cart.coupon_code is not None:
409
        try:
419
        try:
410
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
420
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
411
            totalPrice = 0
421
            totalInsuranceAmt = 0
412
            for t_line in updated_cart.lines:
422
            for t_line in updated_cart.lines:
413
            #Find the line in the database which corresponds to this line
423
            #Find the line in the database which corresponds to this line
414
                line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
424
                line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
415
            #Update its discounted price.
425
            #Update its discounted price.
416
                line.discounted_price = t_line.discountedPrice
426
                line.discounted_price = t_line.discountedPrice
417
            #If discounted price of line is set and this coupon is not a gift voucher that means
427
            #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.
428
            # we will need to correct the insurance price accordingly.
419
                if line.discounted_price and not promotion_client.isGiftVoucher(cart.coupon_code) :
429
#                if line.insurer > 0 and line.discounted_price and not promotion_client.isGiftVoucher(cart.coupon_code) :
-
 
430
#                    cc = CatalogClient().get_client()
420
                    line.insuranceAmount = round((line.insuranceAmount/line.actual_price) * line.discounted_price)
431
#                    insuranceAmt = cc.getInsuranceAmount(line.item_id, line.discounted_price, line.insurer, line.quantity)
421
                    totalPrice = totalPrice + line.actual_price * line.quantity + line.insuranceAmount
432
#                    line.insuranceAmount = insuranceAmt
422
                    cart.total_price = totalPrice
433
#                    totalInsuranceAmt += insuranceAmt
423
            cart.discounted_price = updated_cart.discountedPrice
434
            cart.discounted_price = updated_cart.discountedPrice
424
            session.commit()
435
            session.commit()
425
            if updated_cart.message is not None:
436
            if updated_cart.message is not None:
426
                emival = updated_cart.message
437
                emival = updated_cart.message
427
        except PromotionException as ex:
438
        except PromotionException as ex:
Line 535... Line 546...
535
    if not line:
546
    if not line:
536
        print("Error : No line found for cartId : " + cartId + " and itemId : " + itemId)
547
        print("Error : No line found for cartId : " + cartId + " and itemId : " + itemId)
537
        return False
548
        return False
538
    
549
    
539
    try:
550
    try:
540
        csc = CatalogClient().get_client()
-
 
541
        item = csc.getItem(itemId)
-
 
542
        insuranceAmount = csc.getInsuranceAmount(itemId, item.preferredInsurer, line.quantity)
-
 
543
        if toInsure:
551
        if toInsure:
-
 
552
            csc = CatalogClient().get_client()
-
 
553
            item = csc.getItem(itemId)
-
 
554
            insuranceAmount = csc.getInsuranceAmount(itemId, line.discounted_price if line.discounted_price else line.actual_price, item.preferredInsurer, line.quantity)
544
            line.insurer = item.preferredInsurer
555
            line.insurer = item.preferredInsurer
545
            line.insuranceAmount = insuranceAmount
556
            line.insuranceAmount = insuranceAmount
546
            cart.total_price = cart.total_price + insuranceAmount
557
            cart.total_price = cart.total_price + insuranceAmount
547
            if cart.discounted_price:
558
            if cart.discounted_price:
548
                cart.discounted_price = cart.discounted_price + insuranceAmount
559
                cart.discounted_price = cart.discounted_price - line.insuranceAmount + insuranceAmount
549
        else:
560
        else:
550
            cart.total_price = cart.total_price - insuranceAmount
561
            cart.total_price = cart.total_price - line.insuranceAmount
551
            if cart.discounted_price:
562
            if cart.discounted_price:
552
                cart.discounted_price = cart.discounted_price - insuranceAmount
563
                cart.discounted_price = cart.discounted_price - line.insuranceAmount
553
            line.insurer = 0
564
            line.insurer = 0
554
            line.insuranceAmount = 0
565
            line.insuranceAmount = 0
555
        line.updated_on = datetime.datetime.now()
566
        line.updated_on = datetime.datetime.now()
556
        cart.updated_on = datetime.datetime.now()
567
        cart.updated_on = datetime.datetime.now()
557
        session.commit()
568
        session.commit()