| 412 |
ashish |
1 |
'''
|
|
|
2 |
Created on 05-Aug-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
| 644 |
chandransh |
6 |
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
|
|
|
7 |
LogisticsServiceException
|
|
|
8 |
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
|
| 675 |
chandransh |
9 |
get_shipment_info, initialize, get_logistics_estimation, get_provider,\
|
|
|
10 |
get_providers
|
| 669 |
chandransh |
11 |
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
|
|
|
12 |
to_t_provider
|
| 494 |
rajveer |
13 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
14 |
from shop2020.config.client.ConfigClient import ConfigClient
|
|
|
15 |
import datetime
|
| 644 |
chandransh |
16 |
import math
|
| 472 |
rajveer |
17 |
|
| 412 |
ashish |
18 |
class LogisticsServiceHandler:
|
|
|
19 |
|
| 442 |
rajveer |
20 |
def __init__(self):
|
|
|
21 |
initialize()
|
| 644 |
chandransh |
22 |
config_client = ConfigClient()
|
|
|
23 |
self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
|
|
|
24 |
self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
|
|
|
25 |
self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
|
| 483 |
rajveer |
26 |
|
| 669 |
chandransh |
27 |
def getProvider(self, providerId):
|
| 675 |
chandransh |
28 |
"""
|
|
|
29 |
Returns a provider for a given provider ID. Throws an exception if none found.
|
|
|
30 |
|
|
|
31 |
Parameters:
|
|
|
32 |
- providerId
|
|
|
33 |
"""
|
|
|
34 |
return to_t_provider(get_provider(providerId))
|
| 669 |
chandransh |
35 |
|
| 675 |
chandransh |
36 |
def getAllProviders(self, ):
|
|
|
37 |
"""
|
|
|
38 |
Returns a list containing all the providers.
|
|
|
39 |
"""
|
|
|
40 |
return [to_t_provider(provider) for provider in get_providers()]
|
|
|
41 |
|
| 644 |
chandransh |
42 |
def getLogisticsInfo(self, destination_pincode, itemId):
|
| 483 |
rajveer |
43 |
"""
|
|
|
44 |
Parameters:
|
|
|
45 |
- destination_pincode
|
| 716 |
rajveer |
46 |
- item_id
|
| 483 |
rajveer |
47 |
"""
|
| 644 |
chandransh |
48 |
logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
|
|
|
49 |
logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
|
| 483 |
rajveer |
50 |
return logistics_info
|
| 412 |
ashish |
51 |
|
|
|
52 |
def getEmptyAWB(self, provider_id):
|
|
|
53 |
"""
|
|
|
54 |
Parameters:
|
|
|
55 |
- provider_id
|
|
|
56 |
"""
|
| 442 |
rajveer |
57 |
return get_empty_AWB(provider_id)
|
|
|
58 |
|
| 644 |
chandransh |
59 |
def getShipmentInfo(self, awb, providerId):
|
| 412 |
ashish |
60 |
"""
|
|
|
61 |
Parameters:
|
|
|
62 |
- awb
|
|
|
63 |
"""
|
| 644 |
chandransh |
64 |
awb_updates = get_shipment_info(awb, providerId)
|
|
|
65 |
t_updates = []
|
|
|
66 |
for update in awb_updates:
|
|
|
67 |
t_updates.append(to_t_awbupdate(update))
|
|
|
68 |
return t_updates
|
| 412 |
ashish |
69 |
|
| 644 |
chandransh |
70 |
def getLogisticsEstimation(self, itemId, destination_pin):
|
| 472 |
rajveer |
71 |
"""
|
|
|
72 |
Parameters:
|
|
|
73 |
- itemId
|
|
|
74 |
- destination_pin
|
|
|
75 |
"""
|
| 644 |
chandransh |
76 |
delivery_estimate = get_logistics_estimation(destination_pin)
|
|
|
77 |
delivery_time = 24*delivery_estimate.delivery_time
|
|
|
78 |
if self.cutoff_time <= datetime.datetime.now().hour:
|
| 494 |
rajveer |
79 |
delivery_time = delivery_time + 24
|
| 644 |
chandransh |
80 |
warehouse_loc = delivery_estimate.warehouse_location
|
|
|
81 |
try:
|
|
|
82 |
client = InventoryClient().get_client()
|
|
|
83 |
warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
|
|
|
84 |
except:
|
|
|
85 |
raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
|
|
|
86 |
if items_in_inventory < self.stock_threshold :
|
|
|
87 |
delivery_time = delivery_time + self.time_to_producre
|
|
|
88 |
delivery_time = int(math.ceil(delivery_time/24.0))
|
| 472 |
rajveer |
89 |
|
| 644 |
chandransh |
90 |
logistics_info = LogisticsInfo()
|
|
|
91 |
logistics_info.deliveryTime = delivery_time
|
|
|
92 |
logistics_info.providerId = delivery_estimate.provider_id
|
|
|
93 |
logistics_info.warehouseId = warehouse_id
|
| 477 |
rajveer |
94 |
|
| 644 |
chandransh |
95 |
return logistics_info
|