[NEW] Addons account_budget_forecast creation

This commit is contained in:
Stéphan Sainléger
2022-05-19 12:21:24 +02:00
parent c23f15cf98
commit 0bd839caa3
30 changed files with 2544 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from . import account_analytic_account
from . import account_analytic_line
from . import account_move
from . import budget_forecast
from . import sale_order
from . import product
from . import project
from . import budget_coefficient
from . import crm_lead

View File

@@ -0,0 +1,271 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
from odoo.exceptions import UserError
class AccountAnalyticAccount(models.Model):
_inherit = "account.analytic.account"
budget_forecast_ids = fields.One2many("budget.forecast", "analytic_id", copy=True)
budget_forecast_manpower_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[("budget_category", "=", "manpower")],
copy=True,
)
budget_forecast_material_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[("budget_category", "=", "material")],
copy=True,
)
budget_forecast_equipment_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[("budget_category", "=", "equipment")],
copy=True,
)
budget_forecast_subcontractors_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[("budget_category", "=", "subcontractors")],
copy=True,
)
budget_forecast_miscellaneous_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[("budget_category", "=", "miscellaneous")],
copy=True,
)
project_section_budget_ids = fields.One2many(
"budget.forecast",
"analytic_id",
domain=[
("display_type", "in", ["line_section", "line_subsection"]),
("is_summary", "=", True),
],
copy=False,
)
budget_coefficients_ids = fields.One2many(
"budget.coefficient",
"budget_forecast",
copy=True,
)
global_coeff = fields.Float("Global coefficient", compute="_calc_global_coeff")
plan_amount_without_coeff = fields.Float(
"Plan Amount without coeff", compute="_calc_budget_amount"
)
plan_amount_with_coeff = fields.Float(
"Plan Amount with coeff", compute="_calc_budget_amount"
)
total_expenses = fields.Float("Total expenses", compute="_calc_budget_amount")
remaining_budget = fields.Float("Remaining budget", compute="_calc_budget_amount")
total_incomes = fields.Float("Total incomes", compute="_calc_budget_amount")
project_balance = fields.Float("Project Balance", compute="_calc_budget_amount")
display_actual_amounts = fields.Boolean(
string="Display Actual Amounts", default=False
)
project_managers = fields.Many2many(
"res.users",
string="Project managers",
domain=lambda self: [("groups_id", "in", self.env.ref("base.group_user").id)],
)
opportunity = fields.Many2one(
"crm.lead",
string="Opportunity",
compute="_compute_opportunity",
)
def default_budget_coefficients_ids(self):
for coeff in self.budget_coefficients_ids:
coeff.unlink()
coeff_models = self.env["budget.coefficient.model"].search([])
for model in coeff_models:
vals = {
"name": model.name,
"coeff": model.coeff,
"note": model.note,
"budget_forecast": self.id,
}
self.env["budget.coefficient"].create(vals)
def _compute_opportunity(self):
lead = self.env["crm.lead"].search(
[("analytic_account", "=", self.id)], limit=1
)
if lead:
self.opportunity = lead.id
@api.model
def create(self, values):
record = super(AccountAnalyticAccount, self).create(values)
if not record.project_managers:
record.project_managers = self.env["res.users"].browse(self.env.user.id)
record.default_budget_coefficients_ids()
return record
@api.depends(
"budget_forecast_ids.plan_amount_without_coeff",
"budget_forecast_ids.plan_amount_with_coeff",
"budget_forecast_ids.actual_amount",
)
def _calc_budget_amount(self):
for record in self:
line_ids = record.mapped("budget_forecast_ids").filtered(
lambda line: (line.display_type == "line_section")
and (line.is_summary == True)
)
# Planned amounts
record.plan_amount_without_coeff = sum(
line_ids.mapped("plan_amount_without_coeff")
)
record.plan_amount_with_coeff = sum(
line_ids.mapped("plan_amount_with_coeff")
)
# Expenses
record.total_expenses = sum(line_ids.mapped("actual_amount"))
record.remaining_budget = (
record.plan_amount_with_coeff - record.total_expenses
)
# Incomes
record.total_incomes = 0
domain = [
("analytic_account_id", "=", record.id),
("parent_state", "in", ["draft", "posted"]),
("move_id.type", "in", ["out_invoice", "out_refund"]),
]
invoice_lines = self.env["account.move.line"].search(domain)
for invoice_line in invoice_lines:
if invoice_line.move_id.type == "out_invoice":
record.total_incomes = (
record.total_incomes + invoice_line.price_subtotal
)
elif invoice_line.move_id.type == "out_refund":
record.total_incomes = (
record.total_incomes - invoice_line.price_subtotal
)
# Balance
record.project_balance = record.total_incomes - record.total_expenses
def _calc_global_coeff(self):
for record in self:
line_ids = record.mapped("budget_coefficients_ids")
record.global_coeff = sum(line_ids.mapped("coeff"))
def action_budget_forecast(self):
# Access only if the connected user is the project manager or an Odoo administrator
if not (
self.env.uid in self.project_managers.ids
or self.env.user.has_group("base.group_system")
):
raise UserError(
_(
"You are not manager of this project.\nPlease contact the project manager or your Odoo administrator."
)
)
return {
"type": "ir.actions.act_window",
"name": _("Budget"),
"res_model": self._name,
"res_id": self.id,
"view_mode": "form",
"view_type": "form",
"views": [
(
self.env.ref(
"account_budget_forecast.view_analytic_budget_forecast"
).id,
"form",
)
],
"context": {"budget_forecast": True},
}
def name_get(self):
if self._context.get("budget_forecast"):
res = []
for analytic in self:
res.append((analytic.id, _("Budget")))
return res
return super(AccountAnalyticAccount, self).name_get()
def displayActualAmounts(self):
for record in self:
if record.display_actual_amounts:
record.display_actual_amounts = False
else:
record.display_actual_amounts = True
def _update_summary(self):
for record in self:
summary_line_ids = (
record.mapped("budget_forecast_ids")
.filtered(lambda r: r.is_summary)
.sorted(key=lambda r: r.sequence, reverse=True)
)
for summary_line in summary_line_ids:
# find corresponding category section/sub_section lines
lines = record.mapped("budget_forecast_ids").filtered(
lambda x: (
(not x.is_summary) and (x.summary_id.id == summary_line.id)
)
)
# Calculate the total amounts
summary_line.plan_amount_without_coeff = 0
summary_line.plan_amount_with_coeff = 0
summary_line.actual_amount = 0
for line in lines:
summary_line.plan_amount_without_coeff += (
line.plan_amount_without_coeff
)
summary_line.plan_amount_with_coeff += line.plan_amount_with_coeff
summary_line.actual_amount += line.actual_amount
def action_refresh(self):
for record in self:
line_ids = (
record.mapped("budget_forecast_ids")
.filtered(lambda r: not r.is_summary)
.sorted(key=lambda r: r.sequence, reverse=True)
)
for line in line_ids:
line.refresh()
record._update_summary()
record._calc_budget_amount()
def action_create_quotation(self):
quotation = self.env["sale.order"].create(
{
"company_id": self.company_id.id,
"partner_id": self.env.user.partner_id.id,
}
)
for section in self.budget_forecast_ids.filtered(
lambda s: (s.display_type == "line_section") and (s.is_summary == True)
):
values = {
"order_id": quotation.id,
"product_id": section.product_id.id,
"name": section.product_id.name,
"product_uom_qty": 1.0,
"price_unit": section.plan_amount_with_coeff,
}
self.env["sale.order.line"].create(values)
quotation.analytic_account_id = self.id
quotation.opportunity_id = self.opportunity.id
return {
"type": "ir.actions.act_window",
"name": _("Quotation"),
"res_model": "sale.order",
"res_id": quotation.id,
"view_mode": "form",
"view_type": "form",
"views": [
(
self.env.ref("sale.view_order_form").id,
"form",
)
],
}

