Subversion Repositories SmartDukaan

Rev

Rev 4856 | Go to most recent revision | Details | 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
'''
6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
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:
18
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code):
19
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
20
 
21
    if not cart.lines:
22
        #TODO: Cart has no lines.
23
        return cart
24
 
25
    total_selling_price = 0
26
    total_discounted_price = 0
27
    cart_has_specified_item = False
28
 
29
    for line in cart.lines:
30
        total_selling_price += line.actualPrice * line.quantity
31
 
32
        if line.itemId in args['items']:
33
            cart_has_specified_item = True
34
            line.discountedPrice = round(line.actualPrice - args['discount'], 4)
35
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
36
        else:
37
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
38
 
39
    if cart_has_specified_item is False:
40
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
41
 
42
    cart.totalPrice = round(total_selling_price, 4)
43
    cart.discountedPrice = total_discounted_price
44
    return cart