Subversion Repositories SmartDukaan

Rev

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

Rev 6367 Rev 6433
Line 12... Line 12...
12
import uuid
12
import uuid
13
 
13
 
14
from elixir import session
14
from elixir import session
15
import datetime, traceback
15
import datetime, traceback
16
from string import Template
16
from string import Template
-
 
17
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
17
 
18
 
18
def initialize(dbname = 'user', db_hostname="localhost"):
19
def initialize(dbname = 'user', db_hostname="localhost"):
19
    Dataservice.initialize(dbname, db_hostname)
20
    Dataservice.initialize(dbname, db_hostname)
20
 
21
 
21
def create_promotion(name, rule_execution_src, start_on, end_on):
22
def create_promotion(name, rule_execution_src, start_on, end_on):
Line 60... Line 61...
60
        raise PromotionException(101, 'Could not find Promotion for id' + str(promotionId))
61
        raise PromotionException(101, 'Could not find Promotion for id' + str(promotionId))
61
    
62
    
62
def get_coupon(coupon_code):
63
def get_coupon(coupon_code):
63
    return Coupon.get_by(coupon_code = coupon_code)
64
    return Coupon.get_by(coupon_code = coupon_code)
64
 
65
 
-
 
66
def apply_recharge_coupon(coupon_code, total_amount, user_id):
-
 
67
    coupon = Coupon.get_by(coupon_code = coupon_code)
-
 
68
    discount = 0
-
 
69
    
-
 
70
    if coupon:
-
 
71
        
-
 
72
        args = eval(coupon.arguments) if coupon.arguments is not None else {}
-
 
73
        
-
 
74
        if 'minDiscountableVal' in args and total_amount < int(args['minDiscountableVal']):
-
 
75
            return {0:'This coupon is valid for recharges equal to or more than Rs.' + (args['minDiscountableVal'])}
-
 
76
    
-
 
77
        if 'couponType' in args and (args['couponType']) == 'RECHARGE':
-
 
78
            discount = round(total_amount * 0.1)
-
 
79
        else :
-
 
80
            return {0:'Invalid Coupon'}
-
 
81
        
-
 
82
        todate = datetime.datetime.now()
-
 
83
        if 'endOn' in args and todate > to_py_date(args['endOn']) :
-
 
84
            return {0:'This coupon is expired.'}
-
 
85
        
-
 
86
        if 'startHour' in args :
-
 
87
            startHour = int(args['startHour'])
-
 
88
        else :
-
 
89
            return {0 : 'Currently this coupon is unavailable'}
-
 
90
        
-
 
91
        if 'startMinute' in args :
-
 
92
            startMinute = int(args['startMinute'])
-
 
93
        else :
-
 
94
            return {0 : 'Currently this coupon is unavailable'}
-
 
95
        
-
 
96
        if 'endHour' in args :
-
 
97
            endHour = int(args['endHour'])
-
 
98
        else :
-
 
99
            return {0 : 'Currently this coupon is unavailable'}
-
 
100
        
-
 
101
        if 'endMinute' in args :
-
 
102
            endMinute = int(args['endMinute'])
-
 
103
        else :
-
 
104
            return {0 : 'Currently this coupon is unavailable'}
-
 
105
            
-
 
106
        now = datetime.datetime.now()
-
 
107
        curtime = datetime.time(now.hour, now.minute, now.second)
-
 
108
        starttime = datetime.time(startHour, startMinute, 0)
-
 
109
        endtime = datetime.time(endHour, endMinute, 0)
-
 
110
        if (curtime < starttime or curtime > endtime):
-
 
111
            return {0:'Currently this coupon is unavailable'}
-
 
112
 
-
 
113
        count_coupon_usage_by_user = get_coupon_usage_count_by_user(coupon_code, user_id)
-
 
114
        if count_coupon_usage_by_user >=2:
-
 
115
            return {0:'This coupon is applicable only twice per user'}
-
 
116
        
-
 
117
        count_coupon_usage = get_coupon_usage_count(coupon_code)
-
 
118
        if count_coupon_usage >= 1000:
-
 
119
            return {0:'This promotion is over.'}
-
 
120
        
-
 
121
        if 'maxDiscount' in args and discount > int(args['maxDiscount']):
-
 
122
            return {int(args['maxDiscount']):"Coupon Applied"}
-
 
123
        
-
 
124
        return {discount:'Coupon Applied'}
-
 
125
    else:
-
 
126
        return {0:'Invalid Coupon'}
-
 
127
 
65
def apply_coupon(coupon_code, cart_id):
128
def apply_coupon(coupon_code, cart_id):
66
    coupon = Coupon.get_by(coupon_code = coupon_code)
129
    coupon = Coupon.get_by(coupon_code = coupon_code)
67
    
130
    
68
    if coupon:
131
    if coupon:
69
        todate = datetime.datetime.now()
132
        todate = datetime.datetime.now()
70
        if not (coupon.promotion.start_on <=  todate and coupon.promotion.end_on >= todate):
133
        if not (coupon.promotion.start_on <=  todate and coupon.promotion.end_on >= todate):
71
            raise PromotionException(101, 'Promotion has been expired')
134
            raise PromotionException(101, 'Promotion has expired')
72
    
135
    
73
        user_client = UserClient().get_client()
136
        user_client = UserClient().get_client()
74
        cart = user_client.getCart(cart_id)
137
        cart = user_client.getCart(cart_id)
75
        
138
        
76
        coupon_module = coupon.promotion.rule_execution_src.strip()
139
        coupon_module = coupon.promotion.rule_execution_src.strip()