From a76e8f0bb1850e8104bd499dd389682660e51e12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Garc=C3=ADa?= <alvarogruiz8@gmail.com>
Date: Mon, 24 Feb 2025 10:11:49 +0100
Subject: [PATCH] =?UTF-8?q?[IMP]=20=E2=9C=A8=20Controls=20of=200=20or=20le?=
 =?UTF-8?q?ss=20or=20more=20than=20100=20percent=20are=20removed.=20It=20i?=
 =?UTF-8?q?s=20calculated=20in=20both=20cases.=20Changes=20in=20the=20colo?=
 =?UTF-8?q?rs=20of=20the=20bars=20according=20to=20the=20percentage.=20And?=
 =?UTF-8?q?=20the=20excess=20above=20100=20is=20now=20calculated=20linearl?=
 =?UTF-8?q?y=20and=20proportionally.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Author: Alvaro Garcia <alvaro.garcia@somit.coop>
---
 .../static/src/js/progress_bar.js             | 22 ++++++-----
 .../create_distribution_table_wizard.py       | 39 ++++++++++++-------
 ...create_distribution_table_wizard_views.xml | 22 ++++-------
 3 files changed, 46 insertions(+), 37 deletions(-)

diff --git a/energy_selfconsumption/static/src/js/progress_bar.js b/energy_selfconsumption/static/src/js/progress_bar.js
index 359949dd9..c7ea3699c 100644
--- a/energy_selfconsumption/static/src/js/progress_bar.js
+++ b/energy_selfconsumption/static/src/js/progress_bar.js
@@ -1,12 +1,12 @@
 /** @odoo-module **/
 
-import {Component, useState} from "@odoo/owl";
-import {registry} from "@web/core/registry";
+import { Component, useState } from "@odoo/owl";
+import { registry } from "@web/core/registry";
 
 class ProgressBarWidget extends Component {
   setup() {
     // Obtener valores desde this.props
-    const {record} = this.props;
+    const { record } = this.props;
 
     // Configurar el estado inicial usando hooks de OWL
     this.state = useState({
@@ -16,17 +16,21 @@ class ProgressBarWidget extends Component {
     });
 
     // Calcular el porcentaje basado en los valores actuales
-    this.state.percentage = Math.min(
-      (this.state.current_quantity / this.state.max_quantity) * 100,
-      100
-    );
+    this.state.percentage = (this.state.current_quantity / this.state.max_quantity) * 100;
   }
 
   get progressStyle() {
+    let background_color = '#7C7BAD';
+    if (this.state.percentage > 100) {
+      background_color = '#a10000';
+    } else if (this.state.percentage == 100) {
+      background_color = '#00a12a';
+    }
+    let width = this.state.percentage > 100 ? 100 : this.state.percentage;
     return `
-      width: ${this.state.percentage}%;
+      width: ${width}%;
       height: 100%;
-      background-color: #7C7BAD;
+      background-color: ${background_color};
     `;
   }
 
diff --git a/energy_selfconsumption/wizards/create_distribution_table_wizard.py b/energy_selfconsumption/wizards/create_distribution_table_wizard.py
index 4d604ee62..8116275e5 100644
--- a/energy_selfconsumption/wizards/create_distribution_table_wizard.py
+++ b/energy_selfconsumption/wizards/create_distribution_table_wizard.py
@@ -48,6 +48,11 @@ class CreateDistributionTableWizard(models.TransientModel):
         string="Type distribute excess",
     )
 
+    @api.onchange('distributed_power')
+    def _onchange_distributed_power(self):
+        if self.distributed_power > self.max_distributed_power or self.distributed_power <= 0:
+            self.distribute_excess = 'yes'
+
     @api.model
     def default_get(self, default_fields):
         # OVERRIDE
@@ -83,12 +88,6 @@ class CreateDistributionTableWizard(models.TransientModel):
             / default_fields["max_distributed_power"]
         ) * 100
 
-        if default_fields["percentage_of_distributed_power"] == 0:
-            raise ValidationError(_("Your distribution percentage cannot be 0."))
-
-        if default_fields["percentage_of_distributed_power"] > 100:
-            raise ValidationError(_("Your distribution percentage cannot exceed 100%."))
-
         return default_fields
 
     def create_distribution_table(self):
@@ -133,16 +132,28 @@ class CreateDistributionTableWizard(models.TransientModel):
         coefficient = inscription.participation_real_quantity
 
         if self.distribute_excess == "yes":
-            distribute_excess_float = (
-                self.max_distributed_power - self.distributed_power
-            )
-
-            if self.type_distribute_excess == "proportional":
-                coefficient += distribute_excess_float * (
-                    inscription.participation_real_quantity / self.distributed_power
+            if self.distributed_power < self.max_distributed_power:
+                distribute_excess_float = (
+                    self.max_distributed_power - self.distributed_power
                 )
+
+                if self.type_distribute_excess == "proportional":
+                    coefficient += distribute_excess_float * (
+                        inscription.participation_real_quantity / self.distributed_power
+                    )
+                else:
+                    coefficient += distribute_excess_float / len_inscriptions
             else:
-                coefficient += distribute_excess_float / len_inscriptions
+                distribute_excess_float = (
+                    self.distributed_power - self.max_distributed_power
+                )
+
+                if self.type_distribute_excess == "proportional":
+                    coefficient -= distribute_excess_float * (
+                        inscription.participation_real_quantity / self.distributed_power
+                    )
+                else:
+                    coefficient -= distribute_excess_float / len_inscriptions
 
         coefficient = coefficient / self.max_distributed_power
 
diff --git a/energy_selfconsumption/wizards/create_distribution_table_wizard_views.xml b/energy_selfconsumption/wizards/create_distribution_table_wizard_views.xml
index 170dbc061..92b25e333 100644
--- a/energy_selfconsumption/wizards/create_distribution_table_wizard_views.xml
+++ b/energy_selfconsumption/wizards/create_distribution_table_wizard_views.xml
@@ -13,24 +13,18 @@
                 <form>
                     <sheet>
                         <group>
+                            <field name="type" />
+                        </group>
+                        <group attrs="{'invisible':[('type','!=','fixed')]}">
                             <field name="max_distributed_power" invisible="1" />
                             <field
-                name="distributed_power"
-                widget="progress_bar_widget"
-                options="{'max_quantity': 'max_distributed_power', 'extra_label': 'kWn'}"
-              />
-                        <!-- <field name="distributed_power" widget="progress_bar_widget">
-                            <options>
-                                <option name="max_quantity" expr="max_distributed_power"/>
-                                <option name="extra_label">kW</option>
-                            </options>
-                        </field> -->
-                        </group>
-                        <group>
-                            <field name="type" />
+                                name="distributed_power"
+                                widget="progress_bar_widget"
+                                options="{'max_quantity': 'max_distributed_power', 'extra_label': 'kWn'}"
+                            />
                             <field name="distribute_excess" />
                         </group>
-                        <group attrs="{'invisible':[('distribute_excess','=','no')]}">
+                        <group attrs="{'invisible':['|',('type','!=','fixed'),('distribute_excess','=','no')]}">
                             <field name="type_distribute_excess" />
                         </group>
                     </sheet>
-- 
GitLab