From bfbab59ebf59123e6781e320ce0611cbbd76909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 4 Jan 2024 18:48:14 +0100 Subject: [PATCH] Use usage from context for default contact mechanism on party --- modules/account_invoice/invoice.py | 12 +++++++++++- modules/party/CHANGELOG | 1 + modules/party/party.py | 24 ++++++++++++++++++------ modules/purchase/purchase.py | 1 + modules/sale/sale.py | 2 ++ modules/stock/shipment.py | 19 +++++++++++++++++++ 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index 33ebc1f55c..e18ceb8f89 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -74,12 +74,16 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): _states['readonly'] | Eval('party', True) | Eval('lines', [0])), + }, + context={ + 'party_contact_mechanism_usage': 'invoice', }) company_party = fields.Function( fields.Many2One( 'party.party', "Company Party", context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, depends={'company'}), 'on_change_with_company_party') @@ -143,6 +147,7 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): 'party.party', 'Party', required=True, states=_states, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, depends={'company'}) party_tax_identifier = fields.Many2One( @@ -2134,6 +2139,7 @@ class InvoiceLine(sequence_ordered(), ModelSQL, ModelView, TaxableMixin): 'party.party', "Party", context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, depends=['company']), 'on_change_with_invoice_party', searcher='search_invoice_party') @@ -2158,6 +2164,7 @@ class InvoiceLine(sequence_ordered(), ModelSQL, ModelView, TaxableMixin): }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, depends={'company'}) party_lang = fields.Function(fields.Char('Party Language'), @@ -2167,7 +2174,10 @@ class InvoiceLine(sequence_ordered(), ModelSQL, ModelView, TaxableMixin): states=_states) company = fields.Many2One( 'company.company', "Company", required=True, - states=_states) + states=_states, + context={ + 'party_contact_mechanism_usage': 'invoice', + }) type = fields.Selection([ ('line', 'Line'), ('subtotal', 'Subtotal'), diff --git a/modules/party/CHANGELOG b/modules/party/CHANGELOG index 3b17d8f467..9e6dfab0b1 100644 --- a/modules/party/CHANGELOG +++ b/modules/party/CHANGELOG @@ -1,3 +1,4 @@ +* Use usage from context for default contact mechanism on party Version 7.0.0 - 2023-10-30 -------------------------- diff --git a/modules/party/party.py b/modules/party/party.py index 93c28a3368..d99713a990 100644 --- a/modules/party/party.py +++ b/modules/party/party.py @@ -178,15 +178,15 @@ class Party( return self.name def get_contact_mechanism(self, name): - for mechanism in self.contact_mechanisms: - if mechanism.type == name: - return mechanism.value - return '' + usage = Transaction().context.get('party_contact_mechanism_usage') + mechanism = self.contact_mechanism_get(name, usage) + return mechanism.value if mechanism else '' @classmethod def set_contact_mechanism(cls, parties, name, value): pool = Pool() ContactMechanism = pool.get('party.contact_mechanism') + usage = Transaction().context.get('party_contact_mechanism_usage') contact_mechanisms = [] for party in parties: if getattr(party, name): @@ -196,10 +196,22 @@ class Party( party=party.rec_name, field=type_string)) if value: - contact_mechanisms.append(ContactMechanism( + contact_mechanism = None + if usage: + for contact_mechanism in party.contact_mechanisms: + if (contact_mechanism.type == name + and contact_mechanism.value == value): + break + else: + contact_mechanism = None + if not contact_mechanism: + contact_mechanism = ContactMechanism( party=party, type=name, - value=value)) + value=value) + if usage: + setattr(contact_mechanism, usage, True) + contact_mechanisms.append(contact_mechanism) ContactMechanism.save(contact_mechanisms) @classmethod diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index 579c0b16d4..0d64f765df 100644 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -123,6 +123,7 @@ class Purchase( }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, search_context={ 'related_party': Eval('party'), diff --git a/modules/sale/sale.py b/modules/sale/sale.py index cff4d5f6e5..9c06fc5a50 100644 --- a/modules/sale/sale.py +++ b/modules/sale/sale.py @@ -124,6 +124,7 @@ class Sale( }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'invoice', }, search_context={ 'related_party': Eval('party'), @@ -145,6 +146,7 @@ class Sale( }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'delivery', }, search_context={ 'related_party': Eval('party'), diff --git a/modules/stock/shipment.py b/modules/stock/shipment.py index ddc7eb0109..3f02eca0cc 100644 --- a/modules/stock/shipment.py +++ b/modules/stock/shipment.py @@ -290,6 +290,9 @@ class ShipmentIn( states={ 'readonly': Eval('state') != 'draft', }, + context={ + 'party_contact_mechanism_usage': 'delivery', + }, help="The company the shipment is associated with.") supplier = fields.Many2One('party.party', 'Supplier', states={ @@ -299,6 +302,7 @@ class ShipmentIn( }, required=True, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'delivery', }, depends={'company'}, help="The party that supplied the stock.") @@ -755,6 +759,9 @@ class ShipmentInReturn(ShipmentAssignMixin, Workflow, ModelSQL, ModelView): states={ 'readonly': Eval('state') != 'draft', }, + context={ + 'party_contact_mechanism_usage': 'delivery', + }, help="The company the shipment is associated with.") supplier = fields.Many2One('party.party', 'Supplier', states={ @@ -764,6 +771,7 @@ class ShipmentInReturn(ShipmentAssignMixin, Workflow, ModelSQL, ModelView): }, required=True, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'delivery', }, depends={'company'}, help="The party that supplied the stock.") @@ -1123,6 +1131,9 @@ class ShipmentOut( states={ 'readonly': Eval('state') != 'draft', }, + context={ + 'party_contact_mechanism_usage': 'delivery', + }, help="The company the shipment is associated with.") customer = fields.Many2One('party.party', 'Customer', required=True, states={ @@ -1131,6 +1142,7 @@ class ShipmentOut( }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'delivery', }, depends={'company'}, help="The party that purchased the stock.") @@ -1829,6 +1841,9 @@ class ShipmentOutReturn( states={ 'readonly': Eval('state') != 'draft', }, + context={ + 'party_contact_mechanism_usage': 'delivery', + }, help="The company the shipment is associated with.") customer = fields.Many2One('party.party', 'Customer', required=True, states={ @@ -1837,6 +1852,7 @@ class ShipmentOutReturn( }, context={ 'company': Eval('company', -1), + 'party_contact_mechanism_usage': 'delivery', }, depends={'company'}, help="The party that purchased the stock.") @@ -2290,6 +2306,9 @@ class ShipmentInternal( states={ 'readonly': ~Eval('state').in_(['request', 'draft']), }, + context={ + 'party_contact_mechanism_usage': 'delivery', + }, help="The company the shipment is associated with.") from_location = fields.Many2One('stock.location', "From Location", required=True, states={