Subversion Repositories SmartDukaan

Rev

Rev 4862 | Rev 5331 | 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
'''
4862 varun.gupt 6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
2465 varun.gupt 7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
8
 
9
def execute(cart, coupon_code, args):
10
    if 'items' not in args or 'discount' not in args or 'handset_display_name' not in args:
11
        raise PromotionException(109, 'Error in rule arguments.')
12
 
13
    if 'usage_limit' in args and args['usage_limit'] is not None:
14
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
15
            raise PromotionException(112, 'This promotion is over.')
16
 
17
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
4856 varun.gupt 18
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, cart.userId):
2465 varun.gupt 19
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
20
 
21
    if not cart.lines:
22
        return cart
23
 
24
    total_selling_price = 0
25
    total_discounted_price = 0
26
    cart_has_specified_item = False
4859 varun.gupt 27
    discounts = []
2465 varun.gupt 28
 
29
    for line in cart.lines:
30
        total_selling_price += line.actualPrice * line.quantity
4859 varun.gupt 31
        discount_value = 0
2465 varun.gupt 32
 
33
        if line.itemId in args['items']:
34
            cart_has_specified_item = True
35
            line.discountedPrice = round(line.actualPrice - args['discount'], 4)
36
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
4859 varun.gupt 37
            discount_value = args['discount']
2465 varun.gupt 38
        else:
39
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
4859 varun.gupt 40
 
41
        if discount_value > 0:
42
            discount = Discount()
43
            discount.discount = discount_value
44
            discount.quantity = line.quantity
45
            discount.cart_id = cart.id
46
            discount.item_id = line.itemId
47
 
48
            discounts.append(discount)
2465 varun.gupt 49
 
50
    if cart_has_specified_item is False:
51
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
52
 
53
    cart.totalPrice = round(total_selling_price, 4)
54
    cart.discountedPrice = total_discounted_price
5044 varun.gupt 55
    return cart, discounts
56
 
57
def getDiscountOnItem(item):
58
    return None