Skip to content

Commit

Permalink
[IMP] commission: Add new settlement period (All pending records) to …
Browse files Browse the repository at this point in the history
…settle in only one settlement

TT48574
  • Loading branch information
sergio-teruel committed Sep 26, 2024
1 parent ee4ff57 commit 44b10b5
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 8 deletions.
5 changes: 3 additions & 2 deletions account_commission/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Account commissions
===================

..
..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
Expand Down Expand Up @@ -133,6 +133,7 @@ Contributors

- Pedro M. Baeza
- Manuel Calero
- Sergio Teruel

- `Quartile <https://www.quartile.co>`__:

Expand Down Expand Up @@ -166,7 +167,7 @@ promote its widespread use.

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-pedrobaeza|
|maintainer-pedrobaeza|

This module is part of the `OCA/commission <https://github.com/OCA/commission/tree/17.0/account_commission>`_ project on GitHub.

Expand Down
21 changes: 21 additions & 0 deletions account_commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
* Pexego.
* Davide Corio <[email protected]>
* Joao Alfredo Gama Batista <[email protected]>
* Sandy Carter <[email protected]>
* Giorgio Borelli <[email protected]>
* Daniel Campos <[email protected]>
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Alexei Rivera <[email protected]>

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

* Pedro M. Baeza
* Manuel Calero
* Sergio Teruel

* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin
* Yoshi Tashiro
3 changes: 2 additions & 1 deletion account_commission/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ <h1 class="title">Account commissions</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0fb13edf183d9d1ecf7c4415929fabd687f91bd51e719c0e149a7148c57da4ef
!! source digest: sha256:5788ea4c8f7cbff1e1296b584c797d90bca62e947922efbccef2b06e5dcc79b6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" 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 image-reference" 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 image-reference" href="https://github.com/OCA/commission/tree/17.0/account_commission"><img alt="OCA/commission" src="https://img.shields.io/badge/github-OCA%2Fcommission-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/commission-17-0/commission-17-0-account_commission"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/commission&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds the function to calculate commissions in invoices
Expand Down Expand Up @@ -479,6 +479,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Pedro M. Baeza</li>
<li>Manuel Calero</li>
<li>Sergio Teruel</li>
</ul>
</li>
<li><a class="reference external" href="https://www.quartile.co">Quartile</a>:<ul>
Expand Down
161 changes: 160 additions & 1 deletion account_commission/tests/test_account_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from odoo import fields
from odoo.exceptions import UserError, ValidationError
from odoo.tests import tagged
from odoo.tests import Form, tagged

from odoo.addons.commission.tests.test_commission import TestCommissionBase

Expand Down Expand Up @@ -532,3 +532,162 @@ def test_multi_currency(self):
]
)
self.assertEqual(2, len(settlements))

def test_invoice_partial_refund(self):
commission = self.commission_net_paid
agent = self.agent_monthly
today = fields.Date.today()
# Create an invoice
invoice = self._create_invoice(agent, commission, today, currency=None)
invoice.action_post()
# Register payment for invoice
payment_journal = self.env["account.journal"].search(
[("type", "=", "cash"), ("company_id", "=", invoice.company_id.id)],
limit=1,
)
register_payments = (
self.env["account.payment.register"]
.with_context(active_ids=invoice.id, active_model="account.move")
.create({"journal_id": payment_journal.id})
)
register_payments.action_create_payments()
# Make a parcial refund for the invoice
move_reversal = (
self.env["account.move.reversal"]
.with_context(active_model="account.move", active_ids=invoice.id)
.create(
{
"reason": "no reason",
"journal_id": invoice.journal_id.id,
}
)
)
refund_form = Form(
self.env["account.move"].browse(move_reversal.reverse_moves()["res_id"])
)
with refund_form.invoice_line_ids.edit(0) as line:
line.price_unit -= 2
refund = refund_form.save()
refund.action_post()
# Register payment for the refund
register_payments = (
self.env["account.payment.register"]
.with_context(active_ids=refund.id, active_model="account.move")
.create({"journal_id": payment_journal.id})
)
register_payments.action_create_payments()
# check settlement creation. The commission must be (5 - 3) * 0.1 = 0.4
self._settle_agent_invoice(agent, 1)
settlements = self.settle_model.search([("agent_id", "=", agent.id)])
self.assertEqual(2, len(settlements.line_ids))
self.assertEqual(0.4, sum(settlements.mapped("total")))

