Subversion Repositories SmartDukaan

Rev

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