Subversion Repositories SmartDukaan

Rev

Rev 5970 | Rev 5974 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5970 amit.gupta 1
'''
2
Created on 06-Jul-2011
3
 
4
@author: Varun Gupta
5
'''
5972 amit.gupta 6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
5970 amit.gupta 8
from shop2020.clients.UserClient import UserClient
9
 
5972 amit.gupta 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.')
5970 amit.gupta 13
 
5972 amit.gupta 14
    if 'usage_limit' in args and args['usage_limit'] is not None:
15
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
5970 amit.gupta 16
            raise PromotionException(112, 'This promotion is over.')
17
 
18
    user_client = UserClient().get_client()
19
    user = user_client.getUserByCartId(cart.id)
20
 
5972 amit.gupta 21
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
22
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
5970 amit.gupta 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
31
    discounts = []
32
 
33
    for line in cart.lines:
34
        total_selling_price += line.actualPrice * line.quantity
35
        discount_value = 0
36
 
5972 amit.gupta 37
        if line.itemId in args['items']:
5970 amit.gupta 38
            cart_has_specified_item = True
5972 amit.gupta 39
            line.discountedPrice = round(line.actualPrice - args['discount'], 4)
5970 amit.gupta 40
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
5972 amit.gupta 41
            discount_value = args['discount']
5970 amit.gupta 42
        else:
43
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
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)
53
 
54
    if cart_has_specified_item is False:
5972 amit.gupta 55
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
5970 amit.gupta 56
 
57
    cart.totalPrice = round(total_selling_price, 4)
58
    cart.discountedPrice = total_discounted_price
59
    return cart, discounts
60
 
61
def getDiscountOnItem(item):
62
    return 100