def test_invoice_full_refund(self):
commission = self.commission_net_paid
agent = self.agent_monthly
today = fields.Date.today()
# Create an invoice and refund it
invoice = self._create_invoice(agent, commission, today, currency=None)
invoice.action_post()
move_reversal = (
self.env["account.move.reversal"]
.with_context(active_model="account.move", active_ids=invoice.id)
.create(
{
"reason": "no reason",
"journal_id": invoice.journal_id.id,
}
)
)
move_reversal.reverse_moves()
# check settlement creation. The commission must be: (5 - 5) * 0.1 = 0
self._settle_agent_invoice(agent, 1)
settlements = self.settle_model.search(
[
("agent_id", "=", agent.id),
]
)
self.assertEqual(2, len(settlements.line_ids))
self.assertEqual(0, sum(settlements.mapped("total")))

def test_invoice_modify_refund(self):
commission = self.commission_net_paid
agent = self.agent_monthly
today = fields.Date.today()
# Create an invoice
invoice = self._create_invoice(agent, commission, today, currency=None)
invoice.action_post()
# Create a full refund and a new invoice
move_reversal = (
self.env["account.move.reversal"]
.with_context(active_model="account.move", active_ids=invoice.id)
.create(
{
"reason": "no reason",
"journal_id": invoice.journal_id.id,
}
)
)
invoice2_form = Form(
self.env["account.move"].browse(move_reversal.reverse_moves()["res_id"])
)
with invoice2_form.invoice_line_ids.edit(0) as line:
line.price_unit -= 2
invoice2 = invoice2_form.save()
invoice2.action_post()
# Register payment for the new invoice
payment_journal = self.env["account.journal"].search(
[("type", "=", "cash"), ("company_id", "=", invoice.company_id.id)],
limit=1,
)
register_payments = (
self.env["account.payment.register"]
.with_context(active_ids=invoice2.id, active_model="account.move")
.create({"journal_id": payment_journal.id})
)
register_payments.action_create_payments()

# check settlement creation. The commission must be (5 - 5 + 3) * 0.1 = 0.6
self._settle_agent_invoice(agent, 1)
settlements = self.settle_model.search(
[
("agent_id", "=", agent.id),
]
)
self.assertEqual(3, len(settlements.line_ids))
self.assertAlmostEqual(0.6, sum(settlements.mapped("total")), 2)

def _register_payment(self, invoice):
payment_journal = self.env["account.journal"].search(
[("type", "=", "cash"), ("company_id", "=", self.env.company.id)],
limit=1,
)
register_payments = (
self.env["account.payment.register"]
.with_context(active_ids=invoice.id, active_model="account.move")
.create({"journal_id": payment_journal.id})
)
register_payments.action_create_payments()

def test_invoice_pending_settlement(self):
"""Make in one settlement all pending invoices to wizard date"""
fields.Date.today()
self.commission_net_paid.invoice_state = "paid"
invoice1 = self._create_invoice(
self.agent_pending, self.commission_net_paid, "2024-02-15", currency=None
)
# Register payment for the new invoice
invoice2 = self._create_invoice(
self.agent_pending, self.commission_net_paid, "2024-03-15", currency=None
)
invoice3 = self._create_invoice(
self.agent_pending, self.commission_net_paid, "2024-04-15", currency=None
)
# invoice1.invoice_line_ids.agent_ids._compute_amount()
(invoice1 + invoice2 + invoice3).action_post()
self._register_payment(invoice1)
self._register_payment(invoice2)
self._register_payment(invoice3)
self._settle_agent_invoice(self.agent_pending, 1)
settlements = self.settle_model.search([("state", "=", "settled")])
self.assertEqual(len(settlements.line_ids), 3)
5 changes: 3 additions & 2 deletions commission/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Commissions
===========

