Subversion Repositories SmartDukaan

Rev

Rev 4016 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4016 Rev 5246
Line 3... Line 3...
3
import optparse
3
import optparse
4
import time
4
import time
5
import datetime
5
import datetime
6
import sys
6
import sys
7
from elixir import session
7
from elixir import session
-
 
8
from shop2020.clients.LogisticsClient import LogisticsClient
8
 
9
 
9
if __name__ == '__main__' and __package__ is None:
10
if __name__ == '__main__' and __package__ is None:
10
    import os
11
    import os
11
    sys.path.insert(0, os.getcwd())
12
    sys.path.insert(0, os.getcwd())
12
 
13
 
13
from shop2020.clients.CatalogClient import CatalogClient
-
 
14
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus,\
14
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus, TransactionServiceException
15
    TransactionServiceException
-
 
16
from shop2020.model.v1.order.impl import DataService
15
from shop2020.model.v1.order.impl import DataService
17
from shop2020.model.v1.order.impl.DataAccessors import get_order,\
16
from shop2020.model.v1.order.impl.DataAccessors import get_order, close_session
18
    order_outofstock, close_session
-
 
19
 
17
 
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
 
18
 
43
def mark_order_as_picked_up(order):
19
def change_logistics_provider(order):
44
    '''
20
    '''
45
    Mark order as picked up
21
    Ship the order through different vendor.
46
    '''
22
    '''
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
 
-
 
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
-
 
60
   
-
 
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):
-
 
67
    '''
-
 
68
    Mark order as delivered
-
 
69
    '''
-
 
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
 
-
 
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
 
-
 
95
def mark_order_as_failed(order):
-
 
96
    '''
-
 
97
    Mark order as failed
-
 
98
    '''
-
 
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
 
-
 
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
 
-
 
122
def change_product(order):
-
 
123
    '''
-
 
124
    Ship a product of a different color to the customer.
-
 
125
    '''
-
 
126
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
23
    if order.status not in [OrderStatus.COD_VERIFICATION_PENDING, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW, OrderStatus.ACCEPTED]:
127
        print "This order has already been processed. Please seek help from engineering."
24
        print "This order has already been processed. Please seek help from engineering."
128
        return
25
        return
129
    
26
    
130
    raw_item_id = raw_input("Enter the ID of the item that you want to ship: ")
27
    raw_provider_id = raw_input("Enter the ID of the provider that you want to ship through: ")
131
    try:
28
    try:
132
        item_id = int(raw_item_id)
29
        provider_id = int(raw_provider_id)
133
    except ValueError:
30
    except ValueError:
134
        print("Invalid product Id")
31
        print("Invalid provider 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
-
 
141
    
-
 
142
    #warehouse_id = raw_input("Enter the warehouse id which will fulfil this order. Leave empty to keep it unchanged: ")
-
 
143
    #if warehouse_id is None or warehouse_id == "":
-
 
144
    #    warehouse_id = order.warehouse_id
-
 
145
    
-
 
146
    lineitem = order.lineitems[0]
-
 
147
    
-
 
148
    catalog_client = CatalogClient().get_client()
-
 
149
    catalog_client.reserveItemInWarehouse(item_id, order.warehouse_id, lineitem.quantity)
-
 
150
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
-
 
151
    #TODO: Check that this new item has the same price
-
 
152
        
-
 
153
    lineitem.item_id = item_id
-
 
154
    lineitem.color = color
-
 
155
    
-
 
156
    session.commit()
-
 
157
 
-
 
158
def change_warehouse(order):
-
 
159
    '''
-
 
160
    Update the warehouse which will be used to fulfil this order.
-
 
161
    '''
-
 
162
    if order.status not in [OrderStatus.INIT, OrderStatus.SUBMITTED_FOR_PROCESSING, OrderStatus.INVENTORY_LOW]:
-
 
163
        print "This order has already been processed. Please seek help from engineering."
-
 
164
        return
32
        return
165
    
33
    
166
    print("Current Warehouse: " + str(order.warehouse_id))
34
    if order.logistics_provider_id == provider_id:
167
    
-
 
168
    raw_warehouse_id = raw_input("Enter the ID of the warehouse from where you want to ship: ")
35
        print("Provider Id entered by you is same as provider assigned to order.")
169
    try:
-
 
170
        warehouse_id = int(raw_warehouse_id)
-
 
171
    except ValueError:
-
 
172
        print("Invalid warehouse id")
-
 
173
        return
36
        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]
-
 
180
    catalog_client = CatalogClient().get_client()
-
 
181
    catalog_client.reserveItemInWarehouse(lineitem.item_id, warehouse_id, lineitem.quantity)
-
 
182
    catalog_client.reduceReservationCount(lineitem.item_id, order.warehouse_id, lineitem.quantity)
-
 
183
    
37
    
-
 
38
    logistics_client = LogisticsClient().get_client()
-
 
39
    awb_number = logistics_client.getEmptyAWB(provider_id, order.cod)
-
 
40
    order.logistics_provider_id = provider_id
-
 
41
    order.airwaybill_no = awb_number
184
    order.warehouse_id = warehouse_id
42
    order.track_id = awb_number
185
    session.commit()
43
    session.commit()
186
 
44
 
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: ")
45
    print("Successfully updated the provider id")
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
 
46
 
205
def cancel(order):
47
def cancel(order):
206
    '''
48
    '''
207
    Cancel
49
    Cancel
208
    '''
50
    '''
209
    print("Your session has been closed")
51
    print("Your session has been closed")
210
    return
52
    return
211
 
53
 
212
ACTIONS = {0: cancel,
54
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,
55
           1: change_logistics_provider
218
           6: change_warehouse,
-
 
219
           7: update_weight
-
 
220
           }
56
           }
221
 
57
 
222
def get_py_datetime(time_string):
58
def get_py_datetime(time_string):
223
    time_format = "%Y-%m-%d %H%M"
59
    time_format = "%Y-%m-%d %H%M"
224
    mytime = time.strptime(time_string, time_format)
60
    mytime = time.strptime(time_string, time_format)
Line 245... Line 81...
245
    try:
81
    try:
246
        order = get_order(order_id)
82
        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:")
83
        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))
84
        print("Order Id:\t\t" + str(order.id))
249
        print("Customer Name:\t\t" + order.customer_name)
85
        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)
86
        print("Pincode:\t\t" + order.customer_pincode)
255
        print("Amount:\t\t\t" + str(order.total_amount))
87
        print("Amount:\t\t\t" + str(order.total_amount))
256
        print("Created On:\t\t" + str(order.created_timestamp))
88
        print("Created On:\t\t" + str(order.created_timestamp))
257
        #print("Billed On:\t" + str(order.billing_timestamp))
89
        print("Current Status:\t\t" + str(order.status))
258
        #print("Shipped On:\t" + str(order.shipping_timestamp))
90
        print("Status Description:\t\t" + order.statusDescription)
259
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
91
        print("Logistics provider id:\t\t" + str(order.logistics_provider_id))
260
        print("Status Description:\t" + order.statusDescription)
92
        print("Airway bill number:\t\t" + order.airwaybill_no)
261
        print("Ordered Items description:")
93
        print("Ordered Items description:")
262
        for lineitem in order.lineitems:
94
        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))
95
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
264
        print
96
        print
265
        print
97
        print