Skip to content
Snippets Groups Projects
Commit 627787c0 authored by emanuel buzey's avatar emanuel buzey
Browse files

Merge branch 'feature/add-state-flow' into 'dev'

General flow of states applied

See merge request !172
parents 42622065 0a236b05
Branches dev
No related tags found
2 merge requests!187Release 14.0.1.1.13,!172General flow of states applied
Pipeline #40277 passed
......@@ -2,6 +2,7 @@ from odoo import _, fields, models
STATE_VALUES = [
("draft", _("Draft")),
("inscription", _("In Inscription")),
("activation", _("In Activation")),
("active", _("Active")),
]
......
......@@ -4,6 +4,8 @@ from odoo.exceptions import ValidationError
STATE_VALUES = [
("draft", _("Draft")),
("validated", _("Validated")),
("process", _("In process")),
("active", _("Active")),
]
TYPE_VALUES = [
......@@ -24,6 +26,7 @@ class DistributionTable(models.Model):
name = fields.Char(readonly=True)
selfconsumption_project_id = fields.Many2one('energy_selfconsumption.selfconsumption', required=True)
selfconsumption_project_state = fields.Selection(related='selfconsumption_project_id.state')
type = fields.Selection(TYPE_VALUES, default="fixed", required=True, string="Modality")
state = fields.Selection(STATE_VALUES, default="draft", required=True)
supply_point_assignation_ids = fields.One2many('energy_selfconsumption.supply_point_assignation',
......@@ -38,7 +41,7 @@ class DistributionTable(models.Model):
def create(self, vals):
vals['name'] = self.env.ref('energy_selfconsumption.distribution_table_sequence', False).next_by_id()
return super(DistributionTable, self).create(vals)
@api.onchange('selfconsumption_project_id')
def _onchange_selfconsumption_project_id(self):
self.supply_point_assignation_ids = False
......@@ -47,8 +50,12 @@ class DistributionTable(models.Model):
for record in self:
if not record.coefficient_is_valid:
raise ValidationError(_("Coefficient distribution must sum to 1."))
if not record.selfconsumption_project_id.state == 'activation':
raise ValidationError(_("Self-consumption project is not in activation"))
if record.selfconsumption_project_id.distribution_table_ids.filtered_domain([('state', '=', 'validated')]):
raise ValidationError(_("Self-consumption project already has a validated table"))
if record.selfconsumption_project_id.distribution_table_ids.filtered_domain([('state', '=', 'process')]):
raise ValidationError(_("Self-consumption project already has a table in process"))
record.write({"state": "validated"})
def button_draft(self):
for record in self:
record.write({"state": "draft"})
......@@ -53,9 +53,16 @@ class Selfconsumption(models.Model):
'context': {'create': True, 'default_project_id': self.id},
}
def set_activation(self):
def distribution_table_state(self, actual_state, new_state):
distribution_table_to_activate = self.distribution_table_ids.filtered(lambda table: table.state == actual_state)
distribution_table_to_activate.write({"state": new_state})
def set_in_activation_state(self):
for record in self:
record.write({"state": "activation"})
if not record.distribution_table_ids.filtered_domain([('state', '=', 'validated')]):
raise ValidationError(_("Must have a valid Distribution Table."))
record.write({"state": "activation"})
self.distribution_table_state("validated", "process")
def activate(self):
for record in self:
......@@ -65,6 +72,15 @@ class Selfconsumption(models.Model):
raise ValidationError(_("Project must have a valid CIL."))
if not record.power or record.power <= 0:
raise ValidationError(_("Project must have a valid Generation Power."))
if not record.distribution_table_ids.filtered_domain([('state', '=', 'validated')]):
raise ValidationError(_("Must have a valid Distribution Table."))
record.write({"state": "active"})
self.distribution_table_state("process", "active")
def set_inscription(self, selfconsumption_state):
for record in self:
record.write({"state": "inscription"})
if selfconsumption_state == 'activation':
self.distribution_table_state("process", "validated")
def set_draft(self):
for record in self:
record.write({"state": "draft"})
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="distribution_table_form_view" model="ir.ui.view">
<field name="name">energy_selfconsumption.distribution_table_form_view.form</field>
<field name="model">energy_selfconsumption.distribution_table</field>
......@@ -13,13 +12,21 @@
string="Validate"
name="button_validate"
states="draft"
class="btn btn-primary"
/>
<button
type="object"
string="Reset to draft"
name="button_draft"
attrs="{'invisible':['|',('state','not in',['validated']),('selfconsumption_project_state','not in',['inscription'])]}"
/>
<field
name="state"
widget="statusbar"
readonly="True"
statusbar_visible="draft,validated"
statusbar_visible="draft,validated,process,active"
/>
<field name="selfconsumption_project_state" invisible="1"/>
</header>
<sheet>
<widget
......@@ -106,4 +113,4 @@
</field>
</record>
</data>
</odoo>
\ No newline at end of file
</odoo>
......@@ -10,21 +10,46 @@
<header>
<button
type="object"
string="Set in activation"
name="set_activation"
string="Set in inscription"
name="set_inscription"
args="['draft']"
attrs="{'invisible':[('state','not in',['draft'])]}"
class="btn btn-primary"
/>
<button
type="object"
string="Set in activation"
name="set_in_activation_state"
attrs="{'invisible':[('state','not in',['inscription'])]}"
class="btn btn-primary"
/>
<button
type="object"
string="Activate"
name="activate"
attrs="{'invisible':[('state','not in',['activation'])]}"
class="btn btn-primary"
/>
<button
type="object"
string="Back to In Inscription status"
name="set_inscription"
args="['activation']"
attrs="{'invisible':[('state','not in',['activation'])]}"
confirm="Are you sure you want to change the project status to the previous step?
If so, the distribution table will return to the 'validated' state."
/>
<button
type="object"
string="Set to Draft"
name="set_draft"
attrs="{'invisible':[('state','not in',['inscription'])]}"
/>
<field
name="state"
widget="statusbar"
readonly="True"
statusbar_visible="draft,activation,active"
statusbar_visible="draft,inscription,activation,active"
/>
</header>
<sheet>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment