From 7091a502a23b2851c89e28cf472cdb95384b7886 Mon Sep 17 00:00:00 2001 From: Alexei Rivera Date: Tue, 14 Mar 2023 17:21:39 -0400 Subject: [PATCH] [IMP] account_commission: multi-currency support --- account_commission/__manifest__.py | 2 +- account_commission/models/account_move.py | 1 + .../models/commission_settlement.py | 7 +- account_commission/readme/CONTRIBUTORS.rst | 1 + .../tests/test_account_commission.py | 65 ++++++++++++++++++- .../views/account_move_views.xml | 7 +- .../wizards/commission_make_settle.py | 13 ++-- commission/README.rst | 9 ++- commission/__manifest__.py | 2 +- commission/readme/CONTRIBUTORS.rst | 1 + commission/readme/ROADMAP.rst | 2 - commission/static/description/index.html | 7 +- commission/tests/test_commission.py | 15 +++++ .../views/commission_settlement_views.xml | 13 +++- commission/wizards/commission_make_settle.py | 46 +++++++++---- 15 files changed, 155 insertions(+), 36 deletions(-) diff --git a/account_commission/__manifest__.py b/account_commission/__manifest__.py index c87663d62..bc18a4a60 100644 --- a/account_commission/__manifest__.py +++ b/account_commission/__manifest__.py @@ -3,7 +3,7 @@ # Copyright 2014-2022 Tecnativa - Pedro M. Baeza { "name": "Account commissions", - "version": "15.0.2.1.0", + "version": "15.0.2.1.1", "author": "Tecnativa, Odoo Community Association (OCA)", "category": "Sales Management", "license": "AGPL-3", diff --git a/account_commission/models/account_move.py b/account_commission/models/account_move.py index 019fe622f..526b71795 100644 --- a/account_commission/models/account_move.py +++ b/account_commission/models/account_move.py @@ -199,6 +199,7 @@ class AccountInvoiceLineAgent(models.Model): currency_id = fields.Many2one( related="object_id.currency_id", readonly=True, + store=True, ) @api.depends( diff --git a/account_commission/models/commission_settlement.py b/account_commission/models/commission_settlement.py index 1f36422e0..a8772f989 100644 --- a/account_commission/models/commission_settlement.py +++ b/account_commission/models/commission_settlement.py @@ -78,6 +78,9 @@ def action_invoice(self): def _get_invoice_partner(self): return self[0].agent_id + def _get_invoice_currency(self): + return self[0].currency_id + def _prepare_invoice(self, journal, product, date=False): move_form = Form( self.env["account.move"].with_context(default_move_type="in_invoice") @@ -85,8 +88,10 @@ def _prepare_invoice(self, journal, product, date=False): if date: move_form.invoice_date = date partner = self._get_invoice_partner() + currency = self._get_invoice_currency() move_form.partner_id = partner move_form.journal_id = journal + move_form.currency_id = currency for settlement in self: with move_form.invoice_line_ids.new() as line_form: line_form.product_id = product @@ -118,7 +123,7 @@ def _prepare_invoice(self, journal, product, date=False): return vals def _get_invoice_grouping_keys(self): - return ["company_id", "agent_id"] + return ["company_id", "currency_id", "agent_id"] def make_invoices(self, journal, product, date=False, grouped=False): invoice_vals_list = [] diff --git a/account_commission/readme/CONTRIBUTORS.rst b/account_commission/readme/CONTRIBUTORS.rst index 77b751822..35e90bee4 100644 --- a/account_commission/readme/CONTRIBUTORS.rst +++ b/account_commission/readme/CONTRIBUTORS.rst @@ -7,6 +7,7 @@ * Oihane Crucelaegui * Nicola Malcontenti * Aitor Bouzas +* Alexei Rivera * `Tecnativa `__: diff --git a/account_commission/tests/test_account_commission.py b/account_commission/tests/test_account_commission.py index 142ceba31..d4eb18349 100644 --- a/account_commission/tests/test_account_commission.py +++ b/account_commission/tests/test_account_commission.py @@ -53,11 +53,13 @@ def setUpClass(cls): limit=1, ) - def _create_invoice(self, agent, commission, date=None): + def _create_invoice(self, agent, commission, date=None, currency=None): invoice_form = Form( self.env["account.move"].with_context(default_move_type="out_invoice") ) invoice_form.partner_id = self.partner + if currency: + invoice_form.currency_id = currency with invoice_form.invoice_line_ids.new() as line_form: line_form.product_id = self.product if date: @@ -419,3 +421,64 @@ def test_account_commission_multiple_settlement_ids(self): settlements.make_invoices(self.journal, self.commission_product, grouped=True) invoices = settlements.mapped("invoice_id") self.assertEqual(2, invoices.settlement_count) + + def test_multi_currency(self): + commission = self.commission_net_invoice + agent = self.agent_monthly + today = fields.Date.today() + last_month = today + relativedelta(months=-1) + + # creating invoices with different currencies, same date + invoice = self._create_invoice(agent, commission, today, currency=None) + invoice.action_post() + invoice1 = self._create_invoice(agent, commission, today, self.foreign_currency) + invoice1.action_post() + + # check settlement creation + self._settle_agent_invoice(agent, 1) + settlements = self.settle_model.search( + [ + ("agent_id", "=", agent.id), + ("state", "=", "settled"), + ] + ) + self.assertEqual(2, len(settlements)) + self.assertEqual(2, len(settlements.mapped("currency_id"))) + + # creating some additional invoices + invoice2 = self._create_invoice(agent, commission, today, self.foreign_currency) + invoice2.action_post() + invoice3 = self._create_invoice(agent, commission, last_month, self.foreign_currency) + invoice3.action_post() + invoice4 = self._create_invoice(agent, commission, last_month, self.foreign_currency) + invoice4.action_post() + + # check settlement creation + self._settle_agent_invoice(agent, 1) + settlements = self.settle_model.search( + [ + ("agent_id", "=", agent.id), + ("state", "=", "settled"), + ] + ) + self.assertEqual(3, len(settlements)) + + # check commission invoices + settlements.make_invoices(self.journal, self.commission_product) + invoices = settlements.mapped("invoice_id") + self.assertEqual(3, len(invoices)) + + # check settlement creation after a commission invoicing process + # (previous settlements were already invoiced) + invoice5 = self._create_invoice(agent, commission, today) + invoice5.action_post() + invoice6 = self._create_invoice(agent, commission, today, self.foreign_currency) + invoice6.action_post() + self._settle_agent_invoice(agent, 1) + settlements = self.settle_model.search( + [ + ("agent_id", "=", agent.id), + ("state", "=", "settled"), + ] + ) + self.assertEqual(2, len(settlements)) diff --git a/account_commission/views/account_move_views.xml b/account_commission/views/account_move_views.xml index e6a65c4cb..6cacb0333 100644 --- a/account_commission/views/account_move_views.xml +++ b/account_commission/views/account_move_views.xml @@ -10,7 +10,12 @@ name="commission_id" domain="['|', ('settlement_type', '=', 'sale_invoice'), ('settlement_type', '=', False)]" /> - + + diff --git a/account_commission/wizards/commission_make_settle.py b/account_commission/wizards/commission_make_settle.py index d2e5c32c5..c4870e325 100644 --- a/account_commission/wizards/commission_make_settle.py +++ b/account_commission/wizards/commission_make_settle.py @@ -14,16 +14,19 @@ class CommissionMakeSettle(models.TransientModel): ondelete={"sale_invoice": "cascade"}, ) + def _get_account_settle_domain(self, agent, date_to_agent): + return [ + ("invoice_date", "<", date_to_agent), + ("agent_id", "=", agent.id), + ("settled", "=", False), + ] + def _get_agent_lines(self, agent, date_to_agent): """Filter sales invoice agent lines for this type of settlement.""" if self.settlement_type != "sale_invoice": return super()._get_agent_lines(agent, date_to_agent) return self.env["account.invoice.line.agent"].search( - [ - ("invoice_date", "<", date_to_agent), - ("agent_id", "=", agent.id), - ("settled", "=", False), - ], + self._get_account_settle_domain(agent, date_to_agent), order="invoice_date", ) diff --git a/commission/README.rst b/commission/README.rst index 1ea503bcc..f97dbc143 100644 --- a/commission/README.rst +++ b/commission/README.rst @@ -19,9 +19,9 @@ Commissions .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png :target: https://translation.odoo-community.org/projects/commission-15-0/commission-15-0-commission :alt: Translate me on Weblate -.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/165/15.0 - :alt: Try me on Runbot +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/commission&target_branch=15.0 + :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -109,8 +109,6 @@ Known issues / Roadmap ====================== * Make it totally multi-company aware. -* Be multi-currency aware for settlements. -* Allow to calculate and pay in other currency different from company one. * Set agent popup window with a kanban view with richer information and mobile friendly. @@ -144,6 +142,7 @@ Contributors * Oihane Crucelaegui * Nicola Malcontenti * Aitor Bouzas +* Alexei Rivera * `Tecnativa `__: diff --git a/commission/__manifest__.py b/commission/__manifest__.py index 57a56ccc5..ecf8f897e 100644 --- a/commission/__manifest__.py +++ b/commission/__manifest__.py @@ -3,7 +3,7 @@ # Copyright 2014-2022 Tecnativa - Pedro M. Baeza { "name": "Commissions", - "version": "15.0.2.0.0", + "version": "15.0.2.1.0", "author": "Tecnativa, Odoo Community Association (OCA)", "category": "Invoicing", "license": "AGPL-3", diff --git a/commission/readme/CONTRIBUTORS.rst b/commission/readme/CONTRIBUTORS.rst index 77b751822..35e90bee4 100644 --- a/commission/readme/CONTRIBUTORS.rst +++ b/commission/readme/CONTRIBUTORS.rst @@ -7,6 +7,7 @@ * Oihane Crucelaegui * Nicola Malcontenti * Aitor Bouzas +* Alexei Rivera * `Tecnativa `__: diff --git a/commission/readme/ROADMAP.rst b/commission/readme/ROADMAP.rst index 64f69a38f..b4bdd9c35 100644 --- a/commission/readme/ROADMAP.rst +++ b/commission/readme/ROADMAP.rst @@ -1,5 +1,3 @@ * Make it totally multi-company aware. -* Be multi-currency aware for settlements. -* Allow to calculate and pay in other currency different from company one. * Set agent popup window with a kanban view with richer information and mobile friendly. diff --git a/commission/static/description/index.html b/commission/static/description/index.html index 322f674c1..ddfb0ada8 100644 --- a/commission/static/description/index.html +++ b/commission/static/description/index.html @@ -3,7 +3,7 @@ - + Commissions