| Line 5... |
Line 5... |
| 5 |
'''
|
5 |
'''
|
| 6 |
|
6 |
|
| 7 |
from shop2020.logistics.service.impl import DataService
|
7 |
from shop2020.logistics.service.impl import DataService
|
| 8 |
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
|
8 |
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
|
| 9 |
DeliveryEstimate, WarehouseAllocation, \
|
9 |
DeliveryEstimate, WarehouseAllocation, \
|
| 10 |
PublicHolidays, ServiceableLocationDetails
|
10 |
PublicHolidays, ServiceableLocationDetails, DestinationProviderAllocation
|
| 11 |
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
|
11 |
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
|
| 12 |
DeliveryType
|
12 |
DeliveryType
|
| 13 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
13 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
| 14 |
from elixir import session
|
14 |
from elixir import session
|
| 15 |
import datetime, time
|
15 |
import datetime, time
|
| Line 18... |
Line 18... |
| 18 |
logging.basicConfig(level=logging.DEBUG)
|
18 |
logging.basicConfig(level=logging.DEBUG)
|
| 19 |
|
19 |
|
| 20 |
warehouse_allocation_cache = {}
|
20 |
warehouse_allocation_cache = {}
|
| 21 |
serviceable_location_cache = {}
|
21 |
serviceable_location_cache = {}
|
| 22 |
delivery_estimate_cache = {}
|
22 |
delivery_estimate_cache = {}
|
| 23 |
|
- |
|
| - |
|
23 |
destination_provider_allocation_cache = {}
|
| 24 |
'''
|
24 |
'''
|
| 25 |
This class is for only data transfer. Never used outside this module.
|
25 |
This class is for only data transfer. Never used outside this module.
|
| 26 |
'''
|
26 |
'''
|
| 27 |
class _DeliveryEstimateObject:
|
27 |
class _DeliveryEstimateObject:
|
| 28 |
def __init__(self, delivery_time, reliability, provider_id, warehouse_location):
|
28 |
def __init__(self, delivery_time, reliability, provider_id, warehouse_location):
|
| Line 36... |
Line 36... |
| 36 |
DataService.initialize(dbname, db_hostname)
|
36 |
DataService.initialize(dbname, db_hostname)
|
| 37 |
print "Starting cache population at: " + str(datetime.datetime.now())
|
37 |
print "Starting cache population at: " + str(datetime.datetime.now())
|
| 38 |
__cache_warehouse_allocation_table()
|
38 |
__cache_warehouse_allocation_table()
|
| 39 |
__cache_serviceable_location_details_table()
|
39 |
__cache_serviceable_location_details_table()
|
| 40 |
__cache_delivery_estimate_table()
|
40 |
__cache_delivery_estimate_table()
|
| - |
|
41 |
__cache_destination_provider_allocation_table()
|
| 41 |
close_session()
|
42 |
close_session()
|
| 42 |
print "Done cache population at: " + str(datetime.datetime.now())
|
43 |
print "Done cache population at: " + str(datetime.datetime.now())
|
| 43 |
|
44 |
|
| 44 |
def __cache_delivery_estimate_table():
|
45 |
def __cache_delivery_estimate_table():
|
| 45 |
delivery_estimates = DeliveryEstimate.query.all()
|
46 |
delivery_estimates = DeliveryEstimate.query.all()
|
| Line 60... |
Line 61... |
| 60 |
def __cache_warehouse_allocation_table():
|
61 |
def __cache_warehouse_allocation_table():
|
| 61 |
warehouse_allocations = WarehouseAllocation.query.all()
|
62 |
warehouse_allocations = WarehouseAllocation.query.all()
|
| 62 |
for warehouse_allocation in warehouse_allocations:
|
63 |
for warehouse_allocation in warehouse_allocations:
|
| 63 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
64 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
| 64 |
|
65 |
|
| - |
|
66 |
def __cache_destination_provider_allocation_table():
|
| - |
|
67 |
destiontion_providers = DestinationProviderAllocation.query.all()
|
| - |
|
68 |
for destiontion_provider in destiontion_providers:
|
| - |
|
69 |
destination_provider_allocation_cache[destiontion_provider.destination_pin] = destiontion_provider.provider_less_amount.id, destiontion_provider.provider_more_amount.id
|
| - |
|
70 |
|
| 65 |
def get_provider(provider_id):
|
71 |
def get_provider(provider_id):
|
| 66 |
provider = Provider.get_by(id=provider_id)
|
72 |
provider = Provider.get_by(id=provider_id)
|
| 67 |
return provider
|
73 |
return provider
|
| 68 |
|
74 |
|
| 69 |
def get_providers():
|
75 |
def get_providers():
|
| Line 87... |
Line 93... |
| 87 |
warehouse_location = warehouse_allocation_cache[destination_pin]
|
93 |
warehouse_location = warehouse_allocation_cache[destination_pin]
|
| 88 |
except:
|
94 |
except:
|
| 89 |
print "Unexpected error:", sys.exc_info()[0]
|
95 |
print "Unexpected error:", sys.exc_info()[0]
|
| 90 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
96 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
| 91 |
|
97 |
|
| 92 |
provider_id = __get_logistics_provider_for_destination_pincode(type, destination_pin)
|
98 |
provider_id = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
|
| 93 |
|
99 |
|
| 94 |
if not provider_id:
|
100 |
if not provider_id:
|
| 95 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
101 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
| 96 |
", and warehouse location: " + str(warehouse_location))
|
102 |
", and warehouse location: " + str(warehouse_location))
|
| 97 |
|
- |
|
| 98 |
# query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
|
- |
|
| 99 |
# query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
|
- |
|
| 100 |
# try:
|
- |
|
| 101 |
# if (item_selling_price <= 5000):
|
- |
|
| 102 |
# provider = query_provider.one().provider_less_amount
|
- |
|
| 103 |
# else:
|
- |
|
| 104 |
# provider = query_provider.one().provider_more_amount
|
- |
|
| 105 |
# except Exception as ex:
|
- |
|
| 106 |
# print ex
|
- |
|
| 107 |
# print "Unexpected error:", sys.exc_info()[0]
|
- |
|
| 108 |
# raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
- |
|
| 109 |
# ", and warehouse location: " + str(warehouse_location))
|
- |
|
| 110 |
|
- |
|
| 111 |
try:
|
103 |
try:
|
| 112 |
delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
|
104 |
delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
|
| 113 |
reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
|
105 |
reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
|
| 114 |
delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location)
|
106 |
delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location)
|
| 115 |
return delivery_estimate
|
107 |
return delivery_estimate
|
| 116 |
except Exception as ex:
|
108 |
except Exception as ex:
|
| 117 |
print ex
|
109 |
print ex
|
| 118 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
110 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 119 |
|
111 |
|
| 120 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin):
|
112 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
|
| - |
|
113 |
ret_provider_list = []
|
| 121 |
for provider_id, value in serviceable_location_cache.iteritems():
|
114 |
for provider_id, value in serviceable_location_cache.iteritems():
|
| 122 |
try:
|
115 |
try:
|
| 123 |
dest_code, exp, cod, station_type = value[destination_pin]
|
116 |
dest_code, exp, cod, station_type = value[destination_pin]
|
| 124 |
except:
|
117 |
except:
|
| 125 |
print "Unexpected error:", sys.exc_info()[0]
|
118 |
print "Unexpected error:", sys.exc_info()[0]
|
| 126 |
continue
|
119 |
continue
|
| 127 |
if type == DeliveryType.PREPAID and exp:
|
120 |
if type == DeliveryType.PREPAID and exp:
|
| 128 |
return provider_id
|
121 |
ret_provider_list.append(provider_id)
|
| 129 |
if type == DeliveryType.COD and cod:
|
122 |
if type == DeliveryType.COD and cod:
|
| - |
|
123 |
ret_provider_list.append(provider_id)
|
| - |
|
124 |
if len(ret_provider_list) == 1:
|
| 130 |
return provider_id
|
125 |
return ret_provider_list[0]
|
| - |
|
126 |
elif len(ret_provider_list) == 0:
|
| 131 |
return None
|
127 |
return None
|
| - |
|
128 |
elif destination_provider_allocation_cache.has_key(destination_pin):
|
| - |
|
129 |
if item_selling_price < 10000:
|
| - |
|
130 |
return destination_provider_allocation_cache.get(destination_pin)[0]
|
| 132 |
|
131 |
else:
|
| - |
|
132 |
return destination_provider_allocation_cache.get(destination_pin)[1]
|
| - |
|
133 |
else:
|
| - |
|
134 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin))
|
| - |
|
135 |
|
| 133 |
def add_empty_AWBs(numbers, provider_id, type):
|
136 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 134 |
for number in numbers:
|
137 |
for number in numbers:
|
| 135 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
138 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 136 |
try:
|
139 |
try:
|
| 137 |
query.one()
|
140 |
query.one()
|