| 442 |
rajveer |
1 |
'''
|
|
|
2 |
Created on 13-Sep-2010
|
|
|
3 |
|
|
|
4 |
@author: rajveer
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
from shop2020.logistics.service.impl import DataService
|
|
|
8 |
from shop2020.logistics.service.impl.DataService import AWB, Provider, Shipment,\
|
| 477 |
rajveer |
9 |
ShipmentUpdate, DeliveryEstimate, PincodeWarehouseMapping
|
| 442 |
rajveer |
10 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
|
|
11 |
from elixir import session
|
|
|
12 |
import datetime
|
|
|
13 |
|
|
|
14 |
def initialize():
|
|
|
15 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
|
|
16 |
DataService.initialize()
|
|
|
17 |
|
|
|
18 |
def get_provider_by_id(provider_id):
|
|
|
19 |
query = Provider.query.filter_by(id=provider_id)
|
|
|
20 |
try:
|
|
|
21 |
return query.one()
|
|
|
22 |
except:
|
|
|
23 |
provider = Provider()
|
|
|
24 |
provider.name = "Default"
|
|
|
25 |
session.commit()
|
|
|
26 |
return provider
|
|
|
27 |
|
|
|
28 |
def add_empty_AWBs(numbers, provider_id):
|
|
|
29 |
provider = get_provider_by_id(provider_id)
|
|
|
30 |
for number in numbers:
|
| 444 |
rajveer |
31 |
query = AWB.query.filter(AWB.awb_number == number)
|
|
|
32 |
query = query.filter(AWB.provider_id == provider_id)
|
|
|
33 |
try:
|
|
|
34 |
query.one()
|
|
|
35 |
except:
|
|
|
36 |
awb = AWB()
|
|
|
37 |
awb.awb_number = number
|
|
|
38 |
awb.provider = provider
|
|
|
39 |
awb.is_available = True
|
| 442 |
rajveer |
40 |
session.commit()
|
|
|
41 |
|
| 444 |
rajveer |
42 |
def set_AWB_as_used(awb_number):
|
|
|
43 |
query = AWB.query.filter_by(awb_number = awb_number)
|
|
|
44 |
awb = query.one()
|
|
|
45 |
awb.is_available=False
|
|
|
46 |
session.commit()
|
|
|
47 |
|
| 442 |
rajveer |
48 |
def get_empty_AWB(provider_id):
|
| 444 |
rajveer |
49 |
query = AWB.query.filter_by(provider_id = provider_id) #check the provider thing
|
| 442 |
rajveer |
50 |
query = query.filter_by(is_available=True)
|
|
|
51 |
try:
|
| 444 |
rajveer |
52 |
awbs = query.all()
|
|
|
53 |
awb = awbs[0]
|
|
|
54 |
set_AWB_as_used(awb.awb_number)
|
|
|
55 |
return awb.awb_number
|
| 442 |
rajveer |
56 |
except:
|
|
|
57 |
return "129102" #generate new exception
|
|
|
58 |
|
|
|
59 |
def get_providers():
|
|
|
60 |
return Provider.query.all()
|
|
|
61 |
|
|
|
62 |
def get_provider(provider_id):
|
|
|
63 |
return Provider.get_by(id=provider_id)
|
|
|
64 |
|
|
|
65 |
def get_shipment_info(awb_number):
|
|
|
66 |
shipment = Shipment.get_by(awb = awb_number)
|
|
|
67 |
query = ShipmentUpdate.query.filter_by(shipment = shipment)
|
|
|
68 |
return query.all()
|
|
|
69 |
|
|
|
70 |
def create_shipment(shipment): #add date for the shipment
|
|
|
71 |
new_shipment = Shipment()
|
|
|
72 |
|
|
|
73 |
new_shipment.destination = shipment.destination
|
|
|
74 |
new_shipment.origin = shipment.origin
|
|
|
75 |
new_shipment.recepient_address = shipment.recepient_address
|
|
|
76 |
new_shipment.recepient_name = shipment.recepient_name
|
|
|
77 |
new_shipment.recepient_phone = shipment.recepient_phone
|
|
|
78 |
new_shipment.recepient_pincode = shipment.recepient_pincode
|
|
|
79 |
new_shipment.shipment_contents = shipment.shipment_contents
|
|
|
80 |
new_shipment.shipment_weight = shipment.shipment_weight
|
|
|
81 |
new_shipment.warehouse_name = shipment.warehouse_name
|
|
|
82 |
#new_shipment.timestamp = shipment.timestamp
|
|
|
83 |
if shipment.timestamp is None:
|
|
|
84 |
new_shipment.timestamp = datetime.datetime.today()
|
|
|
85 |
else:
|
|
|
86 |
new_shipment.timestamp = shipment.timestamp
|
|
|
87 |
|
|
|
88 |
new_shipment.awb = shipment.awb
|
|
|
89 |
|
|
|
90 |
session.commit()
|
|
|
91 |
return
|
|
|
92 |
|
|
|
93 |
def update_shipment_status(awb, shipment_update):
|
|
|
94 |
shipment = Shipment.get_by(awb = awb)
|
|
|
95 |
|
|
|
96 |
new_shipment_update = ShipmentUpdate()
|
|
|
97 |
|
|
|
98 |
new_shipment_update.shipment = shipment
|
|
|
99 |
new_shipment_update.description = shipment_update.description
|
|
|
100 |
new_shipment_update.pin = shipment_update.pin
|
|
|
101 |
|
|
|
102 |
new_shipment_update.shipment
|
|
|
103 |
|
|
|
104 |
if shipment_update.timestamp is None:
|
|
|
105 |
new_shipment_update.timestamp = datetime.datetime.today()
|
|
|
106 |
else:
|
|
|
107 |
new_shipment_update.timestamp = shipment_update.timestamp
|
|
|
108 |
|
|
|
109 |
session.commit()
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
def get_shipments(warehouse_id, from_date, to_date, provider_id):
|
|
|
114 |
awbs = AWB.query.filter_by(provider_id = provider_id).all()
|
|
|
115 |
|
|
|
116 |
shipments = []
|
|
|
117 |
for awb in awbs:
|
|
|
118 |
awb_number = awb.awb_number
|
|
|
119 |
query = Shipment.query.filter(Shipment.awb == awb_number)
|
|
|
120 |
query = query.filter(Shipment.timestamp > from_date)
|
|
|
121 |
query = query.filter(Shipment.timestamp < to_date)
|
|
|
122 |
try:
|
|
|
123 |
shipment = query.one()
|
|
|
124 |
shipments.append(shipment)
|
|
|
125 |
except:
|
|
|
126 |
continue
|
|
|
127 |
return shipments
|
|
|
128 |
|
|
|
129 |
def get_todays_shipments(warehouse_id, provider_id):
|
|
|
130 |
from_date = datetime.date.today()
|
|
|
131 |
from_datetime = datetime.datetime(from_date.year, from_date.month, from_date.day)
|
|
|
132 |
to_date = from_date + datetime.timedelta(days=1)
|
|
|
133 |
to_datetime = datetime.datetime(to_date.year, to_date.month, to_date.day)
|
|
|
134 |
|
|
|
135 |
return get_shipments(warehouse_id, from_datetime, to_datetime, provider_id)
|
| 472 |
rajveer |
136 |
|
|
|
137 |
def get_logistics_estimation(item_id, destination_pin, provider_id):
|
| 477 |
rajveer |
138 |
query = PincodeWarehouseMapping.query.filter_by(pincode = destination_pin)
|
|
|
139 |
try:
|
|
|
140 |
warehouse = query.one()
|
|
|
141 |
except:
|
|
|
142 |
warehouse = None
|
|
|
143 |
#need to throw exception
|
|
|
144 |
|
|
|
145 |
warehouse_pin = warehouse.warehouse_pin
|
|
|
146 |
query = DeliveryEstimate.query.filter_by(source_pin = warehouse_pin)
|
|
|
147 |
query = query.filter_by(destination_pin = destination_pin)
|
| 472 |
rajveer |
148 |
query = query.filter_by(provider_id = provider_id)
|
|
|
149 |
try:
|
| 477 |
rajveer |
150 |
return query.one(), warehouse.warehouse_id
|
| 472 |
rajveer |
151 |
except:
|
|
|
152 |
return None
|
|
|
153 |
# need to check the availability in another service before returning estimation.
|
|
|
154 |
|
| 477 |
rajveer |
155 |
def add_delivery_estimate(warehouse_id, destination_pin, provider_id, delivery_time, reliability):
|
| 472 |
rajveer |
156 |
query = Provider.query.filter_by(id=provider_id)
|
|
|
157 |
try:
|
|
|
158 |
provider = query.one()
|
|
|
159 |
except:
|
| 477 |
rajveer |
160 |
print "provider not found"
|
|
|
161 |
|
|
|
162 |
query = PincodeWarehouseMapping.query.filter_by(warehouse_id = warehouse_id)
|
|
|
163 |
try:
|
|
|
164 |
warehouse = query.one()
|
|
|
165 |
warehouse_pin = warehouse.warehouse_pin
|
|
|
166 |
except:
|
|
|
167 |
warehouse_pin = 0
|
|
|
168 |
print "warehouse pin not found"
|
|
|
169 |
|
| 472 |
rajveer |
170 |
delivery_estimate = DeliveryEstimate()
|
| 477 |
rajveer |
171 |
delivery_estimate.source_pin = warehouse_pin
|
| 472 |
rajveer |
172 |
delivery_estimate.destination_pin = destination_pin
|
|
|
173 |
delivery_estimate.provider = provider
|
|
|
174 |
delivery_estimate.delivery_time = delivery_time
|
| 477 |
rajveer |
175 |
delivery_estimate.relibility = reliability
|
|
|
176 |
session.commit()
|
|
|
177 |
|
|
|
178 |
def add_pincode_warehouse_mapping(pincode, warehouse_id, warehouse_pin):
|
|
|
179 |
mapper = PincodeWarehouseMapping()
|
|
|
180 |
mapper.pincode = pincode
|
|
|
181 |
mapper.warehouse_id = warehouse_id
|
|
|
182 |
if warehouse_pin:
|
|
|
183 |
mapper.warehouse_name = warehouse_pin
|
|
|
184 |
session.commit()
|