Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6250 amit.gupta 1
'''
2
Created on 06-Dec-2011
3
 
4
@author: varungupta
5
'''
6
 
7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count_by_user
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
11
from shop2020.utils.Utils import to_py_date
12
import datetime
13
 
14
def execute(cart, coupon_code, args):
6282 amit.gupta 15
    minPrice = 5000
6250 amit.gupta 16
    user_client = UserClient().get_client()
17
    user = user_client.getUserByCartId(cart.id)
18
 
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']:
6250 amit.gupta 25
        discount_value = args['discount']
26
    else:
27
        raise PromotionException(111, 'You are not entitled to use this Gift Voucher')
28
 
29
    todate = datetime.datetime.now()      
30
 
6367 amit.gupta 31
    if 'endOn' in args and todate > to_py_date(args['endOn']):
6250 amit.gupta 32
        raise PromotionException(113, 'This voucher is expired.')
33
 
34
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
35
    if count_coupon_usage >= args['usage_limit_for_user']:
36
        raise PromotionException(112, 'This Gift Voucher is used.')
37
 
6282 amit.gupta 38
 
6250 amit.gupta 39
    discounts = []
40
 
41
    if cart.lines:
42
        total_selling_price = 0
6282 amit.gupta 43
        productFound = False
6250 amit.gupta 44
        for line in cart.lines:
45
            line_total_price = line.actualPrice * line.quantity
6282 amit.gupta 46
            if (not productFound and line.actualPrice >= minPrice):
6250 amit.gupta 47
                discount = Discount()
48
                discount.cart_id = cart.id
49
                discount.item_id = line.itemId
6282 amit.gupta 50
                discount.quantity = 1
51
                discount.discount = discount_value
6250 amit.gupta 52
                discounts.append(discount)
6282 amit.gupta 53
                productFound = True
6250 amit.gupta 54
            total_selling_price += line_total_price
55
 
56
 
6282 amit.gupta 57
        if productFound:
58
            cart.totalPrice = total_selling_price
59
            cart.discountedPrice = total_selling_price - discount_value
60
        else:
61
            raise PromotionException(114, 'Your cart should have at least one item with selling price above Rs.' + str(minPrice)+ '.')
62
 
63
 
6250 amit.gupta 64
    return cart, discounts
65
 
66
def getDiscountOnItem(item):
67
    return None