Subversion Repositories SmartDukaan

Rev

Rev 5298 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 28-May-2012

@author: Varun Gupta

Chat 322 C3222        Discount: Rs.121
Champ Deluxe Duos C3312       Discount: Rs.191
Metro C3530       Discount: Rs.251
Champ Duos E2652       Discount: Rs.120
Chat 527 S5270       Discount: Rs.121
Champ 3.5G S3770       Discount: Rs.215
Star 3 Duos S5222       Discount: Rs.161
Primo S5610       Discount: Rs.241
Star II S5263       Discount: Rs.301
Corby II S3850       Discount: Rs.365
Star II Duos C6712       Discount: Rs.251
Wave 525 S5253       Discount: Rs.391
Galaxy Pocket S5300       Discount: Rs.101
Wave Y S5380       Discount: Rs.341
Galaxy Y S5360       Discount: Rs.415
Galaxy Y Color S5360s       Discount: Rs.199
Galaxy Y Duos S6102       Discount: Rs.595
Galaxy Y Pro Duos B5512       Discount: Rs.502
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user

def execute(cart, coupon_code, args):
    #Allow only first 100 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    
    if count_coupon_usage >= 100:
        raise PromotionException(112, 'This promotion is over.')
    
    if get_coupon_usage_count_by_user(coupon_code, cart.userId) > 0:
        raise PromotionException(112, 'This promotion is over.')
    
    discounts = []
    
    if cart.lines:
        total_selling_price = 0
        total_discounted_price = 0
        
        has_qualified_model = False
        
        for line in cart.lines:
            
            line.discountedPrice = line.actualPrice
            discount_value = 0
            
            if line.itemId in (1010, 1451):  #Chat 322 C3222
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 121
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (3356, 4628, 5730, 2563, 3357):  #Champ Deluxe Duos C3312
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 191
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (1012, 1452, 1453):  #Metro C3530
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 251
                    line.discountedPrice = round(line.actualPrice - discount_value)
             
            elif line.itemId in (999, 988, 5702):  #Champ Duos E2652
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 120
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2576, 2067):  #Chat 527 S5270
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 121
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2073, 2621):  #Champ 3.5G S3770
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 215
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (3358, 4643):  #Star 3 Duos S5222
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 161
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2090, 2256):  #Primo S5610
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 241
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (968, 995):  #Star II S5263
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 301
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (996, 1000, 1524, 1563):  #Corby II S3850
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 365
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (1537, 2227):  #Star II Duos C6712
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 251
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (949, 4690, 948):  #Wave 525 S5253
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 391
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId == 5834:  #Galaxy Pocket S5300
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 101
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2537, 2622):  #Wave Y S5380
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 341
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2091, 3882, 2527):  #Galaxy Y S5360
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 415
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId == 2446:  #Galaxy Y Color S5360s
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 199
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId in (2629, 5839):  #Galaxy Y Duos S6102
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 595
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            elif line.itemId == 2630:  #Galaxy Y Pro Duos B5512
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 502
                    line.discountedPrice = round(line.actualPrice - discount_value)
            
            total_selling_price += line.actualPrice * line.quantity
            total_discounted_price += line.discountedPrice * line.quantity
        
            if discount_value > 0:
                discount = Discount()
                discount.cart_id = cart.id
                discount.item_id = line.itemId
                discount.discount = discount_value
                discount.quantity = line.quantity
                
                discounts.append(discount)
        
        if has_qualified_model is False:
            raise PromotionException(115, 'This coupon is applicable only for selective Blackberry handsets.')
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = total_discounted_price
    
    return cart, discounts

def getDiscountOnItem(item):
    '''
    discount_expressions = [
            '250 if item.id in (5, 6, 7, 8, 9, 1499, 10, 1560, 1561) else None',
            '600 if item.id == 2016 else None',
            '1390 if item.id == 1543 else None',
            '1690 if item.id == 1550 else None'
        ]
    for expression in discount_expressions:
        discount = eval(expression)
        
        if discount is not None:
            return discount
    '''
    return None