From bb7ab27e1886c58a3ec4df839701a4eea114d4c1 Mon Sep 17 00:00:00 2001 From: Saran440 Date: Thu, 25 Apr 2024 17:00:05 +0700 Subject: [PATCH] [FIX] check budget precommit with actual --- .../models/tier_validation.py | 9 ++++++++- budget_control/models/account_move_line.py | 4 ++++ budget_control/models/budget_period.py | 8 ++++++++ budget_control_purchase/models/account_move_line.py | 4 ++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/base_tier_validation_check_budget/models/tier_validation.py b/base_tier_validation_check_budget/models/tier_validation.py index 0d39094f..f69b1b28 100644 --- a/base_tier_validation_check_budget/models/tier_validation.py +++ b/base_tier_validation_check_budget/models/tier_validation.py @@ -22,10 +22,17 @@ def validate_tier(self): lines = getattr(self, "_docline_rel", None) line_type = getattr(self, "_docline_type", None) if self.check_budget and lines and line_type: + doclines = self[lines].sudo() # Special case advance clearing if getattr(self, "advance", False): line_type = "advance" + # -- + if self._name == "account.move" and self.move_type in ( + "in_invoice", + "in_refund", + ): + doclines = self["invoice_line_ids"].sudo() self.env["budget.period"].check_budget_precommit( - self[lines].sudo(), doc_type=line_type + doclines, doc_type=line_type ) return super().validate_tier() diff --git a/budget_control/models/account_move_line.py b/budget_control/models/account_move_line.py index 3318c7fd..118a4424 100644 --- a/budget_control/models/account_move_line.py +++ b/budget_control/models/account_move_line.py @@ -64,3 +64,7 @@ def _get_included_tax(self): if self._name == "account.move.line": return self.env.company.budget_include_tax_account return self.env["account.tax"] + + def uncommit_purchase_budget(self): + """This function for hooks""" + return diff --git a/budget_control/models/budget_period.py b/budget_control/models/budget_period.py index f09dcfbd..c96a7f7c 100644 --- a/budget_control/models/budget_period.py +++ b/budget_control/models/budget_period.py @@ -213,6 +213,11 @@ def check_budget_precommit(self, doclines, doc_type="account"): if not doclines: return doclines = doclines.sudo() + # Allow precommit budget with related origin document (PO) + if doc_type == "account": + budget_moves_uncommit = doclines.with_context( + force_commit=True + ).uncommit_purchase_budget() # Commit budget budget_moves = [] vals_date_commit = [] @@ -231,6 +236,9 @@ def check_budget_precommit(self, doclines, doc_type="account"): doclines.filtered(lambda l: l.id in vals_date_commit).write( {"date_commit": False} ) + # Remove uncommit budget + if budget_moves_uncommit: + budget_moves_uncommit.unlink() @api.model def check_over_returned_budget(self, docline, reverse=False): diff --git a/budget_control_purchase/models/account_move_line.py b/budget_control_purchase/models/account_move_line.py index 81ce3084..e9cc7839 100644 --- a/budget_control_purchase/models/account_move_line.py +++ b/budget_control_purchase/models/account_move_line.py @@ -35,8 +35,8 @@ def uncommit_purchase_budget(self): ): inv_state = ml.move_id.state move_type = ml.move_id.move_type - # Cancel or draft, not commitment line - if inv_state != "posted": + # State Cancel or draft and not context force commit, not commitment line + if not self.env.context.get("force_commit") and inv_state != "posted": self.env["purchase.budget.move"].search( [("move_line_id", "=", ml.id)] ).unlink()