Subversion Repositories SmartDukaan

Rev

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

Rev 12904 Rev 13136
Line 3... Line 3...
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
7
from shop2020.clients.CatalogClient import CatalogClient
-
 
8
from shop2020.clients.InventoryClient import InventoryClient
8
from shop2020.clients.LogisticsClient import LogisticsClient
9
from shop2020.clients.LogisticsClient import LogisticsClient
9
from shop2020.clients.PromotionClient import PromotionClient
10
from shop2020.clients.PromotionClient import PromotionClient
10
from shop2020.clients.TransactionClient import TransactionClient
11
from shop2020.clients.TransactionClient import TransactionClient
11
from shop2020.model.v1 import user
12
from shop2020.model.v1 import user
12
from shop2020.model.v1.user.impl.Converters import to_t_cart
13
from shop2020.model.v1.user.impl.Converters import to_t_cart, to_t_line
13
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, \
14
from shop2020.model.v1.user.impl.Dataservice import Cart, Line, Address, User, \
14
    Discount, InsuranceDetails, PrivateDealUser
15
    Discount, InsuranceDetails, PrivateDealUser
15
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
16
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
16
    DeliveryType
17
    DeliveryType
17
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, InsurerType
18
from shop2020.thriftpy.model.v1.catalog.ttypes import Item, InsurerType
Line 20... Line 21...
20
    OrderStatus, OrderSource
21
    OrderStatus, OrderSource
21
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
22
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, \
22
    ShoppingCartException, PromotionException, CartPlus
23
    ShoppingCartException, PromotionException, CartPlus
23
from shop2020.utils.Utils import to_py_date, to_java_date
24
from shop2020.utils.Utils import to_py_date, to_java_date
24
import datetime
25
import datetime
25
from shop2020.clients.InventoryClient import InventoryClient
-
 
26
 
26
 
27
 
27
 
28
 
28
 
29
 
29
 
30
def get_cart(userId):
30
def get_cart(userId):
Line 205... Line 205...
205
    if not cart:
205
    if not cart:
206
        raise ShoppingCartException(101, "no cart attached to this id")
206
        raise ShoppingCartException(101, "no cart attached to this id")
207
    pc = PromotionClient().get_client()
207
    pc = PromotionClient().get_client()
208
    for t_line in t_cart.lines:
208
    for t_line in t_cart.lines:
209
        line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
209
        line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
-
 
210
        line.discounted_price = None
210
        if not pc.isGiftVoucher(coupon_code):
211
        if not pc.isGiftVoucher(coupon_code):
211
            line.discounted_price = t_line.discountedPrice
212
            line.discounted_price = t_line.discountedPrice
212
        line.dealText = t_line.dealText
213
        #line.dealText = t_line.dealText
213
        line.freebieId = t_line.freebieId
214
        #line.freebieId = t_line.freebieId
214
    
215
    
215
    cart.total_price = t_cart.totalPrice
216
    cart.total_price = t_cart.totalPrice
216
    cart.discounted_price = t_cart.discountedPrice
217
    cart.discounted_price = t_cart.discountedPrice
217
    cart.coupon_code = coupon_code
218
    cart.coupon_code = coupon_code
218
    session.commit()
219
    session.commit()
Line 406... Line 407...
406
        store = logistics_client.getPickupStore(cart.pickupStoreId)
407
        store = logistics_client.getPickupStore(cart.pickupStoreId)
407
        customer_pincode = store.pin
408
        customer_pincode = store.pin
408
    if cart.address_id != None and customer_pincode == None:
409
    if cart.address_id != None and customer_pincode == None:
409
        address = Address.get_by(id=cart.address_id)
410
        address = Address.get_by(id=cart.address_id)
410
        customer_pincode = address.pin
411
        customer_pincode = address.pin
-
 
412
 
-
 
413
    user = User.get_by(active_cart_id = cartId)
-
 
414
    
-
 
415
    dealItems = []
411
    print("couponCode---" + `couponCode` + "   cart.coupon_code----" + `cart.coupon_code`)   
416
    privateDealUser = PrivateDealUser.get_by(id=user.userId)    
