Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2465 varun.gupt 1
'''
2
Created on 06-Jul-2011
3
 
4
@author: Varun Gupta
5
'''
6470 amit.gupta 6
from shop2020.clients.UserClient import UserClient
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
8
    get_coupon_usage_count, get_coupon_usage_count_by_user
4862 varun.gupt 9
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
6470 amit.gupta 10
import datetime
2465 varun.gupt 11
 
12
def execute(cart, coupon_code, args):
6450 amit.gupta 13
    if 'items' not in args or 'handset_display_name' not in args:
2465 varun.gupt 14
        raise PromotionException(109, 'Error in rule arguments.')
15
 
6470 amit.gupta 16
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y%m%d') <= datetime.datetime.now():
17
        raise PromotionException(112, 'This promotion is expired.')
18
 
2465 varun.gupt 19
    if 'usage_limit' in args and args['usage_limit'] is not None:
20
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
21
            raise PromotionException(112, 'This promotion is over.')
22
 
5331 rajveer 23
    user_client = UserClient().get_client()
24
    user = user_client.getUserByCartId(cart.id)
25
 
2465 varun.gupt 26
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
5353 varun.gupt 27
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
2465 varun.gupt 28
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
29
 
30
    if not cart.lines:
31
        return cart
6450 amit.gupta 32
    isPercentageDiscount = False
2465 varun.gupt 33
    total_selling_price = 0
34
    total_discounted_price = 0
35
    cart_has_specified_item = False
4859 varun.gupt 36
    discounts = []
2465 varun.gupt 37
 
6450 amit.gupta 38
    if 'is_percentage_discount' in args:
39
        isPercentageDiscount = args['is_percentage_discount']
2465 varun.gupt 40
    for line in cart.lines:
41
        total_selling_price += line.actualPrice * line.quantity
4859 varun.gupt 42
        discount_value = 0
2465 varun.gupt 43
 
6470 amit.gupta 44
        if line.itemId in args['items'] or (len(args['items'])==0 and args['min_amount'] <= line.actualPrice):
2465 varun.gupt 45
            cart_has_specified_item = True
6469 amit.gupta 46
            if(len(args['items'])==0):
47
                discount_value = args['discount']
48
            else: 
49
                discount_value = args['items'][line.itemId]
6450 amit.gupta 50
            if isPercentageDiscount:
6455 amit.gupta 51
                discount_value = round((line.actualPrice*discount_value)/100, 4)
6450 amit.gupta 52
 
6456 amit.gupta 53
            line.discountedPrice = round(line.actualPrice - discount_value, 4)
2465 varun.gupt 54
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
55
        else:
56
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
4859 varun.gupt 57
 
58
        if discount_value > 0:
59
            discount = Discount()
60
            discount.discount = discount_value
61
            discount.quantity = line.quantity
62
            discount.cart_id = cart.id
63
            discount.item_id = line.itemId
64
 
65
            discounts.append(discount)
2465 varun.gupt 66
 
67
    if cart_has_specified_item is False:
68
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
69
 
70
    cart.totalPrice = round(total_selling_price, 4)
71
    cart.discountedPrice = total_discounted_price
5044 varun.gupt 72
    return cart, discounts
73
 
74
def getDiscountOnItem(item):
75
    return None