Subversion Repositories SmartDukaan

Rev

Rev 6009 | Rev 6367 | 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
 
6250 amit.gupta 7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count_by_user
4941 varun.gupt 10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
6250 amit.gupta 11
from shop2020.utils.Utils import to_py_date
12
import datetime
4186 varun.gupt 13
 
14
def execute(cart, coupon_code, args):
15
 
16
    user_client = UserClient().get_client()
5331 rajveer 17
    user = user_client.getUserByCartId(cart.id)
4186 varun.gupt 18
 
4291 varun.gupt 19
    email = user.email.lower().strip()
20
 
4941 varun.gupt 21
    if email in args['emails']:
22
        discount_value = args['discount']
4291 varun.gupt 23
    else:
4186 varun.gupt 24
        raise PromotionException(111, 'You are not allowed to use this coupon')
25
 
6250 amit.gupta 26
    todate = datetime.datetime.now()
27
    if args['endOn'] and todate > to_py_date(args['endOn']):
28
        raise PromotionException(113, 'This coupon is expired.')
29
 
5353 varun.gupt 30
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
4186 varun.gupt 31
 
4941 varun.gupt 32
    if count_coupon_usage >= args['usage_limit_for_user']:
33
        raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
4186 varun.gupt 34
 
35
    discounts = []
36
 
37
    if cart.lines:
38
        total_selling_price = 0
39
        total_discounted_price = 0
40
 
41
        for line in cart.lines:
6009 amit.gupta 42
            line_total_price = line.actualPrice * line.quantity
43
            line_total_discount = 0
44
            while line_total_discount < line_total_price and discount_value > 0:
45
                if line.actualPrice < discount_value:
46
                    discountGiven = line.actualPrice
47
                    quantity = min(line.quantity,int(discount_value/discountGiven))
48
                else:
49
                    discountGiven = discount_value
50
                    quantity = 1
51
 
52
 
4186 varun.gupt 53
                discount = Discount()
54
                discount.cart_id = cart.id
55
                discount.item_id = line.itemId
6009 amit.gupta 56
                discount.discount = discountGiven
57
                discount.quantity = quantity
58
                discounts.append(discount)
4186 varun.gupt 59
 
6009 amit.gupta 60
                discount_value = discount_value - discountGiven*quantity 
61
                line_total_discount += discountGiven*quantity
4186 varun.gupt 62
 
6009 amit.gupta 63
            total_selling_price += line_total_price
64
            total_discounted_price += line_total_price - line_total_discount
65
 
66
 
67
        cart.totalPrice = total_selling_price
68
        cart.discountedPrice = total_discounted_price
4186 varun.gupt 69
 
5044 varun.gupt 70
    return cart, discounts
71
 
72
def getDiscountOnItem(item):
73
    return None