Skip to content

Commit

Permalink
Merge PR OCA#2543 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by dreispt
  • Loading branch information
OCA-git-bot committed Dec 20, 2023
2 parents f9787bf + 4c157f0 commit e9e44f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
28 changes: 24 additions & 4 deletions sale_invoice_plan/models/sale_invoice_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,21 @@ def _compute_no_edit(self):
@api.depends("percent")
def _compute_amount(self):
for rec in self:
amount_untaxed = rec.sale_id._origin.amount_untaxed
# With invoice already created, no recompute
if rec.invoiced:
rec.amount = rec.amount_invoiced
rec.percent = rec.amount / rec.sale_id.amount_untaxed * 100
rec.percent = rec.amount / amount_untaxed * 100
continue
# For last line, amount is the left over
if rec.last:
installments = rec.sale_id.invoice_plan_ids.filtered(
lambda l: l.invoice_type == "installment"
)
prev_amount = sum((installments - rec).mapped("amount"))
rec.amount = rec.sale_id.amount_untaxed - prev_amount
rec.amount = amount_untaxed - prev_amount
continue
rec.amount = rec.percent * rec.sale_id.amount_untaxed / 100
rec.amount = rec.percent * amount_untaxed / 100

@api.onchange("amount", "percent")
def _inverse_amount(self):
Expand Down Expand Up @@ -137,13 +138,32 @@ def _compute_to_invoice(self):
rec.to_invoice = True
break

def _get_amount_invoice(self, invoices):
"""Hook function"""
amount_invoiced = sum(invoices.mapped("amount_untaxed"))
deposit_product_id = (
self.env["ir.config_parameter"]
.sudo()
.get_param("sale.default_deposit_product_id")
)
if deposit_product_id:
lines = invoices.mapped("invoice_line_ids").filtered(
lambda l: l.product_id.id != int(deposit_product_id)
)
amount_invoiced = sum(lines.mapped("price_subtotal"))
return amount_invoiced

def _compute_invoiced(self):
for rec in self:
invoiced = rec.invoice_move_ids.filtered(
lambda l: l.state in ("draft", "posted")
)
rec.invoiced = invoiced and True or False
rec.amount_invoiced = invoiced[:1].amount_untaxed
rec.amount_invoiced = (
sum(invoiced.mapped("amount_untaxed"))
if rec.invoice_type == "advance"
else rec._get_amount_invoice(invoiced[:1])
)

def _compute_last(self):
for rec in self:
Expand Down
16 changes: 16 additions & 0 deletions sale_invoice_plan/tests/test_sale_invoice_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ def setUpClassicProducts(cls):
"categ_id": cls.product_category.id,
}
)
# Advance Product
deposit_account = cls.env["account.account"].search(
[("internal_group", "=", "income"), ("deprecated", "=", False)], limit=1
)
cls.product_advance = cls.env["product.product"].create(
{
"name": _("Down payment"),
"type": "service",
"invoice_policy": "order",
"property_account_income_id": deposit_account.id,
"taxes_id": False,
}
)
cls.env["ir.config_parameter"].sudo().set_param(
"sale.default_deposit_product_id", cls.product_advance.id
)

def test_00_invoice_plan(self):
# To create next invoice from SO
Expand Down

0 comments on commit e9e44f3

Please sign in to comment.