diff --git a/models/acc_enedis_cdc.py b/models/acc_enedis_cdc.py index 9c2eb91ac47320d900a6823300e54d2662a7ace5..4369894263efd89a5589d4190e1d264e80214103 100644 --- a/models/acc_enedis_cdc.py +++ b/models/acc_enedis_cdc.py @@ -4,10 +4,9 @@ from datetime import datetime from dateutil.relativedelta import relativedelta -from odoo import _, api, fields, models +from odoo import _, api, models from odoo.exceptions import ValidationError from odoo.osv import expression -from odoo.tools import date_utils from odoo.addons.api_connector.tools.date_utils import local_to_utc @@ -49,6 +48,129 @@ class AccEnedisCdc(models.Model): return display_hourly_curves, step, step_display_curve + @api.model + def _read_group_process_groupby(self, gb, query): + """ + Overrides models method to avoid getting wrong display format + And manage granluarity = "minute" for 30 minutes curves + """ + initial_gb = gb + if self._context.get("portal_acc_curves") and gb == "date_slot:minute": + gb = "date_slot:hour" + res = super()._read_group_process_groupby(gb, query) + if self._context.get("portal_acc_curves"): + if res["type"] == "datetime": + res["display_format"] = "dd/MM/YYYY HH:mm" + if initial_gb == "date_slot:minute": + res["groupby"] = res["groupby"].replace("hour", "minute") + res["interval"] = relativedelta(minutes=30) + res["granularity"] = res["granularity"].replace("hour", "minute") + res["qualified_field"] = res["qualified_field"].replace( + "hour", "minute" + ) + return res + + @api.model + def _get_curves( + self, + operation_id, + slot_type, + start_date, + end_date, + prm_id=None, + partner_id=None, + curve_types=None, + extra_curve_type=None, + ): + """ + Fonction permettant de récupérer les courbes de charge pour graphique ou export + + :param: int operation_id: opération concernée + char slot_type: type de slot pour la query ("minute", "hour", "month" ou + "year") + datetime start_date: date début + datetime end_date: date de fin + int prm_id : PRM de soutirage à récupérer + int partner_id: contact associé à la courbe + [char] curve_types: type de données + char extra_curve_type : extra curve to be retrieved (optional) + (allowed values : 'cons', 'autocons', 'prod', 'surplus') + @returns: liste de dictionnaires resultat de la requête : + - 1 entrée par date + - 1 champ par type de courbe + """ + start_date_tz = local_to_utc(start_date, "Europe/Paris") + end_date_tz = local_to_utc(end_date, "Europe/Paris") + domain = [ + ("acc_operation_id", "=", operation_id), + ("date_slot", ">=", start_date_tz), + ("date_slot", "<", end_date_tz), + ] + if partner_id and isinstance(partner_id, int): + partner_domain = [("partner_id", "=", partner_id)] + if prm_id and isinstance(prm_id, int): + partner_domain = expression.AND( + [partner_domain, [("acc_counter_id", "=", prm_id)]] + ) + if extra_curve_type and extra_curve_type in ( + "cons", + "autocons", + "prod", + "surplus", + ): + partner_domain = expression.OR( + [partner_domain, [("comp_data_type", "=", extra_curve_type)]] + ) + else: + partner_domain = [("partner_id", "!=", False)] + domain = expression.AND([domain, partner_domain]) + if curve_types: + domain = expression.AND([domain, [("comp_data_type", "in", curve_types)]]) + res = ( + self.env["acc.enedis.cdc"] + .with_context(fill_temporal=True, tz="Europe/Paris", portal_acc_curves=True) + .read_group( + domain, + ["date_slot", "power:sum"], + ["date_slot:" + slot_type, "comp_data_type"], + lazy=False, + ) + ) + final_res = [] + prev_final_line = {} + for line in res: + if ( + prev_final_line + and prev_final_line["date"] == line["date_slot:" + slot_type] + ): + prev_final_line.update( + { + line["comp_data_type"]: line["power"] / 2 / 1000, + } + ) + else: + new_line = { + "date": line["date_slot:" + slot_type], + line["comp_data_type"]: line["power"] / 2 / 1000, + } + final_res.append(new_line) + prev_final_line = new_line + # Add computed values + for final_line in final_res: + if not curve_types or all( + value in curve_types for value in ["autoprod", "prod", "surplus"] + ): + final_line.update( + {"autoprod": final_line["prod"] - final_line["surplus"]} + ) + if not curve_types or all( + value in curve_types for value in ["allocons", "cons", "autocons"] + ): + final_line.update( + {"allocons": final_line["cons"] - final_line["autocons"]} + ) + return final_res + @api.model def _select_clause(self, date_slot, curve_types): """ @@ -122,9 +244,8 @@ class AccEnedisCdc(models.Model): date start_date : first date to be retrieved date end_date : last date to be retrieved int prm_id : id of PRM to be retrieved (optional) - [char] extra_curve_type : extra curve to be retrieved (optional) - (allowed values : 'cons', 'autocons', 'allocons', - 'prod', 'surplus', 'autoprod') + char extra_curve_type : extra curve to be retrieved (optional) + (allowed values : 'cons', 'autocons', 'prod', 'surplus') int partner_id : id of partner to be retrieved (optional) """ if ( @@ -151,10 +272,8 @@ class AccEnedisCdc(models.Model): if extra_curve_type and extra_curve_type in ( "cons", "autocons", - "allocons", "prod", "surplus", - "autoprod", ): result = f"{result} OR cdc.comp_data_type = '{extra_curve_type}' )" else: