| 442 |
rajveer |
1 |
'''
|
|
|
2 |
Created on 13-Sep-2010
|
|
|
3 |
|
|
|
4 |
@author: rajveer
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
from shop2020.logistics.service.impl import DataService
|
| 644 |
chandransh |
8 |
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
|
| 3218 |
rajveer |
9 |
DeliveryEstimate, WarehouseAllocation, \
|
| 3064 |
chandransh |
10 |
PublicHolidays, ServiceableLocationDetails
|
| 3044 |
chandransh |
11 |
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
|
|
|
12 |
DeliveryType
|
| 442 |
rajveer |
13 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
|
|
14 |
from elixir import session
|
| 1730 |
ankur.sing |
15 |
import datetime, time
|
| 1504 |
ankur.sing |
16 |
import sys
|
| 3355 |
chandransh |
17 |
import logging
|
|
|
18 |
logging.basicConfig(level=logging.DEBUG)
|
| 442 |
rajveer |
19 |
|
| 3217 |
rajveer |
20 |
warehouse_allocation_cache = {}
|
|
|
21 |
serviceable_location_cache = {}
|
|
|
22 |
delivery_estimate_cache = {}
|
|
|
23 |
|
| 3218 |
rajveer |
24 |
'''
|
|
|
25 |
This class is for only data transfer. Never used outside this module.
|
|
|
26 |
'''
|
|
|
27 |
class _DeliveryEstimateObject:
|
|
|
28 |
def __init__(self, delivery_time, reliability, provider_id, warehouse_location):
|
|
|
29 |
self.delivery_time = delivery_time
|
|
|
30 |
self.reliability = reliability
|
|
|
31 |
self.provider_id = provider_id
|
|
|
32 |
self.warehouse_location = warehouse_location
|
|
|
33 |
|
| 3187 |
rajveer |
34 |
def initialize(dbname="logistics", db_hostname="localhost"):
|
| 442 |
rajveer |
35 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
36 |
DataService.initialize(dbname, db_hostname)
|
| 3218 |
rajveer |
37 |
print "Starting cache population at: " + str(datetime.datetime.now())
|
| 3217 |
rajveer |
38 |
__cache_warehouse_allocation_table()
|
|
|
39 |
__cache_serviceable_location_details_table()
|
|
|
40 |
__cache_delivery_estimate_table()
|
|
|
41 |
close_session()
|
| 3218 |
rajveer |
42 |
print "Done cache population at: " + str(datetime.datetime.now())
|
| 442 |
rajveer |
43 |
|
| 3217 |
rajveer |
44 |
def __cache_delivery_estimate_table():
|
|
|
45 |
delivery_estimates = DeliveryEstimate.query.all()
|
|
|
46 |
for delivery_estimate in delivery_estimates:
|
|
|
47 |
delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
|
|
|
48 |
=delivery_estimate.delivery_time, delivery_estimate.reliability
|
|
|
49 |
|
|
|
50 |
def __cache_serviceable_location_details_table():
|
|
|
51 |
serviceable_locations = ServiceableLocationDetails.query.all()
|
|
|
52 |
for serviceable_location in serviceable_locations:
|
|
|
53 |
try:
|
|
|
54 |
provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
|
|
|
55 |
except:
|
|
|
56 |
provider_pincodes = {}
|
|
|
57 |
serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes
|
|
|
58 |
provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.station_type
|
|
|
59 |
|
|
|
60 |
def __cache_warehouse_allocation_table():
|
|
|
61 |
warehouse_allocations = WarehouseAllocation.query.all()
|
|
|
62 |
for warehouse_allocation in warehouse_allocations:
|
|
|
63 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
|
|
64 |
|
| 644 |
chandransh |
65 |
def get_provider(provider_id):
|
| 766 |
rajveer |
66 |
provider = Provider.get_by(id=provider_id)
|
|
|
67 |
return provider
|
| 644 |
chandransh |
68 |
|
|
|
69 |
def get_providers():
|
| 766 |
rajveer |
70 |
providers = Provider.query.all()
|
|
|
71 |
return providers
|
| 644 |
chandransh |
72 |
|
| 3064 |
chandransh |
73 |
def is_cod_allowed(destination_pin):
|
| 3217 |
rajveer |
74 |
cod = False
|
| 3064 |
chandransh |
75 |
try:
|
| 3217 |
rajveer |
76 |
for value in serviceable_location_cache.itervalues():
|
| 3218 |
rajveer |
77 |
cod = cod or value[destination_pin][2]
|
| 3064 |
chandransh |
78 |
except Exception as ex:
|
|
|
79 |
print "Unexpected error:", sys.exc_info()[0]
|
|
|
80 |
|
| 3217 |
rajveer |
81 |
return cod
|
| 3064 |
chandransh |
82 |
|
|
|
83 |
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
|
| 3355 |
chandransh |
84 |
logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
|
| 2341 |
chandransh |
85 |
if warehouse_location is None:
|
|
|
86 |
try:
|
| 3217 |
rajveer |
87 |
warehouse_location = warehouse_allocation_cache[destination_pin]
|
| 2341 |
chandransh |
88 |
except:
|
|
|
89 |
print "Unexpected error:", sys.exc_info()[0]
|
| 3064 |
chandransh |
90 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
| 1504 |
ankur.sing |
91 |
|
| 3217 |
rajveer |
92 |
provider_id = __get_logistics_provider_for_destination_pincode(type, destination_pin)
|
| 3064 |
chandransh |
93 |
|
| 3217 |
rajveer |
94 |
if not provider_id:
|
| 1504 |
ankur.sing |
95 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
|
|
96 |
", and warehouse location: " + str(warehouse_location))
|
| 3064 |
chandransh |
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))
|
| 1504 |
ankur.sing |
110 |
|
| 644 |
chandransh |
111 |
try:
|
| 3218 |
rajveer |
112 |
delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
|
|
|
113 |
reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
|
|
|
114 |
delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location)
|
|
|
115 |
return delivery_estimate
|
| 1504 |
ankur.sing |
116 |
except Exception as ex:
|
|
|
117 |
print ex
|
| 644 |
chandransh |
118 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 3150 |
rajveer |
119 |
|
|
|
120 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin):
|
| 3217 |
rajveer |
121 |
for provider_id, value in serviceable_location_cache.iteritems():
|
|
|
122 |
try:
|
|
|
123 |
dest_code, exp, cod, station_type = value[destination_pin]
|
|
|
124 |
except:
|
|
|
125 |
print "Unexpected error:", sys.exc_info()[0]
|
|
|
126 |
continue
|
|
|
127 |
if type == DeliveryType.PREPAID and exp:
|
|
|
128 |
return provider_id
|
|
|
129 |
if type == DeliveryType.COD and cod:
|
|
|
130 |
return provider_id
|
|
|
131 |
return None
|
| 766 |
rajveer |
132 |
|
| 644 |
chandransh |
133 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 442 |
rajveer |
134 |
for number in numbers:
|
| 644 |
chandransh |
135 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 444 |
rajveer |
136 |
try:
|
|
|
137 |
query.one()
|
|
|
138 |
except:
|
| 644 |
chandransh |
139 |
awb = Awb()
|
| 444 |
rajveer |
140 |
awb.awb_number = number
|
| 644 |
chandransh |
141 |
awb.provider_id = provider_id
|
| 444 |
rajveer |
142 |
awb.is_available = True
|
| 644 |
chandransh |
143 |
awb.type = type
|
|
|
144 |
session.commit()
|
| 442 |
rajveer |
145 |
|
| 444 |
rajveer |
146 |
def set_AWB_as_used(awb_number):
|
| 644 |
chandransh |
147 |
query = Awb.query.filter_by(awb_number = awb_number)
|
| 444 |
rajveer |
148 |
awb = query.one()
|
|
|
149 |
awb.is_available=False
|
|
|
150 |
session.commit()
|
|
|
151 |
|
| 3044 |
chandransh |
152 |
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
|
| 2342 |
chandransh |
153 |
query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True) #check the provider thing
|
| 3044 |
chandransh |
154 |
if type == DeliveryType.PREPAID:
|
|
|
155 |
query = query.filter_by(type="Prepaid")
|
|
|
156 |
if type == DeliveryType.COD:
|
|
|
157 |
query = query.filter_by(type="COD")
|
|
|
158 |
|
| 442 |
rajveer |
159 |
try:
|
| 644 |
chandransh |
160 |
awb = query.first()
|
|
|
161 |
awb.is_available = False
|
|
|
162 |
session.commit()
|
| 444 |
rajveer |
163 |
return awb.awb_number
|
| 442 |
rajveer |
164 |
except:
|
| 644 |
chandransh |
165 |
raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
|
| 1137 |
chandransh |
166 |
|
| 3103 |
chandransh |
167 |
def get_free_awb_count(provider_id, type):
|
|
|
168 |
count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
|
| 1137 |
chandransh |
169 |
if count == None:
|
|
|
170 |
count = 0
|
|
|
171 |
return count
|
| 442 |
rajveer |
172 |
|
| 644 |
chandransh |
173 |
def get_shipment_info(awb_number, provider_id):
|
|
|
174 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
|
|
175 |
query = AwbUpdate.query.filter_by(awb = awb)
|
| 766 |
rajveer |
176 |
info = query.all()
|
|
|
177 |
return info
|
|
|
178 |
|
| 1730 |
ankur.sing |
179 |
def get_holidays(start_date, end_date):
|
|
|
180 |
query = PublicHolidays.query
|
|
|
181 |
if start_date != -1:
|
|
|
182 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
|
|
183 |
if end_date != -1:
|
|
|
184 |
query = query.filter(PublicHolidays.date <= to_py_date(end_date))
|
|
|
185 |
holidays = query.all()
|
|
|
186 |
holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays]
|
|
|
187 |
return holiday_dates
|
|
|
188 |
|
| 4386 |
anupam.sin |
189 |
def get_awb_for_number(awb_num):
|
|
|
190 |
awb = Awb.get_by(awb_number=awb_num)
|
|
|
191 |
if not awb:
|
|
|
192 |
raise LogisticsServiceException(108, "no such awb")
|
|
|
193 |
|
|
|
194 |
return awb
|
|
|
195 |
|
| 766 |
rajveer |
196 |
def close_session():
|
|
|
197 |
if session.is_active:
|
|
|
198 |
print "session is active. closing it."
|
| 2823 |
chandransh |
199 |
session.close()
|
| 3376 |
rajveer |
200 |
|
|
|
201 |
def is_alive():
|
|
|
202 |
try:
|
|
|
203 |
session.query(Awb.id).limit(1).one()
|
|
|
204 |
return True
|
|
|
205 |
except:
|
|
|
206 |
return False
|