| 412 |
ashish |
1 |
'''
|
|
|
2 |
Created on 05-Aug-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
| 483 |
rajveer |
6 |
from shop2020.thriftpy.logistics.ttypes import Shipment, LogisticsInfo
|
| 442 |
rajveer |
7 |
from shop2020.logistics.service.impl.DataAccessor import add_empty_AWBs, get_empty_AWB,\
|
|
|
8 |
get_provider, get_shipment_info, get_shipments, get_todays_shipments, initialize,\
|
| 477 |
rajveer |
9 |
get_providers, create_shipment, update_shipment_status, get_logistics_estimation, add_delivery_estimate,\
|
| 483 |
rajveer |
10 |
add_pincode_warehouse_mapping, get_logistics_info
|
| 442 |
rajveer |
11 |
from shop2020.logistics.service.impl.Converters import to_t_provider,\
|
| 472 |
rajveer |
12 |
to_t_shipment, to_t_shipmentstatusinfo, to_t_shipmentupdate, to_t_itemLogistics
|
| 494 |
rajveer |
13 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
14 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 442 |
rajveer |
15 |
from shop2020.utils.Utils import log_entry, to_py_date
|
| 494 |
rajveer |
16 |
import datetime
|
|
|
17 |
import time
|
| 472 |
rajveer |
18 |
|
| 494 |
rajveer |
19 |
|
| 412 |
ashish |
20 |
class LogisticsServiceHandler:
|
|
|
21 |
|
| 442 |
rajveer |
22 |
def __init__(self):
|
|
|
23 |
initialize()
|
| 483 |
rajveer |
24 |
|
|
|
25 |
def getLogisticsInfo(self, destination_pincode, sku_id):
|
|
|
26 |
"""
|
|
|
27 |
Parameters:
|
|
|
28 |
- destination_pincode
|
|
|
29 |
- sku_id
|
|
|
30 |
"""
|
|
|
31 |
warehouse_id, provider_id, airway_billno, delivery_time = get_logistics_info(destination_pincode, sku_id)
|
|
|
32 |
logistics_info = LogisticsInfo()
|
|
|
33 |
logistics_info.airway_billno = airway_billno
|
|
|
34 |
logistics_info.warehouse_id = warehouse_id
|
|
|
35 |
logistics_info.delivery_estimate = delivery_time
|
|
|
36 |
logistics_info.provider_id = provider_id
|
|
|
37 |
return logistics_info
|
|
|
38 |
|
| 412 |
ashish |
39 |
def addEmptyAWBs(self, numbers, provider_id):
|
|
|
40 |
"""
|
|
|
41 |
Parameters:
|
|
|
42 |
- numbers
|
|
|
43 |
- provider_id
|
|
|
44 |
"""
|
| 442 |
rajveer |
45 |
add_empty_AWBs(numbers, provider_id)
|
| 412 |
ashish |
46 |
|
|
|
47 |
def getEmptyAWB(self, provider_id):
|
|
|
48 |
"""
|
|
|
49 |
Parameters:
|
|
|
50 |
- provider_id
|
|
|
51 |
"""
|
| 442 |
rajveer |
52 |
return get_empty_AWB(provider_id)
|
| 412 |
ashish |
53 |
|
| 442 |
rajveer |
54 |
def getProviders(self):
|
|
|
55 |
"""
|
|
|
56 |
Parameters:
|
|
|
57 |
"""
|
|
|
58 |
log_entry(self, "all providers requested")
|
|
|
59 |
|
|
|
60 |
providers = get_providers()
|
|
|
61 |
ret_providers = []
|
|
|
62 |
for provider in providers:
|
|
|
63 |
if provider:
|
|
|
64 |
ret_providers.append(to_t_provider(provider))
|
|
|
65 |
return ret_providers
|
|
|
66 |
|
| 412 |
ashish |
67 |
|
|
|
68 |
def getProvider(self, provider_id):
|
|
|
69 |
"""
|
|
|
70 |
Parameters:
|
|
|
71 |
- provider_id
|
|
|
72 |
"""
|
| 442 |
rajveer |
73 |
return to_t_provider(get_provider(provider_id))
|
| 412 |
ashish |
74 |
|
| 442 |
rajveer |
75 |
def createShipment(self, shipment):
|
|
|
76 |
"""
|
|
|
77 |
Parameters:
|
|
|
78 |
- shipment
|
|
|
79 |
"""
|
|
|
80 |
return create_shipment(shipment)
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
def updateShipmentStatus(self, awb, shipment_update):
|
|
|
84 |
"""
|
|
|
85 |
Parameters:
|
|
|
86 |
- awb
|
|
|
87 |
- shipment_update
|
|
|
88 |
"""
|
|
|
89 |
return update_shipment_status(awb, shipment_update)
|
|
|
90 |
|
|
|
91 |
|
| 412 |
ashish |
92 |
def getShipmentInfo(self, awb):
|
|
|
93 |
"""
|
|
|
94 |
Parameters:
|
|
|
95 |
- awb
|
|
|
96 |
"""
|
| 442 |
rajveer |
97 |
return to_t_shipmentstatusinfo(get_shipment_info(awb))
|
| 412 |
ashish |
98 |
|
|
|
99 |
def getShipments(self, warehouse_id, from_date, to_date, provider_id):
|
|
|
100 |
"""
|
|
|
101 |
Parameters:
|
|
|
102 |
- warehouse_id
|
|
|
103 |
- from_date
|
|
|
104 |
- to_date
|
|
|
105 |
- provider_id
|
|
|
106 |
"""
|
| 442 |
rajveer |
107 |
log_entry(self, "all shipments requested")
|
|
|
108 |
|
|
|
109 |
from_datetime = to_py_date(from_date)
|
|
|
110 |
to_datetime = to_py_date(to_date)
|
|
|
111 |
|
|
|
112 |
shipments = get_shipments(warehouse_id, from_datetime, to_datetime, provider_id)
|
|
|
113 |
ret_shipments = []
|
|
|
114 |
for shipment in shipments:
|
|
|
115 |
if shipment:
|
|
|
116 |
ret_shipments.append(to_t_shipment(shipment))
|
|
|
117 |
return ret_shipments
|
| 412 |
ashish |
118 |
|
| 472 |
rajveer |
119 |
def getLogisticsEstimation(self, itemId, destination_pin, provider_id):
|
|
|
120 |
"""
|
|
|
121 |
Parameters:
|
|
|
122 |
- itemId
|
|
|
123 |
- destination_pin
|
|
|
124 |
"""
|
|
|
125 |
|
| 494 |
rajveer |
126 |
inventory_client = InventoryClient()
|
|
|
127 |
inventory_client.__start__()
|
|
|
128 |
client = inventory_client.get_client()
|
|
|
129 |
config_client = ConfigClient()
|
|
|
130 |
|
|
|
131 |
cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
|
|
|
132 |
stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
|
|
|
133 |
|
|
|
134 |
|
| 477 |
rajveer |
135 |
delivery_estimate, warehouse_id = get_logistics_estimation(itemId, destination_pin, provider_id)
|
| 494 |
rajveer |
136 |
delivery_time = delivery_estimate.delivery_time
|
|
|
137 |
|
|
|
138 |
items_in_inventory = client.getItemAvailibilityAtWarehouse(warehouse_id, itemId)
|
|
|
139 |
now = datetime.datetime.now()
|
|
|
140 |
print now.hour
|
|
|
141 |
if cutoff_time < now.hour:
|
|
|
142 |
delivery_time = delivery_time + 24
|
|
|
143 |
if items_in_inventory < stock_threshold :
|
|
|
144 |
time_to_procure = int(config_client.get_property('inventory_time_to_procure'))
|
|
|
145 |
delivery_time = delivery_time + time_to_procure
|
| 442 |
rajveer |
146 |
|
| 494 |
rajveer |
147 |
return to_t_itemLogistics(delivery_time, items_in_inventory, warehouse_id)
|
|
|
148 |
|
| 477 |
rajveer |
149 |
def addDeliveryEstimate(self, warahouse_id, destination_pin, provider_id, delivery_time, reliability):
|
| 472 |
rajveer |
150 |
"""
|
|
|
151 |
Parameters:
|
|
|
152 |
- warahouse_id
|
|
|
153 |
- destination_pin
|
|
|
154 |
- provider_id
|
|
|
155 |
- delivery_time
|
|
|
156 |
"""
|
| 477 |
rajveer |
157 |
add_delivery_estimate(warahouse_id, destination_pin, provider_id, delivery_time, reliability)
|
| 472 |
rajveer |
158 |
|
| 477 |
rajveer |
159 |
def addPincodeWarehouseMapping(self, pincode, warehouse_id, warehouse_pin):
|
|
|
160 |
"""
|
|
|
161 |
Parameters:
|
|
|
162 |
- pincode
|
|
|
163 |
- warehouse_id
|
|
|
164 |
- warehouse_name
|
|
|
165 |
"""
|
|
|
166 |
add_pincode_warehouse_mapping(pincode, warehouse_id, warehouse_pin)
|
|
|
167 |
|
| 412 |
ashish |
168 |
def getTodaysShipments(self, warehouse_id, provider_id):
|
|
|
169 |
"""
|
|
|
170 |
Parameters:
|
|
|
171 |
- warehouse_id
|
|
|
172 |
- provider_id
|
|
|
173 |
"""
|
| 442 |
rajveer |
174 |
|
|
|
175 |
log_entry(self, "all shipments requested for today")
|
|
|
176 |
|
|
|
177 |
shipments = get_todays_shipments(warehouse_id, provider_id)
|
|
|
178 |
ret_shipments = []
|
|
|
179 |
for shipment in shipments:
|
|
|
180 |
if shipment:
|
|
|
181 |
ret_shipments.append(to_t_shipment(shipment))
|
|
|
182 |
return ret_shipments
|
|
|
183 |
|
|
|
184 |
'''
|
| 412 |
ashish |
185 |
shipments = []
|
|
|
186 |
i = 0
|
|
|
187 |
while i < 100:
|
|
|
188 |
shipment = Shipment()
|
|
|
189 |
shipment.warehouse_name = "Seattle"
|
|
|
190 |
shipment.awb="1234"+str(i)
|
|
|
191 |
shipment.destination="JP"
|
|
|
192 |
shipment.origin = "US"
|
|
|
193 |
shipment.recepient_address = "1, Microsoft Way, Redmond"
|
|
|
194 |
shipment.recepient_name = "Steve Balmer"
|
|
|
195 |
shipment.recepient_phone = "+14356368790"
|
|
|
196 |
shipment.recepient_pincode = "110001"
|
|
|
197 |
shipment.shipment_contents = "iPhone 4G"
|
|
|
198 |
shipment.shipment_weight = "300 gms"
|
|
|
199 |
shipments.append(shipment)
|
|
|
200 |
i = i+1
|
|
|
201 |
return shipments
|
| 472 |
rajveer |
202 |
'''
|
|
|
203 |
|
| 442 |
rajveer |
204 |
|