| 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, \
|
| 4439 |
rajveer |
10 |
PublicHolidays, ServiceableLocationDetails, DestinationProviderAllocation
|
| 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 = {}
|
| 4439 |
rajveer |
23 |
destination_provider_allocation_cache = {}
|
| 5243 |
rajveer |
24 |
ncr_pincodes = ['110001','110002','110003','110004','110005','110006','110007','110008','110009','110010','110011','110012','110013','110014','110015',\
|
|
|
25 |
'110016','110017','110018','110019','110020','110021','110022','110023','110024','110025','110026','110027','110028','110029','110030',\
|
|
|
26 |
'110031','110032','110033','110034','110035','110038','110041','110044','110045','110046','110047','110048','110049','110051','110052',\
|
|
|
27 |
'110053','110054','110055','110056','110057','110058','110059','110060','110061','110062','110063','110064','110065','110066','110067',\
|
|
|
28 |
'110068','110070','110071','110074','110075','110076','110078','110081','110082','110083','110084','110085','110086','110087','110088',\
|
|
|
29 |
'110089','110091','110092','110093','110094','110095','110096','110101','110103','110104','110105','110106','110107','110108','110109',\
|
|
|
30 |
'110110','110112','110113','110114','110115','110116','110117','110118','110119','110120','110122','110124','110125','110301','110302',\
|
|
|
31 |
'110501','110502','110503','110504','110505','110510','110511','110512','110601','110602','110603','110604','110605','110606','110607',\
|
|
|
32 |
'110608','110609','122001','122002','122003','122005','122006','122008','122009','122010','122015','122017','201010','201011','201012',\
|
|
|
33 |
'201014','201301','201302','201303','201304','201305','201306','201307','201309','201310','201311']
|
| 4866 |
rajveer |
34 |
|
| 3218 |
rajveer |
35 |
'''
|
|
|
36 |
This class is for only data transfer. Never used outside this module.
|
|
|
37 |
'''
|
|
|
38 |
class _DeliveryEstimateObject:
|
| 4866 |
rajveer |
39 |
def __init__(self, delivery_time, reliability, provider_id, warehouse_location, codAllowed):
|
| 3218 |
rajveer |
40 |
self.delivery_time = delivery_time
|
|
|
41 |
self.reliability = reliability
|
|
|
42 |
self.provider_id = provider_id
|
| 4866 |
rajveer |
43 |
self.warehouse_location = warehouse_location
|
|
|
44 |
self.codAllowed = codAllowed
|
| 3218 |
rajveer |
45 |
|
| 3187 |
rajveer |
46 |
def initialize(dbname="logistics", db_hostname="localhost"):
|
| 442 |
rajveer |
47 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
48 |
DataService.initialize(dbname, db_hostname)
|
| 3218 |
rajveer |
49 |
print "Starting cache population at: " + str(datetime.datetime.now())
|
| 3217 |
rajveer |
50 |
__cache_warehouse_allocation_table()
|
|
|
51 |
__cache_serviceable_location_details_table()
|
|
|
52 |
__cache_delivery_estimate_table()
|
| 4439 |
rajveer |
53 |
__cache_destination_provider_allocation_table()
|
| 3217 |
rajveer |
54 |
close_session()
|
| 3218 |
rajveer |
55 |
print "Done cache population at: " + str(datetime.datetime.now())
|
| 442 |
rajveer |
56 |
|
| 3217 |
rajveer |
57 |
def __cache_delivery_estimate_table():
|
|
|
58 |
delivery_estimates = DeliveryEstimate.query.all()
|
|
|
59 |
for delivery_estimate in delivery_estimates:
|
|
|
60 |
delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
|
|
|
61 |
=delivery_estimate.delivery_time, delivery_estimate.reliability
|
|
|
62 |
|
|
|
63 |
def __cache_serviceable_location_details_table():
|
|
|
64 |
serviceable_locations = ServiceableLocationDetails.query.all()
|
|
|
65 |
for serviceable_location in serviceable_locations:
|
|
|
66 |
try:
|
|
|
67 |
provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
|
|
|
68 |
except:
|
|
|
69 |
provider_pincodes = {}
|
|
|
70 |
serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes
|
|
|
71 |
provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.station_type
|
|
|
72 |
|
|
|
73 |
def __cache_warehouse_allocation_table():
|
|
|
74 |
warehouse_allocations = WarehouseAllocation.query.all()
|
|
|
75 |
for warehouse_allocation in warehouse_allocations:
|
|
|
76 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
|
|
77 |
|
| 4439 |
rajveer |
78 |
def __cache_destination_provider_allocation_table():
|
|
|
79 |
destiontion_providers = DestinationProviderAllocation.query.all()
|
|
|
80 |
for destiontion_provider in destiontion_providers:
|
|
|
81 |
destination_provider_allocation_cache[destiontion_provider.destination_pin] = destiontion_provider.provider_less_amount.id, destiontion_provider.provider_more_amount.id
|
|
|
82 |
|
| 644 |
chandransh |
83 |
def get_provider(provider_id):
|
| 766 |
rajveer |
84 |
provider = Provider.get_by(id=provider_id)
|
|
|
85 |
return provider
|
| 644 |
chandransh |
86 |
|
|
|
87 |
def get_providers():
|
| 766 |
rajveer |
88 |
providers = Provider.query.all()
|
|
|
89 |
return providers
|
| 644 |
chandransh |
90 |
|
| 3064 |
chandransh |
91 |
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
|
| 3355 |
chandransh |
92 |
logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
|
| 2341 |
chandransh |
93 |
if warehouse_location is None:
|
|
|
94 |
try:
|
| 3217 |
rajveer |
95 |
warehouse_location = warehouse_allocation_cache[destination_pin]
|
| 2341 |
chandransh |
96 |
except:
|
|
|
97 |
print "Unexpected error:", sys.exc_info()[0]
|
| 3064 |
chandransh |
98 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
| 1504 |
ankur.sing |
99 |
|
| 4866 |
rajveer |
100 |
provider_id, codAllowed = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
|
| 3064 |
chandransh |
101 |
|
| 3217 |
rajveer |
102 |
if not provider_id:
|
| 1504 |
ankur.sing |
103 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
|
|
104 |
", and warehouse location: " + str(warehouse_location))
|
| 644 |
chandransh |
105 |
try:
|
| 3218 |
rajveer |
106 |
delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
|
|
|
107 |
reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
|
| 4866 |
rajveer |
108 |
delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location, codAllowed)
|
| 3218 |
rajveer |
109 |
return delivery_estimate
|
| 1504 |
ankur.sing |
110 |
except Exception as ex:
|
|
|
111 |
print ex
|
| 644 |
chandransh |
112 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 3150 |
rajveer |
113 |
|
| 4439 |
rajveer |
114 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
|
| 4866 |
rajveer |
115 |
if ncr_pincodes.__contains__(destination_pin):
|
|
|
116 |
if item_selling_price <= 20000:
|
| 5243 |
rajveer |
117 |
return 3, True
|
| 4866 |
rajveer |
118 |
if serviceable_location_cache.get(1).has_key(destination_pin):
|
|
|
119 |
dest_code, exp, iscod, station_type = serviceable_location_cache.get(1).get(destination_pin)
|
|
|
120 |
iscod = iscod and item_selling_price <= 10000
|
|
|
121 |
return 1, iscod
|
|
|
122 |
else:
|
|
|
123 |
return None, False
|
|
|
124 |
'''
|
| 3217 |
rajveer |
125 |
for provider_id, value in serviceable_location_cache.iteritems():
|
|
|
126 |
try:
|
|
|
127 |
dest_code, exp, cod, station_type = value[destination_pin]
|
|
|
128 |
except:
|
|
|
129 |
print "Unexpected error:", sys.exc_info()[0]
|
|
|
130 |
continue
|
|
|
131 |
if type == DeliveryType.PREPAID and exp:
|
| 4439 |
rajveer |
132 |
ret_provider_list.append(provider_id)
|
| 3217 |
rajveer |
133 |
if type == DeliveryType.COD and cod:
|
| 4439 |
rajveer |
134 |
ret_provider_list.append(provider_id)
|
| 4803 |
rajveer |
135 |
#FIXME As per ticket location from where only Aramex is servicing, should not be given to Aramex.
|
| 4439 |
rajveer |
136 |
if len(ret_provider_list) == 1:
|
| 4803 |
rajveer |
137 |
if ret_provider_list.__contains__(1):
|
|
|
138 |
return ret_provider_list[0]
|
| 4804 |
rajveer |
139 |
else:
|
| 4803 |
rajveer |
140 |
return None
|
| 4439 |
rajveer |
141 |
elif len(ret_provider_list) == 0:
|
|
|
142 |
return None
|
|
|
143 |
elif destination_provider_allocation_cache.has_key(destination_pin):
|
| 4711 |
rajveer |
144 |
if item_selling_price < 10000:
|
| 4439 |
rajveer |
145 |
return destination_provider_allocation_cache.get(destination_pin)[0]
|
|
|
146 |
else:
|
|
|
147 |
return destination_provider_allocation_cache.get(destination_pin)[1]
|
|
|
148 |
else:
|
| 4450 |
rajveer |
149 |
return ret_provider_list[0]
|
| 4866 |
rajveer |
150 |
'''
|
|
|
151 |
|
| 644 |
chandransh |
152 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 442 |
rajveer |
153 |
for number in numbers:
|
| 644 |
chandransh |
154 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 444 |
rajveer |
155 |
try:
|
|
|
156 |
query.one()
|
|
|
157 |
except:
|
| 644 |
chandransh |
158 |
awb = Awb()
|
| 444 |
rajveer |
159 |
awb.awb_number = number
|
| 644 |
chandransh |
160 |
awb.provider_id = provider_id
|
| 444 |
rajveer |
161 |
awb.is_available = True
|
| 644 |
chandransh |
162 |
awb.type = type
|
|
|
163 |
session.commit()
|
| 442 |
rajveer |
164 |
|
| 444 |
rajveer |
165 |
def set_AWB_as_used(awb_number):
|
| 644 |
chandransh |
166 |
query = Awb.query.filter_by(awb_number = awb_number)
|
| 444 |
rajveer |
167 |
awb = query.one()
|
|
|
168 |
awb.is_available=False
|
|
|
169 |
session.commit()
|
|
|
170 |
|
| 3044 |
chandransh |
171 |
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
|
| 2342 |
chandransh |
172 |
query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True) #check the provider thing
|
| 3044 |
chandransh |
173 |
if type == DeliveryType.PREPAID:
|
|
|
174 |
query = query.filter_by(type="Prepaid")
|
|
|
175 |
if type == DeliveryType.COD:
|
|
|
176 |
query = query.filter_by(type="COD")
|
|
|
177 |
|
| 442 |
rajveer |
178 |
try:
|
| 644 |
chandransh |
179 |
awb = query.first()
|
|
|
180 |
awb.is_available = False
|
|
|
181 |
session.commit()
|
| 444 |
rajveer |
182 |
return awb.awb_number
|
| 442 |
rajveer |
183 |
except:
|
| 644 |
chandransh |
184 |
raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
|
| 1137 |
chandransh |
185 |
|
| 3103 |
chandransh |
186 |
def get_free_awb_count(provider_id, type):
|
|
|
187 |
count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
|
| 1137 |
chandransh |
188 |
if count == None:
|
|
|
189 |
count = 0
|
|
|
190 |
return count
|
| 442 |
rajveer |
191 |
|
| 644 |
chandransh |
192 |
def get_shipment_info(awb_number, provider_id):
|
|
|
193 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
|
|
194 |
query = AwbUpdate.query.filter_by(awb = awb)
|
| 766 |
rajveer |
195 |
info = query.all()
|
|
|
196 |
return info
|
|
|
197 |
|
| 1730 |
ankur.sing |
198 |
def get_holidays(start_date, end_date):
|
|
|
199 |
query = PublicHolidays.query
|
|
|
200 |
if start_date != -1:
|
|
|
201 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
|
|
202 |
if end_date != -1:
|
|
|
203 |
query = query.filter(PublicHolidays.date <= to_py_date(end_date))
|
|
|
204 |
holidays = query.all()
|
|
|
205 |
holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays]
|
|
|
206 |
return holiday_dates
|
|
|
207 |
|
| 766 |
rajveer |
208 |
def close_session():
|
|
|
209 |
if session.is_active:
|
|
|
210 |
print "session is active. closing it."
|
| 2823 |
chandransh |
211 |
session.close()
|
| 3376 |
rajveer |
212 |
|
|
|
213 |
def is_alive():
|
|
|
214 |
try:
|
|
|
215 |
session.query(Awb.id).limit(1).one()
|
|
|
216 |
return True
|
|
|
217 |
except:
|
|
|
218 |
return False
|