Subversion Repositories SmartDukaan

Rev

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