Subversion Repositories SmartDukaan

Rev

Rev 6456 | Rev 6470 | 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):
6450 amit.gupta 11
    if 'items' not in args or 'handset_display_name' not in args:
2465 varun.gupt 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:
5353 varun.gupt 22
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
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
6450 amit.gupta 27
    isPercentageDiscount = False
2465 varun.gupt 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
 
6450 amit.gupta 33
    if 'is_percentage_discount' in args:
34
        isPercentageDiscount = args['is_percentage_discount']
2465 varun.gupt 35
    for line in cart.lines:
36
        total_selling_price += line.actualPrice * line.quantity
4859 varun.gupt 37
        discount_value = 0
2465 varun.gupt 38
 
6469 amit.gupta 39
        if line.itemId in args['items'] or (len(args['items'])==0 and args['min_amount'] < line.actualPrice):
2465 varun.gupt 40
            cart_has_specified_item = True
6469 amit.gupta 41
            if(len(args['items'])==0):
42
                discount_value = args['discount']
43
            else: 
44
                discount_value = args['items'][line.itemId]
6450 amit.gupta 45
            if isPercentageDiscount:
6455 amit.gupta 46
                discount_value = round((line.actualPrice*discount_value)/100, 4)
6450 amit.gupta 47
 
6456 amit.gupta 48
            line.discountedPrice = round(line.actualPrice - discount_value, 4)
2465 varun.gupt 49
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
50
        else:
51
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
4859 varun.gupt 52
 
53
        if discount_value > 0:
54
            discount = Discount()
55
            discount.discount = discount_value
56
            discount.quantity = line.quantity
57
            discount.cart_id = cart.id
58
            discount.item_id = line.itemId
59
 
60
            discounts.append(discount)
2465 varun.gupt 61
 
62
    if cart_has_specified_item is False:
63
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
64
 
65
    cart.totalPrice = round(total_selling_price, 4)
66
    cart.discountedPrice = total_discounted_price
5044 varun.gupt 67
    return cart, discounts
68
 
69
def getDiscountOnItem(item):
70
    return None