Subversion Repositories SmartDukaan

Rev

Rev 3666 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
12
 
13
from shop2020.clients.UserClient import UserClient
14
from shop2020.model.v1.user.impl.CartDataAccessors import create_line_item, create_order
15
from shop2020.model.v1.user.impl import Dataservice
16
from shop2020.clients.TransactionClient import TransactionClient
17
from shop2020.clients.PaymentClient import PaymentClient
18
from shop2020.thriftpy.model.v1.order.ttypes import Transaction, TransactionStatus
19
from shop2020.thriftpy.payments.ttypes import Attribute, PaymentStatus
20
from shop2020.utils.Utils import to_java_date
21
 
22
RTGS_GATEWAY_ID = 6
23
PAYMETHOD_ATTR = 'payMethod'
24
RTGS_ID_ATTR = 'rtgsId'
25
RTGS_BANK_ATTR = 'rtgsBank'
26
RTGS_BRANCH_ATTR = 'rtgsBranch'
27
IFSC_ATTR = 'ifscCode'
28
 
29
user_client = UserClient().get_client()
30
transaction_client = TransactionClient().get_client()
31
payment_client = PaymentClient().get_client()
32
Dataservice.initialize()
33
 
34
def create_orders(user, items):
35
    orders = []
36
    for item in items:
37
        # item[3] is quantity
38
        for i in range (0, int(item[3])):
39
            line_item = create_line_item(int(item[0]), item[2])
40
            orders.append(create_order(user.userId, user.defaultAddressId, line_item))
41
    return orders
42
 
43
 
44
def create_transaction(user, orders):
45
    txn = Transaction()
46
    txn.shoppingCartid = user.activeCartId
47
    txn.customer_id = user.userId
48
    txn.createdOn = to_java_date(datetime.datetime.now())
49
    txn.transactionStatus = TransactionStatus.INIT
50
    txn.statusDescription = "New Order"
51
    txn.orders = orders
52
    transaction_client = TransactionClient().get_client()
53
    txn_id = transaction_client.createTransaction(txn)
54
    return txn_id
55
 
56
def create_payment(user, amount , txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc):
57
    payment_id = payment_client.createPayment(user.userId, amount, RTGS_GATEWAY_ID, txn_id)
58
    payment_client.getPayment(payment_id)
59
    attributes = []
60
    attributes.append(Attribute(PAYMETHOD_ATTR, 'RTGS'))
61
    attributes.append(Attribute(RTGS_ID_ATTR, str(rtgs_id)))
62
    attributes.append(Attribute(RTGS_BANK_ATTR, rtgs_bank))
63
    attributes.append(Attribute(RTGS_BRANCH_ATTR, rtgs_branch))
64
    attributes.append(Attribute(IFSC_ATTR, str(ifsc)))
65
    payment_client.updatePaymentDetails(payment_id, None, None, None, None, None, None, None, None, PaymentStatus.SUCCESS, None, attributes)
66
 
67
def load_orders(file):
68
    workbook = xlrd.open_workbook(file)
69
    sheet = workbook.sheet_by_index(0)
70
    num_rows = sheet.nrows
71
 
72
    email, rtgs_id, rtgs_bank, rtgs_branch, ifsc, amount = sheet.col_values(1)[0:6]
73
 
74
    user = user_client.getUserByEmail(email)
75
    if user.userId == -1:
76
        print "The email is not registered with us : " + email
77
        return
78
    if user.defaultAddressId == 0:
79
        print "No default address for this user : " + email
80
        return
81
 
82
    items = []
83
    total_amount = 0
84
    for rownum in range(10, num_rows):
85
        items.append(sheet.row_values(rownum)[0:5])
86
        total_amount += items[-1][4]
87
 
88
    if amount != total_amount:
89
        print "Amount paid is not equal to the total amount of bulk order."
90
        return
91
 
92
 
93
    orders = create_orders(user, items)
94
    txn_id = create_transaction(user, orders)
95
    create_payment(user, amount, txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc)
96
    transaction_client.changeTransactionStatus(txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order");
97
 
98
 
99
def main():
100
    parser = optparse.OptionParser()
101
    parser.add_option("-f", "--file", dest="file",
102
                   help="Excel file with bulk orders.")
103
    (options, args) = parser.parse_args()
104
    if len(args) != 0:
105
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
106
    if options.file == None:
107
        parser.error("Excel File with bulk orders not supplied.")
108
 
109
    load_orders(options.file)
110
    #load_orders('/home/vikas/tmp/Bulk Orders.xls')
111
 
112
if __name__ == '__main__':
113
    main()