From 28e12a668d98fbe96a38313cb8f4a4e7f684b5b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 11 Sep 2022 01:10:29 +0200 Subject: [PATCH] Allow to store currency on opportunity issue11679 review441381003 --- CHANGELOG | 2 ++ opportunity.py | 40 +++++++++++++++++++++++++++++---------- view/opportunity_form.xml | 3 ++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 020e072225..97d3c24759 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Allow to store currency on opportunity + Version 6.4.0 - 2022-05-02 * Bug fixes (see mercurial logs for details) * Use Date field for monthly report diff --git a/opportunity.py b/opportunity.py index c5abcd8306..3c8a88b31a 100644 --- a/opportunity.py +++ b/opportunity.py @@ -72,9 +72,8 @@ class SaleOpportunity( ('id', If(In('company', Eval('context', {})), '=', '!='), Get(Eval('context', {}), 'company', 0)), ]) - currency = fields.Function(fields.Many2One( - 'currency.currency', "Currency"), - 'on_change_with_currency') + currency = fields.Many2One( + 'currency.currency', "Currency", required=True, states=_states_start) amount = Monetary( "Amount", currency='currency', digits='currency', states=_states_stop, @@ -130,15 +129,18 @@ class SaleOpportunity( @classmethod def __register__(cls, module_name): pool = Pool() + Company = pool.get('company.company') Sale = pool.get('sale.sale') transaction = Transaction() cursor = transaction.connection.cursor() update = transaction.connection.cursor() sql_table = cls.__table__() + company = Company.__table__() sale = Sale.__table__() table = cls.__table_handler__(module_name) number_exists = table.column_exist('number') + currency_exists = table.column_exist('currency') # Migration from 3.8: rename reference into number if table.column_exist('reference') and not number_exists: @@ -174,6 +176,15 @@ class SaleOpportunity( # Migration from 5.0: drop required on description table.not_null_action('description', action='remove') + # Migration from 6.4: store currency + if not currency_exists: + value = company.select( + company.currency, + where=(sql_table.company == company.id)) + cursor.execute(*sql_table.update( + [sql_table.currency], + [value])) + @classmethod def __setup__(cls): super(SaleOpportunity, cls).__setup__() @@ -245,6 +256,16 @@ class SaleOpportunity( def default_company(): return Transaction().context.get('company') + @classmethod + def default_currency(cls, **pattern): + pool = Pool() + Company = pool.get('company.company') + company = pattern.get('company') + if not company: + company = cls.default_company() + if company: + return Company(company).currency.id + @staticmethod def default_employee(): return Transaction().context.get('employee') @@ -294,17 +315,16 @@ class SaleOpportunity( default.setdefault('converted_by') return super(SaleOpportunity, cls).copy(opportunities, default=default) - @fields.depends('company') - def on_change_with_currency(self, name=None): - if self.company: - return self.company.currency.id - - @fields.depends('party') + @fields.depends('party', 'amount', 'company') def on_change_party(self): if self.party and self.party.customer_payment_term: self.payment_term = self.party.customer_payment_term else: self.payment_term = self.default_payment_term() + if self.party: + if not self.amount: + if self.party.customer_currency: + self.currency = self.party.customer_currency def _get_sale_opportunity(self): ''' @@ -319,7 +339,7 @@ class SaleOpportunity( company=self.company, invoice_address=self.address, shipment_address=self.address, - currency=self.company.currency, + currency=self.currency, comment=self.comment, sale_date=None, origin=self, diff --git a/view/opportunity_form.xml b/view/opportunity_form.xml index 799a3406f3..6ce671fa45 100644 --- a/view/opportunity_form.xml +++ b/view/opportunity_form.xml @@ -31,7 +31,8 @@ this repository contains the full copyright notices and license terms. -->