View File

@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class AccountAnalyticLine(models.Model):
_inherit = "account.analytic.line"
budget_forecast_id = fields.Many2one("budget.forecast")
# def _timesheet_preprocess(self, vals):
# vals = super(AccountAnalyticLine, self)._timesheet_preprocess(vals)
# if vals.get("so_line") and not vals.get("product_id"):
# so_line = self.env["sale.order.line"].browse(vals["so_line"])
# vals["product_id"] = so_line.product_id.id
# if vals.get("employee_id") and not vals.get("product_id"):
# employee = self.env["hr.employee"].browse(vals["employee_id"])
# vals["product_id"] = employee.timesheet_product_id.id
# return vals

View File

@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
budget_forecast_id = fields.Many2one("budget.forecast")
@api.depends("budget_forecast_id")
def _transfer_budget_forecast_line(self):
for record in self:
record.analytic_line_ids.budget_forecast_id = record.budget_forecast_id.id

View File

@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from odoo import api, models, fields
class BudgetCoefficientModel(models.Model):
_name = "budget.coefficient.model"
description = "Coefficient Models for the order line price calculation"
name = fields.Char(string="Name", required=True)
coeff = fields.Float(string="Coefficient", required=True, default=0.00)
note = fields.Text(string="Description")
class BudgetCoefficient(models.Model):
_name = "budget.coefficient"
_description = "Coefficients for the order line price calculation"
name = fields.Char(string="Name", required=True)
coeff = fields.Float(string="Coefficient", required=True, default=0.00)
budget_forecast = fields.Many2one("account.analytic.account", string="Budget")
note = fields.Text(string="Description")

