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
Commits on Source (32)
Showing with 351 additions and 26 deletions
from odoo import SUPERUSER_ID, api
import logging
logger = logging.getLogger(__name__)
def migrate(cr, version):
logger.info("Running post migration {}".format(version))
env = api.Environment(cr, SUPERUSER_ID, {})
internal_user_role = env.ref('energy_communities.role_internal_user')
odoo_base_user_ids = [u.id for u in (env.ref("base.user_root"),
env.ref("base.user_admin"),
env.ref("base.default_user"),
env.ref("base.public_user"),
env.ref("base.template_portal_user_id"))]
instance_company_id = env['res.company'].search([('hierarchy_level','=','instance')])
logger.info("Running post migration {}. Instance company: {}".format(version, instance_company_id.name))
coordinator_company_ids = env['res.company'].search([('hierarchy_level','=','coordinator')])
logger.info("Running post migration {}. Coordinator companies: {}".format(version, coordinator_company_ids.mapped('name')))
# get all target users to manage
target_users = env['res.users'].search([('id', 'not in', odoo_base_user_ids)])
logger.info("Running post migration {}. All Target users ({}): {}".format(version,
len(target_users), str(target_users.mapped('name'))))
# get users that must be setted as platform_admins
target_users_platform_admins = target_users.filtered(lambda u: instance_company_id in u.company_ids)
logger.info("Running post migration {}. Platform admin users ({}): {}".format(version,
len(target_users_platform_admins), str(target_users_platform_admins.mapped('name'))))
target_users = target_users - target_users_platform_admins
# get users that must be setted as coordinator_admins
target_users_coordinator_admins = env['res.users']
for u in target_users:
if u.company_ids & coordinator_company_ids:
target_users_coordinator_admins |= u
logger.info("Running post migration {}. Coordinator admin users ({}): {}".format(version,
len(target_users_coordinator_admins), str(target_users_coordinator_admins.mapped('name'))))
# get users that must be setted as CE users (admin or member) per its company_ids
target_users = target_users - target_users_coordinator_admins
logger.info("Running post migration {}. CE admn & member users ({}): {}".format(version,
len(target_users), str(target_users.mapped('name'))))
# loop1 --> update the plattform_admin_users
for u in target_users_platform_admins:
logger.info("Running post migration {}. Platform admin users: {}".format(version, u.name))
# delete all existing role lines
u.write({'role_line_ids':[(5,0,0)]})
# add the internal role
u.write({'role_line_ids':[(0,0,{
"user_id": u.id,
"active": True,
"role_id": internal_user_role.id,})]})
# add the platform admin role
u.write({'role_line_ids':[(0,0,{
"user_id": u.id,
"active": True,
"role_id": env.ref('energy_communities.role_platform_admin').id,})]})
# loop2 --> update the Coordinators users
for u in target_users_coordinator_admins:
logger.info("Running post migration {}. Coordinator admin users: {}".format(version, u.name))
original_company_ids = env['res.company'].browse(u.company_ids.mapped('id'))
logger.info("Original user companies {}".format(original_company_ids.mapped('name')))
# get the companies that are sons of the Coordinator ones
coord_company_ids = u.company_ids & coordinator_company_ids
logger.info("Coordinator companies {}".format(coord_company_ids.mapped('name')))
coord_hierarchy_company_ids = env['res.company']
for coord_c in coord_company_ids:
coord_hierarchy_company_ids |= env['res.company'].search(
[('hierarchy_level','=','community'),('parent_id','=',coord_c.id)])
logger.info("Sons of coord companies {}".format(coord_hierarchy_company_ids.mapped('name')))
# delete all existing role lines
u.write({'role_line_ids':[(5,0,0)]})
# add the internal role
u.write({'role_line_ids':[(0,0,{
"user_id": u.id,
"active": True,
"role_id": internal_user_role.id,})]})
# update the user company_ids
for c in coord_hierarchy_company_ids:
if c not in original_company_ids:
logger.info("Company added to user {}".format(c.name))
u.write({'company_ids':[(4,c.id)]})
# add the coord_admin role
for coord_c in coord_company_ids:
u.write({'role_line_ids':[(0,0,{
"company_id": coord_c.id,
"user_id": u.id,
"active": True,
"role_id": env.ref('energy_communities.role_coord_admin').id,})]})
# add the ce_manager role
for coord_son in coord_hierarchy_company_ids:
u.write({'role_line_ids':[(0,0,{
"company_id": coord_son.id,
"user_id": u.id,
"active": True,
"role_id": env.ref('energy_communities.role_ce_manager').id,})]})
# add role_line (member_ce) for the companies that are not under the coordinator ones
# TODO: it can be that a user originally has a role different than ce_member for those companies
# that are not under the Coordinator's hierarchy, but we here are simplifiying and assigning ce_member
full_hierarchy_company_ids = coord_hierarchy_company_ids | coord_company_ids
for not_coord_hie_c in original_company_ids - full_hierarchy_company_ids:
u.write({'role_line_ids':[(0,0,{
"company_id": not_coord_hie_c.id,
"user_id": u.id,
"active": True,
"role_id": env.ref('energy_communities.role_ce_member').id,})]})
# loop3 --> update the CE users (u)
for u in target_users:
role_to_use = env.ref('energy_communities.role_ce_member')
already_has_internal = False
# get the roles in role_lines that don't have any company_id mantained
roles_no_company = list(set([rl.role_id for rl in u.role_line_ids if (not rl.company_id and rl.active)]))
for r in roles_no_company:
if r == internal_user_role:
already_has_internal = True
if r in (
env.ref('energy_communities.role_ce_admin'),
env.ref('energy_communities.role_ce_manager'),
env.ref('energy_communities.role_coord_admin'),
env.ref('energy_communities.role_coord_worker'),
env.ref('energy_communities.role_platform_admin',)):
role_to_use = env.ref('energy_communities.role_ce_admin')
# review & clean all existing role_line_ids &
# get the role_lines that already have
companies_with_role_line = []
for rl_id in [l.id for l in u.role_line_ids]:
rl = env['res.users.role.line'].browse([rl_id])
if not rl.company_id:
if rl.role_id != internal_user_role:
u.write({'role_line_ids':[(3,rl_id)]})
else: #role_lines with company assigned
if not rl.active:
u.write({'role_line_ids':[(3,rl_id)]})
elif rl.company_id not in u.company_ids:
u.write({'role_line_ids':[(3,rl_id)]})
else:
companies_with_role_line.append(rl.company_id)
# add internal role if needed
if not already_has_internal:
u.write({'role_line_ids':[(0,0,{
"user_id": u.id,
"active": True,
"role_id": internal_user_role.id,})]})
# add remaining per company role_lines
for c in u.company_ids:
if c not in companies_with_role_line:
u.write({'role_line_ids':[(0,0,{
"company_id": c.id,
"user_id": u.id,
"active": True,
"role_id": role_to_use.id,})]})
......@@ -28,20 +28,94 @@
<field name="name">Energy Community Member</field>
<field name="code">role_ce_member</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_user')),
(4, ref('base.group_portal'))]"/>
]"/>
</record>
<record model="res.users.role" id="role_ce_admin">
<field name="name">Energy Community Administrator</field>
<field name="code">role_ce_admin</field>
<field name="implied_ids"
eval="[
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_admin')),
(4, ref('sale.group_delivery_invoice_address')),
(4, ref('account.group_account_invoice')),
(4, ref('account.group_account_manager')),
(4, ref('account.group_account_user')),
(4, ref('base.group_partner_manager')),
(4, ref('base.group_multi_company')),
(4, ref('base.group_allow_export')),
(4, ref('cooperator.cooperator_group_manager')),
(4, ref('sales_team.group_sale_manager')),
(4, ref('purchase.group_purchase_manager')),
(4, ref('account.group_account_manager')),
(4, ref('account_payment_order.group_account_payment')),
(4, ref('crm.group_use_lead')),
(4, ref('mass_mailing.group_mass_mailing_user')),
(4, ref('l10n_es_aeat.group_account_aeat')),
]"/>
</record>
<record model="res.users.role" id="role_ce_manager">
<field name="name">Energy Community Manager</field>
<field name="code">role_ce_manager</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_admin')),
(4, ref('sale.group_delivery_invoice_address')),
(4, ref('account.group_account_invoice')),
(4, ref('account.group_account_manager')),
(4, ref('account.group_account_user')),
(4, ref('base.group_partner_manager')),
(4, ref('base.group_multi_company')),
(4, ref('base.group_allow_export')),
(4, ref('cooperator.cooperator_group_manager')),
(4, ref('sales_team.group_sale_manager')),
(4, ref('purchase.group_purchase_manager')),
(4, ref('account.group_account_manager')),
(4, ref('account_payment_order.group_account_payment')),
(4, ref('crm.group_use_lead')),
(4, ref('mass_mailing.group_mass_mailing_user')),
(4, ref('l10n_es_aeat.group_account_aeat')),
]"/>
</record>
<record model="res.users.role" id="role_coord_admin">
<field name="name">Coordinator Admin</field>
<field name="code">role_coord_admin</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_admin')),
(4, ref('sale.group_delivery_invoice_address')),
(4, ref('account.group_account_invoice')),
(4, ref('account.group_account_manager')),
(4, ref('account.group_account_user')),
(4, ref('base.group_partner_manager')),
(4, ref('base.group_multi_company')),
(4, ref('base.group_allow_export')),
(4, ref('cooperator.cooperator_group_manager')),
(4, ref('sales_team.group_sale_manager')),
(4, ref('purchase.group_purchase_manager')),
(4, ref('account.group_account_manager')),
(4, ref('account_payment_order.group_account_payment')),
(4, ref('crm.group_use_lead')),
(4, ref('mass_mailing.group_mass_mailing_user')),
(4, ref('l10n_es_aeat.group_account_aeat')),
]"/>
</record>
<record model="res.users.role" id="role_coord_worker">
<field name="name">Coordinator Worker</field>
<field name="code">role_coord_worker</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_user')),
(4, ref('sale.group_delivery_invoice_address')),
(4, ref('account.group_account_invoice')),
(4, ref('account.group_account_manager')),
(4, ref('account.group_account_user')),
(4, ref('base.group_partner_manager')),
(4, ref('base.group_user')),
(4, ref('base.group_multi_company')),
(4, ref('base.group_allow_export')),
(4, ref('cooperator.cooperator_group_manager')),
......@@ -59,12 +133,15 @@
<field name="name">Platform admin role</field>
<field name="code">role_platform_admin</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('group_platform_manager')),
(4, ref('group_admin')),
(4, ref('base.group_erp_manager')),
(4, ref('sale.group_delivery_invoice_address')),
(4, ref('account.group_account_invoice')),
(4, ref('account.group_account_manager')),
(4, ref('account.group_account_user')),
(4, ref('base.group_partner_manager')),
(4, ref('base.group_user')),
(4, ref('base.group_system')),
(4, ref('base.group_multi_company')),
(4, ref('base.group_allow_export')),
......@@ -79,4 +156,13 @@
(4, ref('l10n_es_aeat.group_account_aeat'))
]"/>
</record>
<record model="res.users.role" id="role_internal_user">
<field name="name">Internal User</field>
<field name="code">role_internal_user</field>
<field name="implied_ids" eval="[
(5, 0, 0),
(4, ref('base.group_user')),
]"/>
</record>
</odoo>
......@@ -18,5 +18,6 @@
"security/res_groups.xml",
"security/ir.model.access.csv",
"security/ir_rule_data.xml",
"views/inscription_views.xml",
],
}
......@@ -24,7 +24,7 @@ class Project(models.Model):
# address fields
street = fields.Char(required=True)
street2 = fields.Char(required=True)
street2 = fields.Char()
zip = fields.Char(change_default=True, required=True)
city = fields.Char(required=True)
state_id = fields.Many2one(
......@@ -35,5 +35,6 @@ class Project(models.Model):
required=True,
)
country_id = fields.Many2one(
"res.country", string="Country", ondelete="restrict", required=True
"res.country", string="Country", ondelete="restrict", required=True,
default=lambda self: self.env.ref('base.es')
)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="inscription_view_tree" model="ir.ui.view">
<field name="name">Inscription List</field>
<field name="model">energy_project.inscription</field>
<field name="arch" type="xml">
<tree editable="bottom">
<field name="partner_id" options="{'no_create': True}" width="50%" />
<field name="effective_date" width="50%" />
</tree>
</field>
</record>
</data>
</odoo>
......@@ -18,6 +18,15 @@
/>
</record>
<record model="res.users.role" id="energy_communities.role_ce_manager">
<field
name="implied_ids"
eval="[
(4, ref('energy_project.group_admin')),
]"
/>
</record>
<record model="res.users.role" id="energy_communities.role_platform_admin">
<field
name="implied_ids"
......
......@@ -14,14 +14,22 @@ class Selfconsumption(models.Model):
for record in self:
record.distribution_table_count = len(record.distribution_table_ids)
def _compute_inscription_count(self):
for record in self:
record.inscription_count = len(record.inscription_ids)
project_id = fields.Many2one(
"energy_project.project", required=True, ondelete="cascade"
)
code = fields.Char(string="CAU")
power = fields.Float(string="Generation Power (kWh)")
cil = fields.Char(string="CIL", help="Production facility code for liquidation purposes")
owner_id = fields.Many2one("res.partner", string="Owner", required=True, default=lambda self: self.env.company.partner_id)
power = fields.Float(string="Generation Power (kW)")
distribution_table_ids = fields.One2many('energy_selfconsumption.distribution_table', 'selfconsumption_project_id',
readonly=True)
distribution_table_count = fields.Integer(compute=_compute_distribution_table_count)
distribution_table_count = fields.Integer(compute=_compute_distribution_table_count)
inscription_ids = fields.One2many('energy_project.inscription', 'project_id', readonly=True)
inscription_count = fields.Integer(compute=_compute_inscription_count)
def get_distribution_tables(self):
self.ensure_one()
......@@ -33,6 +41,17 @@ class Selfconsumption(models.Model):
'domain': [('selfconsumption_project_id', '=', self.id)],
'context': {'create': True, 'default_selfconsumption_project_id': self.id},
}
def get_inscriptions(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': 'Inscriptions',
'view_mode': 'tree,form',
'res_model': 'energy_project.inscription',
'domain': [('project_id', '=', self.id)],
'context': {'create': True, 'default_project_id': self.id},
}
def set_activation(self):
for record in self:
......@@ -42,6 +61,8 @@ class Selfconsumption(models.Model):
for record in self:
if not record.code:
raise ValidationError(_("Project must have a valid Code."))
if not record.cil:
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')]):
......
......@@ -21,6 +21,9 @@ class SupplyPointAssignation(models.Model):
'supply_point_id.id'))])
distribution_table_id = fields.Many2one('energy_selfconsumption.distribution_table', required=True)
selfconsumption_project_id = fields.Many2one(related='distribution_table_id.selfconsumption_project_id')
distribution_table_state = fields.Selection(related='distribution_table_id.state')
distribution_table_create_date = fields.Datetime(related='distribution_table_id.create_date')
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")
......
......@@ -29,6 +29,11 @@
</header>
<sheet>
<div class="oe_button_box" name="button_box">
<button class="oe_stat_button" type="object" name="get_inscriptions"
icon="fa-pencil-square-o" context="{'default_effective_date': today}">
<field string="Inscriptions" name="inscription_count" widget="statinfo"/>
</button>
<button class="oe_stat_button" type="object" name="get_distribution_tables"
icon="fa-table" context="{'default_owner_id': id}"
attrs="{'invisible': [('state', '==', 'draft')]}">
......@@ -61,10 +66,18 @@
name="code"
attrs="{'readonly': [('state', 'not in', ['draft', 'activation'])]}"
/>
<field
name="cil"
attrs="{'readonly': [('state', 'not in', ['draft', 'activation'])]}"
/>
<field
name="power"
attrs="{'readonly': [('state', 'not in', ['draft', 'activation'])]}"
/>
<field
name="owner_id"
attrs="{'readonly': [('state', 'not in', ['draft', 'activation'])]}"
/>
</group>
<group>
<span class="o_form_label o_td_label" name="address_name">
......@@ -89,22 +102,6 @@
</div>
</group>
</group>
<group>
<field
name="inscription_ids"
widget="one2many"
mode="list"
context="{'default_project_id':id, 'default_effective_date': today}"
>
<tree editable="bottom">
<field
name="partner_id"
options="{'no_create': True}"
/>
<field name="effective_date"/>
</tree>
</field>
</group>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
......@@ -121,6 +118,12 @@
<field name="arch" type="xml">
<tree string="Self-Consumption Projects">
<field name="name"/>
<field name="street"/>
<field name="street2"/>
<field name="state_id"/>
<field name="zip"/>
<field name="country_id"/>
<field name="power"/>
<field name="state"/>
</tree>
</field>
......
......@@ -36,6 +36,18 @@
</div>
</group>
</group>
<notebook>
<page string="Self-consumption Projects" name="selfconsumption_project" autofocus="autofocus">
<field name="supply_point_assignation_ids">
<tree default_order="distribution_table_create_date desc">
<field name="selfconsumption_project_id" string="Name"/>
<field name="distribution_table_state"/>
<field name="distribution_table_create_date"/>
</tree>
</field>
</page>
</notebook>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
......
......@@ -6,7 +6,7 @@ setuptools.setup(
"depends_override": {
"account_lock_date_update": "odoo14-addon-account-lock-date-update==14.0.2.0.1.dev10",
"account_reconciliation_widget": "odoo14-addon-account-reconciliation-widget==14.0.2.0.2",
"community_maps": "odoo14-addon-community-maps==14.0.0.1.12",
"community_maps": "odoo14-addon-community-maps==14.0.0.1.13",
"cooperator_account_payment": "odoo14-addon-cooperator-account-payment==14.0.1.0.2",
"cooperator_account_banking_mandate": "odoo14-addon-cooperator-account-banking-mandate==14.0.1.0.5",
"crm_metadata": "odoo14-addon-crm-metadata==14.0.1.0.0",
......