Skip to content

Commit

Permalink
[IMP]sale_commission:settle commissions based on payment date
Browse files Browse the repository at this point in the history
  • Loading branch information
matteonext authored and stenext committed Sep 19, 2023
1 parent a1fc50a commit 217c77b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
13 changes: 13 additions & 0 deletions sale_commission/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ def _skip_settlement(self):
self.commission_id.invoice_state == "paid"
and self.invoice_id.payment_state not in ["in_payment", "paid"]
) or self.invoice_id.state != "posted"

def _skip_future_payments(self, date_payment_to):
if self.commission_id.invoice_state == "paid":
payments_dates = []
for (
_partial,
_amount,
counterpart_line,
) in self.invoice_id._get_reconciled_invoices_partials():
payments_dates.append(counterpart_line.date)
if any(date_payment_to < date for date in payments_dates):
return True
return False
2 changes: 2 additions & 0 deletions sale_commission/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Oihane Crucelaegui <[email protected]>
* Nicola Malcontenti <[email protected]>
* Aitor Bouzas <[email protected]>
* Nextev <[email protected]>


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

Expand Down
2 changes: 2 additions & 0 deletions sale_commission/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ For settling the commissions to agents:
want to create commissions. It should be at least one day after the last
period date. For example, if you settlements are monthly, you have to put
at least the first day of the following month.
#. You can select to settle commissions up to a specified payment date.
This only applies to commission types with "invoice status=payment based".
#. You can settle only certain agents if you select them on the "Agents"
section. Leave it empty for settling all.
#. Click on "Make settlements" button.
Expand Down
69 changes: 66 additions & 3 deletions sale_commission/tests/test_sale_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright 2020 Tecnativa - Manuel Calero
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html

import dateutil.relativedelta
from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import fields
Expand Down Expand Up @@ -85,6 +86,15 @@ def setUpClass(cls):
"commission_id": cls.commission_net_invoice.id,
}
)
cls.agent_monthly_paid = cls.res_partner_model.create(
{
"name": "Test Agent - Monthly Net Paid",
"agent": True,
"settlement": "monthly",
"lang": "en_US",
"commission_id": cls.commission_net_paid.id,
}
)
cls.agent_biweekly = cls.res_partner_model.create(
{
"name": "Test Agent - Bi-weekly",
Expand Down Expand Up @@ -175,14 +185,15 @@ def _invoice_sale_order(self, sale_order, date=None):
invoice.flush()
return invoice

def _settle_agent(self, agent=None, period=None, date=None):
def _settle_agent(self, agent=None, period=None, date=None, date_payment_to=None):
vals = {
"date_to": (
fields.Datetime.from_string(fields.Datetime.now())
+ dateutil.relativedelta.relativedelta(months=period)
+ relativedelta(months=period)
)
if period
else date,
"date_payment_to": date_payment_to,
}
if agent:
vals["agent_ids"] = [(4, agent.id)]
Expand Down Expand Up @@ -629,3 +640,55 @@ def test_biweekly(self):
[("agent_id", "=", self.agent_biweekly.id)]
)
self.assertEqual(len(settlements), 2)

def register_payment(self, invoice, payment_date):
payment_model = self.env["account.payment.register"]
invoice.action_post()
return (
payment_model.with_context(
{"active_model": "account.move", "active_ids": invoice.ids}
)
.create(
{
"payment_date": payment_date,
"amount": invoice.amount_total,
"journal_id": self.env["account.journal"]
.search([("type", "=", "bank")], limit=1)
.id,
"payment_method_id": self.env.ref(
"account.account_payment_method_manual_out"
).id,
}
)
.action_create_payments()
)

def test_payment_date_settlement(self):
so_after_payment_dt_wizard = self._create_sale_order(
self.agent_monthly, self.commission_net_paid
)
so_after_payment_dt_wizard.action_confirm()
date = fields.Date.today()
inv_after_payment_dt_wizard = self._invoice_sale_order(
so_after_payment_dt_wizard
)
self.register_payment(inv_after_payment_dt_wizard, date + relativedelta(days=2))
sale_order = self._create_sale_order(
self.agent_monthly, self.commission_net_paid
)
sale_order.action_confirm()
inv = self._invoice_sale_order(sale_order)
self.register_payment(inv, date - relativedelta(days=2))
self._settle_agent(self.agent_monthly, 1, date_payment_to=datetime.now())
settlements = self.env["sale.commission.settlement"].search(
[
(
"agent_id",
"=",
self.agent_monthly.id,
),
("state", "=", "settled"),
]
)
self.assertEqual(1, len(settlements))
self.assertEqual(1, len(settlements.line_ids))
10 changes: 8 additions & 2 deletions sale_commission/wizard/wizard_settle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2014-2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import date, timedelta
from datetime import date

from dateutil.relativedelta import relativedelta

Expand All @@ -12,6 +12,8 @@ class SaleCommissionMakeSettle(models.TransientModel):
_description = "Wizard for settling commissions in invoices"

date_to = fields.Date("Up to", required=True, default=fields.Date.today())
date_payment_to = fields.Date("Date Payment Up to", default=fields.Date.today())

agent_ids = fields.Many2many(
comodel_name="res.partner", domain="[('agent', '=', True)]"
)
Expand Down Expand Up @@ -99,12 +101,16 @@ def action_settle(self):
pos += 1
if line._skip_settlement():
continue
if self.date_payment_to and line._skip_future_payments(
self.date_payment_to
):
continue
if line.invoice_date > sett_to:
sett_from = self._get_period_start(agent, line.invoice_date)
sett_to = self._get_next_period_date(
agent,
sett_from,
) - timedelta(days=1)
) - relativedelta(days=1)
settlement = self._get_settlement(
agent, company, sett_from, sett_to
)
Expand Down
1 change: 1 addition & 0 deletions sale_commission/wizard/wizard_settle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</group>
<group colspan="2">
<field name="date_to" />
<field name="date_payment_to" />
</group>
<group string="Agents" colspan="4">
<p colspan="4">(keep empty for making the settlement of all agents)
Expand Down

0 comments on commit 217c77b

Please sign in to comment.