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