Subversion Repositories SmartDukaan

Rev

Rev 6682 | Rev 7475 | 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
 
6682 anupam.sin 21
    if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':
6679 anupam.sin 22
        raise PromotionException(111, 'This coupon is not valid')
23
 
24
    if args['emails'] == '*' or email in args['emails']:
4941 varun.gupt 25
        discount_value = args['discount']
4291 varun.gupt 26
    else:
4186 varun.gupt 27
        raise PromotionException(111, 'You are not allowed to use this coupon')
28
 
6250 amit.gupta 29
    todate = datetime.datetime.now()
6367 amit.gupta 30
    if 'endOn' in args and todate > to_py_date(args['endOn']):
6250 amit.gupta 31
        raise PromotionException(113, 'This coupon is expired.')
32
 
5353 varun.gupt 33
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
4186 varun.gupt 34
 
4941 varun.gupt 35
    if count_coupon_usage >= args['usage_limit_for_user']:
36
        raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
4186 varun.gupt 37
 
38
    discounts = []
39
 
40
    if cart.lines:
6903 anupam.sin 41
        total_discounts = 0
4186 varun.gupt 42
 
43
        for line in cart.lines:
6903 anupam.sin 44
            line_total_price = line.actualPrice * line.quantity + line.insuranceAmount
6009 amit.gupta 45
            line_total_discount = 0
46
            while line_total_discount < line_total_price and discount_value > 0:
47
                if line.actualPrice < discount_value:
48
                    discountGiven = line.actualPrice
49
                    quantity = min(line.quantity,int(discount_value/discountGiven))
50
                else:
51
                    discountGiven = discount_value
52
                    quantity = 1
53
 
4186 varun.gupt 54
                discount = Discount()
55
                discount.cart_id = cart.id
56
                discount.item_id = line.itemId
6009 amit.gupta 57
                discount.discount = discountGiven
58
                discount.quantity = quantity
59
                discounts.append(discount)
4186 varun.gupt 60
 
6009 amit.gupta 61
                discount_value = discount_value - discountGiven*quantity 
62
                line_total_discount += discountGiven*quantity
4186 varun.gupt 63
 
6903 anupam.sin 64
            total_discounts += line_total_discount
6009 amit.gupta 65
 
6903 anupam.sin 66
        cart.discountedPrice = cart.totalPrice - total_discounts
4186 varun.gupt 67
 
5044 varun.gupt 68
    return cart, discounts
69
 
70
def getDiscountOnItem(item):
71
    return None