Rev 1939 | Rev 2439 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/pythonimport timeimport datetimeimport sysfrom elixir import sessionif __name__ == '__main__' and __package__ is None:import ossys.path.insert(0, os.getcwd())from shop2020.clients.InventoryClient import InventoryClientfrom shop2020.thriftpy.model.v1.order.ttypes import OrderStatus,\TransactionServiceExceptionfrom shop2020.model.v1.order.impl import DataServicefrom shop2020.model.v1.order.impl.DataAccessors import get_order,\order_outofstock, close_sessionVALUES_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'''order = get_order(order_id)if order.status != OrderStatus.SHIPPED_FROM_WH:print "Has this order been picked up?"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."returnraw_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")returnorder.status = OrderStatus.SHIPPED_TO_LOGSTorder.statusDescription = "Order picked up by Courier Company"order.pickup_timestamp = pickup_timestampsession.commit()def mark_order_as_delivered(order_id):'''Mark order as delivered'''order = get_order(order_id)if order.status != OrderStatus.SHIPPED_TO_LOGST:print "Has this order been picked up?"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."returnraw_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")returnreceiver = raw_input("Enter the name of receiver: ")if receiver is None or receiver == "":print("Receiver info is mandatory.")returnorder.status = OrderStatus.DELIVERY_SUCCESSorder.statusDescription = "Order delivered"order.delivery_timestamp = delivery_timestamporder.receiver = receiversession.commit()def mark_order_as_failed(order_id):'''Mark order as failed'''order = get_order(order_id)if order.status != OrderStatus.SHIPPED_TO_LOGST:print "Has this order been picked up?"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."returnraw_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")returnreason = raw_input("Enter the reason for failure: ")if reason is None or reason == "":print("Reason for failure is mandatory.")returnorder.status = OrderStatus.FAILEDorder.delivery_timestamp = delivery_timestamporder.statusDescription = "Order Returned to Origin:" + reasonsession.commit()def change_product(order_id):'''Ship a product of a different color to the customer.'''order = get_order(order_id)if order.status != OrderStatus.SUBMITTED_FOR_PROCESSING:print "This order has already been processed. Please seek help from engineering."returnraw_item_id = raw_input("Enter the ID of the item that you want to ship: ")try:item_id = int(raw_item_id)except ValueError:print("Invalid product Id")returncolor = raw_input("Enter the color of the new item: ")if color is None or color == "":print("Color information is mandatory.")return#warehouse_id = raw_input("Enter the warehouse id which will fulfill this order. Leave empty to keep it unchanged: ")#if warehouse_id is None or warehouse_id == "":# warehouse_id = order.warehouse_idlineitem = order.lineitems[0]catalog_client = InventoryClient().get_client()catalog_client.reserveItemInWarehouse(item_id, order.warehouse_id, lineitem.quantity)catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)#TODO: Check that the item has the same pricelineitem.item_id = item_idlineitem.color = colorsession.commit()def cancel(order_id):'''Cancel'''print("Your session has been closed")passACTIONS = {0: order_outofstock,1: mark_order_as_picked_up,2: mark_order_as_delivered,3: mark_order_as_failed,4: change_product,5: 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.")returntry: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("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("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.")returntry:action = int(raw_action)except ValueError:print("Invalid input.")returnif action > 4:print("Invalid input.")returnACTIONS[action](order_id)except TransactionServiceException as tsex:print tsex.messagefinally:close_session()if __name__ == '__main__':main()