| 412 |
ashish |
1 |
'''
|
|
|
2 |
Created on 05-Aug-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
| 3133 |
rajveer |
6 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 5944 |
mandeep.dh |
7 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 494 |
rajveer |
8 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 5944 |
mandeep.dh |
9 |
from shop2020.logistics.service.impl import DataAccessor
|
|
|
10 |
from shop2020.logistics.service.impl.Converters import to_t_awbupdate, \
|
|
|
11 |
to_t_provider, to_t_pickup_store
|
|
|
12 |
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB, \
|
|
|
13 |
get_shipment_info, initialize, get_logistics_estimation, get_provider, \
|
|
|
14 |
get_providers, close_session, get_free_awb_count, get_holidays, is_alive, \
|
|
|
15 |
get_provider_for_pickup_type, get_pickup_store, get_all_pickup_stores, \
|
| 6322 |
amar.kumar |
16 |
get_pickup_store_by_hotspot_id, get_destination_code, update_pincode, \
|
| 6643 |
rajveer |
17 |
add_pincode, store_shipment_info
|
| 5944 |
mandeep.dh |
18 |
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo, \
|
|
|
19 |
LogisticsServiceException, DeliveryType, PickUpType
|
|
|
20 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
| 494 |
rajveer |
21 |
import datetime
|
| 644 |
chandransh |
22 |
import math
|
| 1687 |
vikas |
23 |
import sys
|
| 6322 |
amar.kumar |
24 |
from shop2020.logistics.service.impl.DataService import ServiceableLocationDetails
|
| 472 |
rajveer |
25 |
|
| 412 |
ashish |
26 |
class LogisticsServiceHandler:
|
|
|
27 |
|
| 3187 |
rajveer |
28 |
def __init__(self, dbname='logistics', db_hostname='localhost'):
|
|
|
29 |
initialize(dbname, db_hostname)
|
| 746 |
rajveer |
30 |
try:
|
|
|
31 |
config_client = ConfigClient()
|
| 5978 |
rajveer |
32 |
self.sourceId = int(config_client.get_property("sourceid"))
|
| 746 |
rajveer |
33 |
self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
|
| 5843 |
mandeep.dh |
34 |
self.cod_cutoff_time = 24 #int(config_client.get_property('delivery_cutoff_time'))
|
| 776 |
rajveer |
35 |
self.default_pincode = int(config_client.get_property('default_pincode'))
|
| 1687 |
vikas |
36 |
except Exception as ex:
|
|
|
37 |
print "[ERROR] Unexpected config error:", sys.exc_info()[0]
|
| 5978 |
rajveer |
38 |
self.sourceId = 1
|
| 746 |
rajveer |
39 |
self.cutoff_time = 15
|
| 5843 |
mandeep.dh |
40 |
self.cod_cutoff_time = 24
|
| 4866 |
rajveer |
41 |
self.default_pincode = "110001"
|
| 5978 |
rajveer |
42 |
|
| 3064 |
chandransh |
43 |
|
| 669 |
chandransh |
44 |
def getProvider(self, providerId):
|
| 675 |
chandransh |
45 |
"""
|
|
|
46 |
Returns a provider for a given provider ID. Throws an exception if none found.
|
|
|
47 |
|
|
|
48 |
Parameters:
|
|
|
49 |
- providerId
|
|
|
50 |
"""
|
| 796 |
rajveer |
51 |
try:
|
| 1137 |
chandransh |
52 |
provider = get_provider(providerId)
|
|
|
53 |
if provider:
|
|
|
54 |
return to_t_provider(provider)
|
|
|
55 |
else:
|
|
|
56 |
raise LogisticsServiceException(101, "No Provider found for the given id")
|
| 796 |
rajveer |
57 |
finally:
|
|
|
58 |
close_session()
|
|
|
59 |
|
| 675 |
chandransh |
60 |
def getAllProviders(self, ):
|
|
|
61 |
"""
|
|
|
62 |
Returns a list containing all the providers.
|
|
|
63 |
"""
|
| 796 |
rajveer |
64 |
try:
|
|
|
65 |
return [to_t_provider(provider) for provider in get_providers()]
|
|
|
66 |
finally:
|
|
|
67 |
close_session()
|
|
|
68 |
|
| 5767 |
rajveer |
69 |
def getLogisticsInfo(self, destination_pincode, itemId, type, pickUp):
|
| 483 |
rajveer |
70 |
"""
|
|
|
71 |
Parameters:
|
|
|
72 |
- destination_pincode
|
| 716 |
rajveer |
73 |
- item_id
|
| 3044 |
chandransh |
74 |
- type
|
| 483 |
rajveer |
75 |
"""
|
| 796 |
rajveer |
76 |
try:
|
| 3044 |
chandransh |
77 |
logistics_info = self.get_logistics_estimation_with_type(itemId, destination_pincode, type)
|
| 5767 |
rajveer |
78 |
if pickUp == PickUpType.RUNNER or pickUp == PickUpType.SELF:
|
|
|
79 |
logistics_info.providerId = get_provider_for_pickup_type(pickUp)
|
| 3044 |
chandransh |
80 |
logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId, type)
|
| 796 |
rajveer |
81 |
return logistics_info
|
|
|
82 |
finally:
|
|
|
83 |
close_session()
|
|
|
84 |
|
| 5247 |
rajveer |
85 |
def getEmptyAWB(self, providerId, type):
|
| 412 |
ashish |
86 |
"""
|
|
|
87 |
Parameters:
|
|
|
88 |
- provider_id
|
| 5247 |
rajveer |
89 |
- type
|
| 412 |
ashish |
90 |
"""
|
| 796 |
rajveer |
91 |
try:
|
| 5247 |
rajveer |
92 |
return get_empty_AWB(providerId, type)
|
| 796 |
rajveer |
93 |
finally:
|
|
|
94 |
close_session()
|
|
|
95 |
|
| 6643 |
rajveer |
96 |
def getShipmentInfo(self, awbNumber, providerId):
|
| 412 |
ashish |
97 |
"""
|
|
|
98 |
Parameters:
|
| 6643 |
rajveer |
99 |
- awbNumber
|
| 766 |
rajveer |
100 |
- providerId
|
| 412 |
ashish |
101 |
"""
|
| 796 |
rajveer |
102 |
try:
|
| 6643 |
rajveer |
103 |
awb_updates = get_shipment_info(awbNumber, providerId)
|
| 796 |
rajveer |
104 |
t_updates = []
|
|
|
105 |
for update in awb_updates:
|
|
|
106 |
t_updates.append(to_t_awbupdate(update))
|
|
|
107 |
return t_updates
|
|
|
108 |
finally:
|
|
|
109 |
close_session()
|
| 3044 |
chandransh |
110 |
|
| 6643 |
rajveer |
111 |
def storeShipmentInfo(self, update):
|
|
|
112 |
"""
|
|
|
113 |
Parameters:
|
|
|
114 |
- update
|
|
|
115 |
"""
|
|
|
116 |
try:
|
|
|
117 |
store_shipment_info(update)
|
|
|
118 |
finally:
|
|
|
119 |
close_session()
|
|
|
120 |
|
|
|
121 |
|
| 4630 |
mandeep.dh |
122 |
def getLogisticsEstimation(self, itemId, destination_pin, type):
|
| 472 |
rajveer |
123 |
"""
|
|
|
124 |
Parameters:
|
|
|
125 |
- itemId
|
|
|
126 |
- destination_pin
|
| 4630 |
mandeep.dh |
127 |
- type
|
| 472 |
rajveer |
128 |
"""
|
| 644 |
chandransh |
129 |
try:
|
| 4630 |
mandeep.dh |
130 |
return self.get_logistics_estimation_with_type(itemId, destination_pin, type)
|
| 796 |
rajveer |
131 |
finally:
|
|
|
132 |
close_session()
|
| 3044 |
chandransh |
133 |
|
| 4630 |
mandeep.dh |
134 |
def get_logistics_estimation_with_type(self, itemId, destination_pin, type):
|
| 3044 |
chandransh |
135 |
try:
|
| 5295 |
rajveer |
136 |
#Get the id and location of actual warehouse that'll be used to fulfil this order.
|
| 5944 |
mandeep.dh |
137 |
client = InventoryClient().get_client()
|
| 5983 |
rajveer |
138 |
fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability = client.getItemAvailabilityAtLocation(itemId, self.sourceId)
|
| 3044 |
chandransh |
139 |
except Exception as ex:
|
|
|
140 |
raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
|
| 5295 |
rajveer |
141 |
|
| 6524 |
rajveer |
142 |
delivery_estimate = get_logistics_estimation(destination_pin, sellingPrice)
|
| 3218 |
rajveer |
143 |
if delivery_estimate is None:
|
| 3044 |
chandransh |
144 |
raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")
|
| 5295 |
rajveer |
145 |
|
| 3044 |
chandransh |
146 |
|
| 5270 |
rajveer |
147 |
## Commented below part as we have only Delhi as warehouse city. If we will add some more warehouses in different cities, this could be useful.
|
| 4009 |
chandransh |
148 |
# We are revising the estimates based on the actual warehouse that this order will be assigned to.
|
|
|
149 |
# This warehouse may be located in a zone which is different from the one we allocated for this pincode.
|
| 5270 |
rajveer |
150 |
#delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, warehouse_loc, type)
|
|
|
151 |
#if delivery_estimate is None:
|
|
|
152 |
# raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))
|
| 3044 |
chandransh |
153 |
|
| 6537 |
rajveer |
154 |
delivery_time = 24 * (delivery_estimate.delivery_time + delivery_estimate.delivery_delay)
|
| 3044 |
chandransh |
155 |
|
| 4009 |
chandransh |
156 |
'''
|
|
|
157 |
We're now calculating the expected shipping delay which is independent of
|
|
|
158 |
the courier agency and is completely within our control (well, almost).
|
|
|
159 |
'''
|
| 3355 |
chandransh |
160 |
#Always add the expected delay
|
| 4009 |
chandransh |
161 |
shipping_delay = 24 * expected_delay
|
| 3355 |
chandransh |
162 |
|
| 4829 |
rajveer |
163 |
# Sometimes we set negative shipping delay just in case we know time to procure will be less than the default.
|
|
|
164 |
# If we have received inventory and forgot to remove expected delay from item, it could lead to display negative shipping days.
|
|
|
165 |
if shipping_delay < 0:
|
|
|
166 |
shipping_delay = 0
|
|
|
167 |
|
| 3044 |
chandransh |
168 |
#Further increase the estimate if it's late in the day
|
| 3064 |
chandransh |
169 |
current_hour = datetime.datetime.now().hour
|
|
|
170 |
if type == DeliveryType.PREPAID and self.cutoff_time <= current_hour:
|
| 4009 |
chandransh |
171 |
shipping_delay = shipping_delay + 24
|
| 3044 |
chandransh |
172 |
|
| 4426 |
rajveer |
173 |
#In case of COD,increase delay by one more day
|
|
|
174 |
if type == DeliveryType.COD:
|
|
|
175 |
shipping_delay = shipping_delay + 24
|
| 6524 |
rajveer |
176 |
delivery_estimate.otgAvailable = False
|
| 4426 |
rajveer |
177 |
|
| 4010 |
chandransh |
178 |
delivery_time = delivery_time + shipping_delay
|
|
|
179 |
|
| 4009 |
chandransh |
180 |
shipping_delay = int(math.ceil(shipping_delay/24.0))
|
| 4010 |
chandransh |
181 |
delivery_time = int(math.ceil(delivery_time/24.0))
|
| 3044 |
chandransh |
182 |
|
|
|
183 |
logistics_info = LogisticsInfo()
|
|
|
184 |
logistics_info.deliveryTime = delivery_time
|
| 3218 |
rajveer |
185 |
logistics_info.providerId = delivery_estimate.provider_id
|
| 5110 |
mandeep.dh |
186 |
logistics_info.warehouseId = billingWarehouseId
|
|
|
187 |
logistics_info.fulfilmentWarehouseId = fulfilmentWarehouseId
|
| 4009 |
chandransh |
188 |
logistics_info.shippingTime = shipping_delay
|
| 4870 |
rajveer |
189 |
logistics_info.codAllowed = delivery_estimate.codAllowed
|
| 6524 |
rajveer |
190 |
logistics_info.otgAvailable = delivery_estimate.otgAvailable
|
| 6726 |
rajveer |
191 |
logistics_info.deliveryDelay = delivery_estimate.delivery_delay
|
| 3044 |
chandransh |
192 |
|
| 5595 |
anupam.sin |
193 |
try:
|
|
|
194 |
return logistics_info
|
|
|
195 |
finally:
|
|
|
196 |
close_session()
|
| 3044 |
chandransh |
197 |
|
| 731 |
chandransh |
198 |
def getDestinationCode(self, providerId, pinCode):
|
|
|
199 |
"""
|
|
|
200 |
Returns the short three letter code of a pincode for the given provider.
|
|
|
201 |
Raises an exception if the pin code is not serviced by the given provider.
|
|
|
202 |
|
|
|
203 |
Parameters:
|
|
|
204 |
- providerId
|
|
|
205 |
- pinCode
|
|
|
206 |
"""
|
| 796 |
rajveer |
207 |
try:
|
| 3217 |
rajveer |
208 |
try:
|
| 3218 |
rajveer |
209 |
dest_code = DataAccessor.serviceable_location_cache[providerId][pinCode][0]
|
| 3217 |
rajveer |
210 |
return dest_code
|
|
|
211 |
except:
|
| 6017 |
amar.kumar |
212 |
try:
|
|
|
213 |
dest_code = get_destination_code(providerId, pinCode)
|
|
|
214 |
return dest_code
|
|
|
215 |
except:
|
|
|
216 |
raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))
|
| 796 |
rajveer |
217 |
finally:
|
|
|
218 |
close_session()
|
| 1137 |
chandransh |
219 |
|
| 3103 |
chandransh |
220 |
def getFreeAwbCount(self, providerId, type):
|
| 1137 |
chandransh |
221 |
"""
|
| 3103 |
chandransh |
222 |
Returns the number of unused AWB numbers for the given provider of the given type
|
| 796 |
rajveer |
223 |
|
| 1137 |
chandransh |
224 |
Parameters:
|
|
|
225 |
- providerId
|
| 3103 |
chandransh |
226 |
- type
|
| 1137 |
chandransh |
227 |
"""
|
|
|
228 |
try:
|
| 3103 |
chandransh |
229 |
return get_free_awb_count(providerId, type)
|
| 1137 |
chandransh |
230 |
finally:
|
|
|
231 |
close_session()
|
| 1730 |
ankur.sing |
232 |
|
|
|
233 |
def getHolidays(self, fromDate, toDate):
|
|
|
234 |
"""
|
|
|
235 |
Returns list of Holiday dates between fromDate and toDate (both inclusive)
|
|
|
236 |
fromDate should be passed as milliseconds corresponding to the start of the day.
|
|
|
237 |
If fromDate is passed as -1, fromDate is not considered for filtering
|
|
|
238 |
If toDate is passed as -1, toDate is not considered for filtering
|
| 1137 |
chandransh |
239 |
|
| 1730 |
ankur.sing |
240 |
Parameters:
|
|
|
241 |
- fromDate
|
|
|
242 |
- toDate
|
|
|
243 |
"""
|
|
|
244 |
try:
|
|
|
245 |
return get_holidays(fromDate, toDate)
|
|
|
246 |
finally:
|
|
|
247 |
close_session()
|
| 5527 |
anupam.sin |
248 |
|
|
|
249 |
def getProviderForPickupType(self, pickUp):
|
|
|
250 |
try:
|
|
|
251 |
return get_provider_for_pickup_type(pickUp)
|
|
|
252 |
finally:
|
|
|
253 |
close_session()
|
| 3064 |
chandransh |
254 |
|
| 766 |
rajveer |
255 |
def closeSession(self, ):
|
|
|
256 |
close_session()
|
| 3376 |
rajveer |
257 |
|
|
|
258 |
def isAlive(self, ):
|
|
|
259 |
"""
|
|
|
260 |
For checking weather service is active alive or not. It also checks connectivity with database
|
|
|
261 |
"""
|
|
|
262 |
try:
|
|
|
263 |
return is_alive()
|
|
|
264 |
finally:
|
|
|
265 |
close_session()
|
| 4934 |
amit.gupta |
266 |
|
|
|
267 |
def getEntityLogisticsEstimation(self, catalogItemId, destination_pin, type):
|
|
|
268 |
"""
|
|
|
269 |
Returns a LogisticsInfo structure w/o an airway bill number. Use this method during the estimation phase.
|
|
|
270 |
Raises an exception if this pincode is not allocated to any warehouse zone or provider. Also, if the pincode
|
|
|
271 |
is allocated to a warehouse zone but there are no actual warehouses in that zone, an exception is raised.
|
|
|
272 |
|
|
|
273 |
Parameters:
|
|
|
274 |
- catalogItemId
|
|
|
275 |
- destination_pin
|
|
|
276 |
- type
|
|
|
277 |
"""
|
|
|
278 |
try:
|
|
|
279 |
return self.get_entity_logistics_estimation_with_type(catalogItemId, destination_pin, type)
|
|
|
280 |
finally:
|
|
|
281 |
close_session()
|
|
|
282 |
|
|
|
283 |
def get_entity_logistics_estimation_with_type(self, catalog_item_id, destination_pin, type):
|
|
|
284 |
try:
|
|
|
285 |
client = CatalogClient().get_client()
|
|
|
286 |
items = client.getValidItemsByCatalogId(catalog_item_id)
|
|
|
287 |
except Exception as ex:
|
|
|
288 |
raise LogisticsServiceException(103, "Unable to fetch inventory information about this entity.")
|
|
|
289 |
|
|
|
290 |
estimateList = []
|
|
|
291 |
|
|
|
292 |
for item in items:
|
|
|
293 |
estimationInfo = self.get_logistics_estimation_with_type(item.id, destination_pin, type)
|
|
|
294 |
if item.itemStatus == status.ACTIVE:
|
|
|
295 |
estimateList.append((0, estimationInfo.deliveryTime, item.id))
|
|
|
296 |
elif item.itemStatus == status.PAUSED:
|
|
|
297 |
estimateList.append((1, estimationInfo.deliveryTime, item.id))
|
|
|
298 |
elif item.itemStatus == status.PAUSED_BY_RISK:
|
|
|
299 |
estimateList.append((2, estimationInfo.deliveryTime, item.id))
|
| 6074 |
amit.gupta |
300 |
elif item.itemStatus == status.COMING_SOON:
|
|
|
301 |
estimateList.append((3, estimationInfo.deliveryTime, item.id))
|
| 4934 |
amit.gupta |
302 |
|
|
|
303 |
estimateList.sort()
|
| 5595 |
anupam.sin |
304 |
try:
|
|
|
305 |
return [estimate[-1] for estimate in estimateList]
|
|
|
306 |
finally:
|
|
|
307 |
close_session()
|
| 5555 |
rajveer |
308 |
|
| 5595 |
anupam.sin |
309 |
def getAllPickupStores(self):
|
|
|
310 |
try:
|
|
|
311 |
return [to_t_pickup_store(pickup_store) for pickup_store in get_all_pickup_stores()]
|
|
|
312 |
finally:
|
|
|
313 |
close_session()
|
| 5555 |
rajveer |
314 |
|
|
|
315 |
def getPickupStore(self, storeId):
|
|
|
316 |
"""
|
|
|
317 |
Parameters:
|
|
|
318 |
- storeId
|
|
|
319 |
"""
|
| 5595 |
anupam.sin |
320 |
try:
|
|
|
321 |
storeToReturn = to_t_pickup_store(get_pickup_store(storeId))
|
|
|
322 |
return storeToReturn
|
|
|
323 |
finally:
|
| 5719 |
rajveer |
324 |
close_session()
|
|
|
325 |
|
|
|
326 |
def getPickupStoreByHotspotId(self, hotspotId):
|
|
|
327 |
"""
|
|
|
328 |
Parameters:
|
|
|
329 |
- hotspotId
|
|
|
330 |
"""
|
|
|
331 |
try:
|
|
|
332 |
storeToReturn = to_t_pickup_store(get_pickup_store_by_hotspot_id(hotspotId))
|
|
|
333 |
return storeToReturn
|
|
|
334 |
finally:
|
| 6322 |
amar.kumar |
335 |
close_session()
|
| 6524 |
rajveer |
336 |
def addPincode(self, providerId, pincode, destCode, exp, cod, stationType, otgAvailable):
|
| 6322 |
amar.kumar |
337 |
try:
|
| 6524 |
rajveer |
338 |
add_pincode(providerId, pincode, destCode, exp, cod, stationType, otgAvailable)
|
| 6322 |
amar.kumar |
339 |
finally:
|
|
|
340 |
close_session()
|
| 6524 |
rajveer |
341 |
def updatePincode(self, providerId, pincode, exp, cod, otgAvailable):
|
| 6322 |
amar.kumar |
342 |
try:
|
| 6524 |
rajveer |
343 |
update_pincode(providerId, pincode, exp, cod, otgAvailable)
|
| 6322 |
amar.kumar |
344 |
finally:
|
| 5595 |
anupam.sin |
345 |
close_session()
|