| 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, \
|
| 5555 |
rajveer |
10 |
PublicHolidays, ServiceableLocationDetails, PickupStore
|
| 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
|
| 5964 |
amar.kumar |
14 |
from sqlalchemy.sql import or_
|
| 442 |
rajveer |
15 |
from elixir import session
|
| 1730 |
ankur.sing |
16 |
import datetime, time
|
| 1504 |
ankur.sing |
17 |
import sys
|
| 3355 |
chandransh |
18 |
import logging
|
|
|
19 |
logging.basicConfig(level=logging.DEBUG)
|
| 442 |
rajveer |
20 |
|
| 3217 |
rajveer |
21 |
warehouse_allocation_cache = {}
|
|
|
22 |
serviceable_location_cache = {}
|
|
|
23 |
delivery_estimate_cache = {}
|
| 6370 |
rajveer |
24 |
|
| 3218 |
rajveer |
25 |
'''
|
|
|
26 |
This class is for only data transfer. Never used outside this module.
|
|
|
27 |
'''
|
|
|
28 |
class _DeliveryEstimateObject:
|
| 6537 |
rajveer |
29 |
def __init__(self, delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable):
|
| 3218 |
rajveer |
30 |
self.delivery_time = delivery_time
|
| 6537 |
rajveer |
31 |
self.delivery_delay = delivery_delay
|
| 3218 |
rajveer |
32 |
self.provider_id = provider_id
|
| 4866 |
rajveer |
33 |
self.codAllowed = codAllowed
|
| 6524 |
rajveer |
34 |
self.otgAvailable = otgAvailable
|
| 3218 |
rajveer |
35 |
|
| 3187 |
rajveer |
36 |
def initialize(dbname="logistics", db_hostname="localhost"):
|
| 442 |
rajveer |
37 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
38 |
DataService.initialize(dbname, db_hostname)
|
| 3218 |
rajveer |
39 |
print "Starting cache population at: " + str(datetime.datetime.now())
|
| 3217 |
rajveer |
40 |
__cache_warehouse_allocation_table()
|
|
|
41 |
__cache_serviceable_location_details_table()
|
|
|
42 |
__cache_delivery_estimate_table()
|
|
|
43 |
close_session()
|
| 3218 |
rajveer |
44 |
print "Done cache population at: " + str(datetime.datetime.now())
|
| 442 |
rajveer |
45 |
|
| 3217 |
rajveer |
46 |
def __cache_delivery_estimate_table():
|
|
|
47 |
delivery_estimates = DeliveryEstimate.query.all()
|
|
|
48 |
for delivery_estimate in delivery_estimates:
|
| 5692 |
rajveer |
49 |
delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id]\
|
| 6537 |
rajveer |
50 |
=delivery_estimate.delivery_time, delivery_estimate.delivery_delay
|
| 3217 |
rajveer |
51 |
|
|
|
52 |
def __cache_serviceable_location_details_table():
|
| 5964 |
amar.kumar |
53 |
serviceable_locations = ServiceableLocationDetails.query.filter(or_("exp!=0","cod!=0"))
|
| 3217 |
rajveer |
54 |
for serviceable_location in serviceable_locations:
|
|
|
55 |
try:
|
|
|
56 |
provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
|
|
|
57 |
except:
|
|
|
58 |
provider_pincodes = {}
|
|
|
59 |
serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes
|
| 6524 |
rajveer |
60 |
provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.otgAvailable
|
| 3217 |
rajveer |
61 |
|
|
|
62 |
def __cache_warehouse_allocation_table():
|
|
|
63 |
warehouse_allocations = WarehouseAllocation.query.all()
|
|
|
64 |
for warehouse_allocation in warehouse_allocations:
|
|
|
65 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
|
|
66 |
|
| 644 |
chandransh |
67 |
def get_provider(provider_id):
|
| 766 |
rajveer |
68 |
provider = Provider.get_by(id=provider_id)
|
|
|
69 |
return provider
|
| 644 |
chandransh |
70 |
|
|
|
71 |
def get_providers():
|
| 5387 |
rajveer |
72 |
providers = Provider.query.filter_by(isActive = 1).all()
|
| 766 |
rajveer |
73 |
return providers
|
| 644 |
chandransh |
74 |
|
| 6524 |
rajveer |
75 |
def get_logistics_estimation(destination_pin, item_selling_price):
|
| 5692 |
rajveer |
76 |
logging.info("Getting logistics estimation for pincode:" + destination_pin )
|
| 1504 |
ankur.sing |
77 |
|
| 6524 |
rajveer |
78 |
provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price)
|
| 3064 |
chandransh |
79 |
|
| 3217 |
rajveer |
80 |
if not provider_id:
|
| 5692 |
rajveer |
81 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin))
|
| 644 |
chandransh |
82 |
try:
|
| 5692 |
rajveer |
83 |
delivery_time = delivery_estimate_cache[destination_pin, provider_id][0]
|
| 6537 |
rajveer |
84 |
delivery_delay = delivery_estimate_cache[destination_pin, provider_id][1]
|
|
|
85 |
delivery_estimate = _DeliveryEstimateObject(delivery_time, delivery_delay, provider_id, codAllowed, otgAvailable)
|
| 3218 |
rajveer |
86 |
return delivery_estimate
|
| 1504 |
ankur.sing |
87 |
except Exception as ex:
|
|
|
88 |
print ex
|
| 644 |
chandransh |
89 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 3150 |
rajveer |
90 |
|
| 6524 |
rajveer |
91 |
def __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price):
|
| 7022 |
rajveer |
92 |
if item_selling_price >= 5000 and item_selling_price <= 10000 and serviceable_location_cache.get(6).has_key(destination_pin):
|
| 7021 |
rajveer |
93 |
dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(6).get(destination_pin)
|
|
|
94 |
iscod = iscod and item_selling_price <= 25000
|
|
|
95 |
otgAvailable = otgAvailable and item_selling_price >= 2000
|
|
|
96 |
return 6, iscod, otgAvailable
|
|
|
97 |
|
| 5692 |
rajveer |
98 |
if serviceable_location_cache.get(3).has_key(destination_pin):
|
| 6524 |
rajveer |
99 |
dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(3).get(destination_pin)
|
| 6527 |
rajveer |
100 |
iscod = iscod and item_selling_price <= 25000
|
| 6524 |
rajveer |
101 |
otgAvailable = otgAvailable and item_selling_price >= 2000
|
|
|
102 |
return 3, iscod, otgAvailable
|
| 6370 |
rajveer |
103 |
|
| 4866 |
rajveer |
104 |
if serviceable_location_cache.get(1).has_key(destination_pin):
|
| 6524 |
rajveer |
105 |
dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(1).get(destination_pin)
|
| 6237 |
rajveer |
106 |
iscod = iscod and item_selling_price <= 9600
|
| 6524 |
rajveer |
107 |
otgAvailable = otgAvailable and item_selling_price >= 2000
|
|
|
108 |
return 1, iscod, otgAvailable
|
| 4866 |
rajveer |
109 |
else:
|
| 6615 |
amar.kumar |
110 |
return None, False
|
| 5278 |
rajveer |
111 |
|
| 6017 |
amar.kumar |
112 |
def get_destination_code(providerId, pinCode):
|
|
|
113 |
serviceableLocationDetail = ServiceableLocationDetails.query.filter_by(provider_id = providerId, dest_pincode = pinCode).one()
|
|
|
114 |
return serviceableLocationDetail.dest_code
|
|
|
115 |
|
| 644 |
chandransh |
116 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 442 |
rajveer |
117 |
for number in numbers:
|
| 644 |
chandransh |
118 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 444 |
rajveer |
119 |
try:
|
|
|
120 |
query.one()
|
|
|
121 |
except:
|
| 644 |
chandransh |
122 |
awb = Awb()
|
| 444 |
rajveer |
123 |
awb.awb_number = number
|
| 644 |
chandransh |
124 |
awb.provider_id = provider_id
|
| 444 |
rajveer |
125 |
awb.is_available = True
|
| 644 |
chandransh |
126 |
awb.type = type
|
|
|
127 |
session.commit()
|
| 442 |
rajveer |
128 |
|
| 444 |
rajveer |
129 |
def set_AWB_as_used(awb_number):
|
| 644 |
chandransh |
130 |
query = Awb.query.filter_by(awb_number = awb_number)
|
| 444 |
rajveer |
131 |
awb = query.one()
|
|
|
132 |
awb.is_available=False
|
|
|
133 |
session.commit()
|
|
|
134 |
|
| 3044 |
chandransh |
135 |
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
|
| 2342 |
chandransh |
136 |
query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True) #check the provider thing
|
| 3044 |
chandransh |
137 |
if type == DeliveryType.PREPAID:
|
|
|
138 |
query = query.filter_by(type="Prepaid")
|
|
|
139 |
if type == DeliveryType.COD:
|
|
|
140 |
query = query.filter_by(type="COD")
|
|
|
141 |
|
| 442 |
rajveer |
142 |
try:
|
| 644 |
chandransh |
143 |
awb = query.first()
|
|
|
144 |
awb.is_available = False
|
|
|
145 |
session.commit()
|
| 444 |
rajveer |
146 |
return awb.awb_number
|
| 442 |
rajveer |
147 |
except:
|
| 644 |
chandransh |
148 |
raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
|
| 1137 |
chandransh |
149 |
|
| 3103 |
chandransh |
150 |
def get_free_awb_count(provider_id, type):
|
|
|
151 |
count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
|
| 1137 |
chandransh |
152 |
if count == None:
|
|
|
153 |
count = 0
|
|
|
154 |
return count
|
| 442 |
rajveer |
155 |
|
| 644 |
chandransh |
156 |
def get_shipment_info(awb_number, provider_id):
|
|
|
157 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
|
|
158 |
query = AwbUpdate.query.filter_by(awb = awb)
|
| 766 |
rajveer |
159 |
info = query.all()
|
|
|
160 |
return info
|
|
|
161 |
|
| 6643 |
rajveer |
162 |
def store_shipment_info(update):
|
|
|
163 |
updates = AwbUpdate.query.filter_by(providerId = update.providerId, awbNumber = update.awbNumber, location = update.location, date = to_py_date(update.date), status = update.status, description = update.description).all()
|
|
|
164 |
if not updates:
|
|
|
165 |
dupdate = AwbUpdate()
|
|
|
166 |
dupdate.providerId = update.providerId
|
|
|
167 |
dupdate.awbNumber = update.awbNumber
|
|
|
168 |
dupdate.location = update.location
|
|
|
169 |
dupdate.date = to_py_date(update.date)
|
|
|
170 |
dupdate.status = update.status
|
|
|
171 |
dupdate.description = update.description
|
|
|
172 |
session.commit()
|
|
|
173 |
|
| 1730 |
ankur.sing |
174 |
def get_holidays(start_date, end_date):
|
|
|
175 |
query = PublicHolidays.query
|
|
|
176 |
if start_date != -1:
|
|
|
177 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
|
|
178 |
if end_date != -1:
|
|
|
179 |
query = query.filter(PublicHolidays.date <= to_py_date(end_date))
|
|
|
180 |
holidays = query.all()
|
|
|
181 |
holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays]
|
|
|
182 |
return holiday_dates
|
|
|
183 |
|
| 5527 |
anupam.sin |
184 |
def get_provider_for_pickup_type(pickUp):
|
|
|
185 |
return Provider.query.filter(Provider.pickup == pickUp).first().id
|
|
|
186 |
|
| 766 |
rajveer |
187 |
def close_session():
|
|
|
188 |
if session.is_active:
|
|
|
189 |
print "session is active. closing it."
|
| 2823 |
chandransh |
190 |
session.close()
|
| 3376 |
rajveer |
191 |
|
|
|
192 |
def is_alive():
|
|
|
193 |
try:
|
|
|
194 |
session.query(Awb.id).limit(1).one()
|
|
|
195 |
return True
|
|
|
196 |
except:
|
| 5555 |
rajveer |
197 |
return False
|
|
|
198 |
|
|
|
199 |
def get_all_pickup_stores():
|
| 5572 |
anupam.sin |
200 |
pickupStores = PickupStore.query.all()
|
|
|
201 |
return pickupStores
|
| 5555 |
rajveer |
202 |
|
|
|
203 |
def get_pickup_store(storeId):
|
| 5719 |
rajveer |
204 |
return PickupStore.query.filter_by(id = storeId).one()
|
|
|
205 |
|
|
|
206 |
def get_pickup_store_by_hotspot_id(hotspotId):
|
|
|
207 |
return PickupStore.query.filter_by(hotspotId = hotspotId).one()
|
| 6322 |
amar.kumar |
208 |
|
| 6524 |
rajveer |
209 |
def add_pincode(provider, pincode, destCode, exp, cod, stationType, otgAvailable):
|
| 6322 |
amar.kumar |
210 |
provider = Provider().query.filter_by(id = provider).one()
|
|
|
211 |
serviceableLocationDetails = ServiceableLocationDetails()
|
|
|
212 |
serviceableLocationDetails.provider = provider
|
|
|
213 |
serviceableLocationDetails.dest_pincode = pincode
|
|
|
214 |
serviceableLocationDetails.dest_code = destCode
|
|
|
215 |
serviceableLocationDetails.exp = exp
|
|
|
216 |
serviceableLocationDetails.cod = cod
|
|
|
217 |
serviceableLocationDetails.station_type = stationType
|
| 6524 |
rajveer |
218 |
serviceableLocationDetails.otgAvailable = otgAvailable
|
| 6322 |
amar.kumar |
219 |
session.commit()
|
|
|
220 |
|
| 6524 |
rajveer |
221 |
def update_pincode(providerId, pincode, exp, cod, otgAvailable):
|
| 6615 |
amar.kumar |
222 |
serviceableLocationDetails = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pincode)
|
| 6322 |
amar.kumar |
223 |
serviceableLocationDetails.exp = exp
|
|
|
224 |
serviceableLocationDetails.cod = cod
|
| 6524 |
rajveer |
225 |
serviceableLocationDetails.otgAvailable = otgAvailable
|
| 6322 |
amar.kumar |
226 |
session.commit()
|