133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
from trytond.model import ModelSQL, ModelView, fields
|
|
from trytond.pool import Pool, PoolMeta
|
|
from trytond.transaction import Transaction
|
|
import logging
|
|
from sql import Table
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class AutomationCron(ModelSQL, ModelView):
|
|
"Automation Cron"
|
|
__name__ = 'automation.cron'
|
|
|
|
frequency = fields.Selection([
|
|
('daily', "Daily"),
|
|
('weekly', "Weekly"),
|
|
('monthly', "Monthly"),
|
|
], "Frequency", required=True,
|
|
help="How frequently rates must be updated.")
|
|
|
|
last_update = fields.Date("Last Update", required=True)
|
|
|
|
@classmethod
|
|
def run(cls, crons):
|
|
cls.update_shipment()
|
|
|
|
@classmethod
|
|
def update_shipment(cls):
|
|
# Objets Tryton
|
|
PoolObj = Pool()
|
|
ShipmentIn = PoolObj.get('stock.shipment.in')
|
|
Party = PoolObj.get('party.party')
|
|
Vessel = PoolObj.get('trade.vessel')
|
|
Location = PoolObj.get('stock.location')
|
|
|
|
# Table externe
|
|
t = Table('freight_booking_info')
|
|
cursor = Transaction().connection.cursor()
|
|
|
|
cursor.execute(*t.select(
|
|
t.ShippingInstructionNumber,
|
|
t.ShippingInstructionDate,
|
|
t.ShippingInstructionQuantity,
|
|
t.ShippingInstructionQuantityUnit,
|
|
t.NumberOfContainers,
|
|
t.ContainerType,
|
|
t.Loading,
|
|
t.Destination,
|
|
t.BookingAgent,
|
|
t.Carrier,
|
|
t.Vessel,
|
|
t.BL_Number,
|
|
t.ETD_Date,
|
|
t.BL_Date,
|
|
t.ExpectedController,
|
|
t.Comments,
|
|
t.FintradeBookingKey,
|
|
))
|
|
|
|
for row in cursor.fetchall():
|
|
(
|
|
si_number,
|
|
si_date,
|
|
si_quantity,
|
|
si_unit,
|
|
container_number,
|
|
container_type,
|
|
loading_name,
|
|
destination_name,
|
|
agent_name,
|
|
carrier_name,
|
|
vessel_name,
|
|
bl_number,
|
|
etd_date,
|
|
bl_date,
|
|
controller,
|
|
comments,
|
|
fintrade_booking_key,
|
|
) = row
|
|
|
|
logger.info("ROW_FROM_CRON: %s", row)
|
|
|
|
# ----- Récupération / création des recordsets -----
|
|
def get_or_create_party(name):
|
|
name = name.upper()
|
|
p = Party.search([('name', '=', name)], limit=1)
|
|
if p:
|
|
return p[0]
|
|
new_p = Party()
|
|
new_p.name = name
|
|
Party.save([new_p])
|
|
return new_p
|
|
|
|
def get_or_create_vessel(name, imo=None):
|
|
name = name.upper()
|
|
v = Vessel.search([('vessel_name', '=', name)], limit=1)
|
|
if v:
|
|
return v[0]
|
|
new_v = Vessel()
|
|
new_v.vessel_name = name
|
|
new_v.vessel_imo = imo
|
|
Vessel.save([new_v])
|
|
return new_v
|
|
|
|
def get_or_create_location(name, type_):
|
|
name = name.upper()
|
|
loc = Location.search([('name', '=', name), ('type', '=', type_)], limit=1)
|
|
if loc:
|
|
return loc[0]
|
|
new_loc = Location()
|
|
new_loc.name = name
|
|
new_loc.type = type_
|
|
Location.save([new_loc])
|
|
return new_loc
|
|
|
|
# ----- Vérification si le Shipment existe -----
|
|
shipment = ShipmentIn.search([('reference','=',si_number)], limit=1)
|
|
if shipment:
|
|
sh = shipment[0]
|
|
else:
|
|
sh = ShipmentIn()
|
|
sh.reference = si_number
|
|
sh.from_location = get_or_create_location(loading_name, 'supplier')
|
|
sh.to_location = get_or_create_location(destination_name, 'customer')
|
|
sh.carrier = get_or_create_party(carrier_name)
|
|
sh.supplier = get_or_create_party(agent_name)
|
|
sh.vessel = get_or_create_vessel(vessel_name)
|
|
sh.cargo_mode = 'bulk'
|
|
sh.bl_number = bl_number
|
|
sh.bl_date = bl_date
|
|
sh.etd = etd_date
|
|
ShipmentIn.save([sh])
|
|
logger.info("SHIPMENT_CREATED: %s", sh)
|