Skip to content

Commit f801d17

Browse files
author
matteo.tognini
committed
[IMP]commission: method for improve inheritance
1 parent de96e74 commit f801d17

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

account_commission/models/commission.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ class Commission(models.Model):
1414
"* 'Invoice Based': Commissions are settled when the invoice is issued.\n"
1515
"* 'Payment Based': Commissions are settled when the invoice is paid (or refunded).",
1616
)
17+
settled_dates_based_on = fields.Selection(
18+
[("inv", "Invoice Date"), ("payment", "Payment Date")],
19+
default="inv",
20+
help="Select the date to use for settling the commissions:\n",
21+
)

account_commission/views/commission_views.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
name="invoice_state"
1111
attrs="{'required': [('settlement_type', '=', 'sale_invoice')]}"
1212
/>
13+
<field
14+
name="settled_dates_based_on"
15+
attrs="{'invisible': [('invoice_state', '=', 'open')]}"
16+
/>
1317
</xpath>
1418
</field>
1519
</record>

account_commission/wizards/commission_make_settle.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,32 @@ def _get_agent_lines(self, agent, date_to_agent):
3232
"""Filter sales invoice agent lines for this type of settlement."""
3333
if self.settlement_type != "sale_invoice":
3434
return super()._get_agent_lines(agent, date_to_agent)
35-
return self.env["account.invoice.line.agent"].search(
36-
self._get_account_settle_domain(agent, date_to_agent),
37-
order="invoice_date",
35+
36+
lines = self.env["account.invoice.line.agent"].search(
37+
self._get_account_settle_domain(agent, date_to_agent)
3838
)
3939

40+
if agent.commission_id.settled_dates_based_on == "payment":
41+
invoices = lines.mapped("invoice_id")
42+
payment_date_by_inv = {}
43+
for inv in invoices:
44+
invoice_partials, _ = inv._get_reconciled_invoices_partials()
45+
dates = [
46+
cp_line.date
47+
for _p, _amt, cp_line in invoice_partials
48+
if cp_line.date
49+
]
50+
payment_date_by_inv[inv.id] = max(dates)
51+
52+
return lines.sorted(
53+
key=lambda l: (
54+
payment_date_by_inv.get(l.invoice_id.id),
55+
l.id,
56+
)
57+
)
58+
59+
return lines.sorted(key=lambda l: (l.invoice_date, l.id))
60+
4061
def _prepare_settlement_line_vals(self, settlement, line):
4162
"""Prepare extra settlement values when the source is a sales invoice agent
4263
line.
@@ -56,3 +77,23 @@ def _prepare_settlement_line_vals(self, settlement, line):
5677
def action_settle(self):
5778
self = self.with_context(date_payment_to=self.date_payment_to)
5879
return super().action_settle()
80+
81+
def get_period_date(self, line):
82+
if line.agent_id.commission_id.settled_dates_based_on != "payment":
83+
return super().get_period_date(line)
84+
return self.get_latest_payment_date(line.invoice_id)
85+
86+
def get_latest_payment_date(self, invoice):
87+
"""Get the latest payment date for an invoice."""
88+
payments_dates = []
89+
(
90+
invoice_partials,
91+
exchange_diff_moves,
92+
) = invoice._get_reconciled_invoices_partials()
93+
for (
94+
_partial,
95+
_amount,
96+
counterpart_line,
97+
) in invoice_partials:
98+
payments_dates.append(counterpart_line.date)
99+
return max(payments_dates)

commission/wizards/commission_make_settle.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def _agent_lines_groupby(self, agent_line):
115115
def _agent_lines_sorted(self, agent_line):
116116
return agent_line.company_id.id, agent_line.currency_id.id
117117

118+
def get_period_date(self, line):
119+
"""Get the date to use for period calculation."""
120+
return line.invoice_date
121+
118122
def action_settle(self):
119123
self.ensure_one()
120124
settlement_obj = self.env["commission.settlement"]
@@ -145,8 +149,9 @@ def action_settle(self):
145149
pos += 1
146150
if line._skip_settlement():
147151
continue
148-
if line.invoice_date > sett_to:
149-
sett_from = self._get_period_start(agent, line.invoice_date)
152+
date_to_compare = self.get_period_date(line)
153+
if date_to_compare > sett_to:
154+
sett_from = self._get_period_start(agent, date_to_compare)
150155
sett_to = self._get_next_period_date(agent, sett_from)
151156
sett_to -= timedelta(days=1)
152157
settlement = self._get_settlement(

0 commit comments

Comments
 (0)