Skip to content

Commit

Permalink
[FIX] l10n_it_split_payment: fix unbalanced lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Borruso committed Aug 5, 2022
1 parent de99fae commit 288a730
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 27 deletions.
82 changes: 56 additions & 26 deletions l10n_it_split_payment/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,17 @@ class AccountMove(models.Model):
def _compute_amount(self):
super()._compute_amount()
for move in self:
move.amount_sp = 0
if move.fiscal_position_id.split_payment:
move.amount_sp = move.amount_tax
move.amount_tax = 0
move.amount_total = move.amount_untaxed + move.amount_tax

if move.fiscal_position_id.split_payment:
if move.split_payment:
if move.is_purchase_document():
raise UserError(
_("Can't handle supplier invoices with split payment")
)
else:
move._compute_split_payments()
move.amount_sp = move.amount_tax
move.amount_tax = 0.0
move.amount_total = move.amount_untaxed
move._compute_split_payments()
else:
move.amount_sp = 0.0

def _build_debit_line(self):
if not self.company_id.sp_account_id:
Expand All @@ -59,13 +57,16 @@ def _build_debit_line(self):
"account_id": self.company_id.sp_account_id.id,
"journal_id": self.journal_id.id,
"date": self.invoice_date,
"price_unit": -self.amount_sp,
"amount_currency": self.amount_sp,
"debit": self.amount_sp,
"credit": 0,
"credit": 0.0,
"exclude_from_invoice_tab": True,
"is_split_payment": True,
}
if self.move_type == "out_refund":
vals["debit"] = 0
vals["amount_currency"] = -self.amount_sp
vals["debit"] = 0.0
vals["credit"] = self.amount_sp
return vals

Expand All @@ -86,10 +87,13 @@ def set_receivable_line_ids(self):
self.amount_total * line_client.debit
) / inv_total
else:
receivable_line_amount = 0
receivable_line_amount = 0.0
line_client.with_context(check_move_validity=False).update(
{
"price_unit": -receivable_line_amount,
"amount_currency": receivable_line_amount,
"debit": receivable_line_amount,
"credit": 0.0,
}
)
elif self.move_type == "out_refund":
Expand All @@ -100,37 +104,63 @@ def set_receivable_line_ids(self):
self.amount_total * line_client.credit
) / inv_total
else:
receivable_line_amount = 0
receivable_line_amount = 0.0
line_client.with_context(check_move_validity=False).update(
{
"price_unit": -receivable_line_amount,
"amount_currency": -receivable_line_amount,
"debit": 0.0,
"credit": receivable_line_amount,
}
)

def _compute_split_payments(self):
write_off_line_vals = self._build_debit_line()
line_sp = self.line_ids.filtered(lambda l: l.is_split_payment)
if line_sp:
if self.move_type == "out_invoice" and float_compare(
line_sp[0].debit,
write_off_line_vals["debit"],
precision_rounding=self.currency_id.rounding,
line_sp = line_sp[0].with_context(check_move_validity=False)
if (
self.move_type == "out_invoice"
and float_compare(
line_sp.price_unit,
write_off_line_vals["price_unit"],
precision_rounding=self.currency_id.rounding,
)
!= 0
):
line_sp[0].with_context(check_move_validity=False).update({"debit": 0})
line_sp.update(
{
"price_unit": 0.0,
"amount_currency": 0.0,
"debit": 0.0,
"credit": 0.0,
}
)
self.set_receivable_line_ids()
line_sp[0].debit = write_off_line_vals["debit"]
elif self.move_type == "out_refund" and float_compare(
line_sp[0].credit,
write_off_line_vals["credit"],
precision_rounding=self.currency_id.rounding,
line_sp.write(write_off_line_vals)
elif (
self.move_type == "out_refund"
and float_compare(
line_sp.price_unit,
write_off_line_vals["price_unit"],
precision_rounding=self.currency_id.rounding,
)
!= 0
):
line_sp[0].with_context(check_move_validity=False).update({"credit": 0})
line_sp.update(
{
"price_unit": 0.0,
"amount_currency": 0.0,
"debit": 0.0,
"credit": 0.0,
}
)
self.set_receivable_line_ids()
line_sp[0].credit = write_off_line_vals["credit"]
line_sp.write(write_off_line_vals)
else:
self.set_receivable_line_ids()
if self.amount_sp:
self.line_ids = [(0, 0, write_off_line_vals)]
self.invoice_line_ids = [(0, 0, write_off_line_vals)]


class AccountMoveLine(models.Model):
Expand Down
42 changes: 42 additions & 0 deletions l10n_it_split_payment/tests/test_splitpayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests import tagged
from odoo.tests.common import Form

from odoo.addons.account.tests.test_account_account import TestAccountAccount

Expand Down Expand Up @@ -233,3 +234,44 @@ def test_invoice(self):
self.assertEqual(line.credit, 100)
self.assertTrue(vat_line)
self.assertTrue(credit_line)

def test_balanced_lines(self):
self.assertTrue(self.tax22sp.is_split_payment)

invoice_form = Form(
self.move_model.with_context(default_move_type="out_invoice")
)
invoice_form.invoice_date = self.recent_date
invoice_form.partner_id = self.env.ref("base.res_partner_3")
invoice_form.journal_id = self.sales_journal
invoice_form.fiscal_position_id = self.sp_fp

with invoice_form.invoice_line_ids.new() as line_form:
line_form.name = "service"
line_form.account_id = self.a_sale
line_form.quantity = 1
line_form.price_unit = 100
line_form.tax_ids.clear()
line_form.tax_ids.add(self.tax22sp)

invoice = invoice_form.save()
self.assertTrue(invoice.split_payment)
self.assertEqual(invoice.amount_sp, 22)
self.assertEqual(invoice.amount_total, 100)
self.assertEqual(invoice.amount_residual, 100)
self.assertEqual(invoice.amount_tax, 0)

with invoice_form.invoice_line_ids.new() as line_form:
line_form.name = "service"
line_form.account_id = self.a_sale
line_form.quantity = 1
line_form.price_unit = 100
line_form.tax_ids.clear()
line_form.tax_ids.add(self.tax22sp)

invoice = invoice_form.save()
self.assertTrue(invoice.split_payment)
self.assertEqual(invoice.amount_sp, 44)
self.assertEqual(invoice.amount_total, 200)
self.assertEqual(invoice.amount_residual, 200)
self.assertEqual(invoice.amount_tax, 0)
8 changes: 7 additions & 1 deletion l10n_it_split_payment/views/account_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</field>
</record>

<record id="account_move_form_sp" model="ir.ui.view">
<record id="account_move_form_sp" model="ir.ui.view">
<field name="name">account.move.form.sp</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
Expand All @@ -29,6 +29,12 @@
attrs="{'invisible': [('split_payment', '=', False)]}"
/>
</field>
<xpath
expr="//sheet/notebook/page/field[@name='line_ids']/tree"
position="inside"
>
<field name="is_split_payment" optional="hide" />
</xpath>
</field>
</record>

Expand Down

0 comments on commit 288a730

Please sign in to comment.