Skip to content

Commit

Permalink
[IMP] account_commission: multi-currency support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeirivera87 committed Mar 18, 2023
1 parent 768b0ce commit 7091a50
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 36 deletions.
2 changes: 1 addition & 1 deletion account_commission/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions account_commission/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class AccountInvoiceLineAgent(models.Model):
currency_id = fields.Many2one(
related="object_id.currency_id",
readonly=True,
store=True,
)

@api.depends(
Expand Down
7 changes: 6 additions & 1 deletion account_commission/models/commission_settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,20 @@ 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")
)
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
Expand Down Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions account_commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Alexei Rivera <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`__:

Expand Down
65 changes: 64 additions & 1 deletion account_commission/tests/test_account_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
7 changes: 6 additions & 1 deletion account_commission/views/account_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
name="commission_id"
domain="['|', ('settlement_type', '=', 'sale_invoice'), ('settlement_type', '=', False)]"
/>
<field name="amount" />
<field
name="amount"
widget="monetary"
options="{'currency_field': 'currency_id'}"
/>
<field name="currency_id" invisible="1" />
</tree>
</field>
</record>
Expand Down
13 changes: 8 additions & 5 deletions account_commission/wizards/commission_make_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
9 changes: 4 additions & 5 deletions commission/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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|

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -144,6 +142,7 @@ Contributors
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Alexei Rivera <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`__:

Expand Down
2 changes: 1 addition & 1 deletion commission/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Alexei Rivera <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`__:

Expand Down
2 changes: 0 additions & 2 deletions commission/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 3 additions & 4 deletions commission/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Commissions</title>
<style type="text/css">

Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Commissions</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/commission/tree/15.0/commission"><img alt="OCA/commission" src="https://img.shields.io/badge/github-OCA%2Fcommission-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/commission-15-0/commission-15-0-commission"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/165/15.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/commission/tree/15.0/commission"><img alt="OCA/commission" src="https://img.shields.io/badge/github-OCA%2Fcommission-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/commission-15-0/commission-15-0-commission"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/commission&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module provides the base functions for commission operations to enable the
following:</p>
<ul class="simple">
Expand Down Expand Up @@ -466,8 +466,6 @@ <h1><a class="toc-backref" href="#id2">Usage</a></h1>
<h1><a class="toc-backref" href="#id3">Known issues / Roadmap</a></h1>
<ul class="simple">
<li>Make it totally multi-company aware.</li>
<li>Be multi-currency aware for settlements.</li>
<li>Allow to calculate and pay in other currency different from company one.</li>
<li>Set agent popup window with a kanban view with richer information and
mobile friendly.</li>
</ul>
Expand Down Expand Up @@ -500,6 +498,7 @@ <h2><a class="toc-backref" href="#id7">Contributors</a></h2>
<li>Oihane Crucelaegui &lt;<a class="reference external" href="mailto:oihanecruce&#64;gmail.com">oihanecruce&#64;gmail.com</a>&gt;</li>
<li>Nicola Malcontenti &lt;<a class="reference external" href="mailto:nicola.malcontenti&#64;agilebg.com">nicola.malcontenti&#64;agilebg.com</a>&gt;</li>
<li>Aitor Bouzas &lt;<a class="reference external" href="mailto:aitor.bouzas&#64;adaptivecity.com">aitor.bouzas&#64;adaptivecity.com</a>&gt;</li>
<li>Alexei Rivera &lt;<a class="reference external" href="mailto:arivera&#64;archeti.com">arivera&#64;archeti.com</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Pedro M. Baeza</li>
<li>Manuel Calero</li>
Expand Down
15 changes: 15 additions & 0 deletions commission/tests/test_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ def setUpClass(cls):
}
)
cls.company = cls.env.ref("base.main_company")
cls.foreign_currency = cls.env["res.currency"].create(
{
"name": "Coin X",
"rounding": 0.01,
"symbol": "CX",
}
)
cls.rate = cls.env["res.currency.rate"].create(
{
"company_id": cls.company.id,
"currency_id": cls.foreign_currency.id,
"name": "2023-01-01",
"rate": 25,
}
)
cls.res_partner_model = cls.env["res.partner"]
cls.partner = cls.env.ref("base.res_partner_2")
cls.partner.write({"agent": False})
Expand Down
13 changes: 11 additions & 2 deletions commission/views/commission_settlement_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<field name="date_from" />
<field name="date_to" />
<field name="settlement_type" />
<field name="total" sum="Settled total" />
<field
name="total"
sum="Settled total"
widget='monetary'
options="{'currency_field': 'currency_id'}"
/>
<field name="state" />
<field name="currency_id" invisible="1" />
</tree>
</field>
</record>
Expand Down Expand Up @@ -73,7 +79,10 @@
groups="base.group_multi_company"
/>
<field name="date_to" />
<field name="currency_id" invisible="1" />
<field
name="currency_id"
groups="base.group_multi_currency"
/>
<field name="agent_type" invisible="1" />
</group>
</group>
Expand Down
Loading

0 comments on commit 7091a50

Please sign in to comment.