Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6026 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 = (6737, 6830, 7769)
11
def execute(cart, coupon_code, args):
12
 
13
    if 50 <= get_coupon_usage_count(coupon_code):
14
        raise PromotionException(112, 'This promotion is over.')
15
 
16
    user_client = UserClient().get_client()
17
    user = user_client.getUserByCartId(cart.id)
18
 
19
    if 2 <= get_coupon_usage_count_by_user(coupon_code, user.userId):
20
        raise PromotionException(111, 'You have already used this coupon maximum possible times.')
21
 
22
    if not cart.lines:
23
        return cart
24
 
25
    total_selling_price = 0
26
    total_discounted_price = 0
27
    cart_has_specified_item = False
28
    discounts = []
29
    for line in cart.lines:
30
        total_selling_price += line.actualPrice * line.quantity
31
        discount_value = 0
32
 
33
        if line.itemId in itemTuple:
34
            discount = 1000
35
            cart_has_specified_item = True
36
            line.discountedPrice = round(line.actualPrice - discount, 4)
37
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
38
            discount_value = discount
39
        else:
40
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
41
 
42
        if discount_value > 0:
43
            discount = Discount()
44
            discount.discount = discount_value
45
            discount.quantity = line.quantity
46
            discount.cart_id = cart.id
47
            discount.item_id = line.itemId
48
 
49
            discounts.append(discount)
50
 
51
    if cart_has_specified_item is False:
52
        raise PromotionException(115, 'This coupon is applicable only for Samsung Galaxy S3 16GB')
53
 
54
    cart.totalPrice = round(total_selling_price, 4)
55
    cart.discountedPrice = total_discounted_price
56
    return cart, discounts
57
 
58
def getDiscountOnItem(item):
59
    if item.id in itemTuple:
60
        return 1000 
61
    return None