Subversion Repositories SmartDukaan

Rev

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

Rev 6649 Rev 6679
Line 42... Line 42...
42
    coupon.promotion = promotion
42
    coupon.promotion = promotion
43
    coupon.coupon_code = coupon_code
43
    coupon.coupon_code = coupon_code
44
    coupon.arguments = ""
44
    coupon.arguments = ""
45
    session.commit()
45
    session.commit()
46
 
46
 
47
def create_coupon(promotionId, endOn, email, amount, isCod, usage, prefix=None):
47
def create_coupon(promotionId, arguments, isCod, prefix=None):
48
    if promotionId not in (26,27,28):
48
    if promotionId not in (26,27,28):
49
        raise PromotionException(101, 'Only promotion ids 26 and 27 are expected')
49
        raise PromotionException(101, 'Only promotion ids 26 and 27 are expected')
50
    promotion = Promotion.get_by(id = promotionId)
50
    promotion = Promotion.get_by(id = promotionId)
51
    if promotion:     
51
    if promotion:     
52
        coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:10]
52
        coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:10]
53
        coupon = Coupon.get_by(coupon_code = coupon_code)
53
        coupon = Coupon.get_by(coupon_code = coupon_code)
54
        while coupon is not None:
54
        while coupon is not None:
55
            coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:10]
55
            coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:10]
56
            coupon = Coupon.get_by(coupon_code = coupon_code)
56
            coupon = Coupon.get_by(coupon_code = coupon_code)
57
        s=Template('{"emails":["$email"],"discount":$amount,"usage_limit_for_user":$usage,"endOn":$endOn, "isCod":$isCod}')
-
 
58
        coupon = Coupon()
57
        coupon = Coupon()
59
        coupon.promotion = promotion
58
        coupon.promotion = promotion
60
        coupon.coupon_code = coupon_code
59
        coupon.coupon_code = coupon_code
61
        coupon.arguments = s.substitute(email=email, amount=amount, usage=usage, endOn=endOn, isCod=isCod)
60
        coupon.arguments = arguments
62
        session.commit()
61
        session.commit()
63
        return coupon_code
62
        return coupon_code
64
    else:
63
    else:
65
        raise PromotionException(101, 'Could not find Promotion for id' + str(promotionId))
64
        raise PromotionException(101, 'Could not find Promotion for id' + str(promotionId))
66
    
65
    
Line 68... Line 67...
68
    return Coupon.get_by(coupon_code = coupon_code)
67
    return Coupon.get_by(coupon_code = coupon_code)
69
 
68
 
70
def apply_recharge_coupon(coupon_code, total_amount, user_id):
69
def apply_recharge_coupon(coupon_code, total_amount, user_id):
71
    coupon = Coupon.get_by(coupon_code = coupon_code)
70
    coupon = Coupon.get_by(coupon_code = coupon_code)
72
    discount = 0
71
    discount = 0
73
    
-
 
74
    if coupon:
72
    if coupon:
75
        
-
 
76
        args = eval(coupon.arguments) if coupon.arguments is not None else {}
73
        args = eval(coupon.arguments) if coupon.arguments is not None else {}
77
        
74
        
78
        if 'percent' in args :
75
        todate = datetime.datetime.now()
79
            percentDiscount = args['percent']
76
        if 'endOn' in args and todate > to_py_date(args['endOn']) :
80
        else :
-
 
81
            return {0 : 'Currently this coupon is unavailable'}
77
            return {0:'This coupon has expired'}
82
        
78
        
83
        if 'minDiscountableVal' in args and total_amount < int(args['minDiscountableVal']):
79
        if 'minDiscountableVal' in args and total_amount < int(args['minDiscountableVal']):
84
            return {0:'This coupon is valid for recharges equal to or more than Rs.' + (args['minDiscountableVal'])}
80
            return {0:'This coupon is valid for purchases equal to or more than Rs.' + (args['minDiscountableVal'])}
85
    
81
    
86
        if 'couponType' in args and (args['couponType']) == 'RECHARGE':
82
        if 'couponType' not in args or args['couponType'] != 'recharge' or args['couponType'] != 'both':
87
            discount = round(total_amount * percentDiscount)
-
 
88
        else :
-
 
89
            return {0:'Invalid Coupon'}
83
            return {0:'Invalid Coupon'}
90
        
84
        
-
 
85
        if args['emails'] != '*':
-
 
86
            user_client = UserClient().get_client()
91
        todate = datetime.datetime.now()