View File

@@ -0,0 +1,388 @@
# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, api, _
_logger = logging.getLogger(__name__)
class BudgetForecast(models.Model):
_name = "budget.forecast"
_description = _name
name = fields.Char("Name", store=True)
description = fields.Char("Description", copy=True)
sequence = fields.Integer()
analytic_id = fields.Many2one(
"account.analytic.account",
"Analytic Account",
required=True,
ondelete="restrict",
index=True,
copy=True,
)
budget_category = fields.Selection(
[
("manpower", "Manpower"),
("material", "Material"),
("equipment", "Equipment"),
("subcontractors", "Subcontractors"),
("miscellaneous", "Miscellaneous"),
],
string=_("Budget Category"),
)
is_summary = fields.Boolean(copy=False, default=False, store=True)
summary_id = fields.Many2one("budget.forecast", store=True)
display_type = fields.Selection(
[
("line_section", "Section"),
("line_subsection", "Sub-Section"),
("line_article", "Article"),
("line_note", "Note"),
],
copy=True,
store=True,
)
product_id = fields.Many2one(
"product.product", copy=True, domain="[('budget_level', '=', display_type)]"
)
plan_qty = fields.Float("Plan Quantity", copy=False)
plan_price = fields.Float("Plan Price", default=0.00, copy=False)
plan_amount_without_coeff = fields.Float(
"Plan Amount", compute="_calc_plan_amount_without_coeff", store=True, copy=False
)
plan_amount_with_coeff = fields.Float(
"Plan Amount with coeff",
compute="_calc_plan_amount_with_coeff",
store=True,
copy=False,
)
analytic_line_ids = fields.One2many(
"account.analytic.line", "budget_forecast_id", copy=False
)
actual_qty = fields.Float(
"Actual Quantity",
compute="_calc_actual",
store=True,
compute_sudo=True,
copy=False,
)
actual_amount = fields.Float(
"Expenses",
compute="_calc_actual",
store=True,
compute_sudo=True,
copy=False,
)
diff_expenses = fields.Float(
"Diff", compute="_calc_actual", store=True, compute_sudo=True, copy=False
)
incomes = fields.Float(
"Incomes",
compute="_calc_actual",
store=True,
compute_sudo=True,
copy=False,
)
balance = fields.Float(
"Balance", compute="_calc_actual", store=True, compute_sudo=True, copy=False
)
parent_id = fields.Many2one(
"budget.forecast", store=True, compute_sudo=True, compute="_calc_parent_id"
)
child_ids = fields.One2many("budget.forecast", "parent_id", copy=False)
note = fields.Text(string="Note")
###################################################################################
# Budget lines management
###################################################################################
@api.model_create_multi
@api.returns("self", lambda value: value.id)
def create(self, vals_list):
records = super(BudgetForecast, self).create(vals_list)
for record in records:
if not record.display_type:
record.display_type = "line_article"
elif record.is_summary and record.display_type in [
"line_section",
"line_subsection",
]:
record._create_category_sections()
return records
def _create_category_sections(self):
categories = dict(self._fields["budget_category"].selection)
for category in categories:
# Create other category lines
values = {
"budget_category": category,
"summary_id": self.id,
"is_summary": False,
}
self.copy(values)
def write(self, vals, one_more_loop=True):
res = super(BudgetForecast, self).write(vals)
if one_more_loop:
self._sync_sections_data()
return res
def unlink(self, child_unlink=False):
parent_ids = self.mapped("parent_id")
if not child_unlink:
for record in self:
if not record.is_summary and (
record.display_type
in [
"line_section",
"line_subsection",
]
):
# find similar section/sub_section lines
lines = record.env["budget.forecast"].search(
[
"|",
("summary_id", "=", record.summary_id.id),
("id", "=", record.summary_id.id),
]
)
for line in lines:
line.unlink(True)
res = super(BudgetForecast, self).unlink()
parent_ids.exists()._calc_plan()
return res
@api.onchange("product_id")
def _onchange_product_id(self):
if self.product_id:
self.description = self.product_id.name
if self.display_type == "line_article":
self.plan_price = self.product_id.standard_price
else:
self._calc_plan_price()
else:
self.description = ""
self.plan_price = 0
if self.display_type != "line_article":
self._calc_plan_price()
def _get_budget_category_label(self):
categories = dict(self._fields["budget_category"].selection)
for key, val in categories.items():
if key == self.budget_category:
return val
return ""
@api.onchange("description", "product_id")
def _compute_name(self):
for record in self:
if record.product_id:
name = (
record.description
+ " - "
+ record.product_id.name
+ " - "
+ record._get_budget_category_label()
+ " - "
+ record.analytic_id.name
)
values = {
"name": name,
}
record.write(values, False)
def _sync_sections_data(self):
for record in self:
if record.display_type in ["line_section", "line_subsection"]:
if not record.is_summary:
# find corresponding line summary
summary_line = self.env["budget.forecast"].browse(
record.summary_id.id
)
values = {
"product_id": record.product_id.id,
"description": record.description,
}
summary_line.write(values, False)
summary_line._compute_name()
# find similar category section/sub_section lines
domain = [
("is_summary", "=", False),
("id", "!=", record.id),
]
if not record.is_summary:
domain.extend([("summary_id", "=", record.summary_id.id)])
else:
domain.extend([("summary_id", "=", record.id)])
lines = self.env["budget.forecast"].search(domain)
for line in lines:
values = {
"product_id": record.product_id.id,
"description": record.description,
}
line.write(values, False)
line._compute_name()
@api.depends(
"analytic_id.budget_forecast_ids", "sequence", "parent_id", "child_ids"
)
def _calc_parent_id(self):
for record in self:
if record.display_type == "line_section":
# A Section is the top of the line hierarchy => no parent
record.parent_id = False
continue
found = False
parent_id = False
for line in record.analytic_id.budget_forecast_ids.search(
[
("analytic_id", "=", record.analytic_id.id),
("budget_category", "=", record.budget_category),
]
).sorted(key=lambda r: r.sequence, reverse=True):
if not found and line != record:
continue
if line == record:
found = True
continue
if line.display_type in ["line_article", "line_note"]:
continue
elif line.display_type == "line_subsection":
if record.display_type in ["line_article", "line_note"]:
parent_id = line
break
else:
continue
elif line.display_type == "line_section":
parent_id = line
break
record.parent_id = parent_id
def refresh(self):
self._calc_parent_id()
self._calc_plan()
self._calc_actual()
###################################################################################
# Amounts calculation
###################################################################################
def _calc_plan(self):
self._calc_plan_qty()
self._calc_plan_price()
self._calc_plan_amount_without_coeff()
self._calc_plan_amount_with_coeff()
@api.depends("plan_qty", "plan_price", "child_ids")
def _calc_plan_amount_without_coeff(self):
for record in self:
if record.child_ids:
record.plan_amount_without_coeff = sum(
record.mapped("child_ids.plan_amount_without_coeff")
)
else:
record.plan_amount_without_coeff = record.plan_qty * record.plan_price
@api.depends("plan_qty", "plan_price", "child_ids")
def _calc_plan_amount_with_coeff(self):
for record in self:
record.plan_amount_with_coeff = record.plan_amount_without_coeff * (
1 + record.analytic_id.global_coeff
)
@api.depends("child_ids")
def _calc_plan_qty(self):
for record in self:
if record.child_ids:
record.plan_qty = sum(record.mapped("child_ids.plan_qty"))
@api.depends("child_ids")
def _calc_plan_price(self):
for record in self:
if record.display_type in ["line_section", "line_subsection"]:
if record.child_ids:
lst = record.mapped("child_ids.plan_price")
if lst and (sum(lst) > 0):
record.plan_price = lst and sum(lst)
else:
record.plan_price = record.product_id.standard_price
else:
record.plan_price = record.product_id.standard_price
elif record.display_type == "line_note":
record.plan_price = 0.00
@api.depends("analytic_id.line_ids.amount")
def _calc_actual(self):
for record in self:
if record.display_type in ["line_section", "line_subsection"]:
if record.child_ids:
# Actual expenses are calculated with the child lines
record.actual_qty = sum(record.mapped("child_ids.actual_qty"))
record.actual_amount = sum(record.mapped("child_ids.actual_amount"))
# Incomes are calculated with the analytic lines
line_ids = record.analytic_line_ids.filtered(
lambda x: x.move_id.move_id.type
in ["out_invoice", "out_refund"]
)
record.incomes = sum(line_ids.mapped("amount"))
# Add Invoice lines ids
domain = [
("analytic_account_id", "=", record.analytic_id.id),
("parent_state", "in", ["draft", "posted"]),
("budget_forecast_id", "=", record.id),
("move_id.type", "in", ["out_invoice", "out_refund"]),
]
invoice_lines = self.env["account.move.line"].search(domain)
for invoice_line in invoice_lines:
if invoice_line.move_id.type == "out_invoice":
record.incomes = (
record.incomes + invoice_line.price_subtotal
)
elif invoice_line.move_id.type == "out_refund":
record.incomes = (
record.incomes - invoice_line.price_subtotal
)
record.balance = record.incomes - record.actual_amount
elif record.display_type == "line_note":
record.actual_qty = 0
record.actual_amount = 0.00
else:
line_ids = record.analytic_line_ids.filtered(
lambda x: x.move_id.move_id.type
not in ["out_invoice", "out_refund"]
)
record.actual_qty = abs(sum(line_ids.mapped("unit_amount")))
record.actual_amount = -sum(line_ids.mapped("amount"))
# Add Invoice lines ids
domain = [
("analytic_account_id", "=", record.analytic_id.id),
("parent_state", "in", ["draft", "posted"]),
("budget_forecast_id", "=", record.id),
("move_id.type", "in", ["in_invoice", "in_refund"]),
]
invoice_lines = self.env["account.move.line"].search(domain)
for invoice_line in invoice_lines:
if invoice_line.move_id.type == "in_invoice":
record.actual_qty = record.actual_qty + invoice_line.quantity
record.actual_amount = (
record.actual_amount + invoice_line.price_subtotal
)
elif invoice_line.move_id.type == "in_refund":
record.actual_qty = record.actual_qty - invoice_line.quantity
record.actual_amount = (
record.actual_amount - invoice_line.price_subtotal
)
record.incomes = None
record.balance = None
record.diff_expenses = record.plan_amount_with_coeff - record.actual_amount

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from odoo import _, models, fields, api
from odoo.exceptions import UserError
class Lead(models.Model):
_inherit = "crm.lead"
analytic_account = fields.Many2one(
"account.analytic.account", "Analytic Account", required=False, index=True
)
plan_amount_with_coeff = fields.Float(
related="analytic_account.plan_amount_with_coeff"
)
def action_budget_forecast(self):
if not self.analytic_account:
raise UserError(
_(
"You must add an analytic account to build/access the budget forecast screen."
)
)
return self.analytic_account.action_budget_forecast()
def action_new_quotation(self):
action = super(Lead, self).action_new_quotation()
if self.analytic_account:
action["context"]["default_analytic_account_id"] = self.analytic_account.id
return action

