Subversion Repositories SmartDukaan

Rev

Rev 13169 | Rev 13205 | 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
 
13169 amit.gupta 72
    foundDeal=False
73
 
2465 varun.gupt 74
    for line in cart.lines:
13155 amit.gupta 75
        if line.itemId in dealItems:
13169 amit.gupta 76
            foundDeal = True 
13155 amit.gupta 77
            continue
7781 amit.gupta 78
        if discount_left > 0 :
79
            discount_value = 0
80
 
81
            if min_amount is not None and line.actualPrice < min_amount:
6538 amit.gupta 82
                continue
7781 amit.gupta 83
 
84
            if 'items' in args:
11841 amit.gupta 85
                if line.itemId in args['items'] or '*' in args['items']:
7781 amit.gupta 86
                    if type(args['items'])==dict: 
87
                        discount_value = args['items'][line.itemId]
88
                        cart_has_specified_item = True
89
                    elif discountJ is not None:
90
                        discount_value = discountJ
91
                        cart_has_specified_item = True
92
                else:
93
                    continue
94
            elif 'categories' in args:
95
                catalog_client = CatalogClient().get_client()
96
                category = catalog_client.getItem(line.itemId).category
97
                if category in args['categories']:
98
                    if type(args['categories'])==dict: 
99
                        discount_value = args['categories'][category]
100
                        cart_has_specified_item = True
101
                    elif discountJ is not None:
102
                        discount_value = discountJ 
103
                        cart_has_specified_item = True
104
                else:
105
                    continue
9140 amit.gupta 106
 
13155 amit.gupta 107
            elif 'categoriesexcept' in args:
108
                catalog_client = CatalogClient().get_client()
109
                category = catalog_client.getItem(line.itemId).category
13169 amit.gupta 110
                if category not in args['categoriesexcept']:
13155 amit.gupta 111
                        discount_value = discountJ 
112
                        cart_has_specified_item = True
113
                else:
114
                    continue
115
 
9140 amit.gupta 116
            elif 'brands' in args:
117
                catalog_client = CatalogClient().get_client()
118
                brand = catalog_client.getItem(line.itemId).brand
119
                if brand in args['brands']:
120
                    if type(args['brands'])==dict: 
9141 amit.gupta 121
                        discount_value = args['brands'][brand]
9140 amit.gupta 122
                        cart_has_specified_item = True
123
                    elif discountJ is not None:
124
                        discount_value = discountJ 
125
                        cart_has_specified_item = True
126
                else:
127
                    continue
128
 
7781 amit.gupta 129
            elif discountJ is not None:
130
                discount_value = discountJ 
131
                cart_has_specified_item = True
132
            else :
6538 amit.gupta 133
                continue
7781 amit.gupta 134
 
135
            if isPercentageDiscount:
136
                discount_value = round((line.actualPrice*discount_value)/100, 4)
9168 amit.gupta 137
 
13155 amit.gupta 138
            if appliedOnce:
9168 amit.gupta 139
                quantity = 1
140
            else:
141
                quantity = min(floor(discount_left/discount_value), line.quantity)
6538 amit.gupta 142
 
7781 amit.gupta 143
            if quantity > 0:
144
                discount = Discount()
145
                discount.cart_id = cart.id
146
                discount.item_id = line.itemId
147
                discount.quantity = quantity
148
                discount.discount = discount_value
149
                discounts.append(discount)
150
 
151
                discount_left = discount_left - quantity*discount_value
152
                total_discounts += round(discount_value * quantity, 4)
4859 varun.gupt 153
 
9168 amit.gupta 154
            if appliedOnce :
155
                break
156
 
7781 amit.gupta 157
            if discount_left > 0 and line.quantity - quantity > 0:
158
                discountOne = Discount()
159
                discountOne.cart_id = cart.id
160
                discountOne.item_id = line.itemId
161
                discountOne.quantity = 1
162
                discountOne.discount = discount_left
163
                discounts.append(discountOne)
164
 
165
                discount_left = 0
166
                total_discounts = max_discount
9168 amit.gupta 167
 
7781 amit.gupta 168
        else:
169
            break
9168 amit.gupta 170
 
171
 
2465 varun.gupt 172
    if cart_has_specified_item is False:
13169 amit.gupta 173
        if foundDeal:
174
            raise PromotionException(115, '''Deal items can't be discounted''')
175
        else:
176
            raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
177
 
2465 varun.gupt 178
 
13169 amit.gupta 179
 
8456 amit.gupta 180
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
5044 varun.gupt 181
    return cart, discounts
182
 
183
def getDiscountOnItem(item):
184
    return None