Subversion Repositories SmartDukaan

Rev

Rev 5130 | Go to most recent revision | Details | Compare with Previous | 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,
5130 varun.gupt 7
a discount, equal to 5% of the selling price of phone/tablet, will be awarded on accessories. 
5085 varun.gupt 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
5095 varun.gupt 13
from shop2020.clients.UserClient import UserClient
14
from shop2020.utils.Utils import to_py_date
15
import datetime
5085 varun.gupt 16
 
17
def execute(cart, coupon_code, args):
18
 
19
    #Allow a user to use the coupon only 1 time
20
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
21
 
22
    if count_users_usage > 0:
23
        raise PromotionException(111, 'This promotion is over.')
24
 
25
    discounts = []
26
 
27
    if cart.lines:
5095 varun.gupt 28
        user_client = UserClient().get_client()
29
 
30
        user_state = user_client.getUserState(cart.userId)
31
        active_since = to_py_date(user_state.activeSince)
32
 
5132 varun.gupt 33
        if datetime.date(2012, 5, 7) < datetime.date(active_since.year, active_since.month, active_since.day):
5095 varun.gupt 34
            raise PromotionException(111, 'You are not eligible for this discount')
35
 
5085 varun.gupt 36
        catalog_client = CatalogClient().get_client()
37
 
38
        category_map = {}
39
        for category in catalog_client.getAllCategories():
40
            if category_map.has_key(category.parent_category_id):
41
                category_map[category.parent_category_id].append(category.id)
42
            else:
43
                category_map[category.parent_category_id] = [category.id]
44
 
45
        total_selling_price = 0
46
        total_discounted_price = 0
47
        available_discount = 0
48
 
49
        has_phone = False
50
        has_accessory = False
51
 
52
        items = {}
53
        #Computing Discount Value
54
        for line in cart.lines:
55
            item = catalog_client.getItem(line.itemId)
56
 
57
            items[line.itemId] = item
58
            print item.modelName, ', category: ', item.category
59
            if item.category == 10010 or item.category in category_map[10001]:
5130 varun.gupt 60
 
61
                if available_discount < round(item.sellingPrice * 0.05): 
62
                    available_discount = round(item.sellingPrice * 0.05) 
5085 varun.gupt 63
                has_phone = True
64
 
65
        if not has_phone:
66
            raise PromotionException(115, 'This coupon is valid when a phone/tablet is included in the purchase')
67
 
68
        print 'Available Discount: ', available_discount
69
 
70
        #Applying discount on accessories
71
        for line in cart.lines:
72
            item = items[line.itemId]
73
 
74
            line.discountedPrice = line.actualPrice
75
            quantity_with_discount = 0
76
            total_discount_value = 0
77
 
78
            if item.category in category_map[10011]:
79
                print 'Accessory found: ', line.itemId, ' ', item.modelName
80
                discount_map = {}
81
                has_accessory = True
82
 
83
                for i in range(int(line.quantity)):
84
                    print 'For quantity ', i
85
                    print 'Available Discount: ', available_discount
86
                    discount_value = 0
87
 
88
                    if available_discount > 0:
89
                        quantity_with_discount += 1
90
 
91
                        if line.actualPrice < available_discount:
92
                            line.discountedPrice = 0
93
                            discount_value = line.actualPrice
94
                            available_discount -= line.actualPrice
95
                        else:
96
                            line.discountedPrice = line.actualPrice - available_discount
97
                            discount_value = available_discount
98
                            available_discount = 0
99
 
100
                    if discount_map.has_key(discount_value):
101
                        discount_map[discount_value] += 1
102
                    elif discount_value > 0:
103
                        discount_map[discount_value] = 1
104
                    total_discount_value += discount_value
105
 
106
                for discount_value, quantity in discount_map.iteritems():
107
                    discount = Discount()
108
                    discount.cart_id = cart.id
109
                    discount.item_id = line.itemId
110
                    discount.discount = discount_value
111
                    discount.quantity = quantity
112
                    print discount
113
                    discounts.append(discount)
114
 
115
            print 'quantity_with_discount: ', quantity_with_discount
116
 
117
            total_discounted_price += line.quantity * line.actualPrice -total_discount_value
118
            total_selling_price += line.actualPrice * line.quantity
119
            print 'Total discounted price: ', total_discounted_price
120
 
121
            cart.totalPrice = total_selling_price
122
            cart.discountedPrice = total_discounted_price
123
 
124
        if not has_accessory:
125
            raise PromotionException(115, 'This coupon is valid when an accessory is included in the purchase')
126
 
127
    return cart, discounts
128
 
129
def getDiscountOnItem(item):
5132 varun.gupt 130
    return None