Subversion Repositories SmartDukaan

Rev

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