Rev 7293 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/python'''Created on 12-Oct-2011@author: Vikas'''import optparseimport xlrdimport datetimefrom shop2020.model.v1.user.impl.UserDataAccessors import add_address_for_userfrom shop2020.thriftpy.model.v1.user.ttypes import Addressif __name__ == '__main__' and __package__ is None:import sysimport ossys.path.insert(0, os.getcwd())from shop2020.clients.UserClient import UserClientfrom shop2020.model.v1.user.impl.CartDataAccessors import create_line_item, create_orderfrom shop2020.model.v1.user.impl import Dataservicefrom shop2020.clients.TransactionClient import TransactionClientfrom shop2020.clients.PaymentClient import PaymentClientfrom shop2020.thriftpy.model.v1.order.ttypes import Transaction, TransactionStatus, OrderSourcefrom shop2020.thriftpy.payments.ttypes import Attribute, PaymentStatusfrom shop2020.utils.Utils import to_java_dateRTGS_GATEWAY_ID = 6PAYMETHOD_ATTR = 'payMethod'RTGS_ID_ATTR = 'rtgsId'RTGS_BANK_ATTR = 'rtgsBank'RTGS_BRANCH_ATTR = 'rtgsBranch'IFSC_ATTR = 'ifscCode'ITEM_ID_KEY = 'itemId'ITEM_DESC_KEY = 'itemDesc'PRICE_KEY = 'price'QUANTITY_KEY = 'quantity'AMOUNT_KEY = 'amount'ADDRESS_KEY = 'address'user_client = UserClient().get_client()transaction_client = TransactionClient().get_client()payment_client = PaymentClient().get_client()def create_orders(user, items):orders = []for item in items:line_item = create_line_item(int(item[ITEM_ID_KEY]), item[PRICE_KEY], item[QUANTITY_KEY])addressId = add_address_for_user(item[ADDRESS_KEY], user.userId, False)orders.append(create_order(user.userId, addressId, line_item, 0, 0, 0, 0, None))return ordersdef create_transaction(user, items):txn = Transaction()txn.shoppingCartid = user.activeCartIdtxn.customer_id = user.userIdtxn.createdOn = to_java_date(datetime.datetime.now())txn.transactionStatus = TransactionStatus.INITtxn.statusDescription = "New Order"txn.orders = create_orders(user, items)transaction_client = TransactionClient().get_client()txn_id = transaction_client.createTransaction(txn)return txn_iddef create_payment(user, amount , txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc):payment_id = payment_client.createPayment(user.userId, amount, RTGS_GATEWAY_ID, txn_id, False)payment_client.getPayment(payment_id)attributes = []attributes.append(Attribute(PAYMETHOD_ATTR, '4000'))attributes.append(Attribute(RTGS_ID_ATTR, str(rtgs_id)))attributes.append(Attribute(RTGS_BANK_ATTR, rtgs_bank))attributes.append(Attribute(RTGS_BRANCH_ATTR, rtgs_branch))attributes.append(Attribute(IFSC_ATTR, str(ifsc)))payment_client.updatePaymentDetails(payment_id, None, None, None, None, None, None, None, None, PaymentStatus.SUCCESS, None, attributes)def load_orders(file):workbook = xlrd.open_workbook(file)sheet = workbook.sheet_by_index(0)num_rows = sheet.nrowsemail, rtgs_id, rtgs_bank, rtgs_branch, ifsc, amount = sheet.col_values(1)[0:6]user = user_client.getUserByEmail(email)if user.userId == -1:print "The email is not registered with us : " + emailreturnitems = []total_amount = 0for rownum in range(10, num_rows):item = {}item[ITEM_ID_KEY], item[ITEM_DESC_KEY], item[PRICE_KEY], item[QUANTITY_KEY], item[AMOUNT_KEY] = sheet.row_values(rownum)[0:5]name, line1, city, state, pin, phone = sheet.row_values(rownum)[5:11]total_amount += item[AMOUNT_KEY]items.append(item)address = Address()address.name = nameaddress.line1 = line1address.city = cityaddress.state = stateaddress.pin = str(int(pin))address.phone = str(int(phone))item[ADDRESS_KEY] = addressif amount != total_amount:print "Amount paid is not equal to the total amount of bulk order."returntxn_id = create_transaction(user, items)create_payment(user, amount, txn_id, rtgs_id, rtgs_bank, rtgs_branch, ifsc)transaction_client.changeTransactionStatus(txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order", 0, 0, OrderSource.WEBSITE);transaction_client.changeTransactionStatus(txn_id, TransactionStatus.IN_PROCESS, "RTGS Payment accepted", 0, 0, OrderSource.WEBSITE);def main():parser = optparse.OptionParser()parser.add_option("-f", "--file", dest="file",help="Excel file with bulk orders.")parser.add_option("-d", "--db_server", dest="host",default="localhost",help="Database server.")(options, args) = parser.parse_args()if len(args) != 0:parser.error("You've supplied extra arguments. Are you sure you want to run this program?")if options.file == None:parser.error("Excel File with bulk orders not supplied.")Dataservice.initialize(db_hostname=options.host)load_orders(options.file)#load_orders('/home/vikas/RTGS.xls')if __name__ == '__main__':main()