Subversion Repositories SmartDukaan

Rev

Rev 1939 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

import time
import datetime
import sys
from elixir import session

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,\
    order_outofstock, close_session

VALUES_TO_NAMES = {
    0: "PAYMENT_PENDING",
    1: "PAYMENT_FAILED",
    2: "INIT",
    3: "SUBMITTED_FOR_PROCESSING",
    4: "ACCEPTED",
    5: "INVENTORY_LOW",
    6: "REJECTED",
    7: "BILLED",
    8: "READY_FOR_SHIPPING",
    9: "SHIPPED_FROM_WH",
    10: "SHIPPED_TO_LOGST",
    11: "IN_TRANSIT",
    12: "DELIVERY_SUCCESS",
    13: "DELIVERY_FAILED_FIRST_ATTEMPT",
    14: "DELIVERY_FAILED_SECOND_ATTEMPT",
    15: "DELIVERY_FAILED_THIRD_ATTEMPT",
    16: "DELIVERY_FAILED_WORNG_ADDRESS",
    17: "COMPLETED",
    18: "CANCELED",
    19: "FAILED",
  }

def mark_order_as_picked_up(order_id):
    '''
    Mark order as picked up
    '''
    raw_pickup_timestamp = raw_input("Enter pickup time in YYYY-MM-DD hhmm format: ")
    try:
        pickup_timestamp = get_py_datetime(raw_pickup_timestamp)
        print("Pick up timestamp: " + str(pickup_timestamp))
    except ValueError:
        print("Invalid pickup time")
        return

    order = get_order(order_id)
    if order.status != OrderStatus.SHIPPED_FROM_WH:
        print "Has this order been picked up?"
        print
        print "In case it has been and the same was not updated in the database, please first mark this order as picked up and then try again."
        return
    
    order.status = OrderStatus.SHIPPED_TO_LOGST
    order.statusDescription = "Order picked up by Courier Company"
    order.pickup_timestamp = pickup_timestamp 
    session.commit()

def mark_order_as_delivered(order_id):
    '''
    Mark order as delivered
    '''
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
    try:
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
    except ValueError:
        print("Invalid delivery time stamp")
        return
    
    receiver = raw_input("Enter the name of receiver: ")
    if receiver is None or receiver == "":
        print("Receiver info is mandatory.")
        return
    
    order = get_order(order_id)
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
        print "Has this order been picked up?"
        print
        print "In case it has been and the same was not updated in the database, please first mark this order as picked up and then try again."
        return
    
    order.status = OrderStatus.DELIVERY_SUCCESS
    order.statusDescription = "Order delivered"
    order.delivery_timestamp = delivery_timestamp
    order.receiver = receiver
    session.commit()


def mark_order_as_failed(order_id):
    '''
    Mark order as failed
    '''
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
    try:
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
    except ValueError:
        print("Invalid delivery timestamp")
        return
    
    reason = raw_input("Enter the reason for failure: ")
    if reason is None or reason == "":
        print("Reason for failure is mandatory.")
        return
    
    order = get_order(order_id)
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
        print "Has this order been picked up?"
        print
        print "In case it has been and the same was not updated in the database, please first mark this order as picked up and then try again."
        return
    
    order.status = OrderStatus.FAILED
    order.delivery_timestamp = delivery_timestamp 
    order.statusDescription = "Order Returned to Origin:" + reason
    session.commit()

def cancel(order_id):
    '''
    Cancel
    '''
    pass

ACTIONS = {0: order_outofstock,
           1: mark_order_as_picked_up,
           2: mark_order_as_delivered,
           3: mark_order_as_failed,
           4: cancel
           }

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():
    DataService.initialize(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("Address Line 1:\t\t" + order.customer_address1)
        print("Address Line 2:\t\t" + order.customer_address2)
        print("City:\t\t\t" + order.customer_city)
        print("State:\t\t\t" + order.customer_state)
        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("Billed On:\t" + str(order.billing_timestamp))
        #print("Shipped On:\t" + str(order.shipping_timestamp))
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
        print("Status Description:\t" + order.statusDescription)
        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 == "" or raw_action == "4":
            print("Your session has been closed.")
            return
        try:
            action = int(raw_action)
        except ValueError:
            print("Invalid input.")
            return
        if action > 4:
            print("Invalid input.")
            return
        ACTIONS[action](order_id)
    except TransactionServiceException as tsex:
        print tsex.message
    finally:
        close_session()
    
if __name__ == '__main__':
    main()