Subversion Repositories SmartDukaan

Rev

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