Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] budget_plan_revision: Optimize when create new budget control #411

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions budget_plan_revision/models/budget_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def action_update_plan(self):
super().action_update_plan()
for rec in self:
if not (rec.init_revision or rec.budget_control_ids):
# Search all budget controls matched with analytics and budget period
# Search all budget controls that matched with analytics and budget period
BudgetControl = self.env["budget.control"]
analytics = rec.plan_line.mapped("analytic_account_id")
budget_controls = BudgetControl.search(
Expand All @@ -57,7 +57,7 @@ def action_update_plan(self):
if analytic_account_id not in group_budget_controls:
group_budget_controls[analytic_account_id] = budget_control
else:
group_budget_controls[analytic_account_id] |= budget_control
group_budget_controls[analytic_account_id] += budget_control
# Update consumed and released amount from previous budget control
for line in rec.plan_line:
prev_control = group_budget_controls.get(
Expand Down Expand Up @@ -85,20 +85,28 @@ def action_create_update_budget_control(self):
)
plan_line_revision = self.env["budget.plan.line"]
analytics = no_bc_lines.mapped("analytic_account_id")
# Find revisions of budget_controls, and use latest one to create_revision()
budget_controls = self.env["budget.control"].search(
# Search all budget controls that matched with analytics and budget period
BudgetControl = self.env["budget.control"]
budget_controls = BudgetControl.search(
[
("analytic_account_id", "in", analytics.ids),
("date_from", "<=", self.budget_period_id.bm_date_from),
("date_to", ">=", self.budget_period_id.bm_date_to),
]
)
# Group budget control (by analytic account)
group_budget_controls = {}
for budget_control in budget_controls:
analytic_account_id = budget_control.analytic_account_id.id
if analytic_account_id not in group_budget_controls:
group_budget_controls[analytic_account_id] = budget_control
else:
group_budget_controls[analytic_account_id] += budget_control
# Create budget control revision
for analytic in analytics:
prev_control = budget_controls.filtered_domain(
[("analytic_account_id", "=", analytic.id)]
).sorted("revision_number")[
-1:
] # sorted asc and get the last one
prev_control = group_budget_controls.get(analytic.id, BudgetControl).sorted(
"revision_number"
)[-1:]
if prev_control:
# Check state budget control for user manual cancel.
if prev_control.state != "cancel" and prev_control.active:
Expand Down
Loading