Skip to content

Commit

Permalink
[FIX] l10n_it_split_payment: when editing invoice lines, move lines a…
Browse files Browse the repository at this point in the history
…re not correctly computed
  • Loading branch information
eLBati committed Jun 25, 2024
1 parent d63eab9 commit f6b5fe7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
34 changes: 34 additions & 0 deletions l10n_it_split_payment/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

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


class AccountMove(models.Model):
Expand Down Expand Up @@ -40,3 +41,36 @@ def _compute_amount(self):
else:
move.amount_sp = 0.0
return res

def write(self, vals):
res = super().write(vals)
if self.env.context.get("skip_split_payment_computation"):
return res
for move in self:
if move.split_payment:
split_payment_total = 0
for line in move.line_ids:
if line.display_type == "tax" and not line.is_split_payment:
write_off_line_vals = line._build_writeoff_line()
split_payment_total += write_off_line_vals["debit"]
line_sp = fields.first(
line.move_id.line_ids.filtered(
lambda move_line: move_line.is_split_payment
)
)
if line_sp:
if (
float_compare(
line_sp.price_unit,
write_off_line_vals["price_unit"],
precision_rounding=line.move_id.currency_id.rounding,
)
!= 0
):
line_sp.write(write_off_line_vals)

Check warning on line 70 in l10n_it_split_payment/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_split_payment/models/account_move.py#L70

Added line #L70 was not covered by tests
else:
if line.move_id.amount_sp:
line.move_id.with_context(
skip_split_payment_computation=True
).line_ids = [(0, 0, write_off_line_vals)]
return res
42 changes: 7 additions & 35 deletions l10n_it_split_payment/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_compare


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

is_split_payment = fields.Boolean()
is_split_payment = fields.Boolean(compute="_compute_is_split_payment")

def _compute_is_split_payment(self):
for line in self:
line.is_split_payment = False
if line.account_id == line.company_id.sp_account_id:
line.is_split_payment = True

def _build_writeoff_line(self):
self.ensure_one()
Expand All @@ -36,7 +41,6 @@ def _build_writeoff_line(self):
"debit": self.credit,
"credit": self.debit,
"display_type": "tax",
"is_split_payment": True,
}
if self.move_id.move_type == "out_refund":
vals["amount_currency"] = -self.debit
Expand All @@ -60,35 +64,3 @@ def create(self, vals_list):
container={"records": line.move_id, "self": line.move_id}
)
return lines

def write(self, vals):
res = super().write(vals)
for line in self:
if (
line.move_id.split_payment
and line.display_type == "tax"
and not line.is_split_payment
):
write_off_line_vals = line._build_writeoff_line()
line_sp = fields.first(
line.move_id.line_ids.filtered(
lambda move_line: move_line.is_split_payment
)
)
if line_sp:
if (
float_compare(
line_sp.price_unit,
write_off_line_vals["price_unit"],
precision_rounding=line.move_id.currency_id.rounding,
)
!= 0
):
line_sp.write(write_off_line_vals)
else:
if line.move_id.amount_sp:
line.move_id.line_ids = [(0, 0, write_off_line_vals)]
line.move_id._sync_dynamic_lines(
container={"records": line.move_id, "self": line.move_id}
)
return res

0 comments on commit f6b5fe7

Please sign in to comment.