Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6203 amit.gupta 1
'''
2
Created on 06-Jul-2011
3
 
4
@author: Varun Gupta
5
'''
6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
8
from shop2020.clients.UserClient import UserClient
9
 
10
itemTuple = {3880 : 230, 2082 : 230, 2083 : 230, 7297 : 230, 7548 : 230, \
11
             7813:639, 7841:639, 7848:639, 7849:639, \
12
             6803: 306, 6748: 306, \
13
             2273: 399, 7768: 399}
14
def execute(cart, coupon_code, args):
15
 
16
    if 50 <= get_coupon_usage_count(coupon_code):
17
        raise PromotionException(112, 'This promotion is over.')
18
 
19
    user_client = UserClient().get_client()
20
    user = user_client.getUserByCartId(cart.id)
21
 
22
    if 2 <= get_coupon_usage_count_by_user(coupon_code, user.userId):
23
        raise PromotionException(111, 'You have already used this coupon maximum possible times.')
24
 
25
    if not cart.lines:
26
        return cart
27
 
28
    total_selling_price = 0
29
    total_discounted_price = 0
30
    cart_has_specified_item = False
31
    discounts = []
32
    for line in cart.lines:
33
        total_selling_price += line.actualPrice * line.quantity
34
        discount_value = 0
35
 
36
        discount = itemTuple.get(line.itemId)
37
        if discount:
38
            cart_has_specified_item = True
39
            line.discountedPrice = round(line.actualPrice - discount, 4)
40
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
41
            discount_value = discount
42
        else:
43
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
44
 
45
        if discount_value > 0:
46
            discount1 = Discount()
47
            discount1.discount = discount_value
48
            discount1.quantity = line.quantity
49
            discount1.cart_id = cart.id
50
            discount1.item_id = line.itemId
51
 
52
            discounts.append(discount1)
53
 
54
    if cart_has_specified_item is False:
55
        raise PromotionException(115, 'This coupon is only applicable on specific items')
56
 
57
    cart.totalPrice = round(total_selling_price, 4)
58
    cart.discountedPrice = total_discounted_price
59
    return cart, discounts
60
 
61
def getDiscountOnItem(item):
62
    if item.id in itemTuple:
63
        return 1000 
64
    return None