Subversion Repositories SmartDukaan

Rev

Rev 5972 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5970 amit.gupta 1
'''
2
Created on 06-Jul-2011
3
 
4
@author: Varun Gupta
5
'''
6
from shop2020.clients.CatalogClient import CatalogClient
7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
11
from shop2020.model.v1.user.impl import PromotionDataAccessors
12
 
13
def execute(cart, coupon):
14
 
15
    if coupon.usageLimit is not None:
16
        if coupon.usageLimit <= get_coupon_usage_count(coupon.couponCode):
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 coupon.usageLimitForUser is not None:
23
        if coupon.usageLimitForUser <= get_coupon_usage_count_by_user(coupon.couponCode, user.userId):
24
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
25
 
26
    if not cart.lines:
27
        return cart
28
 
29
    total_selling_price = 0
30
    total_discounted_price = 0
31
    cart_has_specified_item = False
32
    discounts = []
33
 
34
 
35
    couponMappings = PromotionDataAccessors.coupon_item_mapping_cache.get(coupon.couponCode)
36
 
37
    for line in cart.lines:
38
        total_selling_price += line.actualPrice * line.quantity
39
        discount_value = 0
40
 
41
        if couponMappings.has_key(line.itemId) :
42
            cart_has_specified_item = True
43
            line.discountedPrice = round(line.actualPrice - couponMappings[line.itemId], 4)
44
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
45
            discount_value = couponMappings[line.itemId]
46
        else:
47
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
48
 
49
        if discount_value > 0:
50
            discount = Discount()
51
            discount.discount = discount_value
52
            discount.quantity = line.quantity
53
            discount.cart_id = cart.id
54
            discount.item_id = line.itemId
55
 
56
            discounts.append(discount)
57
 
58
    if cart_has_specified_item is False:
59
        raise PromotionException(115, 'This coupon is not applicable current items in cart')
60
 
61
    cart.totalPrice = round(total_selling_price, 4)
62
    cart.discountedPrice = total_discounted_price
63
    return cart, discounts
64
 
65
def getDiscountOnItem(item):
66
    return 100