[UPD] Refectoring of _calc_actual function for budget_lines
- expenses and incomes are calculated for both section, sub-section and article lines - for budget lines with child, the amounts are now always considering both child values and analytic lines linked to the budget line
This commit is contained in:
@@ -335,93 +335,61 @@ class BudgetForecast(models.Model):
|
|||||||
elif record.display_type == "line_note":
|
elif record.display_type == "line_note":
|
||||||
record.plan_price = 0.00
|
record.plan_price = 0.00
|
||||||
|
|
||||||
def _find_analytic_lines(self, move_type, with_timesheets=False):
|
@api.depends("analytic_id.line_ids.amount")
|
||||||
self.ensure_one()
|
def _calc_actual(self):
|
||||||
if with_timesheets:
|
for record in self:
|
||||||
domain = [
|
record.actual_amount = 0.00
|
||||||
"|",
|
record.incomes = 0.00
|
||||||
(
|
|
||||||
"move_id.move_id.move_type",
|
if record.display_type in [
|
||||||
"in",
|
"line_section",
|
||||||
move_type,
|
"line_subsection",
|
||||||
),
|
"line_article",
|
||||||
("timesheet_entry", "=", True),
|
]:
|
||||||
]
|
if record.child_ids:
|
||||||
else:
|
# Addition of the childs values
|
||||||
domain = [
|
record.actual_amount = sum(record.mapped("child_ids.actual_amount"))
|
||||||
(
|
record.incomes = sum(record.mapped("child_ids.incomes"))
|
||||||
"move_id.move_id.move_type",
|
|
||||||
"in",
|
# Retrieve all the analytics lines linked to the current budget line
|
||||||
move_type,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
analytic_lines = (
|
analytic_lines = (
|
||||||
self.env["account.analytic.line"]
|
self.env["account.analytic.line"]
|
||||||
.search(domain)
|
.search([])
|
||||||
.filtered(lambda x: self.analytic_tag in x.tag_ids)
|
.filtered(lambda x: record.analytic_tag in x.tag_ids)
|
||||||
)
|
)
|
||||||
return analytic_lines
|
for line in analytic_lines:
|
||||||
|
if line.move_id:
|
||||||
|
if line.move_id.move_id.move_type in [
|
||||||
|
"out_invoice",
|
||||||
|
"out_refund",
|
||||||
|
"out_receipt",
|
||||||
|
]:
|
||||||
|
record.incomes = record.incomes + line.amount
|
||||||
|
elif line.move_id.move_id.move_type in [
|
||||||
|
"in_invoice",
|
||||||
|
"in_refund",
|
||||||
|
"in_receipt",
|
||||||
|
]:
|
||||||
|
record.actual_amount = record.actual_amount - line.amount
|
||||||
|
elif line.timesheet_entry:
|
||||||
|
record.actual_amount = record.actual_amount - line.amount
|
||||||
|
|
||||||
def _find_draft_invoice_lines(self, move_type):
|
# Retrieve all the DRAFT invoices linked to the current budget line
|
||||||
self.ensure_one()
|
|
||||||
domain = [
|
domain = [
|
||||||
("analytic_account_id", "=", self.analytic_id.id),
|
("analytic_account_id", "=", record.analytic_id.id),
|
||||||
("parent_state", "in", ["draft"]),
|
("parent_state", "in", ["draft"]),
|
||||||
("move_id.move_type", "in", move_type),
|
|
||||||
]
|
]
|
||||||
invoice_lines = (
|
invoice_lines = (
|
||||||
self.env["account.move.line"]
|
self.env["account.move.line"]
|
||||||
.search(domain)
|
.search(domain)
|
||||||
.filtered(lambda x: self.analytic_tag in x.analytic_tag_ids)
|
.filtered(lambda x: record.analytic_tag in x.analytic_tag_ids)
|
||||||
)
|
|
||||||
return invoice_lines
|
|
||||||
|
|
||||||
@api.depends("analytic_id.line_ids.amount")
|
|
||||||
def _calc_actual(self):
|
|
||||||
for record in self:
|
|
||||||
# Section or Sub-section
|
|
||||||
if record.display_type in ["line_section", "line_subsection"]:
|
|
||||||
if record.child_ids:
|
|
||||||
# Actual expenses are calculated with the child lines
|
|
||||||
record.actual_amount = sum(record.mapped("child_ids.actual_amount"))
|
|
||||||
|
|
||||||
# Incomes are calculated with the analytic lines
|
|
||||||
line_ids = record._find_analytic_lines(
|
|
||||||
["out_invoice", "out_refund", "out_receipt"]
|
|
||||||
)
|
|
||||||
record.incomes = sum(line_ids.mapped("amount"))
|
|
||||||
# Add Draft Invoice lines ids to incomes
|
|
||||||
invoice_lines = record._find_draft_invoice_lines(
|
|
||||||
["out_invoice", "out_refund"]
|
|
||||||
)
|
)
|
||||||
for invoice_line in invoice_lines:
|
for invoice_line in invoice_lines:
|
||||||
if invoice_line.move_id.move_type == "out_invoice":
|
if invoice_line.move_id.move_type == "out_invoice":
|
||||||
record.incomes = (
|
record.incomes = record.incomes + invoice_line.price_subtotal
|
||||||
record.incomes + invoice_line.price_subtotal
|
|
||||||
)
|
|
||||||
elif invoice_line.move_id.move_type == "out_refund":
|
elif invoice_line.move_id.move_type == "out_refund":
|
||||||
record.incomes = (
|
record.incomes = record.incomes - invoice_line.price_subtotal
|
||||||
record.incomes - invoice_line.price_subtotal
|
elif invoice_line.move_id.move_type == "in_invoice":
|
||||||
)
|
|
||||||
record.balance = record.incomes - record.actual_amount
|
|
||||||
|
|
||||||
# Note
|
|
||||||
elif record.display_type == "line_note":
|
|
||||||
record.actual_amount = 0.00
|
|
||||||
|
|
||||||
# Product
|
|
||||||
else:
|
|
||||||
line_ids = record._find_analytic_lines(
|
|
||||||
["in_invoice", "in_refund", "in_receipt"], True
|
|
||||||
)
|
|
||||||
record.actual_amount = -sum(line_ids.mapped("amount"))
|
|
||||||
|
|
||||||
# Add Draft Invoice lines ids
|
|
||||||
invoice_lines = record._find_draft_invoice_lines(
|
|
||||||
["in_invoice", "in_refund"]
|
|
||||||
)
|
|
||||||
for invoice_line in invoice_lines:
|
|
||||||
if invoice_line.move_id.move_type == "in_invoice":
|
|
||||||
record.actual_amount = (
|
record.actual_amount = (
|
||||||
record.actual_amount + invoice_line.price_subtotal
|
record.actual_amount + invoice_line.price_subtotal
|
||||||
)
|
)
|
||||||
@@ -430,9 +398,7 @@ class BudgetForecast(models.Model):
|
|||||||
record.actual_amount - invoice_line.price_subtotal
|
record.actual_amount - invoice_line.price_subtotal
|
||||||
)
|
)
|
||||||
|
|
||||||
record.incomes = None
|
record.balance = record.incomes - record.actual_amount
|
||||||
record.balance = None
|
|
||||||
|
|
||||||
record.diff_expenses = record.plan_amount_with_coeff - record.actual_amount
|
record.diff_expenses = record.plan_amount_with_coeff - record.actual_amount
|
||||||
|
|
||||||
def action_view_analytic_lines(self):
|
def action_view_analytic_lines(self):
|
||||||
|
Reference in New Issue
Block a user