20.01.26
This commit is contained in:
@@ -1,60 +1,38 @@
|
|||||||
import requests
|
from trytond.model import ModelSQL, ModelView, fields
|
||||||
from decimal import getcontext, Decimal, ROUND_HALF_UP
|
|
||||||
from datetime import datetime
|
|
||||||
from trytond.model import fields
|
|
||||||
from trytond.model import (ModelSQL, ModelView)
|
|
||||||
from trytond.pool import Pool, PoolMeta
|
from trytond.pool import Pool, PoolMeta
|
||||||
from trytond.transaction import Transaction
|
from trytond.transaction import Transaction
|
||||||
import logging
|
import logging
|
||||||
from sql import Table
|
from sql import Table
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Cron(metaclass=PoolMeta):
|
|
||||||
__name__ = 'ir.cron'
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def __setup__(cls):
|
|
||||||
super().__setup__()
|
|
||||||
cls.method.selection.append(
|
|
||||||
('automation.cron|update_shipment', "Update Shipment from freight booking info"))
|
|
||||||
|
|
||||||
class AutomationCron(ModelSQL, ModelView):
|
class AutomationCron(ModelSQL, ModelView):
|
||||||
"Automation Cron"
|
"Automation Cron"
|
||||||
__name__ = 'automation.cron'
|
__name__ = 'automation.cron'
|
||||||
|
|
||||||
frequency = fields.Selection([
|
frequency = fields.Selection([
|
||||||
('daily', "Daily"),
|
('daily', "Daily"),
|
||||||
('weekly', "Weekly"),
|
('weekly', "Weekly"),
|
||||||
('monthly', "Monthly"),
|
('monthly', "Monthly"),
|
||||||
], "Frequency", required=True,
|
], "Frequency", required=True,
|
||||||
help="How frequently rates must be updated.")
|
help="How frequently rates must be updated.")
|
||||||
|
|
||||||
last_update = fields.Date("Last Update", required=True)
|
last_update = fields.Date("Last Update", required=True)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __setup__(cls):
|
|
||||||
super().__setup__()
|
|
||||||
cls._buttons.update({
|
|
||||||
'run': {},
|
|
||||||
})
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def default_frequency(cls):
|
|
||||||
return 'daily'
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def default_last_update(cls):
|
|
||||||
pool = Pool()
|
|
||||||
Date = pool.get('ir.date')
|
|
||||||
return Date.today()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
@ModelView.button
|
|
||||||
def run(cls, crons):
|
def run(cls, crons):
|
||||||
cls.update_shipment(crons)
|
cls.update_shipment()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_shipment(cls):
|
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')
|
t = Table('freight_booking_info')
|
||||||
cursor = Transaction().connection.cursor()
|
cursor = Transaction().connection.cursor()
|
||||||
|
|
||||||
@@ -86,11 +64,11 @@ class AutomationCron(ModelSQL, ModelView):
|
|||||||
si_unit,
|
si_unit,
|
||||||
container_number,
|
container_number,
|
||||||
container_type,
|
container_type,
|
||||||
loading,
|
loading_name,
|
||||||
destination,
|
destination_name,
|
||||||
agent,
|
agent_name,
|
||||||
carrier,
|
carrier_name,
|
||||||
vessel,
|
vessel_name,
|
||||||
bl_number,
|
bl_number,
|
||||||
etd_date,
|
etd_date,
|
||||||
bl_date,
|
bl_date,
|
||||||
@@ -98,23 +76,57 @@ class AutomationCron(ModelSQL, ModelView):
|
|||||||
comments,
|
comments,
|
||||||
fintrade_booking_key,
|
fintrade_booking_key,
|
||||||
) = row
|
) = row
|
||||||
logger.info("ROW_FROM_CRON:%s",row)
|
|
||||||
Location = Pool().get('stock.location')
|
logger.info("ROW_FROM_CRON: %s", row)
|
||||||
Vessel = Pool().get('trade.vessel')
|
|
||||||
Party = Pool().get('party.party')
|
# ----- Récupération / création des recordsets -----
|
||||||
ShipmentIn = Pool().get('stock.shipment.in')
|
def get_or_create_party(name):
|
||||||
shipment = ShipmentIn.search(['reference','=',si_number])
|
name = name.upper()
|
||||||
if not shipment:
|
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 = ShipmentIn()
|
||||||
sh.reference = si_number
|
sh.reference = si_number
|
||||||
sh.from_location = Location.getLocationByName(loading,'supplier')
|
sh.from_location = get_or_create_location(loading_name, 'supplier')
|
||||||
sh.to_location = Location.getLocationByName(destination,'customer')
|
sh.to_location = get_or_create_location(destination_name, 'customer')
|
||||||
sh.carrier = Party.getPartyByName(carrier)
|
sh.carrier = get_or_create_party(carrier_name)
|
||||||
sh.supplier = Party.getPartyByName(agent)
|
sh.supplier = get_or_create_party(agent_name)
|
||||||
sh.vessel = Vessel.getVesselByName(vessel)
|
sh.vessel = get_or_create_vessel(vessel_name)
|
||||||
sh.cargo_mode = 'bulk'
|
sh.cargo_mode = 'bulk'
|
||||||
sh.bl_number = bl_number
|
sh.bl_number = bl_number
|
||||||
sh.bl_date = bl_date
|
sh.bl_date = bl_date
|
||||||
sh.etd = etd_date
|
sh.etd = etd_date
|
||||||
ShipmentIn.save([sh])
|
ShipmentIn.save([sh])
|
||||||
logger.info("SHIPMENT_CREATED:%s",sh)
|
logger.info("SHIPMENT_CREATED: %s", sh)
|
||||||
|
|||||||
@@ -34,6 +34,5 @@ class Party(metaclass=PoolMeta):
|
|||||||
p = cls()
|
p = cls()
|
||||||
p.name = party
|
p.name = party
|
||||||
cls.save([p])
|
cls.save([p])
|
||||||
Transaction().connection.flush()
|
|
||||||
return p
|
return p
|
||||||
|
|
||||||
Reference in New Issue
Block a user