Skip to content

Commit

Permalink
[MIG] sale_commission_partial_settlement: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsofl committed Nov 19, 2024
1 parent 8b29a92 commit 4b3a0f3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 44 deletions.
1 change: 1 addition & 0 deletions sale_commission_partial_settlement/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from . import account_invoice_line_agent
from . import account_invoice_line_agent_partial
from . import account_partial_reconcile
from . import commission_settlement
from . import commission_settlement_line
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
# Copyright 2023 Nextev
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo import api, fields, models
from odoo.tools.float_utils import float_compare


class AccountInvoiceLineAgent(models.Model):
_inherit = "account.invoice.line.agent"

partial_settled = fields.Monetary(string="Partial Commission Amount Settled")
partial_settled = fields.Monetary(
string="Partial Commission Amount Settled",
compute="_compute_partial_settled",
store=True,
)
is_fully_settled = fields.Boolean(compute="_compute_is_fully_settled", store=True)
invoice_line_agent_partial_ids = fields.One2many(
"account.invoice.line.agent.partial", "invoice_line_agent_id"
)

def _compute_settled(self):
filtered_lines = self.filtered(
lambda x: x.commission_id.payment_amount_type != "paid"
)
for line in self - filtered_lines:
if not line.settlement_line_ids:
line.settled = False
@api.depends(
"invoice_line_agent_partial_ids.amount",
"invoice_line_agent_partial_ids.agent_line.settlement_id.state",
)
def _compute_partial_settled(self):
for rec in self:
rec.partial_settled = sum(
ailap.amount
for ailap in rec.invoice_line_agent_partial_ids
if ailap.mapped("agent_line.settlement_id")[:1].state != "cancel"
)

return super(AccountInvoiceLineAgent, filtered_lines)._compute_settled()
@api.depends(
"commission_id.payment_amount_type", "amount", "settled", "partial_settled"
)
def _compute_is_fully_settled(self):
for rec in self:
if rec.commission_id.payment_amount_type != "paid":
rec.is_fully_settled = rec.settled
else:
rec.is_fully_settled = rec.settled and (
float_compare(
rec.partial_settled,
rec.amount,
precision_rounding=rec.currency_id.rounding,
)
== 0
)

def _partial_commissions(self, date_payment_to):
"""
Expand All @@ -30,7 +58,6 @@ def _partial_commissions(self, date_payment_to):
"""
partial_lines_to_settle = []
partial_payment_remaining = {}
lines_to_update = {}
for line in self:
line_total_amount = line.amount
reconciled_partials, _ = line.invoice_id._get_reconciled_invoices_partials()
Expand All @@ -52,12 +79,10 @@ def _partial_commissions(self, date_payment_to):
partial_payment_remaining[partial.id] = {"remaining_amount": amount}
if line.object_id.price_total <= payment_amount:
partial_lines_to_settle.append(
self._partial_agent_line_values(line, line_total_amount)
self._partial_agent_line_values(
line, line_total_amount, partial
)
)
lines_to_update[line.id] = {
"partial_settled": line_total_amount,
"settled": True,
}
partial_payment_remaining[partial.id] = {
"remaining_amount": amount - line.object_id.price_total
}
Expand All @@ -68,25 +93,17 @@ def _partial_commissions(self, date_payment_to):
line.invoice_id.commission_total * paid_in_proportion
)
partial_lines_to_settle.append(
self._partial_agent_line_values(line, partial_commission)
self._partial_agent_line_values(line, partial_commission, partial)
)
if line.id in lines_to_update:
lines_to_update[line.id]["partial_settled"] += partial_commission
else:
lines_to_update[line.id] = {"partial_settled": partial_commission}

if lines_to_update[line.id]["partial_settled"] >= line_total_amount:
lines_to_update[line.id].update({"settled": True})
break
partial.partial_commission_settled = True
partial_agent_lines = self.env["account.invoice.line.agent.partial"].create(
partial_lines_to_settle
)
return partial_agent_lines, lines_to_update
return partial_agent_lines

def _partial_agent_line_values(self, line, amount):
def _partial_agent_line_values(self, line, amount, partial):
return {
"invoice_line_agent_id": line.id,
"currency_id": line.currency_id.id,
"amount": amount,
"account_partial_reconcile_id": partial.id,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AccountInvoiceLineAgentPartial(models.Model):
column2="settlement_id",
copy=False,
)
account_partial_reconcile_id = fields.Many2one("account.partial.reconcile")
amount = fields.Monetary(
string="Commission Amount",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
from odoo import fields, models
from odoo import api, fields, models


class AccountPartialReconcile(models.Model):
_inherit = "account.partial.reconcile"

partial_commission_settled = fields.Boolean()
account_invoice_line_agent_partial_ids = fields.One2many(
"account.invoice.line.agent.partial", "account_partial_reconcile_id"
)
partial_commission_settled = fields.Boolean(
compute="_compute_partial_commission_settled", store=True
)

@api.depends(
"account_invoice_line_agent_partial_ids",
"account_invoice_line_agent_partial_ids.agent_line.settlement_id.state",
)
def _compute_partial_commission_settled(self):
for rec in self:
rec.partial_commission_settled = bool(
rec.account_invoice_line_agent_partial_ids.filtered(
lambda x: x.mapped("agent_line.settlement_id")[:1].state != "cancel"
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import models


class CommissionSettlement(models.Model):
_inherit = "commission.settlement"

def unlink(self):
self.mapped("line_ids.agent_line_partial_ids").unlink()
return super().unlink()
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ def _compute_settled_amount(self):
rec.settled_amount = rec.agent_line_partial_ids[:1].amount

Check warning on line 24 in sale_commission_partial_settlement/models/commission_settlement_line.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_partial_settlement/models/commission_settlement_line.py#L24

Added line #L24 was not covered by tests
else:
rec.settled_amount = rec.invoice_agent_line_id[:1].amount

def unlink(self):
self.mapped("agent_line_partial_ids").unlink()
return super().unlink()

Check warning on line 30 in sale_commission_partial_settlement/models/commission_settlement_line.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_partial_settlement/models/commission_settlement_line.py#L29-L30

Added lines #L29 - L30 were not covered by tests
11 changes: 7 additions & 4 deletions sale_commission_partial_settlement/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -274,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -438,7 +439,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def action_settle_partial(self):
settlement_obj = self.env["commission.settlement"]
settlement_line_obj = self.env["commission.settlement.line"]
settlement_ids = []

if self.agent_ids:
agents = self.agent_ids
else:
Expand All @@ -32,14 +31,7 @@ def action_settle_partial(self):
for agent in agents:
date_to_agent = self._get_period_start(agent, date_to)
main_agent_line = self.get_partial_agent_lines(agent, date_to_agent)
(
partial_agent_lines,
agent_lines_to_update,
) = main_agent_line._partial_commissions(self.date_to)
for line_id in agent_lines_to_update:
self.env["account.invoice.line.agent"].browse(line_id).update(
agent_lines_to_update[line_id]
)
partial_agent_lines = main_agent_line._partial_commissions(date_to)
for company in partial_agent_lines.mapped(
"invoice_line_agent_id.company_id"
):
Expand Down Expand Up @@ -88,8 +80,10 @@ def get_partial_agent_lines(self, agent, date_to_agent):
[
("invoice_date", "<", date_to_agent),
("agent_id", "=", agent.id),
("settled", "=", False),
("commission_id.payment_amount_type", "=", "paid"),
"|",
("settled", "=", False),
("is_fully_settled", "=", False),
],
order="invoice_date",
)
Expand Down

0 comments on commit 4b3a0f3

Please sign in to comment.