Subversion Repositories SmartDukaan

Rev

Rev 7055 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7054 amar.kumar 1
#!/usr/bin/python
2
 
3
import optparse
4
import time
5
import datetime
6
import sys
7
from elixir import session
8
from random import randrange
9
from shop2020.clients.LogisticsClient import LogisticsClient
10
from shop2020.clients.CatalogClient import CatalogClient
11
from shop2020.clients.InventoryClient import InventoryClient
12
from shop2020.model.v1.order.impl.DataService import Attribute
13
from shop2020.thriftpy.logistics.ttypes import DeliveryType, PickUpType
14
 
15
if __name__ == '__main__' and __package__ is None:
16
    import os
17
    sys.path.insert(0, os.getcwd())
18
 
19
from shop2020.thriftpy.model.v1.order.ttypes import OrderStatus, TransactionServiceException, OrderType
20
from shop2020.model.v1.order.impl import DataService
21
from shop2020.model.v1.order.impl.DataService import Order, LineItem, Transaction
22
from shop2020.model.v1.order.impl.DataAccessors import get_order, close_session
23
from shop2020.model.v1.order.impl.DataAccessors import delhi_pincodes
24
 
25
def cancel(order):
26
    '''
27
    Cancel
28
    '''
29
    print("Your session has been closed")
30
    return
31
 
32
def process_order(order):
33
    '''
34
    Process Order
35
    '''
36
    bulk_order = Order()
37
 
38
    bulk_order.transaction = order.transaction
39
 
40
    bulk_order.customer_id = order.customer_id
41
    bulk_order.customer_email = order.customer_email
42
    bulk_order.customer_name = order.customer_name
43
    bulk_order.customer_pincode = order.customer_pincode
44
    bulk_order.customer_address1 = order.customer_address1
45
    bulk_order.customer_address2 = order.customer_address2
46
    bulk_order.customer_city = order.customer_city
47
    bulk_order.customer_state = order.customer_state
48
    bulk_order.customer_mobilenumber = order.customer_mobilenumber
49
    bulk_order.total_amount = order.total_amount            
50
    bulk_order.total_weight = order.total_weight
51
    bulk_order.status = OrderStatus.ACCEPTED
52
    bulk_order.statusDescription = "In Process";
53
    bulk_order.created_timestamp = order.created_timestamp
54
    bulk_order.accepted_timestamp = datetime.datetime.now()
55
    bulk_order.cod = order.cod
56
    bulk_order.orderType = OrderType.B2Cbulk
57
    bulk_order.pickupStoreId = 0
58
    bulk_order.otg = 0
59
    bulk_order.insurer = 0
60
    bulk_order.insuranceAmount = 0
61
 
62
    logistics_client = LogisticsClient().get_client()
63
    logistics_info = logistics_client.getLogisticsInfo(order.customer_pincode, 1, DeliveryType.PREPAID, PickUpType.SELF)
64
    bulk_order.logistics_provider_id = logistics_info.providerId;
65
    bulk_order.tracking_id = logistics_info.airway_billno
66
    bulk_order.airwaybill_no = logistics_info.airway_billno
67
 
68
    bulk_order.expected_delivery_time = order.expected_delivery_time
69
    bulk_order.promised_delivery_time = order.promised_delivery_time
70
    bulk_order.expected_shipping_time = order.expected_shipping_time
71
    bulk_order.promised_shipping_time = order.promised_shipping_time
72
 
73
    catalog_client = CatalogClient().get_client()
74
    item = catalog_client.getItem(1173)
75
 
76
    litem = LineItem()
77
    litem.item_id = item.id
78
    litem.productGroup = item.productGroup
79
    litem.brand = item.brand
80
    litem.model_number = item.modelNumber
81
    litem.model_name = item.modelName
82
    litem.color = item.color
83
    litem.extra_info = "Complementary Order for A110,  Order ID " + str(order.id)
84
    litem.quantity = 1
85
    litem.unit_price = order.total_amount - order.insuranceAmount - 9999
86
    litem.unit_weight = item.weight
87
    litem.total_price = order.total_amount - order.insuranceAmount - 9999
88
    litem.total_weight = item.weight
89
    litem.vatRate = 5.0
90
 
91
    bulk_order.lineitems.append(litem)
92
 
93
    bulk_order.total_amount = order.total_amount - order.insuranceAmount - 9999
94
 
95
    order.total_amount = 9999 + order.insuranceAmount
96
    order.lineitems[0].unit_price = 9999
97
    order.lineitems[0].total_price = 9999
98
 
99
    inventory_client = InventoryClient().get_client()
100
    item_availability = inventory_client.getItemAvailabilityAtLocation(bulk_order.lineitems[0].item_id,1)
101
    bulk_order.warehouse_id = item_availability[2]
102
    bulk_order.fulfillmentWarehouseId = item_availability[0]
103
 
104
    session.commit()
105
 
106
    return
107
 
108
 
109
ACTIONS = {0: cancel,
110
           1: process_order
111
           }
112
 
113
def get_py_datetime(time_string):
114
    time_format = "%Y-%m-%d %H%M"
115
    mytime = time.strptime(time_string, time_format)
116
    return datetime.datetime(*mytime[:6])
117
 
118
def main():
119
    parser = optparse.OptionParser()
120
    parser.add_option("-H", "--host", dest="hostname",
121
                      default="localhost",
122
                      type="string", help="The HOST where the DB server is running",
123
                      metavar="HOST")
124
    parser.add_option("-O", "--order", dest="orderId",
125
                      type="int", help="A110 OrderId to be processed",
126
                      metavar="NUM")
127
    (options, args) = parser.parse_args()
128
    if len(args) != 0:
129
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
130
    DataService.initialize(db_hostname=options.hostname, echoOn=False)
131
 
132
    orderId = options.orderId
133
    try:
134
        order = get_order(orderId)
135
        print("Please check the details of the order below and ensure that it's the same order which you want to modify:")
136
        print("Order Id:\t\t" + str(order.id))
137
        print("Customer Name:\t\t" + order.customer_name)
138
        print("Pincode:\t\t" + order.customer_pincode)
139
        print("Amount:\t\t\t" + str(order.total_amount))
140
        print("Created On:\t\t" + str(order.created_timestamp))
141
        print("Current Status:\t\t" + str(order.status))
142
        print("Status Description:\t\t" + order.statusDescription)
143
        print("Logistics provider id:\t\t" + str(order.logistics_provider_id))
144
        print("Airway bill number:\t\t" + order.airwaybill_no)
145
        print("Ordered Items description:")
146
        for lineitem in order.lineitems:
147
            print("Item Id:" + str(lineitem.item_id) + "\tBrand: " + str(lineitem.brand) + "\tModel: " + str(lineitem.model_number) + "\tColor: " + str(lineitem.color))
148
        print
149
        print
150
        print("You can perform following operations:")
151
        for (key, val) in ACTIONS.iteritems():
152
            print("[" + str(key) + "]" + val.__doc__ )
153
 
154
        raw_action = raw_input("What do you want to do? ")
155
        if raw_action is None or raw_action == "":
156
            print("Your session has been closed.")
157
            return
158
        try:
159
            action = int(raw_action)
160
        except ValueError:
161
            print("Invalid input.")
162
            return
163
        if action > max(ACTIONS.keys()):
164
            print("Invalid input.")
165
            return
166
        ACTIONS[action](order)
167
    except TransactionServiceException as tsex:
168
        print tsex.message
169
    finally:
170
        close_session()
171
 
172
if __name__ == '__main__':
173
    main()