Subversion Repositories SmartDukaan

Rev

Rev 3260 | 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
 
4016 chandransh 3
import optparse
1911 chandransh 4
import time
5
import datetime
6
import sys
7
from elixir import session
8
 
9
if __name__ == '__main__' and __package__ is None:
10
    import os
11
    sys.path.insert(0, os.getcwd())
12
 
3133 rajveer 13
from shop2020.clients.CatalogClient import CatalogClient
1911 chandransh 14
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus,\
15
    TransactionServiceException
16
from shop2020.model.v1.order.impl import DataService
17
from shop2020.model.v1.order.impl.DataAccessors import get_order,\
18
    order_outofstock, close_session
19
 
20
VALUES_TO_NAMES = {
21
    0: "PAYMENT_PENDING",
22
    1: "PAYMENT_FAILED",
23
    2: "INIT",
24
    3: "SUBMITTED_FOR_PROCESSING",
25
    4: "ACCEPTED",
26
    5: "INVENTORY_LOW",
27
    6: "REJECTED",
28
    7: "BILLED",
29
    8: "READY_FOR_SHIPPING",
30
    9: "SHIPPED_FROM_WH",
31
    10: "SHIPPED_TO_LOGST",
32
    11: "IN_TRANSIT",
33
    12: "DELIVERY_SUCCESS",
34
    13: "DELIVERY_FAILED_FIRST_ATTEMPT",
35
    14: "DELIVERY_FAILED_SECOND_ATTEMPT",
36
    15: "DELIVERY_FAILED_THIRD_ATTEMPT",
37
    16: "DELIVERY_FAILED_WORNG_ADDRESS",
38
    17: "COMPLETED",
39
    18: "CANCELED",
40
    19: "FAILED",
41
  }
42
 
2432 chandransh 43
def mark_order_as_picked_up(order):
1911 chandransh 44
    '''
45
    Mark order as picked up
46
    '''
1939 chandransh 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
 
2432 chandransh 66
def mark_order_as_delivered(order):
1911 chandransh 67
    '''
68
    Mark order as delivered
69
    '''
1939 chandransh 70
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
71
        print "Has this order been picked up?"
72
        print
73
        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."
74
        return
75
 
1911 chandransh 76
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
77
    try:
78
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
79
    except ValueError:
80
        print("Invalid delivery time stamp")
81
        return
82
 
83
    receiver = raw_input("Enter the name of receiver: ")
84
    if receiver is None or receiver == "":
85
        print("Receiver info is mandatory.")
86
        return
87
 
88
    order.status = OrderStatus.DELIVERY_SUCCESS
89
    order.statusDescription = "Order delivered"
90
    order.delivery_timestamp = delivery_timestamp
91
    order.receiver = receiver
92
    session.commit()
93
 
94
 
2432 chandransh 95
def mark_order_as_failed(order):
1911 chandransh 96
    '''
97
    Mark order as failed
98
    '''
1939 chandransh 99
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
100
        print "Has this order been picked up?"
101
        print
102
        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."
103
        return
104
 
1911 chandransh 105
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
106
    try:
107
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
108
    except ValueError:
109
        print("Invalid delivery timestamp")
110
        return
111
 
112
    reason = raw_input("Enter the reason for failure: ")
113
    if reason is None or reason == "":
114
        print("Reason for failure is mandatory.")
115
        return
116
 
117
    order.status = OrderStatus.FAILED
118
    order.delivery_timestamp = delivery_timestamp 
119
    order.statusDescription = "Order Returned to Origin:" + reason
120
    session.commit()
121
 
2432 chandransh 122
def change_product(order):
1939 chandransh 123
    '''
124
    Ship a product of a different color to the customer.
125
    '''
3260 chandransh 126
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
1939 chandransh 127
        print "This order has already been processed. Please seek help from engineering."
128
        return
129
 
130
    raw_item_id = raw_input("Enter the ID of the item that you want to ship: ")
131
    try:
132
        item_id = int(raw_item_id)
133
    except ValueError:
134
        print("Invalid product Id")
135
        return
136
 
137
    color = raw_input("Enter the color of the new item: ")
138
    if color is None or color == "":
139
        print("Color information is mandatory.")
140
        return
2091 chandransh 141
 
2432 chandransh 142
    #warehouse_id = raw_input("Enter the warehouse id which will fulfil this order. Leave empty to keep it unchanged: ")
2091 chandransh 143
    #if warehouse_id is None or warehouse_id == "":
144
    #    warehouse_id = order.warehouse_id
145
 
146
    lineitem = order.lineitems[0]
147
 
3133 rajveer 148
    catalog_client = CatalogClient().get_client()
2091 chandransh 149
    catalog_client.reserveItemInWarehouse(item_id, order.warehouse_id, lineitem.quantity)
150
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
2432 chandransh 151
    #TODO: Check that this new item has the same price
2091 chandransh 152
 
1939 chandransh 153
    lineitem.item_id = item_id
154
    lineitem.color = color
155
 
156
    session.commit()
