From 4f1d149077c5ec89c6c9530546bf9b9cb80cd2a2 Mon Sep 17 00:00:00 2001 From: Saran440 Date: Tue, 30 Jul 2024 16:56:45 +0700 Subject: [PATCH] [FIX] query carry forward --- .../models/budget_commit_forward.py | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/budget_control/models/budget_commit_forward.py b/budget_control/models/budget_commit_forward.py index c1fab456..8fcbf515 100644 --- a/budget_control/models/budget_commit_forward.py +++ b/budget_control/models/budget_commit_forward.py @@ -70,20 +70,43 @@ def _compute_missing_analytic(self): ) ) - def _get_base_domain(self): + def _get_base_from_extension(self, res_model): """For module extension""" - self.ensure_one() - # TODO: Change domain to support amount_commit with json - domain = [ - # ("amount_commit", ">", 0.0), - ("date_commit", "<", self.to_date_commit), - ("fwd_date_commit", "!=", self.to_date_commit), - ] - return domain + return "" - def _get_commit_docline(self, res_model): + def _get_base_domain_extension(self, res_model): """For module extension""" - return [] + return "" + + def _get_name_model(self, res_model, need_replace=False): + return res_model.replace(".", "_") if need_replace else res_model + + def _get_commit_docline(self, res_model): + """Base domain for query""" + self.ensure_one() + model_name_db = self._get_name_model(res_model, need_replace=True) + query = """ + SELECT a.id + FROM %s a + %s + , jsonb_each_text(a.amount_commit) AS kv(key, value) + WHERE value::numeric != 0 AND a.date_commit < '%s' + AND (a.fwd_date_commit != '%s' OR a.fwd_date_commit is null) %s; + """ + query_string = query % ( + model_name_db, + self._get_base_from_extension(res_model), + self.to_date_commit, + self.to_date_commit, + self._get_base_domain_extension(res_model), + ) + # pylint: disable=sql-injection + self.env.cr.execute(query_string) + # Get all domain ids, remove duplicate from many analytics in 1 line + domain_ids = list({row["id"] for row in self.env.cr.dictfetchall()}) + model_name = self._get_name_model(res_model) + obj_ids = self.env[model_name].browse(domain_ids) + return obj_ids def _get_document_number(self, doc): """For module extension"""