Subversion Repositories SmartDukaan

Rev

Rev 11841 | Rev 13169 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2465 varun.gupt 1
'''
2
Created on 06-Jul-2011
3
 
4
@author: Varun Gupta
5
'''
13155 amit.gupta 6
from math import floor
6538 amit.gupta 7
from shop2020.clients.CatalogClient import CatalogClient
13155 amit.gupta 8
from shop2020.model.v1.user.impl import UserDataAccessors
6470 amit.gupta 9
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
10
    get_coupon_usage_count, get_coupon_usage_count_by_user
4862 varun.gupt 11
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
6470 amit.gupta 12
import datetime
2465 varun.gupt 13
 
14
def execute(cart, coupon_code, args):
9168 amit.gupta 15
 
16
 
6538 amit.gupta 17
    if 'handset_display_name' not in args:
2465 varun.gupt 18
        raise PromotionException(109, 'Error in rule arguments.')
6538 amit.gupta 19
 
6471 amit.gupta 20
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
6470 amit.gupta 21
        raise PromotionException(112, 'This promotion is expired.')
22
 
2465 varun.gupt 23
    if 'usage_limit' in args and args['usage_limit'] is not None:
24
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
25
            raise PromotionException(112, 'This promotion is over.')
26
 
13155 amit.gupta 27
    user = UserDataAccessors.get_user_by_cart_id(cart.id)
28
 
5331 rajveer 29
 
13155 amit.gupta 30
 
2465 varun.gupt 31
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
13155 amit.gupta 32
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.id):
2465 varun.gupt 33
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
34
 
35
    if not cart.lines:
36
        return cart
13155 amit.gupta 37
 
38
    appliedOnce = args['appliedOnce'] if 'dealer_flag' in args else True
6538 amit.gupta 39
 
13155 amit.gupta 40
    dealItems = []
41
    if not (args['dealer_flag'] if 'dealer_flag' in args else False):
42
        privateDealUser = UserDataAccessors.get_private_deal_user(user.id)
43
        if privateDealUser is not None and privateDealUser.isActive:
44
            catalog_client = CatalogClient().get_client() 
45
            items = [line.itemId for line in cart.lines]
46
            dealItems = catalog_client.getAllActivePrivateDeals(items, 0)
47
 
48
 
6538 amit.gupta 49
 
50
    discountJ = None
51
    if 'discount' in args:
52
        discountJ  = args ['discount']
53
 
54
    min_amount = None
55
    if 'min_amount' in args:
56
        min_amount  = args ['min_amount']
57
 
7781 amit.gupta 58
    max_discount = 10000
6538 amit.gupta 59
    if 'max_discount' in args:
60
        max_discount  = args ['max_discount']
7781 amit.gupta 61
 
62
    discount_left = max_discount
6538 amit.gupta 63
 
6450 amit.gupta 64
    isPercentageDiscount = False
6538 amit.gupta 65
    if 'is_percentage_discount' in args:
66
        isPercentageDiscount = args['is_percentage_discount']
67
 
6903 anupam.sin 68
    total_discounts = 0
2465 varun.gupt 69
    cart_has_specified_item = False
4859 varun.gupt 70
    discounts = []
13155 amit.gupta 71
 
2465 varun.gupt 72
    for line in cart.lines:
13155 amit.gupta 73
        if line.itemId in dealItems:
74
            print "continued  line.itemId",  line.itemId 
75
            continue
7781 amit.gupta 76
        if discount_left > 0 :
77
            discount_value = 0
78
 
79
            if min_amount is not None and line.actualPrice < min_amount:
6538 amit.gupta 80
                continue
7781 amit.gupta 81
 
82
            if 'items' in args:
11841 amit.gupta 83
                if line.itemId in args['items'] or '*' in args['items']:
7781 amit.gupta 84
                    if type(args['items'])==dict: 
85
                        discount_value = args['items'][line.itemId]
86
                        cart_has_specified_item = True
87
                    elif discountJ is not None:
88
                        discount_value = discountJ
89
                        cart_has_specified_item = True
90
                else:
91
                    continue
92
            elif 'categories' in args:
93
                catalog_client = CatalogClient().get_client()
94
                category = catalog_client.getItem(line.itemId).category
95
                if category in args['categories']:
96
                    if type(args['categories'])==dict: 
97
                        discount_value = args['categories'][category]
98
                        cart_has_specified_item = True
99
                    elif discountJ is not None:
100
                        discount_value = discountJ 
101
                        cart_has_specified_item = True
102
                else:
103
                    continue
9140 amit.gupta 104
 
13155 amit.gupta 105
            elif 'categoriesexcept' in args:
106
                catalog_client = CatalogClient().get_client()
107
                category = catalog_client.getItem(line.itemId).category
108
                if category in args['categoriesexcept']:
109
                    if type(args['categoriesexcept'])==dict:
110
                        discount_value = discountJ 
111
                        cart_has_specified_item = True
112
                else:
113
                    continue
114
 
9140 amit.gupta 115
            elif 'brands' in args:
116
                catalog_client = CatalogClient().get_client()
117
                brand = catalog_client.getItem(line.itemId).brand
118
                if brand in args['brands']:
119
                    if type(args['brands'])==dict: 
9141 amit.gupta 120
                        discount_value = args['brands'][brand]
9140 amit.gupta 121
                        cart_has_specified_item = True
122
                    elif discountJ is not None:
123
                        discount_value = discountJ 
124
                        cart_has_specified_item = True
125
                else:
126
                    continue
127
 
7781 amit.gupta 128
            elif discountJ is not None:
129
                discount_value = discountJ 
130
                cart_has_specified_item = True
131
            else :
6538 amit.gupta 132
                continue
7781 amit.gupta 133
 
134
            if isPercentageDiscount:
135
                discount_value = round((line.actualPrice*discount_value)/100, 4)
9168 amit.gupta 136
 
13155 amit.gupta 137
            if appliedOnce:
9168 amit.gupta 138
                quantity = 1
139
            else:
140
                quantity = min(floor(discount_left/discount_value), line.quantity)
6538 amit.gupta 141
 
7781 amit.gupta 142
            if quantity > 0:
143
                discount = Discount()
144
                discount.cart_id = cart.id
145
                discount.item_id = line.itemId
146
                discount.quantity = quantity
147
                discount.discount = discount_value
148
                discounts.append(discount)
149
 
150
                discount_left = discount_left - quantity*discount_value
151
                total_discounts += round(discount_value * quantity, 4)
4859 varun.gupt 152
 
9168 amit.gupta 153
            if appliedOnce :
154
                break
155
 
7781 amit.gupta 156
            if discount_left > 0 and line.quantity - quantity > 0:
157
                discountOne = Discount()
158
                discountOne.cart_id = cart.id
159
                discountOne.item_id = line.itemId
160
                discountOne.quantity = 1
161
                discountOne.discount = discount_left
162
                discounts.append(discountOne)
163
 
164
                discount_left = 0
165
                total_discounts = max_discount
9168 amit.gupta 166
 
7781 amit.gupta 167
        else:
168
            break
9168 amit.gupta 169
 
170
 
2465 varun.gupt 171
    if cart_has_specified_item is False:
172
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
173
 
8456 amit.gupta 174
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
13155 amit.gupta 175
    print "cart.discountedPrice----",  cart.discountedPrice
5044 varun.gupt 176
    return cart, discounts
177
 
178
def getDiscountOnItem(item):
179
    return None