Subversion Repositories SmartDukaan

Rev

Rev 3133 | Rev 4016 | 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
 
3133 rajveer 12
from shop2020.clients.CatalogClient import CatalogClient
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
 
2432 chandransh 42
def mark_order_as_picked_up(order):
1911 chandransh 43
    '''
44
    Mark order as picked up
45
    '''
1939 chandransh 46
    if order.status != OrderStatus.SHIPPED_FROM_WH:
47
        print "Has this order been picked up?"
48
        print
49
        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."
50
        return
51
 
1911 chandransh 52
    raw_pickup_timestamp = raw_input("Enter pickup time in YYYY-MM-DD hhmm format: ")
53
    try:
54
        pickup_timestamp = get_py_datetime(raw_pickup_timestamp)
55
        print("Pick up timestamp: " + str(pickup_timestamp))
56
    except ValueError:
57
        print("Invalid pickup time")
58
        return
1939 chandransh 59
 
1911 chandransh 60
    order.status = OrderStatus.SHIPPED_TO_LOGST
61
    order.statusDescription = "Order picked up by Courier Company"
62
    order.pickup_timestamp = pickup_timestamp 
63
    session.commit()
64
 
2432 chandransh 65
def mark_order_as_delivered(order):
1911 chandransh 66
    '''
67
    Mark order as delivered
68
    '''
1939 chandransh 69
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
70
        print "Has this order been picked up?"
71
        print
72
        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."
73
        return
74
 
1911 chandransh 75
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
76
    try:
77
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
78
    except ValueError:
79
        print("Invalid delivery time stamp")
80
        return
81
 
82
    receiver = raw_input("Enter the name of receiver: ")
83
    if receiver is None or receiver == "":
84
        print("Receiver info is mandatory.")
85
        return
86
 
87
    order.status = OrderStatus.DELIVERY_SUCCESS
88
    order.statusDescription = "Order delivered"
89
    order.delivery_timestamp = delivery_timestamp
90
    order.receiver = receiver
91
    session.commit()
92
 
93
 
2432 chandransh 94
def mark_order_as_failed(order):
1911 chandransh 95
    '''
96
    Mark order as failed
97
    '''
1939 chandransh 98
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
99
        print "Has this order been picked up?"
100
        print
101
        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."
102
        return
103
 
1911 chandransh 104
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
105
    try:
106
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
107
    except ValueError:
108
        print("Invalid delivery timestamp")
109
        return
110
 
111
    reason = raw_input("Enter the reason for failure: ")
112
    if reason is None or reason == "":
113
        print("Reason for failure is mandatory.")
114
        return
115
 
116
    order.status = OrderStatus.FAILED
117
    order.delivery_timestamp = delivery_timestamp 
118
    order.statusDescription = "Order Returned to Origin:" + reason
119
    session.commit()
120
 
2432 chandransh 121
def change_product(order):
1939 chandransh 122
    '''
123
    Ship a product of a different color to the customer.
124
    '''
3260 chandransh 125
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
1939 chandransh 126
        print "This order has already been processed. Please seek help from engineering."
127
        return
128
 
129
    raw_item_id = raw_input("Enter the ID of the item that you want to ship: ")
130
    try:
131
        item_id = int(raw_item_id)
132
    except ValueError:
133
        print("Invalid product Id")
134
        return
135
 
136
    color = raw_input("Enter the color of the new item: ")
137
    if color is None or color == "":
138
        print("Color information is mandatory.")
139
        return
2091 chandransh 140
 
2432 chandransh 141
    #warehouse_id = raw_input("Enter the warehouse id which will fulfil this order. Leave empty to keep it unchanged: ")
2091 chandransh 142
    #if warehouse_id is None or warehouse_id == "":
143
    #    warehouse_id = order.warehouse_id
144
 
145
    lineitem = order.lineitems[0]
146
 
3133 rajveer 147
    catalog_client = CatalogClient().get_client()
2091 chandransh 148
    catalog_client.reserveItemInWarehouse(item_id, order.warehouse_id, lineitem.quantity)
149
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
2432 chandransh 150
    #TODO: Check that this new item has the same price
2091 chandransh 151
 
1939 chandransh 152
    lineitem.item_id = item_id
153
    lineitem.color = color
154
 
155
    session.commit()
156
 
2432 chandransh 157
def change_warehouse(order):
158
    '''
159
    Update the warehouse which will be used to fulfil this order.
160
    '''
3260 chandransh 161
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
2432 chandransh 162
        print "This order has already been processed. Please seek help from engineering."
