Skip to content
Snippets Groups Projects
Commit c1474b02 authored by ebuzeyTra's avatar ebuzeyTra
Browse files

General flow of states applied

parent b647fa85
No related branches found
No related tags found
2 merge requests!187Release 14.0.1.1.13,!172General flow of states applied
Pipeline #38021 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',
......@@ -34,10 +37,22 @@ class DistributionTable(models.Model):
"res.company", default=lambda self: self.env.company, readonly=True
)
@api.model
def create_table(self, selfconsumption_project_id):
existing_tables = self.search([
('selfconsumption_project_id', '=', selfconsumption_project_id.id),
])
if bool(existing_tables):
return any(table['state'] == 'active' for table in existing_tables)
return True
@api.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)
selfconsumption_project_id = self.env['energy_selfconsumption.selfconsumption'].browse(vals.get('selfconsumption_project_id'))
if self.create_table(selfconsumption_project_id):
vals['name'] = self.env.ref('energy_selfconsumption.distribution_table_sequence', False).next_by_id()
return super(DistributionTable, self).create(vals)
raise ValidationError(_("In order to create a new DistributionTable it is necessary that one of the DistributionTables created is active"))
@api.onchange('selfconsumption_project_id')
def _onchange_selfconsumption_project_id(self):
......@@ -51,4 +66,10 @@ class DistributionTable(models.Model):
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,14 @@ class Selfconsumption(models.Model):
'context': {'create': True, 'default_project_id': self.id},
}
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_activation(self):
for record in self:
record.write({"state": "activation"})
record.write({"state": "activation"})
self.distribution_table_state("validated", "process")
def activate(self):
for record in self:
......@@ -65,6 +70,12 @@ 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):
for record in self.distribution_table_ids:
record.write({"state": "validated"})
for record in self:
record.write({"state": "inscription"})
self.distribution_table_state("process", "validated")
......@@ -14,12 +14,19 @@
name="button_validate"
states="draft"
/>
<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>
......@@ -12,7 +12,7 @@
type="object"
string="Set in activation"
name="set_activation"
attrs="{'invisible':[('state','not in',['draft'])]}"
attrs="{'invisible':[('state','not in',['draft', 'inscription'])]}"
/>
<button
type="object"
......@@ -20,11 +20,19 @@
name="activate"
attrs="{'invisible':[('state','not in',['activation'])]}"
/>
<button
type="object"
string="Reset to inscription"
name="set_inscription"
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."
/>
<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