Skip to content

Commit

Permalink
[IMP] account_commission: Settle based on payment date
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Dec 5, 2024
1 parent 39566f7 commit cad3084
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 19 deletions.
10 changes: 8 additions & 2 deletions account_commission/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ For selecting invoice status in commissions:

* **Invoice Based**: Commissions are settled when the invoice is issued.
* **Payment Based**: Commissions are settled when the invoice is paid or refunded.
Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.
* **Payment Date Based**: Commissions are settled based on the payment date.

Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.

Usage
=====
Expand Down Expand Up @@ -139,6 +141,10 @@ Contributors

* Ethan Hildick

* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>

Maintainers
~~~~~~~~~~~

Expand Down
13 changes: 11 additions & 2 deletions account_commission/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2014-2022 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import datetime

from lxml import etree

Expand Down Expand Up @@ -248,9 +249,17 @@ def _check_settle_integrity(self):
def _skip_settlement(self):
self.ensure_one()
return (
self.commission_id.invoice_state == "paid"
self.commission_id.invoice_state in ("paid", "paid_date")
and self.invoice_id.payment_state not in ["in_payment", "paid", "reversed"]
) or self.invoice_id.state != "posted"

def _get_commission_settlement_date(self):
return self.invoice_date
if self.commission_id.invoice_state == "paid_date":
payments = self.invoice_id._get_reconciled_amls().move_id
commission_settlement_date = max(
payments.mapped("date"),
default=datetime.min,
)
else:
commission_settlement_date = self.invoice_date
return commission_settlement_date
9 changes: 7 additions & 2 deletions account_commission/models/commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ class Commission(models.Model):
_inherit = "commission"

invoice_state = fields.Selection(
[("open", "Invoice Based"), ("paid", "Payment Based")],
[
("open", "Invoice Based"),
("paid", "Payment Based"),
("paid_date", "Payment Date Based"),
],
string="Invoice Status",
default="open",
help="Select the invoice status for settling the commissions:\n"
"* 'Invoice Based': Commissions are settled when the invoice is issued.\n"
"* 'Payment Based': Commissions are settled when the invoice is paid (or refunded).",
"* 'Payment Based': Commissions are settled when the invoice is paid (or refunded)\n."
"* 'Payment Date Based': Commissions are settled based on the payment date.",
)
6 changes: 4 additions & 2 deletions account_commission/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ For selecting invoice status in commissions:

* **Invoice Based**: Commissions are settled when the invoice is issued.
* **Payment Based**: Commissions are settled when the invoice is paid or refunded.
Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.
* **Payment Date Based**: Commissions are settled based on the payment date.

Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.
4 changes: 4 additions & 0 deletions account_commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
* `Studio73 <https://www.studio73.es>`__:

* Ethan Hildick

* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>
16 changes: 11 additions & 5 deletions account_commission/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,15 @@ <h1 class="title">Account commissions</h1>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<p>For selecting invoice status in commissions:</p>
<ol class="arabic simple">
<li>Edit or create a new record to select the invoice status for settling the commissions.<ul>
<ol class="arabic">
<li><p class="first">Edit or create a new record to select the invoice status for settling the commissions.</p>
<ul class="simple">
<li><strong>Invoice Based</strong>: Commissions are settled when the invoice is issued.</li>
<li><strong>Payment Based</strong>: Commissions are settled when the invoice is paid or refunded.
Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.</li>
<li><strong>Payment Based</strong>: Commissions are settled when the invoice is paid or refunded.</li>
<li><strong>Payment Date Based</strong>: Commissions are settled based on the payment date.</li>
</ul>
<p>Note that when refunding an invoice, the corresponding reversed commission will
be settled as well, resulting in a 0 net commission between both operations.</p>
</li>
</ol>
</div>
Expand Down Expand Up @@ -486,6 +488,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Ethan Hildick</li>
</ul>
</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
46 changes: 44 additions & 2 deletions account_commission/tests/test_account_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,19 @@ def test_invoice_modify_refund(self):
self.assertEqual(3, len(settlements.line_ids))
self.assertAlmostEqual(0.6, sum(settlements.mapped("total")), 2)

def _register_payment(self, invoice):
def _register_payment(self, invoice, payment_wiz_vals=None):
if payment_wiz_vals is None:
payment_wiz_vals = {}

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

Expand All @@ -694,3 +698,41 @@ def test_invoice_pending_settlement(self):
self._settle_agent_invoice(self.agent_pending, 1)
settlements = self.settle_model.search([("state", "=", "settled")])
self.assertEqual(len(settlements.line_ids), 3)

def test_settle_invoice_on_payment_date(self):
"""When commission is "Payment Date Based",
the commission is settled based on the date of the payment.
"""
# Arrange
commission = self.commission_model.create(
{
"name": "20% based on payment date",
"fix_qty": 20.0,
"amount_base_type": "net_amount",
"invoice_state": "paid_date",
}
)
agent = self.agent_monthly
agent.commission_id = commission
invoice = self._create_invoice(
agent,
commission,
date="2020-01-15",
)
invoice.action_post()
self._register_payment(
invoice,
payment_wiz_vals={
"payment_date": "2020-02-15",
},
)

# Act
self._settle_agent_invoice(agent=agent, date="2020-02-01")
january_settlement = self.settle_model.search([("state", "=", "settled")])
self._settle_agent_invoice(agent=agent, date="2020-03-01")
february_settlement = self.settle_model.search([("state", "=", "settled")])

# Assert
self.assertFalse(january_settlement)
self.assertEqual(invoice, february_settlement.line_ids.invoice_line_id.move_id)
15 changes: 11 additions & 4 deletions account_commission/wizards/commission_make_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ class CommissionMakeSettle(models.TransientModel):
)

def _get_account_settle_domain(self, agent, date_to_agent):
to_settle_agent_lines = self.env["account.invoice.line.agent"].search(
[
("agent_id", "=", agent.id),
("settled", "=", False),
("object_id.display_type", "=", "product"),
],
)
to_settle_agent_lines = to_settle_agent_lines.filtered(
lambda al: al._get_commission_settlement_date() < date_to_agent
)
return [
("invoice_date", "<", date_to_agent),
("agent_id", "=", agent.id),
("settled", "=", False),
("object_id.display_type", "=", "product"),
("id", "in", to_settle_agent_lines.ids),
]

def _get_agent_lines(self, agent, date_to_agent):
Expand Down

0 comments on commit cad3084

Please sign in to comment.