163
        return
164
 
165
    print("Current Warehouse: " + str(order.warehouse_id))
166
 
167
    raw_warehouse_id = raw_input("Enter the ID of the warehouse from where you want to ship: ")
168
    try:
169
        warehouse_id = int(raw_warehouse_id)
170
    except ValueError:
171
        print("Invalid warehouse id")
172
        return
173
 
174
    if warehouse_id == order.warehouse_id:
175
        print("You have selected the current warehouse again. Nothing to do")
176
        return
177
 
178
    lineitem = order.lineitems[0]
3133 rajveer 179
    catalog_client = CatalogClient().get_client()
2432 chandransh 180
    catalog_client.reserveItemInWarehouse(lineitem.item_id, warehouse_id, lineitem.quantity)
181
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
182
 
183
    order.warehouse_id = warehouse_id
184
    session.commit()
185
 
186
def update_weight(order):
187
    '''
188
    Update the weight of order
189
    '''
190
    raw_weight = raw_input("Enter the final weight to be set: ")
191
    try:
192
        weight = float(raw_weight)
193
    except ValueError:
194
        print("Invalid weight")
195
        return
196
 
197
    order.total_weight = weight
198
    lineitem = order.lineitems[0]
199
    lineitem.total_weight = weight
200
    lineitem.unit_weight = weight
201
    #TODO: Update the weight of item. Problem is that even then, this update will only go to Production and not to Staging. Next content update will wipe out this data.
202
    session.commit()
203
 
2439 chandransh 204
def cancel(order):
1911 chandransh 205
    '''
206
    Cancel
207
    '''
1939 chandransh 208
    print("Your session has been closed")
2439 chandransh 209
    return
1911 chandransh 210
 
2432 chandransh 211
ACTIONS = {0: cancel,
212
           1: order_outofstock,
213
           2: mark_order_as_picked_up,
214
           3: mark_order_as_delivered,
215
           4: mark_order_as_failed,
216
           5: change_product,
217
           6: change_warehouse,
218
           7: update_weight
1911 chandransh 219
           }
220
 
221
def get_py_datetime(time_string):
222
    time_format = "%Y-%m-%d %H%M"
223
    mytime = time.strptime(time_string, time_format)
224
    return datetime.datetime(*mytime[:6])
225
 
226
def main():
227
    DataService.initialize(echoOn=False)
228
    raw_order_id = raw_input("Enter Order Id which you want to modify:")
229
    try:
230
        order_id = int(raw_order_id)
231
        print("You want to modify: " + str(order_id))
232
    except ValueError:
233
        print("Invalid Order Id.")
234
        return
235
 
236
    try:
237
        order = get_order(order_id)
238
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
239
        print("Order Id:\t\t" + str(order.id))
240
        print("Customer Name:\t\t" + order.customer_name)
241
        print("Address Line 1:\t\t" + order.customer_address1)
242
        print("Address Line 2:\t\t" + order.customer_address2)
243
        print("City:\t\t\t" + order.customer_city)
244
        print("State:\t\t\t" + order.customer_state)
245
        print("Pincode:\t\t" + order.customer_pincode)
246
        print("Amount:\t\t\t" + str(order.total_amount))
247
        print("Created On:\t\t" + str(order.created_timestamp))
248
        #print("Billed On:\t" + str(order.billing_timestamp))
249
        #print("Shipped On:\t" + str(order.shipping_timestamp))
250
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
251
        print("Status Description:\t" + order.statusDescription)
1939 chandransh 252
        print("Ordered Items description:")
253
        for lineitem in order.lineitems:
254
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
1911 chandransh 255
        print
256
        print
257
        print("You can perform following operations:")
258
        for (key, val) in ACTIONS.iteritems():
259
            print("[" + str(key) + "]" + val.__doc__ )
260
 
261
        raw_action = raw_input("What do you want to do? ")
1939 chandransh 262
        if raw_action is None or raw_action == "":
1911 chandransh 263
            print("Your session has been closed.")
264
            return
265
        try:
266
            action = int(raw_action)
267
        except ValueError:
268
            print("Invalid input.")
269
            return
2439 chandransh 270
        if action > max(ACTIONS.keys()):
1911 chandransh 271
            print("Invalid input.")
272
            return
2432 chandransh 273
        ACTIONS[action](order)
1911 chandransh 274
    except TransactionServiceException as tsex:
275
        print tsex.message
276
    finally:
277
        close_session()
278
 
279
if __name__ == '__main__':
280
    main()