-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] sale_commission_partial_settlement: handle settlement deletion
This commit fixes an issue where, if a settlement was deleted, any partial commission would not be recreated because it was still considered paid. Includes a small refactor of the files and deletion of some unused fields.
- Loading branch information
Showing
9 changed files
with
154 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sale_commission_partial_settlement/migrations/14.0.1.1.0/post-migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from openupgradelib import openupgrade | ||
|
||
|
||
def unlink_orphan_agent_partials(env): | ||
""" | ||
Delete agent partials that are not linked | ||
to a settlement line | ||
e.g. because the settlement was deleted | ||
but the ailap was left behind | ||
""" | ||
openupgrade.logged_query( | ||
env.cr, | ||
""" | ||
SELECT id | ||
FROM account_invoice_line_agent_partial | ||
WHERE id not in | ||
(SELECT agent_line_partial_id | ||
FROM settlement_agent_line_partial_rel) | ||
""", | ||
) | ||
ids = [row[0] for row in env.cr.fetchall()] | ||
env["account.invoice.line.agent.partial"].browse(ids).unlink() | ||
|
||
|
||
def recompute_partial_settled(env): | ||
env["account.invoice.line.agent"].search( | ||
[("settled", "=", False), ("partial_settled", "!=", 0)] | ||
)._compute_partial_settled() | ||
|
||
|
||
def recompute_settled(env): | ||
""" | ||
Sometimes AILA were not marked as "settled" properly | ||
for commissions of type "paid" | ||
""" | ||
env["account.invoice.line.agent"].search( | ||
[ | ||
("commission_id.payment_amount_type", "=", "paid"), | ||
] | ||
)._compute_settled() | ||
|
||
|
||
@openupgrade.migrate() | ||
def migrate(env, version): | ||
unlink_orphan_agent_partials(env) | ||
recompute_partial_settled(env) | ||
recompute_settled(env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
from . import sale_commission | ||
from . import account_move | ||
from . import account_invoice_line_agent | ||
from . import account_invoice_line_agent_partial | ||
from . import account_partial_reconcile | ||
from . import settlement | ||
from . import sale_commission | ||
from . import sale_commission_settlement | ||
from . import sale_commission_settlement_line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
sale_commission_partial_settlement/models/account_invoice_line_agent_partial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2023 Nextev | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import fields, models | ||
|
||
|
||
class AccountInvoiceLineAgentPartial(models.Model): | ||
_name = "account.invoice.line.agent.partial" | ||
_description = "Partial agent commissions" | ||
|
||
invoice_line_agent_id = fields.Many2one( | ||
"account.invoice.line.agent", required=True, ondelete="cascade" | ||
) | ||
# logically a One2one | ||
agent_line = fields.Many2many( | ||
comodel_name="sale.commission.settlement.line", | ||
relation="settlement_agent_line_partial_rel", | ||
column1="agent_line_partial_id", | ||
column2="settlement_id", | ||
copy=False, | ||
) | ||
account_partial_reconcile_id = fields.Many2one("account.partial.reconcile") | ||
amount = fields.Monetary( | ||
string="Commission Amount", | ||
) | ||
currency_id = fields.Many2one( | ||
related="invoice_line_agent_id.currency_id", | ||
) |
17 changes: 15 additions & 2 deletions
17
sale_commission_partial_settlement/models/account_partial_reconcile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
from odoo import fields, models | ||
from odoo import api, fields, models | ||
|
||
|
||
class AccountPartialReconcile(models.Model): | ||
_inherit = "account.partial.reconcile" | ||
|
||
partial_commission_settled = fields.Boolean() | ||
# Logically a One2one | ||
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") | ||
def _compute_partial_commission_settled(self): | ||
for rec in self: | ||
rec.partial_commission_settled = bool( | ||
rec.account_invoice_line_agent_partial_ids | ||
) |
9 changes: 9 additions & 0 deletions
9
sale_commission_partial_settlement/models/sale_commission_settlement.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from odoo import models | ||
|
||
|
||
class Settlement(models.Model): | ||
_inherit = "sale.commission.settlement" | ||
|
||
def unlink(self): | ||
self.mapped("line_ids.agent_line_partial_ids").unlink() | ||
return super().unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters