Subversion Repositories SmartDukaan

Rev

Rev 7781 | Rev 9140 | 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
 
50
 
6450 amit.gupta 51
    isPercentageDiscount = False
6538 amit.gupta 52
    if 'is_percentage_discount' in args:
53
        isPercentageDiscount = args['is_percentage_discount']
54
 
6903 anupam.sin 55
    total_discounts = 0
2465 varun.gupt 56
    cart_has_specified_item = False
4859 varun.gupt 57
    discounts = []
6538 amit.gupta 58
 
2465 varun.gupt 59
    for line in cart.lines:
7781 amit.gupta 60
        if discount_left > 0 :
61
            discount_value = 0
62
 
63
            if min_amount is not None and line.actualPrice < min_amount:
6538 amit.gupta 64
                continue
7781 amit.gupta 65
 
66
            if 'items' in args:
67
                if line.itemId in args['items'] :
68
                    if type(args['items'])==dict: 
69
                        discount_value = args['items'][line.itemId]
70
                        cart_has_specified_item = True
71
                    elif discountJ is not None:
72
                        discount_value = discountJ
73
                        cart_has_specified_item = True
74
                else:
75
                    continue
76
            elif 'categories' in args:
77
                catalog_client = CatalogClient().get_client()
78
                category = catalog_client.getItem(line.itemId).category
79
                if category in args['categories']:
80
                    if type(args['categories'])==dict: 
81
                        discount_value = args['categories'][category]
82
                        cart_has_specified_item = True
83
                    elif discountJ is not None:
84
                        discount_value = discountJ 
85
                        cart_has_specified_item = True
86
                else:
87
                    continue
88
            elif discountJ is not None:
89
                discount_value = discountJ 
90
                cart_has_specified_item = True
91
            else :
6538 amit.gupta 92
                continue
7781 amit.gupta 93
 
94
            if isPercentageDiscount:
95
                discount_value = round((line.actualPrice*discount_value)/100, 4)
6538 amit.gupta 96
 
7781 amit.gupta 97
            quantity = min(floor(discount_left/discount_value), line.quantity)
98
            if quantity > 0:
99
                discount = Discount()
100
                discount.cart_id = cart.id
101
                discount.item_id = line.itemId
102
                discount.quantity = quantity
103
                discount.discount = discount_value
104
                discounts.append(discount)
105
 
106
                discount_left = discount_left - quantity*discount_value
107
                total_discounts += round(discount_value * quantity, 4)
4859 varun.gupt 108
 
7781 amit.gupta 109
            if discount_left > 0 and line.quantity - quantity > 0:
110
                discountOne = Discount()
111
                discountOne.cart_id = cart.id
112
                discountOne.item_id = line.itemId
113
                discountOne.quantity = 1
114
                discountOne.discount = discount_left
115
                discounts.append(discountOne)
116
 
117
                discount_left = 0
118
                total_discounts = max_discount
119
 
120
        else:
121
            break
2465 varun.gupt 122
 
123
    if cart_has_specified_item is False:
124
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
125
 
8456 amit.gupta 126
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
5044 varun.gupt 127
    return cart, discounts
128
 
129
def getDiscountOnItem(item):
130
    return None