| 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,\
|
|
|
9 |
ShipmentUpdate
|
|
|
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:
|
|
|
31 |
awb = AWB()
|
|
|
32 |
awb.awb_number = number
|
|
|
33 |
awb.provider = provider
|
|
|
34 |
awb.is_available = True
|
|
|
35 |
session.commit()
|
|
|
36 |
|
|
|
37 |
def get_empty_AWB(provider_id):
|
|
|
38 |
provider = Provider.get_by(id = provider_id)
|
|
|
39 |
query = AWB.query.filter_by(provider = provider) #check the provider thing
|
|
|
40 |
query = query.filter_by(is_available=True)
|
|
|
41 |
try:
|
|
|
42 |
return query.one()
|
|
|
43 |
except:
|
|
|
44 |
return "129102" #generate new exception
|
|
|
45 |
|
|
|
46 |
def get_providers():
|
|
|
47 |
return Provider.query.all()
|
|
|
48 |
|
|
|
49 |
def get_provider(provider_id):
|
|
|
50 |
return Provider.get_by(id=provider_id)
|
|
|
51 |
|
|
|
52 |
def get_shipment_info(awb_number):
|
|
|
53 |
shipment = Shipment.get_by(awb = awb_number)
|
|
|
54 |
query = ShipmentUpdate.query.filter_by(shipment = shipment)
|
|
|
55 |
return query.all()
|
|
|
56 |
|
|
|
57 |
def create_shipment(shipment): #add date for the shipment
|
|
|
58 |
new_shipment = Shipment()
|
|
|
59 |
|
|
|
60 |
new_shipment.destination = shipment.destination
|
|
|
61 |
new_shipment.origin = shipment.origin
|
|
|
62 |
new_shipment.recepient_address = shipment.recepient_address
|
|
|
63 |
new_shipment.recepient_name = shipment.recepient_name
|
|
|
64 |
new_shipment.recepient_phone = shipment.recepient_phone
|
|
|
65 |
new_shipment.recepient_pincode = shipment.recepient_pincode
|
|
|
66 |
new_shipment.shipment_contents = shipment.shipment_contents
|
|
|
67 |
new_shipment.shipment_weight = shipment.shipment_weight
|
|
|
68 |
new_shipment.warehouse_name = shipment.warehouse_name
|
|
|
69 |
#new_shipment.timestamp = shipment.timestamp
|
|
|
70 |
if shipment.timestamp is None:
|
|
|
71 |
new_shipment.timestamp = datetime.datetime.today()
|
|
|
72 |
else:
|
|
|
73 |
new_shipment.timestamp = shipment.timestamp
|
|
|
74 |
|
|
|
75 |
new_shipment.awb = shipment.awb
|
|
|
76 |
|
|
|
77 |
session.commit()
|
|
|
78 |
return
|
|
|
79 |
|
|
|
80 |
def update_shipment_status(awb, shipment_update):
|
|
|
81 |
shipment = Shipment.get_by(awb = awb)
|
|
|
82 |
|
|
|
83 |
new_shipment_update = ShipmentUpdate()
|
|
|
84 |
|
|
|
85 |
new_shipment_update.shipment = shipment
|
|
|
86 |
new_shipment_update.description = shipment_update.description
|
|
|
87 |
new_shipment_update.pin = shipment_update.pin
|
|
|
88 |
|
|
|
89 |
new_shipment_update.shipment
|
|
|
90 |
|
|
|
91 |
if shipment_update.timestamp is None:
|
|
|
92 |
new_shipment_update.timestamp = datetime.datetime.today()
|
|
|
93 |
else:
|
|
|
94 |
new_shipment_update.timestamp = shipment_update.timestamp
|
|
|
95 |
|
|
|
96 |
session.commit()
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
def get_shipments(warehouse_id, from_date, to_date, provider_id):
|
|
|
101 |
awbs = AWB.query.filter_by(provider_id = provider_id).all()
|
|
|
102 |
|
|
|
103 |
shipments = []
|
|
|
104 |
for awb in awbs:
|
|
|
105 |
awb_number = awb.awb_number
|
|
|
106 |
query = Shipment.query.filter(Shipment.awb == awb_number)
|
|
|
107 |
query = query.filter(Shipment.timestamp > from_date)
|
|
|
108 |
query = query.filter(Shipment.timestamp < to_date)
|
|
|
109 |
try:
|
|
|
110 |
shipment = query.one()
|
|
|
111 |
shipments.append(shipment)
|
|
|
112 |
except:
|
|
|
113 |
continue
|
|
|
114 |
return shipments
|
|
|
115 |
|
|
|
116 |
def get_todays_shipments(warehouse_id, provider_id):
|
|
|
117 |
from_date = datetime.date.today()
|
|
|
118 |
from_datetime = datetime.datetime(from_date.year, from_date.month, from_date.day)
|
|
|
119 |
to_date = from_date + datetime.timedelta(days=1)
|
|
|
120 |
to_datetime = datetime.datetime(to_date.year, to_date.month, to_date.day)
|
|
|
121 |
|
|
|
122 |
return get_shipments(warehouse_id, from_datetime, to_datetime, provider_id)
|
|
|
123 |
|