Skip to content

Commit

Permalink
[ENH] budget_activity: control key
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 committed Sep 24, 2024
1 parent cb13adf commit 404f113
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
1 change: 1 addition & 0 deletions budget_activity/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import res_company
from . import account_move
from . import budget_activity
from . import account_analytic_line
Expand Down
25 changes: 14 additions & 11 deletions budget_activity/models/base_budget_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class BaseBudgetMove(models.AbstractModel):
_inherit = "base.budget.move"
_budget_control_field = "activity_id"

activity_id = fields.Many2one(
comodel_name="budget.activity",
Expand All @@ -26,19 +25,23 @@ class BaseBudgetMove(models.AbstractModel):

@api.depends("activity_id")
def _compute_activity_account(self):
for rec in self:
rec.account_id = (
rec.activity_id.account_id if rec.activity_id else rec.account_id
)
budget_control_key = self.env.company.budget_control_key
if budget_control_key == "activity_id":
for rec in self:
rec.account_id = (
rec.activity_id.account_id if rec.activity_id else rec.account_id
)

@api.constrains("activity_id", "account_id")
def _check_activity_account(self):
for rec in self.filtered("activity_id"):
if rec.account_id != rec.activity_id.account_id:
raise UserError(
_("Account not equal to Activity's Account: %s")
% rec.activity_id.account_id.display_name
)
budget_control_key = self.env.company.budget_control_key
if budget_control_key == "activity_id":
for rec in self.filtered("activity_id"):
if rec.account_id != rec.activity_id.account_id:
raise UserError(
_("Account not equal to Activity's Account: %s")
% rec.activity_id.account_id.display_name
)


class BudgetDoclineMixinBase(models.AbstractModel):
Expand Down
41 changes: 14 additions & 27 deletions budget_activity/models/budget_period.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import UserError
from odoo import models


class BudgetPeriod(models.Model):
_inherit = "budget.period"

@api.model
def _get_kpi_by_control_key(self, template_lines, control):
activity_id = control["activity_id"]
template_line = self._get_filter_template_line(template_lines, control)
if len(template_line) == 1:
return template_line
# Invalid Template Lines
activity = self.env["budget.activity"].browse(activity_id)
if not template_line:
raise UserError(
_("Chosen activity %s is not valid in template") % activity.display_name
)
raise UserError(
_(
"Template Lines has more than one KPI being "
"referenced by the same account code %s"
)
% (activity.display_name)
)
def _get_control_key_obj(self, control_key, control_id):
if control_key == "activity_id":
control = self.env["budget.activity"].browse(control_id)
control_name = "activity"
return control, control_name
return super()._get_control_key_obj(control_key, control_id)

def _get_filter_template_line(self, all_template_lines, control):
"""Overwrite filter template line from account_id to activity_id"""
activity_id = control["activity_id"]
template_lines = all_template_lines.filtered(
lambda l: activity_id in l.activity_ids.ids
)
return template_lines
budget_control_key = self.env.company.budget_control_key
control_id = control[budget_control_key]
if budget_control_key == "activity_id":
return all_template_lines.filtered(
lambda l: control_id in l.activity_ids.ids
)
return super()._get_filter_template_line(all_template_lines, control)
13 changes: 13 additions & 0 deletions budget_activity/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

budget_control_key = fields.Selection(
selection_add=[("activity_id", "Activity")],
ondelete={"activity_id": "cascade"},
)

0 comments on commit 404f113

Please sign in to comment.