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 456 additions and 108 deletions
......@@ -216,7 +216,6 @@ S_LANDING_PAGE_CREATE = {
"type": "list",
"schema": {"type": "dict", "schema": S_COMMUNITY_SERVICE},
},
"group_image_link": {"type": "string"},
"primary_image_file": {"type": "string"},
"primary_image_file_write_date": {"type": "string"},
"secondary_image_file": {"type": "string"},
......@@ -225,7 +224,6 @@ S_LANDING_PAGE_CREATE = {
"long_description": {"type": "string"},
"why_become_cooperator": {"type": "string"},
"become_cooperator_process": {"type": "string"},
"map_geolocation": {"type": "string"},
"map_reference": {"type": "string"},
"street": {"type": "string"},
"postal_code": {"type": "string"},
......
from . import test_res_company
from . import test_res_users
from . import test_assign_admin_wizard
from . import test_crm_lead
from . import test_multicompany_easy_creation
......@@ -31,7 +31,6 @@ class TestAssingAdminWizard(UserSetupMixin, common.TransactionCase):
"vat": "43217763g",
}
)
wizard.process_data()
add_energy_community_role_mocked.assert_called_once()
......
from datetime import datetime
from faker import Faker
from mock import patch
from odoo.tests import common
from odoo.exceptions import ValidationError
from .helpers import UserSetupMixin, CompanySetupMixin
faker = Faker(locale='es_ES')
class TestCRMLead(UserSetupMixin, CompanySetupMixin, common.TransactionCase):
def create_crm_lead(self):
return self.env["crm.lead"].create({
"name": "CE Name",
"email_from": faker.email(),
"submission_type": "place_submission",
"company_id": self.coordination.id,
"source_id": self.env.ref("energy_communities.ce_source_creation_ce_proposal").id,
"metadata_line_ids": [
(0, 0, {"key": "current_lang", "value": "en_US"}),
(0, 0, {"key": "ce_creation_date", "value": "1994-09-01"}),
(0, 0, {"key": "ce_address", "value": "Av St Narcís"}),
(0, 0, {"key": "ce_city", "value": "Girona"}),
(0, 0, {"key": "ce_zip", "value": "17005"}),
(0, 0, {"key": "contact_phone", "value": "666666666"}),
(0, 0, {"key": "email_from", "value": "random@somcomunitats.coop"}),
(0, 0, {"key": "ce_vat", "value": "38948723V"}),
]
})
def setUp(self):
super().setUp()
self.users_model = self.env['res.users']
self.company_model = self.env['res.company']
self.role_line_model = self.env["res.users.role.line"]
self.instance = self.company_model.search([
('hierarchy_level', '=', 'instance')
])[0]
self.coordination = self.create_company(
'Coordinator', 'coordinator', self.instance.id,
)
self.coord_admin = self.create_user("Coord", "Admin")
self.make_coord_user(self.coordination, self.coord_admin)
self.crm_lead = self.create_crm_lead()
def test__get_default_community_wizard__base_case(self):
result = self.crm_lead._get_default_community_wizard()
lang = self.env["res.lang"].search([("code", "=", "en_US")])
foundation_date = datetime.strptime('1994-09-01', '%Y-%m-%d')
users = self.crm_lead.company_id.get_users()
self.assertEquals(result, {
'name': self.crm_lead.name,
'city': 'Girona',
'default_lang_id': lang.id,
'email': 'random@somcomunitats.coop',
'foundation_date': foundation_date,
'hierarchy_level': 'community',
'parent_id': self.coordination.id,
'crm_lead_id': self.crm_lead.id,
'phone': '666666666',
'street': 'Av St Narcís',
'vat': '38948723V',
'zip_code': '17005',
'user_ids': [user.id for user in users],
'chart_template_id': self.env["account.chart.template"].search([("name", "=", "PGCE PYMEs 2008")])[0].id,
'update_default_taxes': True,
'default_sale_tax_id': self.env["account.tax.template"].search([("name", "=", "IVA 21% (Servicios)")])[0].id,
'default_purchase_tax_id': self.env["account.tax.template"].search([("name", "=", "21% IVA soportado (bienes corrientes)")])[0].id,
'property_cooperator_account': self.env.ref("l10n_es.account_common_4400").id,
'create_user': False,
})
from faker import Faker
from mock import patch
from odoo.tests import common
from odoo.exceptions import ValidationError
from .helpers import UserSetupMixin, CompanySetupMixin
faker = Faker(locale='es_ES')
class TestMultiEasyCreation(UserSetupMixin, CompanySetupMixin, common.TransactionCase):
def create_crm_lead(self):
return self.env["crm.lead"].create({
"name": "CE Name",
"email_from": faker.email(),
"submission_type": "place_submission",
"company_id": self.coordination.id,
"source_id": self.env.ref("energy_communities.ce_source_creation_ce_proposal").id,
"metadata_line_ids": [
(0, 0, {"key": "current_lang", "value": "ca"}),
(0, 0, {"key": "ce_creation_date", "value": "1994-09-01"}),
(0, 0, {"key": "ce_address", "value": "Av St Narcís"}),
(0, 0, {"key": "ce_city", "value": "Girona"}),
(0, 0, {"key": "ce_zip", "value": "17005"}),
(0, 0, {"key": "contact_phone", "value": "666666666"}),
(0, 0, {"key": "email_from", "value": "random@somcomunitats.coop"}),
(0, 0, {"key": "ce_vat", "value": "38948723V"}),
]
})
def setUp(self):
super().setUp()
self.users_model = self.env['res.users']
self.company_model = self.env['res.company']
self.role_line_model = self.env["res.users.role.line"]
self.instance = self.company_model.search([
('hierarchy_level', '=', 'instance')
])[0]
self.coordination = self.create_company(
'Coordinator', 'coordinator', self.instance.id,
)
self.coord_admin = self.create_user("Coord", "Admin")
self.make_coord_user(self.coordination, self.coord_admin)
self.crm_lead = self.create_crm_lead()
self.wizard = self.env["account.multicompany.easy.creation.wiz"].create(
self.crm_lead.get_default_community_wizard()
)
def test__action_accept__company_created(self):
self.wizard.action_accept()
companies = self.env["res.company"].search([("parent_id", "=", self.coordination.id)])
self.assertEquals(len(companies), 1)
company = companies[0]
self.assertEquals(company.hierarchy_level, "community")
@patch("odoo.addons.energy_communities.models.res_users.ResUsers.make_ce_user")
def test__add_company_managers(self, make_ce_user_mocked):
new_company = self.create_company(
"Community", "community", self.coordination.id
)
self.wizard.new_company_id = new_company
self.wizard.add_company_managers()
make_ce_user_mocked.assert_called_with(new_company, "role_ce_manager")
def test__add_company_log(self):
new_company = self.create_company(
"Community", "community", self.coordination.id
)
self.wizard.new_company_id = new_company
self.wizard.add_company_log()
self.assertTrue(new_company.message_ids)
expected_msg = '<p>Community created from: <a href="/web#id={}&amp;view_type=form&amp;model=crm.lead&amp;menu_id={}">{}</a></p>'.format(
self.crm_lead.id, self.env.ref("crm.crm_menu_root").id, self.crm_lead.name
)
self.assertEquals(expected_msg, new_company.message_ids[0].body)
from odoo.exceptions import ValidationError
from odoo.tests import common
from .helpers import CompanySetupMixin, UserSetupMixin
class TestResCompany(common.SingleTransactionCase):
class TestResCompany(CompanySetupMixin, UserSetupMixin, common.TransactionCase):
def test_hierarchy_level_company_instance(self):
company_instance = self.env["res.company"].search(
[("hierarchy_level", "=", "instance")]
......@@ -95,3 +97,52 @@ class TestResCompany(common.SingleTransactionCase):
"parent_id": company_community.id,
}
)
def test__get_users__without_roles(self):
# Given a coord company and coord admin
company_instance = self.env["res.company"].search(
[("hierarchy_level", "=", "instance")]
)
company_coordinator = self.create_company(
"Som", "coordinator", company_instance.id
)
user = self.create_user("Tom", "Bombadil")
self.make_coord_user(company_coordinator, user)
# When we want all users of company_coordinator
community_users = company_coordinator.get_users()
# Then returns all community users
platform_admins = (
self.env["res.users.role.line"]
.search(
[
(
"role_id",
"=",
self.env.ref("energy_communities.role_platform_admin").id,
),
]
)
.user_id
)
self.assertEqual(community_users, user + platform_admins)
def test__get_users__with_roles(self):
# Given a coord company and coord admin
company_instance = self.env["res.company"].search(
[("hierarchy_level", "=", "instance")]
)
company_coordinator = self.create_company(
"Som", "coordinator", company_instance.id
)
first_user = self.create_user("Tom", "Bombadil")
second_user = self.create_user("Frodo", "Baggins")
self.make_coord_user(company_coordinator, first_user)
self.make_coord_user(company_coordinator, second_user, "role_coord_worker")
# When we want all users of company_coordinator
community_users = company_coordinator.get_users(["role_coord_worker"])
# Then returns all community users
self.assertEqual(community_users, second_user)
# Add faker into requirements.txt ?? 🤔
from unittest.mock import patch
from faker import Faker
......@@ -90,7 +89,6 @@ class TestResUsers(CompanySetupMixin, UserSetupMixin, common.TransactionCase):
def test__make_ce_user__already_user(self):
self.community_admin.make_ce_user(self.community.id, "role_ce_member")
rl = self.role_line_model.search(
[
("user_id", "=", self.community_admin.id),
......@@ -103,7 +101,6 @@ class TestResUsers(CompanySetupMixin, UserSetupMixin, common.TransactionCase):
def test__make_ce_user__new_user(self):
self.random_user.make_ce_user(self.community.id, "role_ce_member")
rl = self.role_line_model.search(
[
("user_id", "=", self.random_user.id),
......
......@@ -14,7 +14,6 @@
<field name="search_view_id" ref="crm.view_crm_case_opportunities_filter" />
</record>
-->
<record id="ce_view_crm_case_opportunities_filter" model="ir.ui.view">
<field name="name">ce.view.crm.opportunities.leads.filter</field>
<field name="model">crm.lead</field>
......@@ -79,14 +78,25 @@
<field name="inherit_id" ref="crm.crm_lead_view_form" />
<field name="arch" type="xml">
<xpath expr="//sheet" position="before">
<header>
<header>
<field name="source_id" invisible="1" />
<field name="finished" invisible="1" />
<field name="company_hierarchy_level" invisible="1" />
<field name="can_be_assigned_to_coordinator" invisible="1" />
<field name="is_instance_company" invisible="1" />
<button
name="action_assign_crm_to_coordinator_company"
type="object"
string="Assing CRM to coordinator company"
groups="energy_communities.group_platform_manager"
attrs="{'invisible': [('is_instance_company', '=', False)]}"
attrs="{'invisible':[('can_be_assigned_to_coordinator', '!=', True)]}"
/>
<button
name="action_create_community"
type="object"
string="Create community"
groups="energy_communities.group_platform_manager"
attrs="{'invisible':['|',('source_id', '!=', %(ce_source_creation_ce_proposal)d),('probability','=',100)]}"
/>
</header>
</xpath>
......@@ -161,16 +171,6 @@
</field>
</record>
<record id="action_create_map_place_from_lead" model="ir.actions.server">
<field name="name">Create Map Place</field>
<field name="model_id" ref="crm.model_crm_lead" />
<field name="binding_model_id" ref="crm.model_crm_lead" />
<field name="state">code</field>
<field
name="code"
> for rec in records: rec._create_map_place_proposal() </field>
</record>
<record id="action_build_community_company_from_lead" model="ir.actions.server">
<field name="name">Build Community Company</field>
<field name="model_id" ref="crm.model_crm_lead" />
......@@ -181,5 +181,4 @@
> for rec in records: rec._build_community_company() </field>
</record>
</odoo>
<?xml version="1.0" ?>
<odoo>
<record id="landing_page_form_view" model="ir.ui.view">
<field name="name">landing.page.form.view</field>
<field name="model">landing.page</field>
<field name="arch" type="xml">
<form create="false">
<header>
<button
<record id="landing_page_form_view" model="ir.ui.view">
<field name="name">landing.page.form.view</field>
<field name="model">landing.page</field>
<field name="arch" type="xml">
<form create="false">
<header>
<button
name="action_landing_page_status"
type="object"
string="Publish"
attrs="{'invisible': [('status','!=','draft')]}"
/>
<button
<button
name="action_landing_page_status"
type="object"
string="Unpublish"
attrs="{'invisible': [('status','!=','publish')]}"
/>
<button
name="action_update_wp"
name="action_create_landing_place"
type="object"
string="Create map place"
attrs="{'invisible': [('map_place_id','!=',False)]}"
/>
<button
name="action_update_public_data"
type="object"
string="Update wordpress data"
string="Update (wp landing / map place)"
/>
<field name="status" widget="statusbar" />
</header>
<group style="width:40px;">
<h4
<field name="status" widget="statusbar" />
</header>
<group style="width:40px;">
<h4
style="color:green;"
attrs="{'invisible': [('status','!=','publish')]}"
>Published</h4>
<h4
<h4
style="color:orange;"
attrs="{'invisible': [('status','!=','draft')]}"
>Draft</h4>
</group>
<group>
<field name="wp_lastupdate_datetime" readonly="True" />
<field name="wp_landing_page_id" readonly="True" />
<field name="name" />
<field name="allow_new_members" />
<field name="number_of_members" />
<field name="community_type" />
<field name="community_secondary_type" />
<field name="community_status" />
</group>
<group>
<field name="external_website_link" />
<field name="twitter_link" />
<field name="instagram_link" />
<field name="telegram_link" />
<field name="group_image_link" />
</group>
<group col="4">
<field name="primary_image_file" widget="image" />
<field name="secondary_image_file" widget="image" />
</group>
<group>
<field name="short_description" />
<field name="long_description" />
<field name="why_become_cooperator" widget="html" />
<field name="become_cooperator_process" widget="html" />
</group>
<group>
<field name="map_geolocation" />
<field name="map_place_id" />
<field name="street" />
<field name="postal_code" />
<field name="city" />
</group>
<group>
<field name="community_active_services" />
</group>
</form>
</field>
</record>
</group>
<group>
<field name="publicdata_lastupdate_datetime" readonly="True" />
<field name="wp_landing_page_id" readonly="True" />
<field name="name" />
<field name="allow_new_members" />
<field name="number_of_members" />
<field name="community_type" />
<field name="community_secondary_type" />
<field name="community_status" />
</group>
<group>
<field name="external_website_link" />
<field name="twitter_link" />
<field name="instagram_link" />
<field name="telegram_link" />
</group>
<group col="4">
<field name="primary_image_file" widget="image" />
<field name="secondary_image_file" widget="image" />
</group>
<group>
<field name="short_description" />
<field name="long_description" />
<field name="why_become_cooperator" widget="html" />
<field name="become_cooperator_process" widget="html" />
</group>
<group>
<field name="lat" />
<field name="lng" />
<field name="map_place_id" />
<field name="street" />
<field name="postal_code" />
<field name="city" />
</group>
<group>
<field name="community_active_services" />
</group>
</form>
</field>
</record>
</odoo>
......@@ -33,6 +33,7 @@
attrs="{'invisible': [('landing_page_id','=',False)]}"
/>
</div>
<field name="admins" invisible="True" />
</xpath>
<xpath expr="//notebook" position="inside">
<page string="Energy Communities">
......@@ -43,8 +44,12 @@
name="parent_id"
domain="[('id', 'in', parent_id_filtered_ids)]"
/>
<field name="legal_form" />
<field name="legal_name" />
<field name="ce_status" />
<field name="voluntary_share_id" />
<field name="ce_tag_ids" />
<field name="admins" />
</group>
</page>
</xpath>
......@@ -65,6 +70,13 @@
<xpath expr="//field[@name='create_user']" position="after">
<field name="create_user_in_keycloak" />
</xpath>
<xpath expr="//form" position="inside">
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />
<field name="activity_ids" widget="mail_activity" />
<field name="message_ids" widget="mail_thread" />
</div>
</xpath>
</field>
</record>
<record id="view_company_tree" model="ir.ui.view">
......
......@@ -30,7 +30,7 @@ class AssignAdminWizard(models.TransientModel):
@api.model
def _get_available_roles(self):
company = self.env["res.company"].browse(self.env.company.id)
company = self.env.company
if company.hierarchy_level == "community":
return [
("role_ce_admin", _("Energy Community Administrator")),
......@@ -41,6 +41,13 @@ class AssignAdminWizard(models.TransientModel):
("role_coord_admin", _("Coordinator Admin")),
("role_coord_worker", _("Coordinator Worker")),
]
elif company.hierarchy_level == "instance":
return [
("role_coord_admin", _("Coordinator Admin")),
("role_coord_worker", _("Coordinator Worker")),
("role_ce_admin", _("Energy Community Administrator")),
("role_ce_member", _("Energy Community Member")),
]
return []
def process_data(self):
......@@ -67,5 +74,4 @@ class AssignAdminWizard(models.TransientModel):
)
else:
user.add_energy_community_role(company_id, self.role)
return True
......@@ -2,6 +2,8 @@ import logging
from odoo import _, api, fields, models
from ..models.res_company import _CE_STATUS_VALUES, _LEGAL_FROM_VALUES
_logger = logging.getLogger(__name__)
......@@ -25,10 +27,6 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
property_cooperator_account = fields.Many2one(
comodel_name="account.account",
string="Cooperator Account",
domain=[
("internal_type", "=", "receivable"),
("deprecated", "=", False),
],
help="This account will be"
" the default one as the"
" receivable account for the"
......@@ -53,6 +51,75 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
readonly=True,
)
default_lang_id = fields.Many2one(
comodel_name="res.lang",
string="Language",
)
street = fields.Char(
string="Address",
)
city = fields.Char(
string="City",
)
zip_code = fields.Char(
string="ZIP code",
)
foundation_date = fields.Date(string="Foundation date")
vat = fields.Char(
string="VAT",
)
email = fields.Char(
string="Email",
)
phone = fields.Char(
string="Phone",
)
website = fields.Char(
string="Website",
)
state_id = fields.Many2one(
comodel_name="res.country.state",
string="State",
)
legal_form = fields.Selection(
selection=_LEGAL_FROM_VALUES,
string="Legal form",
)
legal_name = fields.Char(string="Legal name")
ce_status = fields.Selection(
selection=_CE_STATUS_VALUES,
string="Energy Community state",
)
def add_company_managers(self):
coord_members = self.parent_id.get_users(
["role_coord_admin", "role_coord_worker"]
)
for manager in coord_members:
manager.make_ce_user(self.new_company_id, "role_ce_manager")
def add_company_log(self):
message = _(
"Community created from: <a href='/web#id={id}&view_type=form&model=crm.lead&menu_id={menu_id}'>{name}</a>"
)
self_new_company = self.with_company(self.new_company_id)
self_new_company.new_company_id.message_post(
body=message.format(
id=self.crm_lead_id.id,
menu_id=self.env.ref("crm.crm_menu_root").id,
name=self.crm_lead_id.name,
)
)
def update_product_category_company_share(self):
new_company_id = self.new_company_id.id
self_new_company = self.with_company(new_company_id)
......@@ -98,24 +165,28 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
product_category_company_share.write(values)
def create_capital_share_product_template(self):
new_company_id = self.new_company_id.id
self_new_company = self.with_company(new_company_id)
# We use sudo to be able to copy the product and not needing to be in the main company
taxes_id = self.env.ref(
"l10n_es.{}_account_tax_template_s_iva_ns".format(self.new_company_id.id)
)
self.new_product_share_template = self.sudo().product_share_template.copy(
{
"name": self.product_share_template.name,
"company_id": self.new_company_id.id,
"list_price": self.capital_share,
"active": True,
"taxes_id": taxes_id,
}
)
self.new_company_id.initial_subscription_share_amount = self.capital_share
def update_values_from_crm_lead(self):
if self.crm_lead_id:
vals = self.crm_lead_id._get_company_create_vals()
self.new_company_id.write(vals)
self_new_company.new_company_id.initial_subscription_share_amount = (
self.capital_share
)
def set_cooperative_account(self):
new_company = self.new_company_id
self_new_company = self.with_company(self.new_company_id)
new_company = self_new_company.new_company_id
new_company.write(
{
"property_cooperator_account": self.match_account(
......@@ -140,22 +211,54 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
)
def action_accept(self):
action = super().action_accept()
self.update_values_from_crm_lead()
super().action_accept()
self.with_delay()._after_action_accept_hook()
self.crm_lead_id.action_set_won_rainbowman()
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"type": "success",
"title": _("Company creation successful"),
"message": _("The new community has been correctly created"),
"sticky": False,
"next": {"type": "ir.actions.act_window_close"},
},
}
def _after_action_accept_hook(self):
if self.property_cooperator_account:
self.set_cooperative_account()
self.new_company_id.create_user = self.create_user
self_new_company = self.with_company(self.new_company_id)
self_new_company.new_company_id.create_user = self.create_user
self.update_product_category_company_share()
self.create_capital_share_product_template()
return action
self.add_company_managers()
self.add_company_log()
def create_company(self):
self.new_company_id = self.env["res.company"].create(
{
"name": self.name,
"user_ids": [(6, 0, self.user_ids.ids)],
"parent_id": self.parent_id.id,
}
self.new_company_id = (
self.env["res.company"]
.sudo()
.create(
{
"name": self.name,
"user_ids": [(6, 0, self.user_ids.ids)],
"parent_id": self.parent_id.id,
"street": self.street,
"website": self.website,
"email": self.email,
"foundation_date": self.foundation_date,
"vat": self.vat,
"city": self.city,
"state_id": self.state_id,
"legal_form": self.legal_form,
"legal_name": self.legal_name,
"ce_status": self.ce_status,
"phone": self.phone,
"default_lang_id": self.default_lang_id.id,
}
)
)
allowed_company_ids = (
self.env.context.get("allowed_company_ids", []) + self.new_company_id.ids
......@@ -165,6 +268,6 @@ class AccountMulticompanyEasyCreationWiz(models.TransientModel):
)
self.with_context(
allowed_company_ids=allowed_company_ids
).chart_template_id.try_loading(company=new_company)
).sudo().chart_template_id.try_loading(company=new_company)
self.create_bank_journals()
self.create_sequences()
......@@ -23,12 +23,22 @@
</page>
</xpath>
<xpath expr="//page" position="before">
<page string="Energy Communities">
<group string="Energy Communities">
<field name="parent_id" />
</group>
<group string="Create from CRM Lead">
<field name="crm_lead_id" />
<page string="Energy Community">
<group string="Energy Community">
<field name="parent_id" required="1" />
<field name="vat" required="1" />
<field name="state_id" required="1" />
<field name="city" required="1" />
<field name="street" required="1" />
<field name="zip_code" required="1" />
<field name="foundation_date" required="1" />
<field name="email" required="1" />
<field name="legal_form" required="1" />
<field name="legal_name" required="1" />
<field name="ce_status" />
<field name="phone" required="1" />
<field name="default_lang_id" required="1" />
<field name="website" />
</group>
</page>
</xpath>
......