412
    if couponCode and not cart.coupon_code:
417
    if privateDealUser is not None and privateDealUser.isActive:
413
        print 'auto application is on'
418
        itemIds = [cartLine.itemId for cartLine in cart.lines]
-
 
419
        deals = inventory_client.getAllActivePrivateDeals(itemIds, 0)
414
        cart.coupon_code = couponCode
420
        dealItems = deals.keys()
-
 
421
 
415
    if not customer_pincode:
422
    if not customer_pincode:
416
        user = User.get_by(active_cart_id = cartId)
-
 
417
        default_address_id = user.default_address_id
423
        default_address_id = user.default_address_id
418
        if default_address_id:
424
        if default_address_id:
419
            address = Address.get_by(id = default_address_id)
425
            address = Address.get_by(id = default_address_id)
420
            customer_pincode = address.pin
426
            customer_pincode = address.pin
421
    if not customer_pincode:
427
    if not customer_pincode:
Line 424... Line 430...
424
    cart.total_price = 0
430
    cart.total_price = 0
425
    for line in cart_lines:
431
    for line in cart_lines:
426
        old_estimate = line.estimate
432
        old_estimate = line.estimate
427
        item_id = line.item_id
433
        item_id = line.item_id
428
        item = inventory_client.getItemForSource(item_id, sourceId)
434
        item = inventory_client.getItemForSource(item_id, sourceId)
-
 
435
        
429
        item_shipping_info = inventory_client.isActive(item_id) 
436
        item_shipping_info = inventory_client.isActive(item_id) 
430
        if item_shipping_info.isActive:
437
        if item_shipping_info.isActive:
431
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
438
            if item_shipping_info.isRisky and item_shipping_info.quantity < line.quantity:
432
                line.quantity = 1
439
                line.quantity = 1
433
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
440
                retval = "Try adding a smaller quantity of " + item.brand + " " + item.modelNumber + " (" + item.color + ")"
434
            
441
                 
-
 
442
            if item_id in dealItems:
-
 
443
                line.actual_price = deals[item_id].dealPrice
-
 
444
                if deals[item_id].dealTextOption==0:
-
 
445
                    line.dealText = ''
-
 
446
                if deals[item_id].dealTextOption==2:
-
 
447
                    line.dealText =  deals[item_id].dealText
-
 
448
                    
-
 
449
                if deals[item_id].dealFreebieOption==0:
-
 
450
                    line.freebieId = 0
-
 
451
                if deals[item_id].dealFreebieOption==2:
-
 
452
                    line.freebieId =  deals[item_id].dealFreebieItemId
-
 
453
 
-
 
454
            else:
435
            line.actual_price = item.sellingPrice 
455
                line.actual_price = item.sellingPrice 
-
 
456
                line.dealText = None
-
 
457
                line.freebieId = None
436
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
458
            cart.total_price = cart.total_price + (line.actual_price * line.quantity)
437
            try:
459
            try:
438
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
460
                item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, DeliveryType.PREPAID).deliveryTime
439
            except LogisticsServiceException:
461
            except LogisticsServiceException:
440
                item_delivery_estimate = -1
462
                item_delivery_estimate = -1
Line 479... Line 501...
479
    if cart.checked_out_on is not None:
501
    if cart.checked_out_on is not None:
480
        if cart.updated_on > cart.checked_out_on:
502
        if cart.updated_on > cart.checked_out_on:
481
            cart.checked_out_on = None
503
            cart.checked_out_on = None
482
    session.commit()
504
    session.commit()
483
    
505
    
484
    cart = Cart.get_by(id=cartId)
-
 
485
    cart_lines = cart.lines
-
 
486
    for line in cart_lines :
-
 
487
        if line.insurer > 0 :
-
 
488
            insure_item(line.item_id, cartId, True, InsurerType._NAMES_TO_VALUES.get("DEVICE"))
-
 
489
        if line.dataProtectionInsurer > 0:
-
 
490
            insure_item(line.item_id, cartId, True, InsurerType._NAMES_TO_VALUES.get("DATA"))
-
 
491
    if cart.coupon_code is not None:
506
    if cart.coupon_code is not None:
492
        try:
