Subversion Repositories SmartDukaan

Rev

Rev 6018 | 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):
6026 amit.gupta 11
    itemTuple = (2091,2527,3882,6829,7243,5834,7227,2629,5839,3358,4643,7298,2215,2481,4630,5731,1493,1502,2073,2621,5840,5841,5842,4961,7509,7510,7511,7335,7508,5,6,7,8,9,2094,2274,2280,6823,7517,7565,7332,2026,2074,2020,2021,1584,2058,1595,3361,4627,2039,7295,4645,7126,7503,4646,4859,2082,2083,3880,7297,7548,4647)
5997 amit.gupta 12
    if 'discount' not in args or 'handset_display_name' not in args:
5972 amit.gupta 13
        raise PromotionException(109, 'Error in rule arguments.')
5970 amit.gupta 14
 
5972 amit.gupta 15
    if 'usage_limit' in args and args['usage_limit'] is not None:
16
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
5970 amit.gupta 17
            raise PromotionException(112, 'This promotion is over.')
18
 
19
    user_client = UserClient().get_client()
20
    user = user_client.getUserByCartId(cart.id)
21
 
5972 amit.gupta 22
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
23
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
5970 amit.gupta 24
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
25
 
26
    if not cart.lines:
27
        return cart
28
 
29
    total_selling_price = 0
30
    total_discounted_price = 0
31
    cart_has_specified_item = False
32
    discounts = []
33
    for line in cart.lines:
34
        total_selling_price += line.actualPrice * line.quantity
35
        discount_value = 0
36
 
5997 amit.gupta 37
        if line.itemId in itemTuple:
6018 amit.gupta 38
            discount = args['discount']
5970 amit.gupta 39
            cart_has_specified_item = True
6018 amit.gupta 40
            line.discountedPrice = round(line.actualPrice - discount, 4)
5970 amit.gupta 41
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
6018 amit.gupta 42
            discount_value = discount
5970 amit.gupta 43
        else:
44
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
45
 
46
        if discount_value > 0:
47
            discount = Discount()
48
            discount.discount = discount_value
49
            discount.quantity = line.quantity
50
            discount.cart_id = cart.id
51
            discount.item_id = line.itemId
52
 
53
            discounts.append(discount)
54
 
55
    if cart_has_specified_item is False:
5972 amit.gupta 56
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
5970 amit.gupta 57
 
58
    cart.totalPrice = round(total_selling_price, 4)
59
    cart.discountedPrice = total_discounted_price
60
    return cart, discounts
61
 
62
def getDiscountOnItem(item):
5997 amit.gupta 63
    return None