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