View File

@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, _
class ProductTemplate(models.Model):
_inherit = "product.template"
budget_level = fields.Selection(
[
("line_section", "Section"),
("line_subsection", "Sub-Section"),
("line_article", "Article"),
],
string="Budget level",
default="line_article",
)

View File

@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from odoo import models, _
import json
class Project(models.Model):
_inherit = "project.project"
def _plan_get_stat_button(self):
res = super(Project, self)._plan_get_stat_button()
action = self.analytic_account_id.action_budget_forecast()
res.append(
{
"name": _("Budget"),
"icon": "fa-usd",
"action": {
"data-model": action["res_model"],
"data-views": json.dumps(action["views"]),
"data-res-id": action["res_id"],
},
}
)
return res

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, _, api
from odoo.exceptions import Warning
class SaleOrder(models.Model):
_inherit = "sale.order"
plan_amount_with_coeff = fields.Float(
related="analytic_account_id.plan_amount_with_coeff"
)
def action_budget_forecast(self):
if not self.analytic_account_id:
raise Warning(_("Please set the analytic account"))
return self.analytic_account_id.action_budget_forecast()
@api.returns("self", lambda value: value.id)
def copy(self, default=None):
record = super(SaleOrder, self).copy(default=default)
if self.analytic_account_id.budget_forecast_ids:
if self.name in self.analytic_account_id.name:
name = self.analytic_account_id.name.replace(self.name, record.name)
else:
name = "%s: %s" % (self.analytic_account_id.name, record.name)
record.analytic_account_id = self.analytic_account_id.copy(
default=dict(name=name)
)
return record