Subversion Repositories SmartDukaan

Rev

Rev 6471 | Rev 6903 | 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
2465 varun.gupt 12
 
13
def execute(cart, coupon_code, args):
6538 amit.gupta 14
    if 'handset_display_name' not in args:
2465 varun.gupt 15
        raise PromotionException(109, 'Error in rule arguments.')
6538 amit.gupta 16
 
6471 amit.gupta 17
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
6470 amit.gupta 18
        raise PromotionException(112, 'This promotion is expired.')
19
 
2465 varun.gupt 20
    if 'usage_limit' in args and args['usage_limit'] is not None:
21
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
22
            raise PromotionException(112, 'This promotion is over.')
23
 
5331 rajveer 24
    user_client = UserClient().get_client()
25
    user = user_client.getUserByCartId(cart.id)
26
 
2465 varun.gupt 27
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
5353 varun.gupt 28
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
2465 varun.gupt 29
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
30
 
31
    if not cart.lines:
32
        return cart
6538 amit.gupta 33
 
34
 
35
    discountJ = None
36
    if 'discount' in args:
37
        discountJ  = args ['discount']
38
 
39
    min_amount = None
40
    if 'min_amount' in args:
41
        min_amount  = args ['min_amount']
42
 
43
    max_discount = None
44
    if 'max_discount' in args:
45
        max_discount  = args ['max_discount']
46
 
47
 
6450 amit.gupta 48
    isPercentageDiscount = False
6538 amit.gupta 49
    if 'is_percentage_discount' in args:
50
        isPercentageDiscount = args['is_percentage_discount']
51
 
52
 
2465 varun.gupt 53
    total_selling_price = 0
54
    total_discounted_price = 0
55
    cart_has_specified_item = False
4859 varun.gupt 56
    discounts = []
6538 amit.gupta 57
 
2465 varun.gupt 58
    for line in cart.lines:
59
        total_selling_price += line.actualPrice * line.quantity
4859 varun.gupt 60
        discount_value = 0
2465 varun.gupt 61
 
6538 amit.gupta 62
        if min_amount is not None and line.actualPrice < min_amount:
63
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
64
            continue
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
                total_discounted_price += round(line.actualPrice * line.quantity, 4)
76
                continue
77
        elif 'categories' in args:
78
            catalog_client = CatalogClient().get_client()
79
            category = catalog_client.getItem(line.itemId).category
80
            if category in args['categories']:
81
                if type(args['categories'])==dict: 
82
                    discount_value = args['categories'][category]
83
                    cart_has_specified_item = True
84
                elif discountJ is not None:
85
                    discount_value = discountJ 
86
                    cart_has_specified_item = True
87
            else:
88
                total_discounted_price += round(line.actualPrice * line.quantity, 4)
89
                continue
90
        elif discountJ is not None:
91
            discount_value = discountJ 
2465 varun.gupt 92
            cart_has_specified_item = True
6538 amit.gupta 93
        else :
94
            total_discounted_price += round(line.actualPrice * line.quantity, 4) 
95
            continue
96
 
97
        if isPercentageDiscount:
98
            discount_value = round((line.actualPrice*discount_value)/100, 4)
99
        if max_discount is not None:
100
            discount_value = min(max_discount, discount_value)
101
 
102
        line.discountedPrice = round(line.actualPrice - discount_value, 4)
103
        total_discounted_price += round(line.discountedPrice * line.quantity, 4)
4859 varun.gupt 104
        if discount_value > 0:
105
            discount = Discount()
106
            discount.discount = discount_value
107
            discount.quantity = line.quantity
108
            discount.cart_id = cart.id
109
            discount.item_id = line.itemId
110
 
111
            discounts.append(discount)
2465 varun.gupt 112
 
113
    if cart_has_specified_item is False:
114
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
115
 
116
    cart.totalPrice = round(total_selling_price, 4)
117
    cart.discountedPrice = total_discounted_price
5044 varun.gupt 118
    return cart, discounts
119
 
120
def getDiscountOnItem(item):
121
    return None