Subversion Repositories SmartDukaan

Rev

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