diff --git a/energy_selfconsumption/static/src/js/progress_bar.js b/energy_selfconsumption/static/src/js/progress_bar.js index 359949dd9dedc7e949d6308b254dcefab384a800..c7ea3699cf5333bf81ec8e41779a178e9930e9ed 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 4d604ee621cf02d48434b6e8932a39ac5f962bc6..8116275e52000984acb3856ff79fd0867649574b 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 170dbc0615a82f72b9aaf5ecbaa6c83a72066a5e..92b25e333e5d2578b2e0a45b605cac9b133b65d2 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>