Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4186 varun.gupt 1
'''
2
Created on 06-Dec-2011
3
 
4
@author: varungupta
5
'''
6
 
4941 varun.gupt 7
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
4186 varun.gupt 8
from shop2020.clients.UserClient import UserClient
9
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
10
 
11
def execute(cart, coupon_code, args):
12
 
13
    user_client = UserClient().get_client()
5331 rajveer 14
    user = user_client.getUserByCartId(cart.id)
4186 varun.gupt 15
 
4291 varun.gupt 16
    email = user.email.lower().strip()
17
 
4941 varun.gupt 18
    if email in args['emails']:
19
        discount_value = args['discount']
4291 varun.gupt 20
    else:
4186 varun.gupt 21
        raise PromotionException(111, 'You are not allowed to use this coupon')
22
 
5353 varun.gupt 23
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
4186 varun.gupt 24
 
4941 varun.gupt 25
    if count_coupon_usage >= args['usage_limit_for_user']:
26
        raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
4186 varun.gupt 27
 
28
    discounts = []
29
 
30
    if cart.lines:
31
        total_selling_price = 0
32
        total_discounted_price = 0
4187 varun.gupt 33
        has_used_coupon = False
4186 varun.gupt 34
 
35
        for line in cart.lines:
36
            line.discountedPrice = line.actualPrice
37
 
4187 varun.gupt 38
            if has_used_coupon:
39
                break
4186 varun.gupt 40
 
4291 varun.gupt 41
            line.discountedPrice = line.actualPrice - discount_value
4187 varun.gupt 42
 
43
            has_used_coupon = True
44
 
4186 varun.gupt 45
            total_selling_price += line.actualPrice * line.quantity
6001 amit.gupta 46
            total_discounted_price += line.discountedPrice + (line.quantity-1)*line.actualPrice
4186 varun.gupt 47
 
48
            if discount_value > 0:
49
                discount = Discount()
50
                discount.cart_id = cart.id
51
                discount.item_id = line.itemId
52
                discount.discount = discount_value
53
                discount.quantity = 1
54
 
55
                discounts.append(discount)
56
 
57
            cart.totalPrice = total_selling_price
58
            cart.discountedPrice = total_discounted_price
59
 
5044 varun.gupt 60
    return cart, discounts
61
 
62
def getDiscountOnItem(item):
63
    return None