Skip to content

Commit

Permalink
Merge branch '16.0' of https://github.com/ecosoft-odoo/budgeting into…
Browse files Browse the repository at this point in the history
… 16.0-mig-budget_activity
  • Loading branch information
Saran440 committed Sep 24, 2024
2 parents 404f113 + 4323cce commit bf29a8a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
1 change: 0 additions & 1 deletion budget_control/models/base_budget_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
class BaseBudgetMove(models.AbstractModel):
_name = "base.budget.move"
_description = "Document Budget Moves"
_budget_control_field = "account_id"
_order = "analytic_account_id, date, id"

reference = fields.Char(
Expand Down
46 changes: 25 additions & 21 deletions budget_control/models/budget_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,66 +334,70 @@ def _prepare_controls(self, budget_period, doclines):
lambda l: l.date >= budget_period.bm_date_from
and l.date <= budget_period.bm_date_to
)
budget_control_key = self.env.company.budget_control_key
need_control = self.env.context.get("need_control")
for budget_move in budget_moves_period:
if budget_period.control_all_analytic_accounts:
if (
budget_move.analytic_account_id
and budget_move[budget_move._budget_control_field]
):
if budget_move.analytic_account_id and budget_move[budget_control_key]:
controls.add(
(
budget_move.analytic_account_id.id,
budget_move[budget_move._budget_control_field].id,
budget_move[budget_control_key].id,
)
)
else: # analytic in control or force control by send context
if (
budget_move.analytic_account_id in control_analytics
and budget_move[budget_move._budget_control_field]
and budget_move[budget_control_key]
) or need_control:
controls.add(
(
budget_move.analytic_account_id.id,
budget_move[budget_move._budget_control_field].id,
budget_move[budget_control_key].id,
)
)
# Convert to list of dicts for readability
return [
{"analytic_id": x[0], budget_move._budget_control_field: x[1]}
for x in controls
]
return [{"analytic_id": x[0], budget_control_key: x[1]} for x in controls]

def _get_filter_template_line(self, all_template_lines, control):
account_id = control["account_id"]
template_lines = all_template_lines.filtered(
lambda l: account_id in l.account_ids.ids
)
budget_control_key = self.env.company.budget_control_key
if budget_control_key == "account_id":
control_id = control[budget_control_key]
template_lines = all_template_lines.filtered(
lambda l: control_id in l.account_ids.ids
)
return template_lines

def _get_control_key_obj(self, control_key, control_id):
if control_key == "account_id":
control = self.env["account.account"].browse(control_id)
control_name = "account code"
return control, control_name

@api.model
def _get_kpi_by_control_key(self, template_lines, control):
"""
By default, control key is account_id as it can be used to get KPI
In future, this can be other key, i.e., activity_id based on installed module
"""
account_id = control["account_id"]
control_key = self.env.company.budget_control_key
control_id = control[control_key]
template_line = self._get_filter_template_line(template_lines, control)
if len(template_line) == 1:
return template_line
# Invalid Template Lines
account = self.env["account.account"].browse(account_id)
control, control_name = self._get_control_key_obj(control_key, control_id)
if not template_line:
raise UserError(
_("Chosen account code %s is not valid in template")
% account.display_name
_("Chosen %(name)s %(display_name)s is not valid in template")
% ({"name": control_name, "display_name": control.display_name})
)
raise UserError(
_(
"Template Lines has more than one KPI being "
"referenced by the same account code %s"
"referenced by the same %(name)s %(display_name)s"
)
% (account.display_name)
% ({"name": control_name, "display_name": control.display_name})
)

def _get_where_domain(self, analytic_id, template_lines):
Expand Down
6 changes: 6 additions & 0 deletions budget_control/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ class ResCompany(models.Model):
comodel_name="budget.template",
string="Budget Template",
)
budget_control_key = fields.Selection(
selection=[("account_id", "Account")],
string="Control Key",
required=True,
default="account_id",
)
4 changes: 4 additions & 0 deletions budget_control/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class ResConfigSettings(models.TransientModel):
related="company_id.budget_template_id",
readonly=False,
)
budget_control_key = fields.Selection(
related="company_id.budget_control_key",
readonly=False,
)
group_required_analytic = fields.Boolean(
string="Required Analytic Account",
implied_group="budget_control.group_required_analytic",
Expand Down
17 changes: 17 additions & 0 deletions budget_control/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="budget_control_key" />
<div class="text-muted">
Default Key to used get KPI.
</div>
<div class="content-group">
<div class="mt16">
<field
name="budget_control_key"
class="o_light_label"
widget="selection"
/>
</div>
</div>
</div>
</div>
</div>
<h2>Budget Period</h2>
<div
Expand Down

0 comments on commit bf29a8a

Please sign in to comment.