Subversion Repositories SmartDukaan

Rev

Rev 6002 | Rev 6250 | 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
33
 
34
        for line in cart.lines:
6009 amit.gupta 35
            line_total_price = line.actualPrice * line.quantity
36
            line_total_discount = 0
37
            while line_total_discount < line_total_price and discount_value > 0:
38
                if line.actualPrice < discount_value:
39
                    discountGiven = line.actualPrice
40
                    quantity = min(line.quantity,int(discount_value/discountGiven))
41
                else:
42
                    discountGiven = discount_value
43
                    quantity = 1
44
 
45
 
4186 varun.gupt 46
                discount = Discount()
47
                discount.cart_id = cart.id
48
                discount.item_id = line.itemId
6009 amit.gupta 49
                discount.discount = discountGiven
50
                discount.quantity = quantity
51
                discounts.append(discount)
4186 varun.gupt 52
 
6009 amit.gupta 53
                discount_value = discount_value - discountGiven*quantity 
54
                line_total_discount += discountGiven*quantity
4186 varun.gupt 55
 
6009 amit.gupta 56
            total_selling_price += line_total_price
57
            total_discounted_price += line_total_price - line_total_discount
58
 
59
 
60
        cart.totalPrice = total_selling_price
61
        cart.discountedPrice = total_discounted_price
4186 varun.gupt 62
 
5044 varun.gupt 63
    return cart, discounts
64
 
65
def getDiscountOnItem(item):
66
    return None