Subversion Repositories SmartDukaan

Rev

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