diff --git a/controllers/__init__.py b/controllers/__init__.py deleted file mode 100644 index b0c3ec38f45f4fd345016803f0f2bde93f3c2e45..0000000000000000000000000000000000000000 --- a/controllers/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright 2021 Le Filament (<http://www.le-filament.com>) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from . import main diff --git a/controllers/main.py b/controllers/main.py deleted file mode 100644 index 540b75f3f94375f6cf79f58c01bffc6b51448004..0000000000000000000000000000000000000000 --- a/controllers/main.py +++ /dev/null @@ -1,17 +0,0 @@ -# © 2019 Le Filament (<http://www.le-filament.com>) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import json - -from odoo import http -from odoo.http import request - - -class Main(http.Controller): - - @http.route(['/get_data_conso'], type='json', auth="user", website=True) - def get_conso(self, operation_id, scale, **kw): - values = operation_id.graph_view_conso(scale) - - @http.route(['/get_data_prod'], type='json', auth="user", website=True) - def get_conso(self, operation_id, scale, **kw): - values = operation_id.graph_view_prod(scale) \ No newline at end of file diff --git a/models/acc_operation.py b/models/acc_operation.py index 78f9bafb28de22fb7dcef2bae190d88a124a71d0..b729f1c347ba5a5c7600a66aa1864be1d33af221 100644 --- a/models/acc_operation.py +++ b/models/acc_operation.py @@ -1,16 +1,14 @@ # Copyright 2021 Le Filament (<http://www.le-filament.com>) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import fields, models, api -from datetime import datetime, timedelta -from odoo.tools import date_utils - -import dateutil.parser as parser from dateutil.relativedelta import relativedelta +from odoo import api, fields, models +from odoo.tools import date_utils + class AccOperation(models.Model): - _inherit = 'acc.operation' + _inherit = "acc.operation" # ------------------------------------------------------ # Actions @@ -21,16 +19,17 @@ class AccOperation(models.Model): :return: Vue Qweb """ self.ensure_one() - partner_id = self.env['res.partner'].browse(self.env.context.get('default_partner_id')) + self.env["res.partner"].browse(self.env.context.get("default_partner_id")) action = self.env["ir.actions.actions"]._for_xml_id( - "acc_cdc.acc_operation_action_client_courbes") - action['params'] = { - 'operation_ids': self.ids, + "acc_cdc.acc_operation_action_client_courbes" + ) + action["params"] = { + "operation_ids": self.ids, } - action['context'] = { - 'active_id': self.id, - 'active_ids': self.ids, - 'search_default_name': self.name, + action["context"] = { + "active_id": self.id, + "active_ids": self.ids, + "search_default_name": self.name, } return action @@ -43,49 +42,62 @@ class AccOperation(models.Model): :return: la somme de l'autoconso totale de tous les consommateurs """ # Récupère les dates du dernier mois disponible - date_start, date_end = self.get_last_day('month') + date_start, date_end = self.get_last_day("month") # Conso Totale - autoconso_ids = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', '=', self.id), - ('comp_data_type', '=', 'autocons'), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)]) - autoconso_tot = (sum(autoconso_ids.mapped('power')) / 1000) / 2 + autoconso_ids = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "=", self.id), + ("comp_data_type", "=", "autocons"), + ("date_slot", ">=", date_start), + ("date_slot", "<", date_end), + ] + ) + autoconso_tot = (sum(autoconso_ids.mapped("power")) / 1000) / 2 return autoconso_tot def get_power_tot(self): """ - Fonction retournant la consommation totale, l'autoconsommation totale et la production totale du dernier mois. + Fonction retournant la consommation totale, l'autoconsommation totale + et la production totale du dernier mois. :return: la somme de la conso totale de tous les consommateurs, la somme de l autoconso totale de tous les consommateurs, la sommme de la prod totale de tous les producteurs """ # Récupère les dates du dernier mois disponible - date_start, date_end = self.get_last_day('month') + date_start, date_end = self.get_last_day("month") # Conso Totale - conso_ids = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', '=', self.id), - ('comp_data_type', '=', 'cons'), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)]) - conso_tot = (sum(conso_ids.mapped('power')) / 1000) / 2 + conso_ids = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "=", self.id), + ("comp_data_type", "=", "cons"), + ("date_slot", ">=", date_start), + ("date_slot", "<", date_end), + ] + ) + conso_tot = (sum(conso_ids.mapped("power")) / 1000) / 2 # Prod Totale - prod_ids = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', '=', self.id), - ('comp_data_type', '=', 'prod'), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)]) - prod_tot = (sum(prod_ids.mapped('power')) / 1000) / 2 + prod_ids = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "=", self.id), + ("comp_data_type", "=", "prod"), + ("date_slot", ">=", date_start), + ("date_slot", "<", date_end), + ] + ) + prod_tot = (sum(prod_ids.mapped("power")) / 1000) / 2 # Auto cons Totale - autoconso_ids = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', '=', self.id), - ('comp_data_type', '=', 'autocons'), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)]) - autoconso_tot = (sum(autoconso_ids.mapped('power')) / 1000) / 2 + autoconso_ids = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "=", self.id), + ("comp_data_type", "=", "autocons"), + ("date_slot", ">=", date_start), + ("date_slot", "<", date_end), + ] + ) + autoconso_tot = (sum(autoconso_ids.mapped("power")) / 1000) / 2 return conso_tot, prod_tot, autoconso_tot def get_power_install_tot(self): @@ -129,28 +141,32 @@ class AccOperation(models.Model): """ # Get last date slot recorded - last_record = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', 'in', self.ids), - ], limit=1, order='date_slot DESC') + last_record = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "in", self.ids), + ], + limit=1, + order="date_slot DESC", + ) date_end = last_record.date_slot - if scale == 'semestre': + if scale == "semestre": date_first = date_end - relativedelta(months=1) - date_end = date_utils.end_of(date_first, 'month') + date_end = date_utils.end_of(date_first, "month") date_start = date_end - relativedelta(months=5) - date_start = date_utils.start_of(date_start, 'month') - elif scale == 'month': + date_start = date_utils.start_of(date_start, "month") + elif scale == "month": date_start = date_end - relativedelta(months=1) date_start = date_utils.start_of(date_start, scale) date_end = date_utils.end_of(date_start, scale) - elif scale == 'week': + elif scale == "week": date_start = date_end - relativedelta(days=6) - date_start = date_utils.start_of(date_start, 'day') - elif scale == 'day': - date_start = date_utils.start_of(date_end, 'day') + date_start = date_utils.start_of(date_start, "day") + elif scale == "day": + date_start = date_utils.start_of(date_end, "day") else: date_first = date_end - relativedelta(months=1) - date_end = date_utils.end_of(date_first, 'month') + date_end = date_utils.end_of(date_first, "month") date_start = date_utils.start_of(date_end, scale) return date_start, date_end @@ -162,9 +178,13 @@ class AccOperation(models.Model): """ # Get last date slot recorded - first_record = self.env['acc.enedis.cdc'].search([ - ('acc_operation_id', 'in', self.ids), - ], limit=1, order='date_slot ASC') + first_record = self.env["acc.enedis.cdc"].search( + [ + ("acc_operation_id", "in", self.ids), + ], + limit=1, + order="date_slot ASC", + ) date_min = first_record.date_slot return date_min @@ -177,32 +197,33 @@ class AccOperation(models.Model): # Calculate delta between 2 dates delta = (date_end - date_start).days if delta < 1: - step_display_courbe = 'hour' - step = 'hour' + step_display_courbe = "hour" + step = "hour" if not scale: - scale = 'day' - elif delta < 29: - step_display_courbe = 'day' - step = 'hour' + scale = "day" + elif delta < 32 and scale != "month": + step_display_courbe = "day" + step = "hour" if not scale: - scale = 'week' + scale = "week" elif delta <= 365: - step = 'month' - step_display_courbe = 'month' + step = "month" + step_display_courbe = "month" if not scale: - scale = 'month' + scale = "month" else: - step = 'month' - step_display_courbe = 'month' + step = "month" + step_display_courbe = "month" if not scale: - scale = 'year' + scale = "year" return scale, step, step_display_courbe def get_cdc_by_query_cons(self, slot_type, date_start, date_end, prm_ids=None): """ Fonction permettant de récupérer les données pour la - construction des chart pour une ou des opérations données pour les consommateurs + construction des chart pour une ou des opérations données + pour les consommateurs :param slot_type: type de slot pour la query ('month' ou 'hour' ou 'year') date_start: date début date_end: date de fin @@ -232,35 +253,44 @@ class AccOperation(models.Model): GROUP BY date_trunc(%s, A.date_slot) ORDER BY date_slot ASC; """ - query_params = (slot_type, tuple(self.ids), tuple(prm_ids.ids), date_start, date_end, slot_type) + query_params = ( + slot_type, + tuple(self.ids), + tuple(prm_ids.ids), + date_start, + date_end, + slot_type, + ) self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: - if slot_type == 'month' or slot_type == 'year': + if slot_type == "month" or slot_type == "year": data_autocons.append(int(row[1])) data_prod.append(int(row[2])) data_allocons.append(int(row[3])) label.append(row[0]) else: - data_autocons.append({'x': row[0], 'y': round(row[1], 2)}) - data_prod.append({'x': row[0], 'y': round(row[2], 2)}) - data_allocons.append({'x': row[0], 'y': round(row[3], 2)}) + data_autocons.append({"x": row[0], "y": round(row[1], 2)}) + data_prod.append({"x": row[0], "y": round(row[2], 2)}) + data_allocons.append({"x": row[0], "y": round(row[3], 2)}) label.append(row[0]) cdc_jour = { - 'autocons': data_autocons, - 'prod': data_prod, - 'allocons': data_allocons, - 'label': label, - 'cons': data_cons, - 'surplus': data_surplus, + "autocons": data_autocons, + "prod": data_prod, + "allocons": data_allocons, + "label": label, + "cons": data_cons, + "surplus": data_surplus, } return cdc_jour - def get_cdc_by_query_histo_cons(self, slot_type, date_start, date_end, prm_ids=None): + def get_cdc_by_query_histo_cons( + self, slot_type, date_start, date_end, prm_ids=None + ): """ - Fonction permettant de récupérer les données pour la - construction des chart pour une ou des opérations données pour les consommateurs + Fonction permettant de récupérer les données pour la construction + des chart pour une ou des opérations données pour les consommateurs :param slot_type: type de slot pour la query ('month' ou 'hour' ou 'year') date_start: date début date_end: date de fin @@ -286,7 +316,14 @@ class AccOperation(models.Model): GROUP BY date_trunc(%s, A.date_slot) ORDER BY date_slot ASC; """ - query_params = (slot_type, tuple(self.ids), tuple(prm_ids.ids), date_start, date_end, slot_type) + query_params = ( + slot_type, + tuple(self.ids), + tuple(prm_ids.ids), + date_start, + date_end, + slot_type, + ) self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: @@ -295,16 +332,16 @@ class AccOperation(models.Model): label_histo.append(row[0]) cdc_jour = { - 'autocons_histo': data_autocons_histo, - 'allocons_histo': data_allocons_histo, - 'label_histo': label_histo, + "autocons_histo": data_autocons_histo, + "allocons_histo": data_allocons_histo, + "label_histo": label_histo, } return cdc_jour def get_cdc_by_query_prod(self, slot_type, date_start, date_end, prm_ids=None): """ - Fonction permettant de récupérer les données pour la - construction des chart pour une ou des opérations données pour les consommateurs + Fonction permettant de récupérer les données pour la construction des chart + pour une ou des opérations données pour les consommateurs :param slot_type: type de slot pour la query ('month' ou 'hour' ou 'year') date_start: date début date_end: date de fin @@ -334,37 +371,46 @@ class AccOperation(models.Model): GROUP BY date_trunc(%s, A.date_slot) ORDER BY date_slot ASC; """ - query_params = (slot_type, tuple(prm_ids.ids), tuple(self.ids), date_start, date_end, slot_type) + query_params = ( + slot_type, + tuple(prm_ids.ids), + tuple(self.ids), + date_start, + date_end, + slot_type, + ) self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: - if slot_type == 'month' or slot_type == 'year': + if slot_type == "month" or slot_type == "year": data_autocons.append(int(row[1])) data_surplus.append(int(row[2])) label.append(row[0]) - elif slot_type == 'day': + elif slot_type == "day": data_autocons_histo.append(round(row[1], 2)) data_surplus_histo.append(round(row[2], 2)) label_histo.append(row[0]) else: - data_autocons.append({'x': row[0], 'y': round(row[1], 2)}) - data_surplus.append({'x': row[0], 'y': round(row[2], 2)}) + data_autocons.append({"x": row[0], "y": round(row[1], 2)}) + data_surplus.append({"x": row[0], "y": round(row[2], 2)}) label.append(row[0]) cdc_jour = { - 'autocons_prod': data_autocons, - 'label': label, - 'surplus': data_surplus, - 'autocons_histo': data_autocons_histo, - 'label_histo': label_histo, - 'surplus_histo': data_surplus_histo, + "autocons_prod": data_autocons, + "label": label, + "surplus": data_surplus, + "autocons_histo": data_autocons_histo, + "label_histo": label_histo, + "surplus_histo": data_surplus_histo, } return cdc_jour - def get_cdc_by_query_histo_prod(self, slot_type, date_start, date_end, prm_ids=None): + def get_cdc_by_query_histo_prod( + self, slot_type, date_start, date_end, prm_ids=None + ): """ - Fonction permettant de récupérer les données pour la - construction des chart pour une ou des opérations données pour les consommateurs + Fonction permettant de récupérer les données pour la construction des chart + pour une ou des opérations données pour les consommateurs :param slot_type: type de slot pour la query ('month' ou 'hour' ou 'year') date_start: date début date_end: date de fin @@ -391,7 +437,14 @@ class AccOperation(models.Model): GROUP BY date_trunc(%s, A.date_slot) ORDER BY date_slot ASC; """ - query_params = (slot_type, tuple(prm_ids.ids), tuple(self.ids), date_start, date_end, slot_type) + query_params = ( + slot_type, + tuple(prm_ids.ids), + tuple(self.ids), + date_start, + date_end, + slot_type, + ) self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: @@ -400,9 +453,9 @@ class AccOperation(models.Model): label_histo.append(row[0]) cdc_jour = { - 'autocons_prod_histo': data_autocons_prod_histo, - 'label_histo': label_histo, - 'surplus_histo': data_surplus_histo, + "autocons_prod_histo": data_autocons_prod_histo, + "label_histo": label_histo, + "surplus_histo": data_surplus_histo, } return cdc_jour @@ -448,7 +501,7 @@ class AccOperation(models.Model): self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: - if slot_type == 'month' or slot_type == 'year': + if slot_type == "month" or slot_type == "year": data_cons.append(int(row[1])) data_autocons.append(int(row[2])) data_prod.append(int(row[3])) @@ -457,22 +510,22 @@ class AccOperation(models.Model): data_autocons_prod.append(int(row[6])) label.append(row[0]) else: - data_cons.append({'x': row[0], 'y': round(row[1], 2)}) - data_autocons.append({'x': row[0], 'y': round(row[2], 2)}) - data_prod.append({'x': row[0], 'y': round(row[3], 2)}) - data_surplus.append({'x': row[0], 'y': round(row[4], 2)}) - data_allocons.append({'x': row[0], 'y': round(row[5], 2)}) - data_autocons_prod.append({'x': row[0], 'y': round(row[6], 2)}) + data_cons.append({"x": row[0], "y": round(row[1], 2)}) + data_autocons.append({"x": row[0], "y": round(row[2], 2)}) + data_prod.append({"x": row[0], "y": round(row[3], 2)}) + data_surplus.append({"x": row[0], "y": round(row[4], 2)}) + data_allocons.append({"x": row[0], "y": round(row[5], 2)}) + data_autocons_prod.append({"x": row[0], "y": round(row[6], 2)}) label.append(row[0]) cdc_jour = { - 'autocons': data_autocons, - 'autocons_prod': data_autocons_prod, - 'cons': data_cons, - 'prod': data_prod, - 'surplus': data_surplus, - 'allocons': data_allocons, - 'label': label, + "autocons": data_autocons, + "autocons_prod": data_autocons_prod, + "cons": data_cons, + "prod": data_prod, + "surplus": data_surplus, + "allocons": data_allocons, + "label": label, } return cdc_jour @@ -520,11 +573,11 @@ class AccOperation(models.Model): label_histo.append(row[0]) cdc_jour = { - 'autocons_histo': data_autocons_histo, - 'surplus_histo': data_surplus_histo, - 'allocons_histo': data_allocons_histo, - 'autocons_prod_histo': data_autocons_prod_histo, - 'label_histo': label_histo, + "autocons_histo": data_autocons_histo, + "surplus_histo": data_surplus_histo, + "allocons_histo": data_allocons_histo, + "autocons_prod_histo": data_autocons_prod_histo, + "label_histo": label_histo, } return cdc_jour @@ -542,84 +595,82 @@ class AccOperation(models.Model): offsetGridLines = True result = {} - result['line_chart_conso_line'] = { - 'type': 'line', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ - { - 'label': 'Conso', - 'data': chart_data['allocons'], - 'backgroundColor': 'rgba(57, 120, 187, 0.2)', - 'borderColor': 'rgba(57, 120, 187, 1)', - 'borderWidth': 1, - 'hoverRadius': 1, - 'radius': 0, - }, - { - 'label': 'Production solaire', - 'data': chart_data['prod'], - 'backgroundColor': 'rgba(244, 165, 25, 0)', - 'borderColor': 'rgba(244, 165, 25, 1)', - 'borderWidth': 2, - 'hoverRadius': 1, - 'radius': 0, - }, + result["line_chart_conso_line"] = { + "type": "line", + "data": { + "labels": chart_data["label"], + "datasets": [ + { + "label": "Conso", + "data": chart_data["allocons"], + "backgroundColor": "rgba(57, 120, 187, 0.2)", + "borderColor": "rgba(57, 120, 187, 1)", + "borderWidth": 1, + "hoverRadius": 1, + "radius": 0, + }, + { + "label": "Production solaire", + "data": chart_data["prod"], + "backgroundColor": "rgba(244, 165, 25, 0)", + "borderColor": "rgba(244, 165, 25, 1)", + "borderWidth": 2, + "hoverRadius": 1, + "radius": 0, + }, + { + "label": "Autoconso", + "data": chart_data["autocons"], + "backgroundColor": "rgba(91, 154, 81, 0.4)", + "borderColor": "rgba(91, 154, 81, 1)", + "borderWidth": 2, + "hoverRadius": 1, + "radius": 0, + }, + ], + }, + "options": { + "scales": { + "xAxes": [ { - 'label': 'Autoconso', - 'data': chart_data['autocons'], - 'backgroundColor': 'rgba(91, 154, 81, 0.4)', - 'borderColor': 'rgba(91, 154, 81, 1)', - 'borderWidth': 2, - 'hoverRadius': 1, - 'radius': 0, - }, - ], - }, - 'options': { - 'scales': { - 'xAxes': [{ - 'type': 'time', - 'time': { - 'unit': scale, + "type": "time", + "time": { + "unit": scale, }, - 'gridLines': { - 'offsetGridLines': offsetGridLines - }, - }], - 'yAxes': [{ - 'scaleLabel': { - 'display': True, - 'labelString': 'kW', + "gridLines": {"offsetGridLines": offsetGridLines}, + } + ], + "yAxes": [ + { + "scaleLabel": { + "display": True, + "labelString": "kW", } - }] - }, - 'tooltips': { - 'backgroundColor': '#f5f5f5', - 'titleFontColor': '#333', - 'bodyFontColor': '#666', - 'bodySpacing': 4, - 'xPadding': 12, - 'mode': 'x', - 'intersect': False, - }, - 'elements': { - 'point': { - 'radius': 0 } - } + ], }, - } - result['line_chart_prod_prm'] = { - 'type': 'line', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ + "tooltips": { + "backgroundColor": "#f5f5f5", + "titleFontColor": "#333", + "bodyFontColor": "#666", + "bodySpacing": 4, + "xPadding": 12, + "mode": "x", + "intersect": False, + }, + "elements": {"point": {"radius": 0}}, + }, + } + result["line_chart_prod_prm"] = { + "type": "line", + "data": { + "labels": chart_data["label"], + "datasets": [ { - 'label': 'Production solaire', - 'data': chart_data['prod'], - 'backgroundColor': 'rgba(244, 165, 25, 0)', - 'borderColor': 'rgba(244, 165, 25, 1)', + "label": "Production solaire", + "data": chart_data["prod"], + "backgroundColor": "rgba(244, 165, 25, 0)", + "borderColor": "rgba(244, 165, 25, 1)", }, ], }, @@ -635,71 +686,70 @@ class AccOperation(models.Model): """ result = {} - if scale_spe == 'week': - data_autocons = chart_data['autocons_histo'] - data_allocons = chart_data['allocons_histo'] - data_label = chart_data['label_histo'] + if scale_spe == "week": + data_autocons = chart_data["autocons_histo"] + data_allocons = chart_data["allocons_histo"] + data_label = chart_data["label_histo"] else: - data_autocons = chart_data['autocons'] - data_allocons = chart_data['allocons'] - data_label = chart_data['label'] - result['histo_chart_conso'] = { - 'type': 'bar', - 'data': { - 'labels': data_label, - 'datasets': [ + data_autocons = chart_data["autocons"] + data_allocons = chart_data["allocons"] + data_label = chart_data["label"] + result["histo_chart_conso"] = { + "type": "bar", + "data": { + "labels": data_label, + "datasets": [ { - 'label': 'AutoConso', - 'data': data_autocons, - 'backgroundColor': 'rgba(91, 154, 81, 0.4)', - 'hoverBackgroundColor': 'rgba(91, 154, 81, 0.7)', + "label": "AutoConso", + "data": data_autocons, + "backgroundColor": "rgba(91, 154, 81, 0.4)", + "hoverBackgroundColor": "rgba(91, 154, 81, 0.7)", }, { - 'label': 'AlloConso', - 'data': data_allocons, - 'backgroundColor': 'rgba(57, 120, 187, 0.4)', - 'hoverBackgroundColor': 'rgba(57, 120, 187, 0.7)', - }] - }, - 'options': { - 'plugins': { - 'datalabels': { - 'color': 'white', - 'font': { - 'weight': 'bold' - }, + "label": "AlloConso", + "data": data_allocons, + "backgroundColor": "rgba(57, 120, 187, 0.4)", + "hoverBackgroundColor": "rgba(57, 120, 187, 0.7)", + }, + ], + }, + "options": { + "plugins": { + "datalabels": { + "color": "white", + "font": {"weight": "bold"}, } }, - 'interaction': { - 'intersect': False, + "interaction": { + "intersect": False, }, - 'scales': { - 'xAxes': [{ - 'type': 'time', - 'time': { - 'unit': scale - }, - 'stacked': True, - 'offset': True, - 'gridLines': { - 'offsetGridLines': True - }, - - }], - 'yAxes': [{ - 'stacked': True, - 'ticks': { - 'beginAtZero': True, - }, - 'scaleLabel': { - 'display': True, - 'labelString': 'kWh', + "scales": { + "xAxes": [ + { + "type": "time", + "time": {"unit": scale}, + "stacked": True, + "offset": True, + "gridLines": {"offsetGridLines": True}, + } + ], + "yAxes": [ + { + "stacked": True, + "ticks": { + "beginAtZero": True, + }, + "scaleLabel": { + "display": True, + "labelString": "kWh", + }, } - }] + ], }, - 'tooltips': { - } - } + "tooltips": { + "mode": "x", + }, + }, } return result @@ -713,58 +763,57 @@ class AccOperation(models.Model): result = {} - if scale == 'hour': - sum_res1 = sum(int(item['y']) for item in chart_data['autocons']) - sum_res2 = sum(int(item['y']) for item in chart_data['allocons']) + if scale == "hour": + sum_res1 = sum(int(item["y"]) for item in chart_data["autocons"]) + sum_res2 = sum(int(item["y"]) for item in chart_data["allocons"]) else: - sum_res1 = sum(chart_data['autocons']) - sum_res2 = sum(chart_data['allocons']) + sum_res1 = sum(chart_data["autocons"]) + sum_res2 = sum(chart_data["allocons"]) if sum_res1 == 0: - label = ['Alloconso'] + label = ["Alloconso"] res = [sum_res2] elif sum_res2 == 0: - label = ['Autoconso'] + label = ["Autoconso"] res = [sum_res1] else: - label = ['Autoconso', 'Alloconso'] + label = ["Autoconso", "Alloconso"] res = [sum_res1, sum_res2] - result['donuts_chart_conso'] = { - 'type': 'doughnut', - 'data': { - 'labels': label, - 'datasets': [{ - 'label': 'Inférieur à 3', - 'data': res, - 'backgroundColor': [ - 'rgba(91, 154, 81, 0.4)', - 'rgba(57, 120, 187, 0.4)', - ], - 'hoverBackgroundColor': [ - 'rgba(91, 154, 81, 0.7)', - 'rgba(57, 120, 187, 0.7)', - ], - 'borderWidth': 1 - }], + result["donuts_chart_conso"] = { + "type": "doughnut", + "data": { + "labels": label, + "datasets": [ + { + "label": "Inférieur à 3", + "data": res, + "backgroundColor": [ + "rgba(91, 154, 81, 0.4)", + "rgba(57, 120, 187, 0.4)", + ], + "hoverBackgroundColor": [ + "rgba(91, 154, 81, 0.7)", + "rgba(57, 120, 187, 0.7)", + ], + "borderWidth": 1, + } + ], }, - 'options': { - 'cutoutPercentage': 60, - 'animation': { - 'animateScale': True, + "options": { + "cutoutPercentage": 60, + "animation": { + "animateScale": True, }, - 'plugins': { - 'datalabels': { - 'color': 'white', - 'font': { - 'weight': 'bold' - }, - 'padding': 6, + "plugins": { + "datalabels": { + "color": "white", + "font": {"weight": "bold"}, + "padding": 6, } }, - 'tooltips': { - } - } + "tooltips": {}, + }, } return result @@ -781,69 +830,67 @@ class AccOperation(models.Model): # offsetGridLines = True offsetGridLines = True result = {} - result['line_chart_prod_line'] = { - 'type': 'line', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ + result["line_chart_prod_line"] = { + "type": "line", + "data": { + "labels": chart_data["label"], + "datasets": [ { - 'label': 'AutoProd', - 'data': chart_data['autocons_prod'], - 'backgroundColor': 'rgba(91, 154, 81, 0.4)', - 'borderColor': 'rgba(91, 154, 81, 1)', - 'borderWidth': 2, - 'hoverRadius': 1, - 'radius': 0, - 'fill': 'origin' + "label": "AutoProd", + "data": chart_data["autocons_prod"], + "backgroundColor": "rgba(91, 154, 81, 0.4)", + "borderColor": "rgba(91, 154, 81, 1)", + "borderWidth": 2, + "hoverRadius": 1, + "radius": 0, + "fill": "origin", }, { - 'label': 'Surplus', - 'data': chart_data['surplus'], - 'backgroundColor': 'rgba(225, 80, 96, 0.4)', - 'borderColor': 'rgba(225, 80, 96, 1)', - 'borderWidth': 2, - 'hoverRadius': 1, - 'radius': 0, - 'fill': '-1' + "label": "Surplus", + "data": chart_data["surplus"], + "backgroundColor": "rgba(225, 80, 96, 0.4)", + "borderColor": "rgba(225, 80, 96, 1)", + "borderWidth": 2, + "hoverRadius": 1, + "radius": 0, + "fill": "-1", }, ], }, - 'options': { - 'scales': { - 'xAxes': [{ - 'type': 'time', - 'time': { - 'unit': scale, - }, - 'ticks': { - # 'min': result.date_start, - }, - 'gridLines': { - 'offsetGridLines': offsetGridLines + "options": { + "scales": { + "xAxes": [ + { + "type": "time", + "time": { + "unit": scale, + }, + "ticks": { + # 'min': result.date_start, + }, + "gridLines": {"offsetGridLines": offsetGridLines}, } - }], - 'yAxes': [{ - 'stacked': True, - 'scaleLabel': { - 'display': True, - 'labelString': 'kW', + ], + "yAxes": [ + { + "stacked": True, + "scaleLabel": { + "display": True, + "labelString": "kW", + }, } - }] - }, - 'tooltips': { - 'backgroundColor': '#f5f5f5', - 'titleFontColor': '#333', - 'bodyFontColor': '#666', - 'bodySpacing': 4, - 'xPadding': 12, - 'mode': 'x', - 'intersect': False, + ], }, - 'elements': { - 'point': { - 'radius': 0 - } + "tooltips": { + "backgroundColor": "#f5f5f5", + "titleFontColor": "#333", + "bodyFontColor": "#666", + "bodySpacing": 4, + "xPadding": 12, + "mode": "x", + "intersect": False, }, + "elements": {"point": {"radius": 0}}, }, } return result @@ -857,58 +904,57 @@ class AccOperation(models.Model): """ result = {} - if scale == 'hour': - sum_res1 = sum(int(item['y']) for item in chart_data['autocons_prod']) - sum_res2 = sum(int(item['y']) for item in chart_data['surplus']) + if scale == "hour": + sum_res1 = sum(int(item["y"]) for item in chart_data["autocons_prod"]) + sum_res2 = sum(int(item["y"]) for item in chart_data["surplus"]) else: - sum_res1 = sum(chart_data['autocons_prod']) - sum_res2 = sum(chart_data['surplus']) + sum_res1 = sum(chart_data["autocons_prod"]) + sum_res2 = sum(chart_data["surplus"]) if sum_res1 == 0: - label = ['Surplus'] + label = ["Surplus"] res = [sum_res2] elif sum_res2 == 0: - label = ['AutoProd'] + label = ["AutoProd"] res = [sum_res1] else: - label = ['AutoProd', 'Surplus'] + label = ["AutoProd", "Surplus"] res = [sum_res1, sum_res2] - result['donuts_chart_prod'] = { - 'type': 'doughnut', - 'data': { - 'labels': label, - 'datasets': [{ - 'label': 'Inférieur à 3', - 'data': res, - 'backgroundColor': [ - 'rgba(91, 154, 81, 0.4)', - 'rgba(225, 80, 96, 0.4)', - ], - 'hoverBackgroundColor': [ - 'rgba(91, 154, 81, 0.7)', - 'rgba(225, 80, 96, 0.7)', - ], - 'borderWidth': 1 - }], + result["donuts_chart_prod"] = { + "type": "doughnut", + "data": { + "labels": label, + "datasets": [ + { + "label": "Inférieur à 3", + "data": res, + "backgroundColor": [ + "rgba(91, 154, 81, 0.4)", + "rgba(225, 80, 96, 0.4)", + ], + "hoverBackgroundColor": [ + "rgba(91, 154, 81, 0.7)", + "rgba(225, 80, 96, 0.7)", + ], + "borderWidth": 1, + } + ], }, - 'options': { - 'plugins': { - 'datalabels': { - 'color': 'white', - 'font': { - 'weight': 'bold' - }, - 'padding': 6, + "options": { + "plugins": { + "datalabels": { + "color": "white", + "font": {"weight": "bold"}, + "padding": 6, } }, - 'cutoutPercentage': 60, - 'animation': { - 'animateScale': True, + "cutoutPercentage": 60, + "animation": { + "animateScale": True, }, - 'tooltips': { - } - } + "tooltips": {}, + }, } return result @@ -922,74 +968,74 @@ class AccOperation(models.Model): """ result = {} - if scale_spe == 'week': - data_autocons = chart_data['autocons_prod_histo'] - data_surplus = chart_data['surplus_histo'] - data_label = chart_data['label_histo'] + if scale_spe == "week": + data_autocons = chart_data["autocons_prod_histo"] + data_surplus = chart_data["surplus_histo"] + data_label = chart_data["label_histo"] else: - data_autocons = chart_data['autocons_prod'] - data_surplus = chart_data['surplus'] - data_label = chart_data['label'] - - result['histo_chart_prod'] = { - 'type': 'bar', - 'data': { - 'labels': data_label, - 'datasets': [ + data_autocons = chart_data["autocons_prod"] + data_surplus = chart_data["surplus"] + data_label = chart_data["label"] + + result["histo_chart_prod"] = { + "type": "bar", + "data": { + "labels": data_label, + "datasets": [ { - 'label': 'AutoProd', - 'data': data_autocons, - 'backgroundColor': 'rgba(91, 154, 81, 0.4)', - 'hoverBackgroundColor': 'rgba(91, 154, 81, 0.7)', - 'borderColor': 'rgba(91, 154, 81, 1)', + "label": "AutoProd", + "data": data_autocons, + "backgroundColor": "rgba(91, 154, 81, 0.4)", + "hoverBackgroundColor": "rgba(91, 154, 81, 0.7)", + "borderColor": "rgba(91, 154, 81, 1)", }, { - 'label': 'Surplus', - 'data': data_surplus, - 'backgroundColor': 'rgba(225, 80, 96, 0.4)', - 'hoverBackgroundColor': 'rgba(225, 80, 96, 0.7)', - 'borderColor': 'rgba(225, 80, 96, 1)', - }] + "label": "Surplus", + "data": data_surplus, + "backgroundColor": "rgba(225, 80, 96, 0.4)", + "hoverBackgroundColor": "rgba(225, 80, 96, 0.7)", + "borderColor": "rgba(225, 80, 96, 1)", + }, + ], }, - 'options': { - 'plugins': { - 'datalabels': { - 'color': 'white', - 'font': { - 'weight': 'bold' - }, + "options": { + "plugins": { + "datalabels": { + "color": "white", + "font": {"weight": "bold"}, } }, - 'interaction': { - 'intersect': False, + "interaction": { + "intersect": False, }, - 'scales': { - 'xAxes': [{ - 'type': 'time', - 'time': { - 'unit': scale - }, - 'stacked': True, - 'offset': True, - 'gridLines': { - 'offsetGridLines': True + "scales": { + "xAxes": [ + { + "type": "time", + "time": {"unit": scale}, + "stacked": True, + "offset": True, + "gridLines": {"offsetGridLines": True}, } - }], - 'yAxes': [{ - 'stacked': True, - 'ticks': { - 'beginAtZero': True, - }, - 'type': 'linear', - 'scaleLabel': { - 'display': True, - 'labelString': 'kWh', + ], + "yAxes": [ + { + "stacked": True, + "ticks": { + "beginAtZero": True, + }, + "type": "linear", + "scaleLabel": { + "display": True, + "labelString": "kWh", + }, } - }] + ], }, - 'tooltips': { + "tooltips": { + "mode": "x", }, - } + }, } return result @@ -1008,8 +1054,10 @@ class AccOperation(models.Model): """ chart_data = self.get_cdc_by_query(step_courbe, date_start, date_end, None) - if scale == 'week': - chart_data_histo = self.get_cdc_by_query_histo('day', date_start, date_end, None) + if scale == "week": + chart_data_histo = self.get_cdc_by_query_histo( + "day", date_start, date_end, None + ) chart_data.update(chart_data_histo) return chart_data @@ -1026,95 +1074,127 @@ class AccOperation(models.Model): """ result_graph = {} # Get the operations depending to the domain - operation_ids = self.env['acc.operation'].search(domain) + operation_ids = self.env["acc.operation"].search(domain) if operation_ids: # Get date start and date end depending on type of scale if first_day and last_day: date_start = fields.Datetime.to_datetime(first_day) date_end = fields.Datetime.to_datetime(last_day) - date_end = date_utils.end_of(date_end, 'day') + date_end = date_utils.end_of(date_end, "day") elif first_day: - date_start, date_end = operation_ids.get_last_day('day') - date_end = date_utils.end_of(date_end, 'day') + date_start, date_end = operation_ids.get_last_day("day") + date_end = date_utils.end_of(date_end, "day") else: - date_start, date_end = operation_ids.get_last_day('month') - date_end = date_utils.end_of(date_end, 'day') + date_start, date_end = operation_ids.get_last_day("month") + date_end = date_utils.end_of(date_end, "day") counter_ids = None if acc_counter_id: - counter_ids = self.env['acc.counter'].search([('name', '=', acc_counter_id)]).ids + counter_ids = ( + self.env["acc.counter"].search([("name", "=", acc_counter_id)]).ids + ) scale, step_courbe, step_display_courbe = operation_ids.get_step_from_date( - date_start=date_start, date_end=date_end, scale=scale) + date_start=date_start, date_end=date_end, scale=scale + ) # Get the data to display in chart - chart_data = operation_ids.get_cdc(scale=scale, step_courbe=step_courbe, date_start=date_start, date_end=date_end, - prm_ids=counter_ids) + chart_data = operation_ids.get_cdc( + scale=scale, + step_courbe=step_courbe, + date_start=date_start, + date_end=date_end, + prm_ids=counter_ids, + ) # Build the chart with data and options - result_graph_line = self.chart_data_line_prod(chart_data, step_display_courbe) + result_graph_line = self.chart_data_line_prod( + chart_data, step_display_courbe + ) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_prod(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_prod( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_prod(chart_data, step_courbe) result_graph.update(result_graph_donuts) - result_graph_line = self.chart_data_line_cons(chart_data, step_display_courbe) + result_graph_line = self.chart_data_line_cons( + chart_data, step_display_courbe + ) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_cons(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_cons( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_cons(chart_data, step_courbe) result_graph.update(result_graph_donuts) # result_graph.update(result_graph2) - result_graph.update({'scale': scale}) + result_graph.update({"scale": scale}) date_end = date_end.strftime("%d/%m/%Y") date_min = operation_ids.get_first_day() date_min = date_min.strftime("%d/%m/%Y") - result_graph.update({ - 'date_end': date_end, - 'date_min': date_min, - 'date_start': date_start, - }) + result_graph.update( + { + "date_end": date_end, + "date_min": date_min, + "date_start": date_start, + } + ) return result_graph # ------------------------------------------------------ # Functions to manage route # ------------------------------------------------------ - def build_graph_data_options(self, chart_data, step_display_courbe=None, scale=None, step_courbe=None): + def build_graph_data_options( + self, chart_data, step_display_courbe=None, scale=None, step_courbe=None + ): result_graph = {} result_graph_line = self.chart_data_line_prod(chart_data, step_display_courbe) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_prod(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_prod( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_prod(chart_data, step_courbe) result_graph.update(result_graph_donuts) result_graph_line = self.chart_data_line_cons(chart_data, step_display_courbe) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_cons(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_cons( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_cons(chart_data, step_courbe) result_graph.update(result_graph_donuts) return result_graph - def build_graph_data_options_prod(self, chart_data, step_display_courbe=None, scale=None, step_courbe=None): + def build_graph_data_options_prod( + self, chart_data, step_display_courbe=None, scale=None, step_courbe=None + ): result_graph = {} result_graph_line = self.chart_data_line_prod(chart_data, step_display_courbe) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_prod(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_prod( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_prod(chart_data, step_courbe) result_graph.update(result_graph_donuts) return result_graph - def build_graph_data_options_cons(self, chart_data, step_display_courbe=None, scale=None, step_courbe=None): + def build_graph_data_options_cons( + self, chart_data, step_display_courbe=None, scale=None, step_courbe=None + ): result_graph = {} result_graph_line = self.chart_data_line_cons(chart_data, step_display_courbe) result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_cons(chart_data, step_display_courbe, scale) + result_graph_histo = self.chart_data_histo_cons( + chart_data, step_display_courbe, scale + ) result_graph.update(result_graph_histo) result_graph_donuts = self.chart_data_donuts_cons(chart_data, step_courbe) result_graph.update(result_graph_donuts) @@ -1137,37 +1217,56 @@ class AccOperation(models.Model): if not date_start and not date_end: date_start, date_end = self.get_last_day(scale) else: - date_end = date_utils.end_of(date_end, 'day') + date_end = date_utils.end_of(date_end, "day") scale, step_courbe, step_display_courbe = self.get_step_from_date( - date_start=date_start, date_end=date_end, scale=scale) + date_start=date_start, date_end=date_end, scale=scale + ) # Get the data to display in chart chart_data = self.get_cdc( - scale=scale, step_courbe=step_courbe, date_start=date_start, - date_end=date_end, prm_ids=None) + scale=scale, + step_courbe=step_courbe, + date_start=date_start, + date_end=date_end, + prm_ids=None, + ) # Build the chart with data and options result_graph = self.build_graph_data_options( - chart_data, step_display_courbe=step_display_courbe, scale=scale, step_courbe=step_courbe) + chart_data, + step_display_courbe=step_display_courbe, + scale=scale, + step_courbe=step_courbe, + ) - date_deb, date_max = self.get_last_day('day') + date_deb, date_max = self.get_last_day("day") date_max = date_max.strftime("%d/%m/%Y") date_min = self.get_first_day() date_min = date_min.strftime("%d/%m/%Y") - result_graph.update({ - 'date_start': date_start, - 'date_end': date_end, - 'date_min': date_min, - 'date_max': date_max, - 'scale': scale - }) + result_graph.update( + { + "date_start": date_start, + "date_end": date_end, + "date_min": date_min, + "date_max": date_max, + "scale": scale, + } + ) return result_graph # ------------------------------------------------------ # Functions to manage route # ------------------------------------------------------ - def graph_view_type(self, type=None, scale=None, date_start=None, date_end=None, prm_id=None, partner_id=None): + def graph_view_type( + self, + type=None, + scale=None, + date_start=None, + date_end=None, + prm_id=None, + partner_id=None, + ): """ Fonction appelée pour l'affichage des courbes consommation sur le portail @@ -1182,47 +1281,66 @@ class AccOperation(models.Model): date_start, date_end = self.get_last_day(scale) is_scale = True else: - date_end = date_utils.end_of(date_end, 'day') + date_end = date_utils.end_of(date_end, "day") is_scale = False # Get the step to display curve in chart scale, step_courbe, step_display_courbe = self.get_step_from_date( - date_start=date_start, date_end=date_end, scale=scale) - if scale == 'week' or scale == 'day': + date_start=date_start, date_end=date_end, scale=scale + ) + if scale == "week" or scale == "day": is_curve_line = True else: is_curve_line = False # Get PRM ids if prm_id: - acc_counter_ids = self.env['acc.counter'].browse(prm_id) + acc_counter_ids = self.env["acc.counter"].browse(prm_id) elif partner_id: - acc_counter_ids = self.env['acc.counter'].search([('partner_id', '=', partner_id)]) + acc_counter_ids = self.env["acc.counter"].search( + [("partner_id", "=", partner_id)] + ) else: - if type == 'cons': + if type == "cons": acc_counter_ids = self.acc_delivery_ids else: acc_counter_ids = self.acc_injection_ids - if type == 'cons': - chart_data = self.get_cdc_by_query_cons(step_courbe, date_start, date_end, acc_counter_ids) - if scale == 'week': - chart_data_histo = self.get_cdc_by_query_histo_cons('day', date_start, date_end, acc_counter_ids) + if type == "cons": + chart_data = self.get_cdc_by_query_cons( + step_courbe, date_start, date_end, acc_counter_ids + ) + if scale == "week": + chart_data_histo = self.get_cdc_by_query_histo_cons( + "day", date_start, date_end, acc_counter_ids + ) chart_data.update(chart_data_histo) # Build the chart with data and options - result_graph = self.build_graph_data_options_cons(chart_data, step_display_courbe=step_display_courbe, - scale=scale, step_courbe=step_courbe) + result_graph = self.build_graph_data_options_cons( + chart_data, + step_display_courbe=step_display_courbe, + scale=scale, + step_courbe=step_courbe, + ) else: - chart_data = self.get_cdc_by_query_prod(step_courbe, date_start, date_end, acc_counter_ids) - if scale == 'week': - chart_data_histo = self.get_cdc_by_query_histo_prod('day', date_start, date_end, acc_counter_ids) + chart_data = self.get_cdc_by_query_prod( + step_courbe, date_start, date_end, acc_counter_ids + ) + if scale == "week": + chart_data_histo = self.get_cdc_by_query_histo_prod( + "day", date_start, date_end, acc_counter_ids + ) chart_data.update(chart_data_histo) # Build the chart with data and options - result_graph = self.build_graph_data_options_prod(chart_data, step_display_courbe=step_display_courbe, - scale=scale, step_courbe=step_courbe) - date_deb, date_max = self.get_last_day('day') + result_graph = self.build_graph_data_options_prod( + chart_data, + step_display_courbe=step_display_courbe, + scale=scale, + step_courbe=step_courbe, + ) + date_deb, date_max = self.get_last_day("day") date_max = date_max.strftime("%d/%m/%Y") date_min = self.get_first_day() date_min = date_min.strftime("%d/%m/%Y") @@ -1230,12 +1348,14 @@ class AccOperation(models.Model): if not is_scale: scale = False - result_graph.update({ - 'date_start': date_start, - 'date_end': date_end, - 'date_min': date_min, - 'date_max': date_max, - 'scale': scale, - 'is_curve_line': is_curve_line - }) + result_graph.update( + { + "date_start": date_start, + "date_end": date_end, + "date_min": date_min, + "date_max": date_max, + "scale": scale, + "is_curve_line": is_curve_line, + } + ) return result_graph