Subversion Repositories SmartDukaan

Rev

Rev 1939 | Go to most recent revision | Details | 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
    '''
45
    raw_pickup_timestamp = raw_input("Enter pickup time in YYYY-MM-DD hhmm format: ")
46
    try:
47
        pickup_timestamp = get_py_datetime(raw_pickup_timestamp)
48
        print("Pick up timestamp: " + str(pickup_timestamp))
49
    except ValueError:
50
        print("Invalid pickup time")
51
        return
52
 
53
    order = get_order(order_id)
54
    if order.status != OrderStatus.SHIPPED_FROM_WH:
55
        print "Has this order been picked up?"
56
        print
57
        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."
58
        return
59
 
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
    '''
69
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
70
    try:
71
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
72
    except ValueError:
73
        print("Invalid delivery time stamp")
74
        return
75
 
76
    receiver = raw_input("Enter the name of receiver: ")
77
    if receiver is None or receiver == "":
78
        print("Receiver info is mandatory.")
79
        return
80
 
81
    order = get_order(order_id)
82
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
83
        print "Has this order been picked up?"
84
        print
85
        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."
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
    '''
99
    raw_delivery_timestamp = raw_input("Enter delivery time in YYYY-MM-DD hhmm format: ")
100
    try:
101
        delivery_timestamp = get_py_datetime(raw_delivery_timestamp)
102
    except ValueError:
103
        print("Invalid delivery timestamp")
104
        return
105
 
106
    reason = raw_input("Enter the reason for failure: ")
107
    if reason is None or reason == "":
108
        print("Reason for failure is mandatory.")
109
        return
110
 
111
    order = get_order(order_id)
112
    if order.status != OrderStatus.SHIPPED_TO_LOGST:
113
        print "Has this order been picked up?"
114
        print
115
        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."
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
 
123
def cancel(order_id):
124
    '''
125
    Cancel
126
    '''
127
    pass
128
 
129
ACTIONS = {0: order_outofstock,
130
           1: mark_order_as_picked_up,
131
           2: mark_order_as_delivered,
132
           3: mark_order_as_failed,
133
           4: cancel
134
           }
135
 
136
def get_py_datetime(time_string):
137
    time_format = "%Y-%m-%d %H%M"
138
    mytime = time.strptime(time_string, time_format)
139
    return datetime.datetime(*mytime[:6])
140
 
141
def main():
142
    DataService.initialize(echoOn=False)
143
    raw_order_id = raw_input("Enter Order Id which you want to modify:")
144
    try:
145
        order_id = int(raw_order_id)
146
        print("You want to modify: " + str(order_id))
147
    except ValueError:
148
        print("Invalid Order Id.")
149
        return
150
 
151
    try:
152
        order = get_order(order_id)
153
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
154
        print("Order Id:\t\t" + str(order.id))
155
        print("Customer Name:\t\t" + order.customer_name)
156
        print("Address Line 1:\t\t" + order.customer_address1)
157
        print("Address Line 2:\t\t" + order.customer_address2)
158
        print("City:\t\t\t" + order.customer_city)
159
        print("State:\t\t\t" + order.customer_state)
160
        print("Pincode:\t\t" + order.customer_pincode)
161
        print("Amount:\t\t\t" + str(order.total_amount))
162
        print("Created On:\t\t" + str(order.created_timestamp))
163
        #print("Billed On:\t" + str(order.billing_timestamp))
164
        #print("Shipped On:\t" + str(order.shipping_timestamp))
165
        print("Current Status:\t\t" + VALUES_TO_NAMES[order.status])
166
        print("Status Description:\t" + order.statusDescription)
167
        print
168
        print
169
        print("You can perform following operations:")
170
        for (key, val) in ACTIONS.iteritems():
171
            print("[" + str(key) + "]" + val.__doc__ )
172
 
173
        raw_action = raw_input("What do you want to do? ")
174
        if raw_action is None or raw_action == "" or raw_action == "4":
175
            print("Your session has been closed.")
176
            return
177
        try:
178
            action = int(raw_action)
179
        except ValueError:
180
            print("Invalid input.")
181
            return
182
        if action > 4:
183
            print("Invalid input.")
184
            return
185
        ACTIONS[action](order_id)
186
    except TransactionServiceException as tsex:
187
        print tsex.message
188
    finally:
189
        close_session()
190
 
191
if __name__ == '__main__':
192
    main()