157
 
2432 chandransh 158
def change_warehouse(order):
159
    '''
160
    Update the warehouse which will be used to fulfil this order.
161
    '''
3260 chandransh 162
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
2432 chandransh 163
        print "This order has already been processed. Please seek help from engineering."
164
        return
165
 
166
    print("Current Warehouse: " + str(order.warehouse_id))
167
 
168
    raw_warehouse_id = raw_input("Enter the ID of the warehouse from where you want to ship: ")
169
    try:
170
        warehouse_id = int(raw_warehouse_id)
171
    except ValueError:
172
        print("Invalid warehouse id")
173
        return
174
 
175
    if warehouse_id == order.warehouse_id:
176
        print("You have selected the current warehouse again. Nothing to do")
177
        return
178
 
179
    lineitem = order.lineitems[0]
3133 rajveer 180
    catalog_client = CatalogClient().get_client()
2432 chandransh 181
    catalog_client.reserveItemInWarehouse(lineitem.item_id, warehouse_id, lineitem.quantity)
182
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
183
 
184
    order.warehouse_id = warehouse_id
185
    session.commit()
186
 
187
def update_weight(order):
188
    '''
189
    Update the weight of order
190
    '''
191
    raw_weight = raw_input("Enter the final weight to be set: ")
192
    try:
193
        weight = float(raw_weight)
194
    except ValueError:
195
        print("Invalid weight")
196
        return
197
 
198
    order.total_weight = weight
199
    lineitem = order.lineitems[0]
200
    lineitem.total_weight = weight
201
    lineitem.unit_weight = weight
202
    #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.
203
    session.commit()
204
 
2439 chandransh 205
def cancel(order):
1911 chandransh 206
    '''
207
    Cancel
208
    '''
1939 chandransh 209
    print("Your session has been closed")
2439 chandransh 210
    return
1911 chandransh 211
 
2432 chandransh 212
ACTIONS = {0: cancel,
213
           1: order_outofstock,
214
           2: mark_order_as_picked_up,
215
           3: mark_order_as_delivered,
216
           4: mark_order_as_failed,
217
           5: change_product,
218
           6: change_warehouse,
219
           7: update_weight
1911 chandransh 220
           }
221
 
222
def get_py_datetime(time_string):
223
    time_format = "%Y-%m-%d %H%M"
224
    mytime = time.strptime(time_string, time_format)
225
    return datetime.datetime(*mytime[:6])
226
 
227
def main():
4016 chandransh 228
    parser = optparse.OptionParser()
229
    parser.add_option("-H", "--host", dest="hostname",
230
                      default="localhost",
231
                      type="string", help="The HOST where the DB server is running",
232
                      metavar="HOST")
233
    (options, args) = parser.parse_args()
234
    if len(args) != 0:
235
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
236
    DataService.initialize(db_hostname=options.hostname, echoOn=False)
1911 chandransh 237
    raw_order_id = raw_input("Enter Order Id which you want to modify:")
238
    try:
239
        order_id = int(raw_order_id)
240
        print("You want to modify: " + str(order_id))
241
    except ValueError:
242
        print("Invalid Order Id.")
243
        return
244
 
245
    try:
246
        order = get_order(order_id)
247
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
248
        print("Order Id:\t\t" + str(order.id))
249
        print("Customer Name:\t\t" + order.customer_name)
250
        print("Address Line 1:\t\t" + order.customer_address1)
251
        print("Address Line 2:\t\t" + order.customer_address2)
252
        print("City:\t\t\t" + order.customer_city)
253
        print("State:\t\t\t" + order.customer_state)
254
        print("Pincode:\t\t" + order.customer_pincode)
255
        print("Amount:\t\t\t" + str(order.total_amount))
256
        print("Created On:\t\t" + str(order.created_timestamp))
257
        #print("Billed On:\t" + str(order.billing_timestamp))
258
        #print("Shipped On:\t" + str(order.shipping_timestamp))
259
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
260
        print("Status Description:\t" + order.statusDescription)
1939 chandransh 261
        print("Ordered Items description:")
262
        for lineitem in order.lineitems:
263
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
1911 chandransh 264
        print
265
        print
266
        print("You can perform following operations:")
267
        for (key, val) in ACTIONS.iteritems():
268
            print("[" + str(key) + "]" + val.__doc__ )
269
 
270
        raw_action = raw_input("What do you want to do? ")
1939 chandransh 271
        if raw_action is None or raw_action == "":
1911 chandransh 272
            print("Your session has been closed.")
273
            return
274
        try:
275
            action = int(raw_action)
276
        except ValueError:
277
            print("Invalid input.")
278
            return
2439 chandransh 279
        if action > max(ACTIONS.keys()):
1911 chandransh 280
            print("Invalid input.")
281
            return
2432 chandransh 282
        ACTIONS[action](order)
1911 chandransh 283
    except TransactionServiceException as tsex:
284
        print tsex.message
285
    finally:
286
        close_session()
287
 
288
if __name__ == '__main__':
289
    main()