Subversion Repositories SmartDukaan

Rev

Rev 5085 | 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,
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
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
 
33
        if datetime.date(2012, 5, 1) < datetime.date(active_since.year, active_since.month, active_since.day):
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]:
60
                print 'is phone/tablet'
61
                available_discount += round(item.sellingPrice * 0.05 * line.quantity)
62
                has_phone = True
63
 
64
        if not has_phone:
65
            raise PromotionException(115, 'This coupon is valid when a phone/tablet is included in the purchase')
66
 
67
        print 'Available Discount: ', available_discount
68
 
69
        #Applying discount on accessories
70
        for line in cart.lines:
71
            item = items[line.itemId]
72
 
73
            line.discountedPrice = line.actualPrice
74
            quantity_with_discount = 0
75
            total_discount_value = 0
76
 
77
            if item.category in category_map[10011]:
78
                print 'Accessory found: ', line.itemId, ' ', item.modelName
79
                discount_map = {}
80
                has_accessory = True
81
 
82
                for i in range(int(line.quantity)):
83
                    print 'For quantity ', i
84
                    print 'Available Discount: ', available_discount
85
                    discount_value = 0
86
 
87
                    if available_discount > 0:
88
                        quantity_with_discount += 1
89
 
90
                        if line.actualPrice < available_discount:
91
                            line.discountedPrice = 0
92
                            discount_value = line.actualPrice
93
                            available_discount -= line.actualPrice
94
                        else:
95
                            line.discountedPrice = line.actualPrice - available_discount
96
                            discount_value = available_discount
97
                            available_discount = 0
98
 
99
                    if discount_map.has_key(discount_value):
100
                        discount_map[discount_value] += 1
101
                    elif discount_value > 0:
102
                        discount_map[discount_value] = 1
103
                    total_discount_value += discount_value
104
 
105
                for discount_value, quantity in discount_map.iteritems():
106
                    discount = Discount()
107
                    discount.cart_id = cart.id
108
                    discount.item_id = line.itemId
109
                    discount.discount = discount_value
110
                    discount.quantity = quantity
111
                    print discount
112
                    discounts.append(discount)
113
 
114
            print 'quantity_with_discount: ', quantity_with_discount
115
 
116
            total_discounted_price += line.quantity * line.actualPrice -total_discount_value
117
            total_selling_price += line.actualPrice * line.quantity
118
            print 'Total discounted price: ', total_discounted_price
119
 
120
            cart.totalPrice = total_selling_price
121
            cart.discountedPrice = total_discounted_price
122
 
123
        if not has_accessory:
124
            raise PromotionException(115, 'This coupon is valid when an accessory is included in the purchase')
125
 
126
    return cart, discounts
127
 
128
def getDiscountOnItem(item):
129
    return None