..
..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
Expand Down Expand Up @@ -156,6 +156,7 @@ Contributors

- Pedro M. Baeza
- Manuel Calero
- Sergio Teruel

- `Quartile <https://www.quartile.co>`__:

Expand Down Expand Up @@ -185,7 +186,7 @@ promote its widespread use.

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-pedrobaeza|
|maintainer-pedrobaeza|

This module is part of the `OCA/commission <https://github.com/OCA/commission/tree/17.0/commission>`_ project on GitHub.

Expand Down
1 change: 1 addition & 0 deletions commission/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ResPartner(models.Model):
("quaterly", "Quarterly"),
("semi", "Semi-annual"),
("annual", "Annual"),
("pending", "Pending commissions"),
],
string="Settlement period",
default="monthly",
Expand Down
21 changes: 21 additions & 0 deletions commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
* Pexego.
* Davide Corio <[email protected]>
* Joao Alfredo Gama Batista <[email protected]>
* Sandy Carter <[email protected]>
* Giorgio Borelli <[email protected]>
* Daniel Campos <[email protected]>
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Alexei Rivera <[email protected]>

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

* Pedro M. Baeza
* Manuel Calero
* Sergio Teruel

* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin
* Yoshi Tashiro
3 changes: 2 additions & 1 deletion commission/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ <h1 class="title">Commissions</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0f54149f86b86660382708274a8cb05ec4cabea68b0ede1531e0147bb309d5ab
!! source digest: sha256:40d29c55743992c85878e87e4534c3ea18a1fc387a8fc20cbbde1b529abce7c0
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" 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 image-reference" 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 image-reference" href="https://github.com/OCA/commission/tree/17.0/commission"><img alt="OCA/commission" src="https://img.shields.io/badge/github-OCA%2Fcommission-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/commission-17-0/commission-17-0-commission"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/commission&amp;target_branch=17.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
Expand Down Expand Up @@ -506,6 +506,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Pedro M. Baeza</li>
<li>Manuel Calero</li>
<li>Sergio Teruel</li>
</ul>
</li>
<li><a class="reference external" href="https://www.quartile.co">Quartile</a>:<ul>
Expand Down
8 changes: 8 additions & 0 deletions commission/tests/test_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def setUpClass(cls):
"lang": "en_US",
}
)
cls.agent_pending = cls.res_partner_model.create(
{
"name": "Test Agent - Pending",
"agent": True,
"settlement": "pending",
"lang": "en_US",
}
)

# Expected to be used in inheriting modules.
def _get_make_settle_vals(self, agent=None, period=None, date=None):
Expand Down
8 changes: 7 additions & 1 deletion commission/wizards/commission_make_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def _get_period_start(self, agent, date_to):
return date(month=1, year=date_to.year, day=1)
elif agent.settlement == "annual":
return date(month=1, year=date_to.year, day=1)
elif agent.settlement == "pending":
return date(month=date_to.month, year=date_to.year, day=date_to.day)

def _get_next_period_date(self, agent, current_date):
if agent.settlement == "monthly":
Expand All @@ -66,6 +68,10 @@ def _get_next_period_date(self, agent, current_date):
return current_date + relativedelta(months=6)
elif agent.settlement == "annual":
return current_date + relativedelta(years=1)
elif agent.settlement == "pending":
return date(
month=self.date_to.month, year=self.date_to.year, day=self.date_to.day
)

def _get_settlement(self, agent, company, currency, sett_from, sett_to):
self.ensure_one()
Expand Down Expand Up @@ -97,7 +103,7 @@ def _prepare_settlement_line_vals(self, settlement, line):
"settlement_id": settlement.id,
}

def _get_agent_lines(self, date_to_agent):
def _get_agent_lines(self, agent, date_to_agent):
"""Need to be extended according to settlement_type."""
raise NotImplementedError()

Expand Down

0 comments on commit 44b10b5

Please sign in to comment.