Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1911 chandransh 1
#!/usr/bin/python
2
 
3
import time
4
import datetime
5
import sys
6
from elixir import session
7
 
8
if __name__ == '__main__' and __package__ is None:
9
    import os
10
    sys.path.insert(0, os.getcwd())
11
 
2091 chandransh 12
from shop2020.clients.InventoryClient import InventoryClient
1911 chandransh 13
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus,\
14
    TransactionServiceException
15
from shop2020.model.v1.order.impl import DataService
16
from shop2020.model.v1.order.impl.DataAccessors import get_order,\
17
    order_outofstock, close_session
18
 
19
VALUES_TO_NAMES = {
20
    0: "PAYMENT_PENDING",
21
    1: "PAYMENT_FAILED",
22
    2: "INIT",
23
    3: "SUBMITTED_FOR_PROCESSING",
24
    4: "ACCEPTED",
25
    5: "INVENTORY_LOW",
26
    6: "REJECTED",
27
    7: "BILLED",
28
    8: "READY_FOR_SHIPPING",
29
    9: "SHIPPED_FROM_WH",
30
    10: "SHIPPED_TO_LOGST",
31
    11: "IN_TRANSIT",
32
    12: "DELIVERY_SUCCESS",
33
    13: "DELIVERY_FAILED_FIRST_ATTEMPT",
34
    14: "DELIVERY_FAILED_SECOND_ATTEMPT",
35
    15: "DELIVERY_FAILED_THIRD_ATTEMPT",
36
    16: "DELIVERY_FAILED_WORNG_ADDRESS",
37
    17: "COMPLETED",
38
    18: "CANCELED",
39
    19: "FAILED",
40
  }
41
 
42
def mark_order_as_picked_up(order_id):
43
    '''
44
    Mark order as picked up
45
    '''
1939 chandransh 46
    order = get_order(order_id)
47
    if order.status != OrderStatus.SHIPPED_FROM_WH:
48
        print "Has this order been picked up?"
49
        print
50
        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."
51
        return
52
 
1911 chandransh 53
    raw_pickup_timestamp = raw_input("Enter pickup time in YYYY-MM-DD hhmm format: ")
54
    try:
55
        pickup_timestamp = get_py_datetime(raw_pickup_timestamp)
56
        print("Pick up timestamp: " + str(pickup_timestamp))
57
    except ValueError:
58
        print("Invalid pickup time")
59
        return
1939 chandransh 60
 
1911 chandransh 61
    order.status = OrderStatus.SHIPPED_TO_LOGST
62
    order.statusDescription = "Order picked up by Courier Company"
63
    order.pickup_timestamp = pickup_timestamp 
64
    session.commit()
65
 
66
def mark_order_as_delivered(order_id):
67
    '''
68
    Mark order as delivered
69
    '''
1939 chandransh 70
    order = get_order(order_id)
71
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
72
        print "Has this order been picked up?"
73
        print
74
        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."
75
        return
76
 
1911 chandransh 77
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
78
    try:
79
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
80
    except ValueError:
81
        print("Invalid delivery time stamp")
82
        return
83
 
84
    receiver = raw_input("Enter the name of receiver: ")
85
    if receiver is None or receiver == "":
86
        print("Receiver info is mandatory.")
87
        return
88
 
89
    order.status = OrderStatus.DELIVERY_SUCCESS
90
    order.statusDescription = "Order delivered"
91
    order.delivery_timestamp = delivery_timestamp
92
    order.receiver = receiver
93
    session.commit()
94
 
95
 
96
def mark_order_as_failed(order_id):
97
    '''
98
    Mark order as failed
99
    '''
1939 chandransh 100
    order = get_order(order_id)
101
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
102
        print "Has this order been picked up?"
103
        print
104
        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."
105
        return
106
 
1911 chandransh 107
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
108
    try:
109
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
110
    except ValueError:
111
        print("Invalid delivery timestamp")
112
        return
113
 
114
    reason = raw_input("Enter the reason for failure: ")
115
    if reason is None or reason == "":
116
        print("Reason for failure is mandatory.")
117
        return
118
 
119
    order.status = OrderStatus.FAILED
120
    order.delivery_timestamp = delivery_timestamp 
121
    order.statusDescription = "Order Returned to Origin:" + reason