507
        try:
493
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
508
            updated_cart = promotion_client.applyCoupon(cart.coupon_code, cart.id)
494
#            totalInsuranceAmt = 0
-
 
495
#            for t_line in updated_cart.lines:
-
 
496
#            #Find the line in the database which corresponds to this line
-
 
497
#                line = Line.query.filter_by(cart = cart).filter_by(item_id = t_line.itemId).one()
-
 
498
#            #Update its discounted price.
-
 
499
#                line.discounted_price = t_line.discountedPrice
-
 
500
#            #If discounted price of line is set and this coupon is not a gift voucher that means
-
 
501
#            # we will need to correct the insurance price accordingly.
-
 
502
##                if line.insurer > 0 and line.discounted_price and not promotion_client.isGiftVoucher(cart.coupon_code) :
-
 
503
##                    cc = CatalogClient().get_client()
-
 
504
##                    insuranceAmt = cc.getInsuranceAmount(line.item_id, line.discounted_price, line.insurer, line.quantity)
-
 
505
##                    line.insuranceAmount = insuranceAmt
-
 
506
##                    totalInsuranceAmt += insuranceAmt
-
 
507
#            cart.total_price = updated_cart.totalPrice
-
 
508
#            cart.discounted_price = updated_cart.discountedPrice
-
 
509
#            session.commit()
-
 
510
            if updated_cart.message is not None:
509
            if updated_cart.message is not None:
511
                emival = updated_cart.message
510
                emival = updated_cart.message
512
        except PromotionException as ex:
511
        except PromotionException as ex:
513
            remove_coupon(cart.id)
512
            remove_coupon(cart.id)
514
            #retval = ex.message
513
            #retval = ex.message
-
 
514
 
-
 
515
    
-
 
516
    cart = Cart.get_by(id=cartId)
-
 
517
    cart_lines = cart.lines
-
 
518
    map_lines = {}
-
 
519
    
-
 
520
    insurerFlag = False
-
 
521
    for line in cart_lines:
-
 
522
        if line.insurer > 0 or line.dataProtectionInsurer > 0:
-
 
523
            line_map = {}
-
 
524
            line_map['insurer'] = line.insurer
-
 
525
            line_map['dpinsurer'] = line.dataProtectionInsurer
-
 
526
            line_map['amount'] = line.discounted_price if line.discounted_price else line.actual_price
-
 
527
            line_map['quantity'] = line.quantity
-
 
528
            insurerFlag = True
-
 
529
            map_lines[line.item_id] = line_map
-
 
530
        else:
-
 
531
            continue
-
 
532
        
-
 
533
    if insurerFlag:
-
 
534
        line_map = inventory_client.checkServices(map_lines)
-
 
535
        for line in cart_lines :
-
 
536
            if line_map.has_key(line.item_id):
-
 
537
                line = line_map[line.item_id]
-
 
538
                if line['insurer'] > 0:
-
 
539
                    if cart.discounted_price:
-
 
540
                        cart.discounted_price = cart.discounted_price + line['insureramount']
-
 
541
                    line.insurer = line['insurer']
-
 
542
                    line.insuranceAmount = line['insureramount']
-
 
543
                    cart.total_price = cart.total_price + line['insureramount']
-
 
544
                if line['dpinsurer'] > 0:
-
 
545
                    if cart.discounted_price:
-
 
546
                        cart.discounted_price = cart.discounted_price + line['dpinsureramount']
-
 
547
                    line.dataProtectionInsurer = line['dpinsurer']
-
 
548
                    line.dataProtectionAmount = line['dpinsureramount']
-
 
549
                    cart.total_price = cart.total_price + line['dpinsureramount']
-
 
550
                line.updated_on = datetime.datetime.now()
-
 
551
        cart.updated_on = datetime.datetime.now()
515
    session.close()
552
    session.close()
516
    return [retval, emival]
553
    return [retval, emival]
517
 
554
 
518
def merge_cart(fromCartId, toCartId):
555
def merge_cart(fromCartId, toCartId):
519
    fromCart = Cart.get_by(id=fromCartId)
556
    fromCart = Cart.get_by(id=fromCartId)