Subversion Repositories SmartDukaan

Rev

Rev 4016 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

import optparse
import time
import datetime
import sys
from elixir import session
from shop2020.clients.LogisticsClient import LogisticsClient

if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus, TransactionServiceException
from shop2020.model.v1.order.impl import DataService
from shop2020.model.v1.order.impl.DataAccessors import get_order, close_session


def change_logistics_provider(order):
    '''
    Ship the order through different vendor.
    '''
    if order.status not in [OrderStatus.COD_VERIFICATION_PENDING, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.ACCEPTED]:
        print "This order has already been processed. Please seek help from engineering."
        return
    
    raw_provider_id = raw_input("Enter the ID of the provider that you want to ship through: ")
    try:
        provider_id = int(raw_provider_id)
    except ValueError:
        print("Invalid provider Id")
        return
    
    if order.logistics_provider_id == provider_id:
        print("Provider Id entered by you is same as provider assigned to order.")
        return
    
    logistics_client = LogisticsClient().get_client()
    awb_number = logistics_client.getEmptyAWB(provider_id, order.cod)
    order.logistics_provider_id = provider_id
    order.airwaybill_no = awb_number
    order.track_id = awb_number
    session.commit()

    print("Successfully updated the provider id")

def cancel(order):
    '''
    Cancel
    '''
    print("Your session has been closed")
    return

ACTIONS = {0: cancel,
           1: change_logistics_provider
           }

def get_py_datetime(time_string):
    time_format = "%Y-%m-%d %H%M"
    mytime = time.strptime(time_string, time_format)
    return datetime.datetime(*mytime[:6])

def main():
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    (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?")
    DataService.initialize(db_hostname=options.hostname, echoOn=False)
    raw_order_id = raw_input("Enter Order Id which you want to modify:")
    try:
        order_id = int(raw_order_id)
        print("You want to modify: " + str(order_id))
    except ValueError:
        print("Invalid Order Id.")
        return
    
    try:
        order = get_order(order_id)
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
        print("Order Id:\t\t" + str(order.id))
        print("Customer Name:\t\t" + order.customer_name)
        print("Pincode:\t\t" + order.customer_pincode)
        print("Amount:\t\t\t" + str(order.total_amount))
        print("Created On:\t\t" + str(order.created_timestamp))
        print("Current Status:\t\t" + str(order.status))
        print("Status Description:\t\t" + order.statusDescription)
        print("Logistics provider id:\t\t" + str(order.logistics_provider_id))
        print("Airway bill number:\t\t" + order.airwaybill_no)
        print("Ordered Items description:")
        for lineitem in order.lineitems:
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
        print
        print
        print("You can perform following operations:")
        for (key, val) in ACTIONS.iteritems():
            print("[" + str(key) + "]" + val.__doc__ )

        raw_action = raw_input("What do you want to do? ")
        if raw_action is None or raw_action == "":
            print("Your session has been closed.")
            return
        try:
            action = int(raw_action)
        except ValueError:
            print("Invalid input.")
            return
        if action > max(ACTIONS.keys()):
            print("Invalid input.")
            return
        ACTIONS[action](order)
    except TransactionServiceException as tsex:
        print tsex.message
    finally:
        close_session()
    
if __name__ == '__main__':
    main()