| 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, \
|
| 1730 |
ankur.sing |
9 |
DeliveryEstimate, WarehouseAllocation, DestinationProviderAllocation,\
|
| 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
|
| 3098 |
rajveer |
17 |
from shop2020.utils.caching.SimpleCaching import memoized
|
| 442 |
rajveer |
18 |
|
| 1248 |
chandransh |
19 |
def initialize(dbname="logistics"):
|
| 442 |
rajveer |
20 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 1248 |
chandransh |
21 |
DataService.initialize(dbname)
|
| 442 |
rajveer |
22 |
|
| 644 |
chandransh |
23 |
def get_provider(provider_id):
|
| 766 |
rajveer |
24 |
provider = Provider.get_by(id=provider_id)
|
|
|
25 |
return provider
|
| 644 |
chandransh |
26 |
|
|
|
27 |
def get_providers():
|
| 766 |
rajveer |
28 |
providers = Provider.query.all()
|
|
|
29 |
return providers
|
| 644 |
chandransh |
30 |
|
| 3145 |
rajveer |
31 |
'''
|
|
|
32 |
Cache the return values of the function for one day
|
|
|
33 |
'''
|
|
|
34 |
@memoized(86400)
|
| 3064 |
chandransh |
35 |
def is_cod_allowed(destination_pin):
|
|
|
36 |
try:
|
|
|
37 |
sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin, cod = True)
|
|
|
38 |
except Exception as ex:
|
|
|
39 |
print "Unexpected error:", sys.exc_info()[0]
|
|
|
40 |
|
|
|
41 |
if sld:
|
|
|
42 |
return True
|
|
|
43 |
else:
|
|
|
44 |
return False
|
|
|
45 |
|
| 3098 |
rajveer |
46 |
'''
|
| 3145 |
rajveer |
47 |
Cache the return values of the function for one day
|
| 3098 |
rajveer |
48 |
'''
|
| 3144 |
rajveer |
49 |
@memoized(86400)
|
| 3064 |
chandransh |
50 |
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
|
| 2341 |
chandransh |
51 |
if warehouse_location is None:
|
|
|
52 |
try:
|
|
|
53 |
warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
|
|
|
54 |
except:
|
|
|
55 |
print "Unexpected error:", sys.exc_info()[0]
|
| 3064 |
chandransh |
56 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
| 1504 |
ankur.sing |
57 |
|
|
|
58 |
try:
|
| 3064 |
chandransh |
59 |
sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin)
|
|
|
60 |
if type == DeliveryType.PREPAID and sld.exp:
|
|
|
61 |
provider = sld.provider
|
|
|
62 |
if type == DeliveryType.COD and sld.cod:
|
|
|
63 |
provider = sld.provider
|
| 1504 |
ankur.sing |
64 |
except Exception as ex:
|
|
|
65 |
print "Unexpected error:", sys.exc_info()[0]
|
| 3064 |
chandransh |
66 |
|
|
|
67 |
if not provider:
|
| 1504 |
ankur.sing |
68 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
|
|
69 |
", and warehouse location: " + str(warehouse_location))
|
| 3064 |
chandransh |
70 |
|
|
|
71 |
# query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
|
|
|
72 |
# query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
|
|
|
73 |
# try:
|
|
|
74 |
# if (item_selling_price <= 5000):
|
|
|
75 |
# provider = query_provider.one().provider_less_amount
|
|
|
76 |
# else:
|
|
|
77 |
# provider = query_provider.one().provider_more_amount
|
|
|
78 |
# except Exception as ex:
|
|
|
79 |
# print ex
|
|
|
80 |
# print "Unexpected error:", sys.exc_info()[0]
|
|
|
81 |
# raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
|
|
82 |
# ", and warehouse location: " + str(warehouse_location))
|
| 1504 |
ankur.sing |
83 |
|
| 644 |
chandransh |
84 |
query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
|
|
|
85 |
query = query.filter_by(destination_pin = destination_pin)
|
| 1504 |
ankur.sing |
86 |
query = query.filter_by(provider = provider)
|
| 644 |
chandransh |
87 |
try:
|
|
|
88 |
return query.first()
|
| 1504 |
ankur.sing |
89 |
except Exception as ex:
|
|
|
90 |
print ex
|
| 644 |
chandransh |
91 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 766 |
rajveer |
92 |
|
| 644 |
chandransh |
93 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 442 |
rajveer |
94 |
for number in numbers:
|
| 644 |
chandransh |
95 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 444 |
rajveer |
96 |
try:
|
|
|
97 |
query.one()
|
|
|
98 |
except:
|
| 644 |
chandransh |
99 |
awb = Awb()
|
| 444 |
rajveer |
100 |
awb.awb_number = number
|
| 644 |
chandransh |
101 |
awb.provider_id = provider_id
|
| 444 |
rajveer |
102 |
awb.is_available = True
|
| 644 |
chandransh |
103 |
awb.type = type
|
|
|
104 |
session.commit()
|
| 442 |
rajveer |
105 |
|
| 444 |
rajveer |
106 |
def set_AWB_as_used(awb_number):
|
| 644 |
chandransh |
107 |
query = Awb.query.filter_by(awb_number = awb_number)
|
| 444 |
rajveer |
108 |
awb = query.one()
|
|
|
109 |
awb.is_available=False
|
|
|
110 |
session.commit()
|
|
|
111 |
|
| 3044 |
chandransh |
112 |
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
|
| 2342 |
chandransh |
113 |
query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True) #check the provider thing
|
| 3044 |
chandransh |
114 |
if type == DeliveryType.PREPAID:
|
|
|
115 |
query = query.filter_by(type="Prepaid")
|
|
|
116 |
if type == DeliveryType.COD:
|
|
|
117 |
query = query.filter_by(type="COD")
|
|
|
118 |
|
| 442 |
rajveer |
119 |
try:
|
| 644 |
chandransh |
120 |
awb = query.first()
|
|
|
121 |
awb.is_available = False
|
|
|
122 |
session.commit()
|
| 444 |
rajveer |
123 |
return awb.awb_number
|
| 442 |
rajveer |
124 |
except:
|
| 644 |
chandransh |
125 |
raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
|
| 1137 |
chandransh |
126 |
|
| 3103 |
chandransh |
127 |
def get_free_awb_count(provider_id, type):
|
|
|
128 |
count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
|
| 1137 |
chandransh |
129 |
if count == None:
|
|
|
130 |
count = 0
|
|
|
131 |
return count
|
| 442 |
rajveer |
132 |
|
| 644 |
chandransh |
133 |
def get_shipment_info(awb_number, provider_id):
|
|
|
134 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
|
|
135 |
query = AwbUpdate.query.filter_by(awb = awb)
|
| 766 |
rajveer |
136 |
info = query.all()
|
|
|
137 |
return info
|
|
|
138 |
|
| 3145 |
rajveer |
139 |
'''
|
|
|
140 |
Cache the return values of the function for one day
|
|
|
141 |
'''
|
|
|
142 |
@memoized(86400)
|
| 1730 |
ankur.sing |
143 |
def get_holidays(start_date, end_date):
|
|
|
144 |
query = PublicHolidays.query
|
|
|
145 |
if start_date != -1:
|
|
|
146 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
|
|
147 |
if end_date != -1:
|
|
|
148 |
query = query.filter(PublicHolidays.date <= to_py_date(end_date))
|
|
|
149 |
holidays = query.all()
|
|
|
150 |
holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays]
|
|
|
151 |
return holiday_dates
|
|
|
152 |
|
| 766 |
rajveer |
153 |
def close_session():
|
|
|
154 |
if session.is_active:
|
|
|
155 |
print "session is active. closing it."
|
| 2823 |
chandransh |
156 |
session.close()
|