122
    session.commit()
123
 
1939 chandransh 124
def change_product(order_id):
125
    '''
126
    Ship a product of a different color to the customer.
127
    '''
128
    order = get_order(order_id)
129
    if order.status != OrderStatus.SUBMITTED_FOR_PROCESSING:
130
        print "This order has already been processed. Please seek help from engineering."
131
        return
132
 
133
    raw_item_id = raw_input("Enter the ID of the item that you want to ship: ")
134
    try:
135
        item_id = int(raw_item_id)
136
    except ValueError:
137
        print("Invalid product Id")
138
        return
139
 
140
    color = raw_input("Enter the color of the new item: ")
141
    if color is None or color == "":
142
        print("Color information is mandatory.")
143
        return
2091 chandransh 144
 
145
    #warehouse_id = raw_input("Enter the warehouse id which will fulfill this order. Leave empty to keep it unchanged: ")
146
    #if warehouse_id is None or warehouse_id == "":
147
    #    warehouse_id = order.warehouse_id
148
 
149
    lineitem = order.lineitems[0]
150
 
151
    catalog_client = InventoryClient().get_client()
152
    catalog_client.reserveItemInWarehouse(item_id, order.warehouse_id, lineitem.quantity)
153
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
1939 chandransh 154
    #TODO: Check that the item has the same price
2091 chandransh 155
 
1939 chandransh 156
    lineitem.item_id = item_id
157
    lineitem.color = color
158
 
159
    session.commit()
160
 
1911 chandransh 161
def cancel(order_id):
162
    '''
163
    Cancel
164
    '''
1939 chandransh 165
    print("Your session has been closed")
1911 chandransh 166
    pass
167
 
168
ACTIONS = {0: order_outofstock,
169
           1: mark_order_as_picked_up,
170
           2: mark_order_as_delivered,
171
           3: mark_order_as_failed,
1939 chandransh 172
           4: change_product,
173
           5: cancel
1911 chandransh 174
           }
175
 
176
def get_py_datetime(time_string):
177
    time_format = "%Y-%m-%d %H%M"
178
    mytime = time.strptime(time_string, time_format)
179
    return datetime.datetime(*mytime[:6])
180
 
181
def main():
182
    DataService.initialize(echoOn=False)
183
    raw_order_id = raw_input("Enter Order Id which you want to modify:")
184
    try:
185
        order_id = int(raw_order_id)
186
        print("You want to modify: " + str(order_id))
187
    except ValueError:
188
        print("Invalid Order Id.")
189
        return
190
 
191
    try:
192
        order = get_order(order_id)
193
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
194
        print("Order Id:\t\t" + str(order.id))
195
        print("Customer Name:\t\t" + order.customer_name)
196
        print("Address Line 1:\t\t" + order.customer_address1)
197
        print("Address Line 2:\t\t" + order.customer_address2)
198
        print("City:\t\t\t" + order.customer_city)
199
        print("State:\t\t\t" + order.customer_state)
200
        print("Pincode:\t\t" + order.customer_pincode)
201
        print("Amount:\t\t\t" + str(order.total_amount))
202
        print("Created On:\t\t" + str(order.created_timestamp))
203
        #print("Billed On:\t" + str(order.billing_timestamp))
204
        #print("Shipped On:\t" + str(order.shipping_timestamp))
205
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
206
        print("Status Description:\t" + order.statusDescription)
1939 chandransh 207
        print("Ordered Items description:")
208
        for lineitem in order.lineitems:
209
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
1911 chandransh 210
        print
211
        print
212
        print("You can perform following operations:")
213
        for (key, val) in ACTIONS.iteritems():
214
            print("[" + str(key) + "]" + val.__doc__ )
215
 
216
        raw_action = raw_input("What do you want to do? ")
1939 chandransh 217
        if raw_action is None or raw_action == "":
1911 chandransh 218
            print("Your session has been closed.")
219
            return
220
        try:
221
            action = int(raw_action)
222
        except ValueError:
223
            print("Invalid input.")
224
            return
225
        if action > 4:
226
            print("Invalid input.")
227
            return
228
        ACTIONS[action](order_id)
229
    except TransactionServiceException as tsex:
230
        print tsex.message
231
    finally:
232
        close_session()
233
 
234
if __name__ == '__main__':
235
    main()