Subversion Repositories SmartDukaan

Rev

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

'''
Created on 28-May-2012

@author: Varun Gupta

Allows first 300 users to use the coupon

Galaxy Y Pro Duos B5512       Discount: Rs.502
Chat 322 C3222        Discount: Rs.171
Champ Deluxe Duos C3312       Discount: Rs.211
Champ Duos E2652       Discount: Rs.155
Champ 3.5G S3770       Discount: Rs.255
Corby II S3850       Discount: Rs.361
Star 3 Duos S5222       Discount: Rs.151
Chat 527 S5270       Discount: Rs.205
Wave Y S5380       Discount: Rs.345
Metro C3530       Discount: Rs.251
Primo S5610       Discount: Rs.241
Star II S5263       Discount: Rs.301
Star II Duos C6712       Discount: Rs.251
Wave 525 S5253       Discount: Rs.391
Galaxy Y Color S5360s       Discount: Rs.199

Champ Megacam C3303i    Discount: Rs.80
Champ Deluxe C3312s    Discount: Rs.191
Metro Duos C3322    Discount: Rs.151
Metro C3520    Discount: Rs.40
Star II Duos C6712    Discount: Rs.271
E1200, E1205    Discount: Rs.61
Guru FM E1220    Discount: Rs.51
Omnia W I8350    Discount: Rs.701
Galaxy S II I9100    Discount: Rs.905
Galaxy Note N7000    Discount: Rs.901
'''
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
from shop2020.clients.UserClient import UserClient

def execute(cart, coupon_code, args):
    user_client = UserClient().get_client()
    user = user_client.getUserByCartId(cart.id)

    #Allow only first 300 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    
    if count_coupon_usage >= 300:
        raise PromotionException(112, 'This promotion is over.')
    
    if get_coupon_usage_count_by_user(coupon_code, user.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 = 171
            
            elif line.itemId in (3356, 4628, 5730, 2563, 3357):  #Champ Deluxe Duos C3312
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 211
            
            elif line.itemId in (1012, 1452, 1453):  #Metro C3530
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 251
             
            elif line.itemId in (999, 988, 5702):  #Champ Duos E2652
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 155
            
            elif line.itemId in (2576, 2067):  #Chat 527 S5270
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 205
            
            elif line.itemId in (2073, 2621):  #Champ 3.5G S3770
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 255
            
            elif line.itemId in (3358, 4643):  #Star 3 Duos S5222
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 151
            
            elif line.itemId in (2090, 2256):  #Primo S5610
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 241
            
            elif line.itemId in (968, 995):  #Star II S5263
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 301
            
            elif line.itemId in (996, 1000, 1524, 1563):  #Corby II S3850
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 361
            
            elif line.itemId in (1537, 2227):  #Star II Duos C6712
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 251
            
            elif line.itemId in (949, 4690, 948):  #Wave 525 S5253
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 391
            
            elif line.itemId in (2537, 2622):  #Wave Y S5380
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 345
            
            elif line.itemId == 2446:  #Galaxy Y Color S5360s
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 199
            
            elif line.itemId == 2630:  #Galaxy Y Pro Duos B5512
                
                has_qualified_model = True
                
                if line.actualPrice:
                    discount_value = 502
            
            elif line.itemId in (1001, 2226, 1004, 1011, 998): #Champ Megacam C3303i
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 80
            
            elif line.itemId == 4629: #Champ Deluxe C3312s
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 191 
            
            elif line.itemId in (2532, 4689, 1500): #Metro Duos C3322
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 151 
            
            elif line.itemId in (2445, 4636): #Metro C3520
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 40
            
            elif line.itemId in (1537, 2227): #Star II Duos C6712
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 271 
            
            elif line.itemId in (5700, 5701): #E1200, E1205
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 61
            
            elif line.itemId == 2603: #Guru FM E1220
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 51
            
            elif line.itemId == 2470: #Omnia W I8350
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 701
            
            elif line.itemId in (1493, 1502): #Galaxy S II I9100
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 905
            
            elif line.itemId in (2215, 2481): #Galaxy Note N7000
                has_qualified_model = True
                
                if line.actualPrice:    discount_value = 901
            
            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)
                line.discountedPrice = round(line.actualPrice - discount_value)
            
            total_selling_price += line.actualPrice * line.quantity
            total_discounted_price += line.discountedPrice * line.quantity
            
        if has_qualified_model is False:
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = total_discounted_price
    
    return cart, discounts

def getDiscountOnItem(item):
    
    discount_expressions = [
            '171 if item.id in (1010, 1451) else None',
            '211 if item.id in (3356, 4628, 5730, 2563, 3357) else None',
            '251 if item.id in (1012, 1452, 1453) else None',
            '155 if item.id in (999, 988, 5702) else None',
            '205 if item.id in (2576, 2067) else None',
            '255 if item.id in (2073, 2621) else None',
            '151 if item.id in (3358, 4643) else None',
            '241 if item.id in (2090, 2256) else None',
            '301 if item.id in (968, 995) else None',
            '361 if item.id in (996, 1000, 1524, 1563) else None',
            '251 if item.id in (1537, 2227) else None',
            '391 if item.id in (949, 4690, 948) else None',
            '345 if item.id in (2537, 2622) else None',
            '199 if item.id == 2446 else None',
            '502 if item.id == 2630 else None',
            '80 if item.id in (1001, 2226, 1004, 1011, 998) else None',
            '191 if item.id == 4629 else None',
            '151 if item.id in (2532, 4689, 1500) else None',
            '40 if item.id in (2445, 4636) else None',
            '271 if item.id in (1537, 2227) else None',
            '61 if item.id in (5700, 5701) else None',
            '51 if item.id == 2603 else None',
            '701 if item.id == 2470 else None',
            '905 if item.id in (1493, 1502) else None',
            '901 if item.id in (2215, 2481) else None'
        ]
    for expression in discount_expressions:
        discount = eval(expression)
        
        if discount is not None:
            return discount
        
    return None