Subversion Repositories SmartDukaan

Rev

Rev 6903 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6903 Rev 7475
Line 20... Line 20...
20
    
20
    
21
    if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':
21
    if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':
22
        raise PromotionException(111, 'This coupon is not valid')
22
        raise PromotionException(111, 'This coupon is not valid')
23
    
23
    
24
    if args['emails'] == '*' or email in args['emails']:
24
    if args['emails'] == '*' or email in args['emails']:
25
        discount_value = args['discount']
25
        discount_value_left = args['discount']
26
    else:
26
    else:
27
        raise PromotionException(111, 'You are not allowed to use this coupon')
27
        raise PromotionException(111, 'You are not allowed to use this coupon')
28
    
28
    
29
    todate = datetime.datetime.now()
29
    todate = datetime.datetime.now()
30
    if 'endOn' in args and todate > to_py_date(args['endOn']):
30
    if 'endOn' in args and todate > to_py_date(args['endOn']):
Line 36... Line 36...
36
        raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
36
        raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
37
    
37
    
38
    discounts = []
38
    discounts = []
39
    
39
    
40
    if cart.lines:
40
    if cart.lines:
41
        total_discounts = 0
41
        total_discount_given = 0
42
        
42
        
43
        for line in cart.lines:
43
        for line in cart.lines:
44
            line_total_price = line.actualPrice * line.quantity + line.insuranceAmount
44
            line_total_price = line.actualPrice * line.quantity + line.insuranceAmount
45
            line_total_discount = 0
45
            discountGivenForOneLine = 0
46
            while line_total_discount < line_total_price and discount_value > 0:
46
            while discountGivenForOneLine < line_total_price and discount_value_left > 0:
-
 
47
                priceOfOneItemIncludingInsurance = line.actualPrice + line.insuranceAmount/line.quantity
47
                if line.actualPrice < discount_value:
48
                if priceOfOneItemIncludingInsurance < discount_value_left:
48
                    discountGiven = line.actualPrice
49
                    discountGiven = priceOfOneItemIncludingInsurance
49
                    quantity = min(line.quantity,int(discount_value/discountGiven))
50
                    quantity = min(line.quantity,int(discount_value_left/discountGiven))
50
                else:
51
                else:
51
                    discountGiven = discount_value
52
                    discountGiven = discount_value_left
52
                    quantity = 1
53
                    quantity = 1
53
                
54
                
54
                discount = Discount()
55
                discount = Discount()
55
                discount.cart_id = cart.id
56
                discount.cart_id = cart.id
56
                discount.item_id = line.itemId
57
                discount.item_id = line.itemId
57
                discount.discount = discountGiven
58
                discount.discount = discountGiven
58
                discount.quantity = quantity
59
                discount.quantity = quantity
59
                discounts.append(discount)
60
                discounts.append(discount)
60
 
61
 
61
                discount_value = discount_value - discountGiven*quantity 
62
                discount_value_left = discount_value_left - discountGiven*quantity 
62
                line_total_discount += discountGiven*quantity
63
                discountGivenForOneLine += discountGiven*quantity
63
            
64
            
64
            total_discounts += line_total_discount
65
            total_discount_given += discountGivenForOneLine
65
            
66
            
66
        cart.discountedPrice = cart.totalPrice - total_discounts
67
        cart.discountedPrice = cart.totalPrice - total_discount_given
67
    
68
    
68
    return cart, discounts
69
    return cart, discounts
69
 
70
 
70
def getDiscountOnItem(item):
71
def getDiscountOnItem(item):
71
    return None
72
    return None
72
73