Subversion Repositories SmartDukaan

Rev

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