Subversion Repositories SmartDukaan

Rev

Rev 3666 | Rev 3768 | Go to most recent revision | Details | Compare with Previous | 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
 
3666 vikas 29
ITEM_ID_KEY = 'itemId'
30
ITEM_DESC_KEY = 'itemDesc'
31
PRICE_KEY = 'price'
32
QUANTITY_KEY = 'quantity'
33
AMOUNT_KEY = 'amount'
34
 
3657 vikas 35
user_client = UserClient().get_client()
36
transaction_client = TransactionClient().get_client()
37
payment_client = PaymentClient().get_client()
38
Dataservice.initialize()
39
 
40
def create_orders(user, items):
41
    orders = []
42
    for item in items:
3666 vikas 43
        for i in range (0, int(item[QUANTITY_KEY])):
44
            line_item = create_line_item(int(item[ITEM_ID_KEY]), item[PRICE_KEY])
3657 vikas 45
            orders.append(create_order(user.userId, user.defaultAddressId, line_item))
46
    return orders
47
 
48
 
3666 vikas 49
def create_transaction(user, items):
3657 vikas 50
    txn = Transaction()
51
    txn.shoppingCartid = user.activeCartId
52
    txn.customer_id = user.userId
53
    txn.createdOn = to_java_date(datetime.datetime.now())
54
    txn.transactionStatus = TransactionStatus.INIT
55
    txn.statusDescription = "New Order"
3666 vikas 56
    txn.orders = create_orders(user, items)
3657 vikas 57
    transaction_client = TransactionClient().get_client()
58
    txn_id = transaction_client.createTransaction(txn)
59
    return txn_id
60
 
61
def create_payment(user, amount , txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc):
62
    payment_id = payment_client.createPayment(user.userId, amount, RTGS_GATEWAY_ID, txn_id)
63
    payment_client.getPayment(payment_id)
64
    attributes = []
3724 vikas 65
    attributes.append(Attribute(PAYMETHOD_ATTR, '4000'))
3657 vikas 66
    attributes.append(Attribute(RTGS_ID_ATTR, str(rtgs_id)))
67
    attributes.append(Attribute(RTGS_BANK_ATTR, rtgs_bank))
68
    attributes.append(Attribute(RTGS_BRANCH_ATTR, rtgs_branch))
69
    attributes.append(Attribute(IFSC_ATTR, str(ifsc)))
70
    payment_client.updatePaymentDetails(payment_id, None, None, None, None, None, None, None, None, PaymentStatus.SUCCESS, None, attributes)
71
 
72
def load_orders(file):
73
    workbook = xlrd.open_workbook(file)
74
    sheet = workbook.sheet_by_index(0)
75
    num_rows = sheet.nrows
76
 
77
    email, rtgs_id, rtgs_bank, rtgs_branch, ifsc, amount = sheet.col_values(1)[0:6]
78
 
79
    user = user_client.getUserByEmail(email)
80
    if user.userId == -1:
81
        print "The email is not registered with us : " + email
82
        return
83
    if user.defaultAddressId == 0:
84
        print "No default address for this user : " + email
85
        return
86
 
87
    items = []
88
    total_amount = 0
89
    for rownum in range(10, num_rows):
3666 vikas 90
        item = {}
91
        item[ITEM_ID_KEY], item[ITEM_DESC_KEY], item[PRICE_KEY], item[QUANTITY_KEY], item[AMOUNT_KEY] = sheet.row_values(rownum)[0:5]
92
        total_amount += item[AMOUNT_KEY]
93
        items.append(item)
3657 vikas 94
 
95
    if amount != total_amount:
96
        print "Amount paid is not equal to the total amount of bulk order."
97
        return
98
 
3666 vikas 99
    txn_id = create_transaction(user, items)
3657 vikas 100
    create_payment(user, amount, txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc)
101
    transaction_client.changeTransactionStatus(txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order");
3666 vikas 102
    transaction_client.changeTransactionStatus(txn_id, TransactionStatus.IN_PROCESS, "RTGS Payment accepted");
3657 vikas 103
 
104
def main():
105
    parser = optparse.OptionParser()
106
    parser.add_option("-f", "--file", dest="file",
107
                   help="Excel file with bulk orders.")
108
    (options, args) = parser.parse_args()
109
    if len(args) != 0:
110
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
111
    if options.file == None:
112
        parser.error("Excel File with bulk orders not supplied.")
113
 
114
    load_orders(options.file)
115
    #load_orders('/home/vikas/tmp/Bulk Orders.xls')
116
 
117
if __name__ == '__main__':
118
    main()