From 07ba685c0c9072470c214273f9721d63bd01126b Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Tue, 28 Jul 2026 11:08:24 +0200 Subject: [PATCH] Refresh custom duplicate payment terms on party change --- modules/purchase_trade/duplicate.py | 33 +++++++++++++++++++++ modules/purchase_trade/tests/test_module.py | 24 +++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/modules/purchase_trade/duplicate.py b/modules/purchase_trade/duplicate.py index 4184856..7784a30 100644 --- a/modules/purchase_trade/duplicate.py +++ b/modules/purchase_trade/duplicate.py @@ -115,6 +115,21 @@ class TradeCustomDuplicateStart(ModelView): 'invisible': ~Bool(Eval('create_matched_mirror')), }) + @fields.depends('party', 'source_model') + def on_change_party(self): + payment_term = TradeCustomDuplicate._party_payment_term( + self.party, self.source_model) + if payment_term: + self.payment_term = payment_term + + @fields.depends('mirror_party', 'source_model') + def on_change_mirror_party(self): + payment_term = TradeCustomDuplicate._party_payment_term( + self.mirror_party, + TradeCustomDuplicate._opposite_model(self.source_model)) + if payment_term: + self.mirror_payment_term = payment_term + @fields.depends('source', 'mirror_mode') def on_change_mirror_mode(self): record = TradeCustomDuplicate._reference_record(self.source) @@ -164,6 +179,24 @@ class TradeCustomDuplicate(Wizard): def _opposite_type(source_model): return 'Sale' if source_model == 'purchase.purchase' else 'Purchase' + @staticmethod + def _opposite_model(source_model): + if source_model == 'purchase.purchase': + return 'sale.sale' + if source_model == 'sale.sale': + return 'purchase.purchase' + return None + + @staticmethod + def _party_payment_term(party, model_name): + if not party: + return None + if model_name == 'purchase.purchase': + return getattr(party, 'supplier_payment_term', None) + if model_name == 'sale.sale': + return getattr(party, 'customer_payment_term', None) + return None + @staticmethod def _record_from_lot_report(record): if getattr(record, '__name__', None) != 'lot.report': diff --git a/modules/purchase_trade/tests/test_module.py b/modules/purchase_trade/tests/test_module.py index 4ab7784..db93d57 100644 --- a/modules/purchase_trade/tests/test_module.py +++ b/modules/purchase_trade/tests/test_module.py @@ -8927,6 +8927,30 @@ description self.assertIs(reloaded.shipment_address, shipment_address) reloaded.save.assert_called_once() + def test_custom_duplicate_party_change_uses_default_payment_term(self): + 'custom duplicate party change refreshes payment term' + payment_term = SimpleNamespace(id=5) + start = duplicate_module.TradeCustomDuplicateStart() + start.source_model = 'sale.sale' + start.party = SimpleNamespace(customer_payment_term=payment_term) + start.payment_term = None + + start.on_change_party() + + self.assertIs(start.payment_term, payment_term) + + def test_custom_duplicate_mirror_party_change_uses_default_payment_term(self): + 'custom duplicate mirror party change refreshes mirror payment term' + payment_term = SimpleNamespace(id=7) + start = duplicate_module.TradeCustomDuplicateStart() + start.source_model = 'purchase.purchase' + start.mirror_party = SimpleNamespace(customer_payment_term=payment_term) + start.mirror_payment_term = None + + start.on_change_mirror_party() + + self.assertIs(start.mirror_payment_term, payment_term) + def test_custom_duplicate_creates_purchase_open_virtual_lot(self): 'custom duplicate creates the purchase opening lot.qt explicitly' saved_lots = []