87
            user = user_client.getUserById(user_id)
92
        if 'endOn' in args and todate > to_py_date(args['endOn']) :
88
            if not (user.email in args['emails']):
93
            return {0:'This coupon is expired.'}
89
                return {0:'Invalid coupon'}
94
        
90
            
95
        if 'startHour' in args :
91
        if 'startHour' in args and 'startMinute' in args and 'endHour' in args and 'endMinute' in args :
96
            startHour = int(args['startHour'])
92
            startHour = int(args['startHour'])
97
        else :
-
 
98
            return {0 : 'Currently this coupon is unavailable'}
-
 
99
        
-
 
100
        if 'startMinute' in args :
-
 
101
            startMinute = int(args['startMinute'])
93
            startMinute = int(args['startMinute'])
102
        else :
-
 
103
            return {0 : 'Currently this coupon is unavailable'}
-
 
104
        
-
 
105
        if 'endHour' in args :
-
 
106
            endHour = int(args['endHour'])
94
            endHour = int(args['endHour'])   
107
        else :
-
 
108
            return {0 : 'Currently this coupon is unavailable'}
-
 
109
        
-
 
110
        if 'endMinute' in args :
-
 
111
            endMinute = int(args['endMinute'])
95
            endMinute = int(args['endMinute'])
112
        else :
-
 
113
            return {0 : 'Currently this coupon is unavailable'}
-
 
114
            
-
 
115
        now = datetime.datetime.now()
96
            now = datetime.datetime.now()
116
        curtime = datetime.time(now.hour, now.minute, now.second)
97
            curtime = datetime.time(now.hour, now.minute, now.second)
117
        starttime = datetime.time(startHour, startMinute, 0)
98
            starttime = datetime.time(startHour, startMinute, 0)
118
        endtime = datetime.time(endHour, endMinute, 0)
99
            endtime = datetime.time(endHour, endMinute, 0)
119
        if (curtime < starttime or curtime > endtime):
100
            if (curtime < starttime or curtime > endtime):
120
            return {0:'Currently this coupon is unavailable'}
101
                return {0:'Currently this coupon is unavailable'}
121
 
102
 
122
        if 'userLimit' in args :
103
        if 'usage_limit_for_user' in args :
123
            userLimit = args['userLimit']
104
            userLimit = args['usage_limit_for_user']
124
        else :
105
        else :
125
            return {0 : 'Currently this coupon is unavailable'}
106
            userLimit = 1
126
        
107
        
127
        count_coupon_usage_by_user = get_coupon_usage_count_by_user(coupon_code, user_id)
108
        count_coupon_usage_by_user = get_coupon_usage_count_by_user(coupon_code, user_id)
128
        if count_coupon_usage_by_user >= userLimit:
109
        if count_coupon_usage_by_user >= userLimit:
129
            return {0:'This coupon is applicable only ' + str(userLimit) + ' times per user'}
110
            return {0:'This coupon is applicable only ' + str(userLimit) + ' time per user'}
130
        
111
        
131
        if 'globalLimit' in args :
112
        if 'globalLimit' in args :
132
            globalLimit = args['globalLimit']
113
            globalLimit = args['globalLimit']
-
 
114
            count_coupon_usage = get_coupon_usage_count(coupon_code)
133
        else :
115
            if count_coupon_usage >= globalLimit:
134
            return {0 : 'Currently this coupon is unavailable'}
116
                return {0:'This promotion is over.'}
135
        
117
        
-
 
118
        if 'discountType' in args:
136
        count_coupon_usage = get_coupon_usage_count(coupon_code)
119
            discountType = args['discountType']
-
 
120
            if discountType == 'percent' :
-
 
121
                discount = round(total_amount * (args['discount']/100))
137
        if count_coupon_usage >= globalLimit:
122
            if discountType == 'absolute' :
-
 
123
                discount = min(args['discount'], total_amount)
-
 
124
            else :
138
            return {0:'This promotion is over.'}
125
                return {0 : 'Invalid coupon'}
139
        
126
        
140
        if 'maxDiscount' in args and discount > int(args['maxDiscount']):
127
        if 'maxDiscount' in args and discount > int(args['maxDiscount']):
141
            return {int(args['maxDiscount']):"Coupon Applied"}
128
            return {int(args['maxDiscount']):"Coupon Applied"}
142
        
129
        
143
        return {discount:'Coupon Applied'}
130
        return {discount:'Coupon Applied'}