From 4b3a0f3a5ce19ce58e3b5e31f5546cc642a73050 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 19 Nov 2024 14:20:04 +0100 Subject: [PATCH] [MIG] sale_commission_partial_settlement: Migration to 16.0 --- .../models/__init__.py | 1 + .../models/account_invoice_line_agent.py | 73 ++++++++++++------- .../account_invoice_line_agent_partial.py | 1 + .../models/account_partial_reconcile.py | 21 +++++- .../models/commission_settlement.py | 9 +++ .../models/commission_settlement_line.py | 4 + .../static/description/index.html | 11 ++- .../wizard/commission_make_settle.py | 14 +--- 8 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 sale_commission_partial_settlement/models/commission_settlement.py diff --git a/sale_commission_partial_settlement/models/__init__.py b/sale_commission_partial_settlement/models/__init__.py index d98e158fb..c782fe739 100644 --- a/sale_commission_partial_settlement/models/__init__.py +++ b/sale_commission_partial_settlement/models/__init__.py @@ -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 diff --git a/sale_commission_partial_settlement/models/account_invoice_line_agent.py b/sale_commission_partial_settlement/models/account_invoice_line_agent.py index 6f1562eb7..4c17e0481 100644 --- a/sale_commission_partial_settlement/models/account_invoice_line_agent.py +++ b/sale_commission_partial_settlement/models/account_invoice_line_agent.py @@ -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): """ @@ -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() @@ -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 } @@ -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, } diff --git a/sale_commission_partial_settlement/models/account_invoice_line_agent_partial.py b/sale_commission_partial_settlement/models/account_invoice_line_agent_partial.py index f6f608dea..7e4ff420a 100644 --- a/sale_commission_partial_settlement/models/account_invoice_line_agent_partial.py +++ b/sale_commission_partial_settlement/models/account_invoice_line_agent_partial.py @@ -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", ) diff --git a/sale_commission_partial_settlement/models/account_partial_reconcile.py b/sale_commission_partial_settlement/models/account_partial_reconcile.py index 7c1714d4d..a97dee0bc 100644 --- a/sale_commission_partial_settlement/models/account_partial_reconcile.py +++ b/sale_commission_partial_settlement/models/account_partial_reconcile.py @@ -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" + ) + ) diff --git a/sale_commission_partial_settlement/models/commission_settlement.py b/sale_commission_partial_settlement/models/commission_settlement.py new file mode 100644 index 000000000..263c7744f --- /dev/null +++ b/sale_commission_partial_settlement/models/commission_settlement.py @@ -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() diff --git a/sale_commission_partial_settlement/models/commission_settlement_line.py b/sale_commission_partial_settlement/models/commission_settlement_line.py index 63dba3971..f916565ab 100644 --- a/sale_commission_partial_settlement/models/commission_settlement_line.py +++ b/sale_commission_partial_settlement/models/commission_settlement_line.py @@ -24,3 +24,7 @@ def _compute_settled_amount(self): rec.settled_amount = rec.agent_line_partial_ids[:1].amount else: rec.settled_amount = rec.invoice_agent_line_id[:1].amount + + def unlink(self): + self.mapped("agent_line_partial_ids").unlink() + return super().unlink() diff --git a/sale_commission_partial_settlement/static/description/index.html b/sale_commission_partial_settlement/static/description/index.html index 74000c070..2b9e0dc17 100644 --- a/sale_commission_partial_settlement/static/description/index.html +++ b/sale_commission_partial_settlement/static/description/index.html @@ -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. @@ -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 } @@ -300,7 +301,7 @@ span.pre { white-space: pre } -span.problematic { +span.problematic, pre.problematic { color: red } span.section-subtitle { @@ -438,7 +439,9 @@

Contributors

Maintainers

This module is maintained by the OCA.

-Odoo Community Association + +Odoo Community Association +

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.

diff --git a/sale_commission_partial_settlement/wizard/commission_make_settle.py b/sale_commission_partial_settlement/wizard/commission_make_settle.py index 3e4065233..7d93b24a0 100644 --- a/sale_commission_partial_settlement/wizard/commission_make_settle.py +++ b/sale_commission_partial_settlement/wizard/commission_make_settle.py @@ -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: @@ -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" ): @@ -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", )