| 5508 |
rajveer |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
If a order has been marked as delivered for more than
|
|
|
5 |
5 days and it has spicedeck voucher issuance penning, voucher should be issued.
|
|
|
6 |
|
|
|
7 |
@author: Rajveer
|
|
|
8 |
'''
|
|
|
9 |
|
|
|
10 |
import datetime
|
|
|
11 |
import optparse
|
|
|
12 |
import sys
|
|
|
13 |
import urllib2
|
|
|
14 |
from string import Template
|
|
|
15 |
from shop2020.clients.HelperClient import HelperClient
|
|
|
16 |
|
|
|
17 |
if __name__ == '__main__' and __package__ is None:
|
|
|
18 |
import os
|
|
|
19 |
sys.path.insert(0, os.getcwd())
|
|
|
20 |
|
|
|
21 |
from shop2020.clients.PromotionClient import PromotionClient
|
|
|
22 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus
|
|
|
23 |
from shop2020.model.v1.order.impl import DataService
|
|
|
24 |
from shop2020.model.v1.order.impl.DataService import RechargeVoucherTracker
|
|
|
25 |
from shop2020.thriftpy.model.v1.user.ttypes import Voucher, VoucherType
|
|
|
26 |
from elixir import session
|
|
|
27 |
|
|
|
28 |
voucherGenerationUrl = "http://www.spicedeck.com/mcommerce/couponregister.sdesk";
|
|
|
29 |
voucherActivationUrl = "http://www.spicedeck.com/mcommerce/couponassign.sdesk";
|
|
|
30 |
|
|
|
31 |
def issue_voucher_to_orders(days, db_hostname):
|
|
|
32 |
DataService.initialize(db_hostname=db_hostname, echoOn=True)
|
|
|
33 |
current_time = datetime.datetime.now()
|
|
|
34 |
to_datetime = datetime.datetime(current_time.year, current_time.month, current_time.day)
|
|
|
35 |
to_datetime = to_datetime - datetime.timedelta(days-1)
|
|
|
36 |
vouchers = RechargeVoucherTracker.query.filter_by(voucherIssued=False).all()
|
|
|
37 |
for voucher in vouchers:
|
|
|
38 |
order = voucher.order
|
|
|
39 |
if order.status == OrderStatus.DELIVERY_SUCCESS and order.delivery_timestamp <= to_datetime:
|
|
|
40 |
storeVoucher("sachingyal", "abc1234", voucher.amount)
|
|
|
41 |
coupon = issueVoucher("sachingyal", "abc1234", order.customer_id, order.customer_email, voucher.amount)
|
|
|
42 |
voucher.issuedOn = current_time
|
|
|
43 |
voucher.voucherIssued = True
|
|
|
44 |
voucher.voucherCode = coupon
|
|
|
45 |
sendEmail(order.customer_email, voucher.amount, order.id, coupon)
|
|
|
46 |
session.commit()
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
def storeVoucher(username, password, amount):
|
|
|
50 |
coupon = generateVoucher(username, password, amount)
|
|
|
51 |
voucher = Voucher()
|
|
|
52 |
voucher.voucherCode = coupon
|
|
|
53 |
voucher.voucherType = VoucherType.SPICEDECK_MOBILE
|
|
|
54 |
voucher.amount = amount
|
|
|
55 |
pc = PromotionClient().get_client()
|
|
|
56 |
pc.addVoucher(voucher)
|
|
|
57 |
|
|
|
58 |
def generateVoucher(username, password, amount):
|
|
|
59 |
params = "userid=" + username + "&password=" + password + "&amount=" + str(amount)
|
|
|
60 |
returnValue = urllib2.urlopen(voucherGenerationUrl + "?" + params).read()
|
|
|
61 |
if "-1" == returnValue:
|
|
|
62 |
raise Exception("Credit balance low.")
|
|
|
63 |
|
|
|
64 |
elif "0" == returnValue:
|
|
|
65 |
raise Exception("Network or some other failure.")
|
|
|
66 |
|
|
|
67 |
return returnValue;
|
|
|
68 |
|
|
|
69 |
def issueVoucher(username, password, userId, userEmail, amount):
|
|
|
70 |
pc = PromotionClient().get_client()
|
|
|
71 |
voucher = pc.assignVoucher(userId, userEmail, VoucherType.SPICEDECK_MOBILE, amount)
|
|
|
72 |
isActivated = activateVoucher(username, password, voucher.voucherCode, voucher.userEmail);
|
|
|
73 |
if not isActivated:
|
|
|
74 |
raise Exception("Voucher could not get activated.");
|
|
|
75 |
return voucher.voucherCode
|
|
|
76 |
def activateVoucher(username, password, voucherCode, userEmail):
|
|
|
77 |
params = "userid=" + username + "&password=" + password + "&cid=" + voucherCode + "&email=" + userEmail
|
|
|
78 |
returnValue = urllib2.urlopen(voucherActivationUrl + "?" + params).read()
|
|
|
79 |
if "1" == returnValue:
|
|
|
80 |
return True;
|
|
|
81 |
elif "-1" == returnValue:
|
|
|
82 |
raise Exception("Invalid voucher.")
|
|
|
83 |
elif "0" == returnValue:
|
|
|
84 |
raise Exception("Network or some other failure.")
|
|
|
85 |
return False
|
|
|
86 |
|
|
|
87 |
def sendEmail(email, amount, orderId, voucherCode):
|
|
|
88 |
html = """
|
|
|
89 |
<html>
|
|
|
90 |
<body>
|
|
|
91 |
<div>
|
|
|
92 |
<p>
|
|
|
93 |
Hi,<br /><br />
|
|
|
94 |
Recharge voucher of $amount against order id $orderId is mentioned below. You can use it to recharge mobile through www.spicedeck.com.
|
|
|
95 |
</p>
|
|
|
96 |
|
|
|
97 |
<p>
|
|
|
98 |
<strong>Product: $voucherCode </strong>
|
|
|
99 |
</p>
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
<p>
|
|
|
103 |
Regards,<br/>
|
|
|
104 |
Saholic Customer Support Team<br/>
|
|
|
105 |
www.saholic.com<br/>
|
|
|
106 |
Email: help@saholic.com<br/>
|
|
|
107 |
</p>
|
|
|
108 |
</div>
|
|
|
109 |
</body>
|
|
|
110 |
</html>
|
|
|
111 |
"""
|
|
|
112 |
|
|
|
113 |
html = Template(html).substitute(dict(orderId=orderId,amount=amount,voucherCode=voucherCode))
|
|
|
114 |
|
|
|
115 |
try:
|
|
|
116 |
helper_client = HelperClient().get_client()
|
|
|
117 |
helper_client.saveUserEmailForSending(email, "", "Recharge voucher against order id " + str(orderId), html, str(orderId), "RechargeVoucher")
|
|
|
118 |
except Exception as e:
|
|
|
119 |
print e
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
def main():
|
|
|
123 |
parser = optparse.OptionParser()
|
|
|
124 |
parser.add_option("-d", "--days", dest="days",
|
|
|
125 |
default=5, type="int",
|
|
|
126 |
help="issue voucher to the delivered orders before NUM days",
|
|
|
127 |
metavar="NUM")
|
|
|
128 |
parser.add_option("-H", "--host", dest="hostname",
|
|
|
129 |
default="localhost",
|
|
|
130 |
type="string", help="The HOST where the DB server is running",
|
|
|
131 |
metavar="HOST")
|
|
|
132 |
(options, args) = parser.parse_args()
|
|
|
133 |
if len(args) != 0:
|
|
|
134 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
135 |
issue_voucher_to_orders(options.days, options.hostname)
|
|
|
136 |
|
|
|
137 |
if __name__ == '__main__':
|
|
|
138 |
main()
|