Subversion Repositories SmartDukaan

Rev

Rev 5095 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5085 varun.gupt 1
'''
2
Created on 30-Apr-2012
3
 
4
@author: Varun Gupta
5
 
6
When SAccessory, coupon code is applied to cart, containing phone(s) as well as accessories,
7
a discount, equal to 5% of the selling price of phone(s), will be awarded on accessories. 
8
'''
9
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
10
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
11
from shop2020.clients.CatalogClient import CatalogClient
12
from shop2020.thriftpy.model.v1.user.ttypes import Discount
13
 
14
def execute(cart, coupon_code, args):
15
 
16
    #Allow a user to use the coupon only 1 time
17
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
18
 
19
    if count_users_usage > 0:
20
        raise PromotionException(111, 'This promotion is over.')
21
 
22
    discounts = []
23
 
24
    if cart.lines:
25
        catalog_client = CatalogClient().get_client()
26
 
27
        category_map = {}
28
        for category in catalog_client.getAllCategories():
29
            if category_map.has_key(category.parent_category_id):
30
                category_map[category.parent_category_id].append(category.id)
31
            else:
32
                category_map[category.parent_category_id] = [category.id]
33
 
34
        total_selling_price = 0
35
        total_discounted_price = 0
36
        available_discount = 0
37
 
38
        has_phone = False
39
        has_accessory = False
40
 
41
        items = {}
42
        #Computing Discount Value
43
        for line in cart.lines:
44
            item = catalog_client.getItem(line.itemId)
45
 
46
            items[line.itemId] = item
47
            print item.modelName, ', category: ', item.category
48
            if item.category == 10010 or item.category in category_map[10001]:
49
                print 'is phone/tablet'
50
                available_discount += round(item.sellingPrice * 0.05 * line.quantity)
51
                has_phone = True
52
 
53
        if not has_phone:
54
            raise PromotionException(115, 'This coupon is valid when a phone/tablet is included in the purchase')
55
 
56
        print 'Available Discount: ', available_discount
57
 
58
        #Applying discount on accessories
59
        for line in cart.lines:
60
            item = items[line.itemId]
61
 
62
            line.discountedPrice = line.actualPrice
63
            quantity_with_discount = 0
64
            total_discount_value = 0
65
 
66
            if item.category in category_map[10011]:
67
                print 'Accessory found: ', line.itemId, ' ', item.modelName
68
                discount_map = {}
69
                has_accessory = True
70
 
71
                for i in range(int(line.quantity)):
72
                    print 'For quantity ', i
73
                    print 'Available Discount: ', available_discount
74
                    discount_value = 0
75
 
76
                    if available_discount > 0:
77
                        quantity_with_discount += 1
78
 
79
                        if line.actualPrice < available_discount:
80
                            line.discountedPrice = 0
81
                            discount_value = line.actualPrice
82
                            available_discount -= line.actualPrice
83
                        else:
84
                            line.discountedPrice = line.actualPrice - available_discount
85
                            discount_value = available_discount
86
                            available_discount = 0
87
 
88
                    if discount_map.has_key(discount_value):
89
                        discount_map[discount_value] += 1
90
                    elif discount_value > 0:
91
                        discount_map[discount_value] = 1
92
                    total_discount_value += discount_value
93
 
94
                for discount_value, quantity in discount_map.iteritems():
95
                    discount = Discount()
96
                    discount.cart_id = cart.id
97
                    discount.item_id = line.itemId
98
                    discount.discount = discount_value
99
                    discount.quantity = quantity
100
                    print discount
101
                    discounts.append(discount)
102
 
103
            print 'quantity_with_discount: ', quantity_with_discount
104
 
105
            total_discounted_price += line.quantity * line.actualPrice -total_discount_value
106
            total_selling_price += line.actualPrice * line.quantity
107
            print 'Total discounted price: ', total_discounted_price
108
 
109
            cart.totalPrice = total_selling_price
110
            cart.discountedPrice = total_discounted_price
111
 
112
        if not has_accessory:
113
            raise PromotionException(115, 'This coupon is valid when an accessory is included in the purchase')
114
 
115
    return cart, discounts
116
 
117
def getDiscountOnItem(item):
118
    return None