Subversion Repositories SmartDukaan

Rev

Rev 9140 | Rev 9168 | 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
'''
6538 amit.gupta 6
from shop2020.clients.CatalogClient import CatalogClient
6470 amit.gupta 7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
4862 varun.gupt 10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
6470 amit.gupta 11
import datetime
7781 amit.gupta 12
from math import floor
2465 varun.gupt 13
 
14
def execute(cart, coupon_code, args):
6538 amit.gupta 15
    if 'handset_display_name' not in args:
2465 varun.gupt 16
        raise PromotionException(109, 'Error in rule arguments.')
6538 amit.gupta 17
 
6471 amit.gupta 18
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
6470 amit.gupta 19
        raise PromotionException(112, 'This promotion is expired.')
20
 
2465 varun.gupt 21
    if 'usage_limit' in args and args['usage_limit'] is not None:
22
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
23
            raise PromotionException(112, 'This promotion is over.')
24
 
5331 rajveer 25
    user_client = UserClient().get_client()
26
    user = user_client.getUserByCartId(cart.id)
27
 
2465 varun.gupt 28
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
5353 varun.gupt 29
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
2465 varun.gupt 30
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
31
 
32
    if not cart.lines:
33
        return cart
6538 amit.gupta 34
 
35
 
36
    discountJ = None
37
    if 'discount' in args:
38
        discountJ  = args ['discount']
39
 
40
    min_amount = None
41
    if 'min_amount' in args:
42
        min_amount  = args ['min_amount']
43
 
7781 amit.gupta 44
    max_discount = 10000
6538 amit.gupta 45
    if 'max_discount' in args:
46
        max_discount  = args ['max_discount']
7781 amit.gupta 47
 
48
    discount_left = max_discount
6538 amit.gupta 49
 
6450 amit.gupta 50
    isPercentageDiscount = False
6538 amit.gupta 51
    if 'is_percentage_discount' in args:
52
        isPercentageDiscount = args['is_percentage_discount']
53
 
6903 anupam.sin 54
    total_discounts = 0
2465 varun.gupt 55
    cart_has_specified_item = False
4859 varun.gupt 56
    discounts = []
6538 amit.gupta 57
 
2465 varun.gupt 58
    for line in cart.lines:
7781 amit.gupta 59
        if discount_left > 0 :
60
            discount_value = 0
61
 
62
            if min_amount is not None and line.actualPrice < min_amount:
6538 amit.gupta 63
                continue
7781 amit.gupta 64
 
65
            if 'items' in args:
66
                if line.itemId in args['items'] :
67
                    if type(args['items'])==dict: 
68
                        discount_value = args['items'][line.itemId]
69
                        cart_has_specified_item = True
70
                    elif discountJ is not None:
71
                        discount_value = discountJ
72
                        cart_has_specified_item = True
73
                else:
74
                    continue
75
            elif 'categories' in args:
76
                catalog_client = CatalogClient().get_client()
77
                category = catalog_client.getItem(line.itemId).category
78
                if category in args['categories']:
79
                    if type(args['categories'])==dict: 
80
                        discount_value = args['categories'][category]
81
                        cart_has_specified_item = True
82
                    elif discountJ is not None:
83
                        discount_value = discountJ 
84
                        cart_has_specified_item = True
85
                else:
86
                    continue
9140 amit.gupta 87
 
88
            elif 'brands' in args:
89
                catalog_client = CatalogClient().get_client()
90
                brand = catalog_client.getItem(line.itemId).brand
91
                if brand in args['brands']:
92
                    if type(args['brands'])==dict: 
9141 amit.gupta 93
                        discount_value = args['brands'][brand]
9140 amit.gupta 94
                        cart_has_specified_item = True
95
                    elif discountJ is not None:
96
                        discount_value = discountJ 
97
                        cart_has_specified_item = True
98
                else:
99
                    continue
100
 
7781 amit.gupta 101
            elif discountJ is not None:
102
                discount_value = discountJ 
103
                cart_has_specified_item = True
104
            else :
6538 amit.gupta 105
                continue
7781 amit.gupta 106
 
107
            if isPercentageDiscount:
108
                discount_value = round((line.actualPrice*discount_value)/100, 4)
6538 amit.gupta 109
 
7781 amit.gupta 110
            quantity = min(floor(discount_left/discount_value), line.quantity)
111
            if quantity > 0:
112
                discount = Discount()
113
                discount.cart_id = cart.id
114
                discount.item_id = line.itemId
115
                discount.quantity = quantity
116
                discount.discount = discount_value
117
                discounts.append(discount)
118
 
119
                discount_left = discount_left - quantity*discount_value
120
                total_discounts += round(discount_value * quantity, 4)
4859 varun.gupt 121
 
7781 amit.gupta 122
            if discount_left > 0 and line.quantity - quantity > 0:
123
                discountOne = Discount()
124
                discountOne.cart_id = cart.id
125
                discountOne.item_id = line.itemId
126
                discountOne.quantity = 1
127
                discountOne.discount = discount_left
128
                discounts.append(discountOne)
129
 
130
                discount_left = 0
131
                total_discounts = max_discount
132
 
133
        else:
134
            break
2465 varun.gupt 135
 
136
    if cart_has_specified_item is False:
137
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
138
 
8456 amit.gupta 139
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
5044 varun.gupt 140
    return cart, discounts
141
 
142
def getDiscountOnItem(item):
143
    return None