From d6fa273d1173cf6bffa4aa9cb41e5f60a533588e Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Mon, 20 Jul 2026 09:16:28 +0200 Subject: [PATCH] Faircot cron --- modules/automation/cron.py | 48 ++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/modules/automation/cron.py b/modules/automation/cron.py index 06a4cca..939edc1 100644 --- a/modules/automation/cron.py +++ b/modules/automation/cron.py @@ -225,6 +225,12 @@ class AutomationCron(ModelSQL, ModelView): return '' return value + def normalize_booking_number(value): + value = str(value or '').strip() + if value.lower() in {'none', 'null'}: + return '' + return value + def sync_shipment_from_booking(shipment, bl_number, bl_date, etd_date, controller): changed = False @@ -271,33 +277,45 @@ class AutomationCron(ModelSQL, ModelView): etd_date, bl_date, controller, comments, fintrade_booking_key ) = row + si_reference = normalize_booking_number(si_number) - logger.info(f"Traitement shipment {i}/{len(rows2)} : SI {si_number}") + logger.info( + "Traitement shipment %s/%s : SI %s reference=%s", + i, len(rows2), si_number, si_reference) try: with Transaction().new_transaction() as trans_shipment: - logger.info(f"Debut transaction pour SI {si_number}") + logger.info( + "Debut transaction pour SI %s reference=%s", + si_number, si_reference) - existing_shipment = ShipmentIn.search([ - ('reference', '=', si_number) - ], limit=1) + existing_shipments = ShipmentIn.search([ + ('reference', '=', si_reference) + ]) - if existing_shipment: - shipment = existing_shipment[0] - sync_shipment_from_booking( - shipment, bl_number, bl_date, etd_date, - controller) + if existing_shipments: + if len(existing_shipments) > 1: + logger.info( + "FREIGHT_BOOKING_DUPLICATE_SHIPMENT_REFERENCE " + "reference=%s shipment_ids=%s", + si_reference, + [shipment.id for shipment in existing_shipments]) + for existing in existing_shipments: + sync_shipment_from_booking( + existing, bl_number, bl_date, etd_date, + controller) + shipment = existing_shipments[0] if shipment.incoming_moves: logger.info( "Shipment %s existe deja avec lots, " "booking data synchronized", - si_number) + si_reference) trans_shipment.commit() continue logger.info( "Shipment %s existe deja sans lots, verification freight_booking_lots", - si_number) + si_reference) with Transaction().set_context( _purchase_trade_skip_physical_lot_tolerance_warning=True): inv_date, inv_nb = ( @@ -313,11 +331,11 @@ class AutomationCron(ModelSQL, ModelView): ShipmentIn.save([shipment]) logger.info( "Shipment %s mis a jour avec %s incoming move(s)", - si_number, len(shipment.incoming_moves)) + si_reference, len(shipment.incoming_moves)) else: logger.info( "Shipment %s existe sans lots et aucun lot disponible pour l'instant", - si_number) + si_reference) trans_shipment.commit() continue @@ -377,7 +395,7 @@ class AutomationCron(ModelSQL, ModelView): raise ValueError(error_msg) shipment = ShipmentIn() - shipment.reference = si_number + shipment.reference = si_reference shipment.from_location = loc_from shipment.to_location = loc_to shipment.carrier = None # carrier