From 117c33a2ea7934f7f5f0f3a9bde65afa4f6438bb Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Tue, 20 Jan 2026 14:55:04 +0100 Subject: [PATCH] 20.01.26 --- modules/automation/cron.py | 126 +++++++++++++++++--------------- modules/purchase_trade/party.py | 1 - 2 files changed, 69 insertions(+), 58 deletions(-) diff --git a/modules/automation/cron.py b/modules/automation/cron.py index e95a8f4..263fba4 100644 --- a/modules/automation/cron.py +++ b/modules/automation/cron.py @@ -1,60 +1,38 @@ -import requests -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.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 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): "Automation Cron" __name__ = 'automation.cron' frequency = fields.Selection([ - ('daily', "Daily"), - ('weekly', "Weekly"), - ('monthly', "Monthly"), - ], "Frequency", required=True, - help="How frequently rates must be updated.") - + ('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 __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): - cls.update_shipment(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() @@ -86,11 +64,11 @@ class AutomationCron(ModelSQL, ModelView): si_unit, container_number, container_type, - loading, - destination, - agent, - carrier, - vessel, + loading_name, + destination_name, + agent_name, + carrier_name, + vessel_name, bl_number, etd_date, bl_date, @@ -98,23 +76,57 @@ class AutomationCron(ModelSQL, ModelView): comments, fintrade_booking_key, ) = row - logger.info("ROW_FROM_CRON:%s",row) - Location = Pool().get('stock.location') - Vessel = Pool().get('trade.vessel') - Party = Pool().get('party.party') - ShipmentIn = Pool().get('stock.shipment.in') - shipment = ShipmentIn.search(['reference','=',si_number]) - if not shipment: + + 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 = Location.getLocationByName(loading,'supplier') - sh.to_location = Location.getLocationByName(destination,'customer') - sh.carrier = Party.getPartyByName(carrier) - sh.supplier = Party.getPartyByName(agent) - sh.vessel = Vessel.getVesselByName(vessel) + 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) + logger.info("SHIPMENT_CREATED: %s", sh) diff --git a/modules/purchase_trade/party.py b/modules/purchase_trade/party.py index 08fab37..fde79b8 100755 --- a/modules/purchase_trade/party.py +++ b/modules/purchase_trade/party.py @@ -34,6 +34,5 @@ class Party(metaclass=PoolMeta): p = cls() p.name = party cls.save([p]) - Transaction().connection.flush() return p \ No newline at end of file