| 3657 |
vikas |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
'''
|
|
|
4 |
Created on 12-Oct-2011
|
|
|
5 |
|
|
|
6 |
@author: Vikas
|
|
|
7 |
'''
|
|
|
8 |
|
|
|
9 |
import optparse
|
|
|
10 |
import xlrd
|
|
|
11 |
import datetime
|
| 6481 |
rajveer |
12 |
from shop2020.model.v1.user.impl.UserDataAccessors import add_address_for_user
|
|
|
13 |
from shop2020.thriftpy.model.v1.user.ttypes import Address
|
| 3657 |
vikas |
14 |
|
| 4031 |
chandransh |
15 |
if __name__ == '__main__' and __package__ is None:
|
|
|
16 |
import sys
|
|
|
17 |
import os
|
|
|
18 |
sys.path.insert(0, os.getcwd())
|
|
|
19 |
|
| 3657 |
vikas |
20 |
from shop2020.clients.UserClient import UserClient
|
|
|
21 |
from shop2020.model.v1.user.impl.CartDataAccessors import create_line_item, create_order
|
|
|
22 |
from shop2020.model.v1.user.impl import Dataservice
|
|
|
23 |
from shop2020.clients.TransactionClient import TransactionClient
|
|
|
24 |
from shop2020.clients.PaymentClient import PaymentClient
|
|
|
25 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, TransactionStatus
|
|
|
26 |
from shop2020.thriftpy.payments.ttypes import Attribute, PaymentStatus
|
|
|
27 |
from shop2020.utils.Utils import to_java_date
|
|
|
28 |
|
|
|
29 |
RTGS_GATEWAY_ID = 6
|
|
|
30 |
PAYMETHOD_ATTR = 'payMethod'
|
|
|
31 |
RTGS_ID_ATTR = 'rtgsId'
|
|
|
32 |
RTGS_BANK_ATTR = 'rtgsBank'
|
|
|
33 |
RTGS_BRANCH_ATTR = 'rtgsBranch'
|
|
|
34 |
IFSC_ATTR = 'ifscCode'
|
|
|
35 |
|
| 3666 |
vikas |
36 |
ITEM_ID_KEY = 'itemId'
|
|
|
37 |
ITEM_DESC_KEY = 'itemDesc'
|
|
|
38 |
PRICE_KEY = 'price'
|
|
|
39 |
QUANTITY_KEY = 'quantity'
|
|
|
40 |
AMOUNT_KEY = 'amount'
|
| 6481 |
rajveer |
41 |
ADDRESS_KEY = 'address'
|
| 3666 |
vikas |
42 |
|
| 3657 |
vikas |
43 |
user_client = UserClient().get_client()
|
|
|
44 |
transaction_client = TransactionClient().get_client()
|
|
|
45 |
payment_client = PaymentClient().get_client()
|
|
|
46 |
|
|
|
47 |
def create_orders(user, items):
|
|
|
48 |
orders = []
|
|
|
49 |
for item in items:
|
| 3768 |
vikas |
50 |
line_item = create_line_item(int(item[ITEM_ID_KEY]), item[PRICE_KEY], item[QUANTITY_KEY])
|
| 6481 |
rajveer |
51 |
addressId = add_address_for_user(item[ADDRESS_KEY], user.userId, False)
|
|
|
52 |
orders.append(create_order(user.userId, addressId, line_item, 0, 0))
|
| 3657 |
vikas |
53 |
return orders
|
|
|
54 |
|
|
|
55 |
|
| 3666 |
vikas |
56 |
def create_transaction(user, items):
|
| 3657 |
vikas |
57 |
txn = Transaction()
|
|
|
58 |
txn.shoppingCartid = user.activeCartId
|
|
|
59 |
txn.customer_id = user.userId
|
|
|
60 |
txn.createdOn = to_java_date(datetime.datetime.now())
|
|
|
61 |
txn.transactionStatus = TransactionStatus.INIT
|
|
|
62 |
txn.statusDescription = "New Order"
|
| 3666 |
vikas |
63 |
txn.orders = create_orders(user, items)
|
| 3657 |
vikas |
64 |
transaction_client = TransactionClient().get_client()
|
|
|
65 |
txn_id = transaction_client.createTransaction(txn)
|
|
|
66 |
return txn_id
|
|
|
67 |
|
|
|
68 |
def create_payment(user, amount , txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc):
|
|
|
69 |
payment_id = payment_client.createPayment(user.userId, amount, RTGS_GATEWAY_ID, txn_id)
|
|
|
70 |
payment_client.getPayment(payment_id)
|
|
|
71 |
attributes = []
|
| 3724 |
vikas |
72 |
attributes.append(Attribute(PAYMETHOD_ATTR, '4000'))
|
| 3657 |
vikas |
73 |
attributes.append(Attribute(RTGS_ID_ATTR, str(rtgs_id)))
|
|
|
74 |
attributes.append(Attribute(RTGS_BANK_ATTR, rtgs_bank))
|
|
|
75 |
attributes.append(Attribute(RTGS_BRANCH_ATTR, rtgs_branch))
|
|
|
76 |
attributes.append(Attribute(IFSC_ATTR, str(ifsc)))
|
|
|
77 |
payment_client.updatePaymentDetails(payment_id, None, None, None, None, None, None, None, None, PaymentStatus.SUCCESS, None, attributes)
|
|
|
78 |
|
|
|
79 |
def load_orders(file):
|
|
|
80 |
workbook = xlrd.open_workbook(file)
|
|
|
81 |
sheet = workbook.sheet_by_index(0)
|
|
|
82 |
num_rows = sheet.nrows
|
|
|
83 |
|
|
|
84 |
email, rtgs_id, rtgs_bank, rtgs_branch, ifsc, amount = sheet.col_values(1)[0:6]
|
|
|
85 |
|
|
|
86 |
user = user_client.getUserByEmail(email)
|
|
|
87 |
if user.userId == -1:
|
|
|
88 |
print "The email is not registered with us : " + email
|
|
|
89 |
return
|
|
|
90 |
|
|
|
91 |
items = []
|
|
|
92 |
total_amount = 0
|
|
|
93 |
for rownum in range(10, num_rows):
|
| 3666 |
vikas |
94 |
item = {}
|
|
|
95 |
item[ITEM_ID_KEY], item[ITEM_DESC_KEY], item[PRICE_KEY], item[QUANTITY_KEY], item[AMOUNT_KEY] = sheet.row_values(rownum)[0:5]
|
| 6481 |
rajveer |
96 |
name, line1, city, state, pin, phone = sheet.row_values(rownum)[5:11]
|
| 3666 |
vikas |
97 |
total_amount += item[AMOUNT_KEY]
|
|
|
98 |
items.append(item)
|
| 3657 |
vikas |
99 |
|
| 6481 |
rajveer |
100 |
address = Address()
|
|
|
101 |
address.name = name
|
|
|
102 |
address.line1 = line1
|
|
|
103 |
address.city = city
|
|
|
104 |
address.state = state
|
|
|
105 |
address.pin = str(pin)
|
|
|
106 |
address.phone = str(phone)
|
|
|
107 |
item[ADDRESS_KEY] = address
|
|
|
108 |
|
| 3657 |
vikas |
109 |
if amount != total_amount:
|
|
|
110 |
print "Amount paid is not equal to the total amount of bulk order."
|
|
|
111 |
return
|
|
|
112 |
|
| 3666 |
vikas |
113 |
txn_id = create_transaction(user, items)
|
| 3657 |
vikas |
114 |
create_payment(user, amount, txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc)
|
|
|
115 |
transaction_client.changeTransactionStatus(txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order");
|
| 3666 |
vikas |
116 |
transaction_client.changeTransactionStatus(txn_id, TransactionStatus.IN_PROCESS, "RTGS Payment accepted");
|
| 3657 |
vikas |
117 |
|
|
|
118 |
def main():
|
|
|
119 |
parser = optparse.OptionParser()
|
|
|
120 |
parser.add_option("-f", "--file", dest="file",
|
|
|
121 |
help="Excel file with bulk orders.")
|
| 3768 |
vikas |
122 |
parser.add_option("-d", "--db_server", dest="host",
|
|
|
123 |
default="localhost",
|
|
|
124 |
help="Database server.")
|
| 3657 |
vikas |
125 |
(options, args) = parser.parse_args()
|
|
|
126 |
if len(args) != 0:
|
|
|
127 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
| 3815 |
vikas |
128 |
if options.file == None:
|
|
|
129 |
parser.error("Excel File with bulk orders not supplied.")
|
| 3768 |
vikas |
130 |
|
|
|
131 |
Dataservice.initialize(db_hostname=options.host)
|
| 3657 |
vikas |
132 |
|
| 3815 |
vikas |
133 |
load_orders(options.file)
|
|
|
134 |
#load_orders('/home/vikas/RTGS.xls')
|
| 3657 |
vikas |
135 |
|
|
|
136 |
if __name__ == '__main__':
|
| 3815 |
vikas |
137 |
main()
|