Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • coopdevs/comunitats-energetiques/odoo-ce
1 result
Show changes
Showing
with 700 additions and 36 deletions
This diff is collapsed.
This diff is collapsed.
from . import selfconsumption
from . import supply_point
from . import partner
from . import distribution_table
from . import supply_point_assignation
from odoo import fields, models, api, _
from odoo.exceptions import ValidationError
STATE_VALUES = [
("draft", _("Draft")),
("validated", _("Validated")),
]
TYPE_VALUES = [
("fixed", _("Fixed"))
]
class DistributionTable(models.Model):
_name = 'energy_selfconsumption.distribution_table'
_description = 'Distribution Table'
@api.depends('supply_point_assignation_ids.coefficient')
def _compute_coefficient_is_valid(self):
for record in self:
record.coefficient_is_valid = not fields.Float.compare(
sum(record.supply_point_assignation_ids.mapped('coefficient')), 1.00000,
precision_rounding=0.00001)
name = fields.Char(readonly=True)
selfconsumption_project_id = fields.Many2one('energy_selfconsumption.selfconsumption', required=True)
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',
'distribution_table_id')
coefficient_is_valid = fields.Boolean(compute=_compute_coefficient_is_valid, readonly=True, store=False)
active = fields.Boolean(default=True)
company_id = fields.Many2one(
"res.company", default=lambda self: self.env.company, readonly=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)
@api.onchange('selfconsumption_project_id')
def _onchange_selfconsumption_project_id(self):
self.supply_point_assignation_ids = False
def button_validate(self):
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"))
record.write({"state": "validated"})
......@@ -21,5 +21,5 @@ class ResPartner(models.Model):
'view_mode': 'tree,form',
'res_model': 'energy_selfconsumption.supply_point',
'domain': [('owner_id', '=', self.id)],
'context': {'create': True, 'default_owner_id': self.id},
'context': {'create': True, 'default_owner_id': self.id, 'default_country_id': self.env.ref('base.es').id},
}
......@@ -10,11 +10,29 @@ class Selfconsumption(models.Model):
}
_description = "Self-consumption Energy Project"
def _compute_distribution_table_count(self):
for record in self:
record.distribution_table_count = len(record.distribution_table_ids)
project_id = fields.Many2one(
"energy_project.project", required=True, ondelete="cascade"
)
code = fields.Char(string="CAU")
power = fields.Float(string="Generation Power (kWh)")
distribution_table_ids = fields.One2many('energy_selfconsumption.distribution_table', 'selfconsumption_project_id',
readonly=True)
distribution_table_count = fields.Integer(compute=_compute_distribution_table_count)
def get_distribution_tables(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': 'Distribution Tables',
'view_mode': 'tree,form',
'res_model': 'energy_selfconsumption.distribution_table',
'domain': [('selfconsumption_project_id', '=', self.id)],
'context': {'create': True, 'default_selfconsumption_project_id': self.id},
}
def set_activation(self):
for record in self:
......@@ -26,4 +44,6 @@ class Selfconsumption(models.Model):
raise ValidationError(_("Project must have a valid Code."))
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"})
......@@ -28,3 +28,6 @@ class SupplyPoint(models.Model):
country_id = fields.Many2one(
"res.country", string="Country", ondelete="restrict", required=True
)
supply_point_assignation_ids = fields.One2many('energy_selfconsumption.supply_point_assignation','supply_point_id',
readonly=True)
from odoo import fields, models, api
from odoo.exceptions import ValidationError
class SupplyPointAssignation(models.Model):
_name = 'energy_selfconsumption.supply_point_assignation'
_description = 'Supply Point Assignation'
@api.depends('distribution_table_id')
def _compute_supply_point_filtered_ids(self):
'''
List of supply point of partners subscribed to the project and not in the list of the distribution table to
prevent multiple assignations of same supply point.
Used to filter out in the view.
:return:
'''
for record in self:
record.supply_point_filtered_ids = \
record.distribution_table_id.selfconsumption_project_id.inscription_ids.mapped('partner_id.supply_ids') \
.filtered_domain([('id', 'not in', record.distribution_table_id.supply_point_assignation_ids.mapped(
'supply_point_id.id'))])
distribution_table_id = fields.Many2one('energy_selfconsumption.distribution_table', required=True)
supply_point_id = fields.Many2one('energy_selfconsumption.supply_point', required=True)
coefficient = fields.Float(string='Distribution coefficient', digits=(1, 5), required=True,
help="The sum of all the coefficients must result in 1")
owner_id = fields.Many2one("res.partner", related='supply_point_id.owner_id')
code = fields.Char(related='supply_point_id.code')
table_coefficient_is_valid = fields.Boolean(related='distribution_table_id.coefficient_is_valid')
supply_point_filtered_ids = fields.One2many('energy_selfconsumption.supply_point',
compute=_compute_supply_point_filtered_ids, readonly=True)
company_id = fields.Many2one(
"res.company", default=lambda self: self.env.company, readonly=True
)
@api.constrains('coefficient')
def constraint_coefficient(self):
for record in self:
if record.coefficient < 0:
raise ValidationError("Coefficient can't be negative.")
@api.onchange('coefficient')
def _onchange_coefficient(self):
if self.coefficient < 0:
self.coefficient = -self.coefficient
if self.coefficient > 1:
self.coefficient = 1
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_energy_selfconsumption_selfconsumption_user,energy_selfconsumption.selfconsumption.user,model_energy_selfconsumption_selfconsumption,energy_project.group_user,1,0,0,0
access_energy_selfconsumption_supply_point_user,energy_selfconsumption.supply_point.user,model_energy_selfconsumption_supply_point,energy_project.group_user,1,0,0,0
access_energy_selfconsumption_distribution_table_user,energy_selfconsumption.distribution_table.user,model_energy_selfconsumption_distribution_table,energy_project.group_user,1,0,0,0
access_energy_selfconsumption_supply_point_assignation_user,energy_selfconsumption.supply_point_assignation.user,model_energy_selfconsumption_supply_point_assignation,energy_project.group_user,1,0,0,0
access_energy_selfconsumption_selfconsumption_admin,energy_selfconsumption.selfconsumption.admin,model_energy_selfconsumption_selfconsumption,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_supply_point_admin,energy_selfconsumption.supply_point.admin,model_energy_selfconsumption_supply_point,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_distribution_table_admin,energy_selfconsumption.distribution_table.admin,model_energy_selfconsumption_distribution_table,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_supply_point_assignation_admin,energy_selfconsumption.supply_point_assignation.admin,model_energy_selfconsumption_supply_point_assignation,energy_project.group_admin,1,1,1,1
......@@ -3,12 +3,36 @@
<record model="ir.rule" id="supply_point_company_rule">
<field name="name">Supply Point multi-company</field>
<field
name="model_id"
ref="energy_selfconsumption.model_energy_selfconsumption_supply_point"
name="model_id"
ref="energy_selfconsumption.model_energy_selfconsumption_supply_point"
/>
<field name="global" eval="True" />
<field name="global" eval="True"/>
<field
name="domain_force"
name="domain_force"
>['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>
<record model="ir.rule" id="distribution_table_rule">
<field name="name">Distribution Table multi-company</field>
<field
name="model_id"
ref="energy_selfconsumption.model_energy_selfconsumption_distribution_table"
/>
<field name="global" eval="True"/>
<field
name="domain_force"
>['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>
<record model="ir.rule" id="supply_point_assignation_rule">
<field name="name">Supply Point Assignation multi-company</field>
<field
name="model_id"
ref="energy_selfconsumption.model_energy_selfconsumption_supply_point_assignation"
/>
<field name="global" eval="True"/>
<field
name="domain_force"
>['|', ('company_id', '=', False), ('company_id', 'in', company_ids)]</field>
</record>
</odoo>
<?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>
<field name="arch" type="xml">
<form string="Distribution Table">
<header>
<button
type="object"
string="Validate"
name="button_validate"
states="draft"
/>
<field
name="state"
widget="statusbar"
readonly="True"
statusbar_visible="draft,validated"
/>
</header>
<sheet>
<widget
name="web_ribbon"
text="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
/>
<field name="active" invisible="True"/>
<group>
<group>
<field name="selfconsumption_project_id"
attrs="{'readonly': [('state', 'not in', ['draft'])]}"
options="{'no_create': True}"/>
</group>
<group>
<field name="type" attrs="{'readonly': [('state', 'not in', ['draft'])]}"/>
</group>
</group>
<group>
<field
name="supply_point_assignation_ids"
widget="one2many"
mode="list"
context="{'default_distribution_table_id':id}"
attrs="{'readonly': [('state', 'not in', ['draft'])]}"
>
<tree editable="bottom">
<field name="supply_point_filtered_ids" invisible="True"/>
<field name="table_coefficient_is_valid" invisible="True"/>
<field
name="supply_point_id"
options="{'no_create': True}"
domain="[('id', 'in', supply_point_filtered_ids)]"
/>
<field name="owner_id"/>
<field name="code"/>
<field name="coefficient" sum="True"
decoration-success="table_coefficient_is_valid == True"/>
</tree>
</field>
</group>
</sheet>
</form>
</field>
</record>
<record id="distribution_table_tree_view" model="ir.ui.view">
<field name="name">energy_selfconsumption.distribution_table.tree</field>
<field name="model">energy_selfconsumption.distribution_table</field>
<field name="arch" type="xml">
<tree string="Distribution Table">
<field name="name"/>
<field name="selfconsumption_project_id"/>
<field name="create_date"/>
<field name="type"/>
<field name="state"/>
</tree>
</field>
</record>
<record id="distribution_table_search_view" model="ir.ui.view">
<field name="name">energy_selfconsumption.distribution_table.search</field>
<field name="model">energy_selfconsumption.distribution_table</field>
<field name="arch" type="xml">
<search string="Distribution Table">
<group expand="1" string="Group By">
<filter string="Self-consumption Project" name="selfconsumption_project_id" domain="[]"
context="{'group_by':'selfconsumption_project_id'}"/>
</group>
</search>
</field>
</record>
<record id="distribution_table_act_window" model="ir.actions.act_window">
<field name="name">Distribution Table</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">energy_selfconsumption.distribution_table</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
There is no examples click here to add new Distribution Table.
</p>
</field>
</record>
</data>
</odoo>
\ No newline at end of file
......@@ -28,8 +28,7 @@
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button class="oe_stat_button" type="object" name="get_supply_points"
icon="fa-lightbulb-o">
<button class="oe_stat_button" type="object" name="get_supply_points" icon="fa-lightbulb-o">
<field string="Supply Points" name="supply_point_count" widget="statinfo"/>
</button>
</div>
......
......@@ -28,6 +28,13 @@
/>
</header>
<sheet>
<div class="oe_button_box" name="button_box">
<button class="oe_stat_button" type="object" name="get_distribution_tables"
icon="fa-table" context="{'default_owner_id': id}"
attrs="{'invisible': [('state', '==', 'draft')]}">
<field string="Distribution Tables" name="distribution_table_count" widget="statinfo"/>
</button>
</div>
<widget
name="web_ribbon"
text="Archived"
......@@ -48,6 +55,7 @@
<group>
<group>
<field name="company_id" invisible="True"/>
<field name="id" invisible="True"/>
<field name="type" invisible="True"/>
<field
name="code"
......
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="supply_point_assignation_form_view" model="ir.ui.view">
<field name="name">energy_selfconsumption.supply_point_assignation.form</field>
<field name="model">energy_selfconsumption.supply_point_assignation</field>
<field name="arch" type="xml">
<form string="Supply Point Assignations">
<sheet>
<group>
<group>
<field name="distribution_table_id" readonly="True"/>
<field name="supply_point_id" readonly="True"/>
<field name="coefficient" readonly="True"/>
</group>
<group>
<field name="owner_id"/>
<field name="code"/>
<field name="table_coefficient_is_valid"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record id="supply_point_assignation_tree_view" model="ir.ui.view">
<field name="name">energy_selfconsumption.supply_point_assignation.tree</field>
<field name="model">energy_selfconsumption.supply_point_assignation</field>
<field name="arch" type="xml">
<tree string="Supply Point Assignations">
<field name="distribution_table_id"/>
<field name="supply_point_id"/>
<field name="coefficient"/>
</tree>
</field>
</record>
</data>
</odoo>
\ No newline at end of file
......@@ -68,6 +68,10 @@
There is no examples click here to add new Supply Points.
</p>
</field>
<field
name="context"
eval="{'default_country_id': ref('base.es')}"
/>
</record>
<menuitem
......
......@@ -4,11 +4,12 @@
Module glue between Energy Self-consumption and Cooperator
""",
"description": """
Module glue between Energy Self-consumption and Cooperator
""",
"author": "Coopdevs Treball SCCL & Som Energia SCCL",
"website": "https://somcomunitats.coop/",
"category": "Customizations",
"version": "14.0.1.0.1",
"version": "14.0.1.1.2",
"depends": [
"energy_selfconsumption",
"cooperator",
......
......@@ -21,5 +21,16 @@
action="action_partner_cooperator_form"
/>
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.button.context</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="energy_selfconsumption.view_partner_form_inherit" />
<field name="arch" type="xml">
<xpath expr="//button[@name='get_supply_points']" position="attributes">
<attribute name="context">{'default_cooperator_id': id}</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
../../../../account_banking_mandate_cooperator
\ No newline at end of file
../../../../account_payment_cooperator
\ No newline at end of file
../../../../cooperator_account_banking_mandate
\ No newline at end of file