Skip to content

Commit

Permalink
Merge branch '14.0-imp-cooperator_multi_company_subscription_journal'…
Browse files Browse the repository at this point in the history
… into 14.0-imp-cooperator_multi_company
  • Loading branch information
huguesdk committed Jul 14, 2023
2 parents a38973b + f5388d0 commit ac8e174
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 59 deletions.
10 changes: 4 additions & 6 deletions cooperator/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
<field name="name">Company Share</field>
</record>

<record id="subscription_journal" model="account.journal">
<field name="name">Subscription Journal</field>
<field name="code">SUBJ</field>
<field name="type">sale</field>
</record>

<function model="res.company" name="_create_cooperator_sequences">
<value model="res.company" search="[]" />
</function>

<function model="res.company" name="_init_cooperator_data">
<value model="res.company" search="[]" />
</function>
</data>
</odoo>
25 changes: 8 additions & 17 deletions cooperator/demo/coop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,14 @@
<field name="country_id" ref="base.be" />
</record>

<record id="account_cooperator_demo" model="account.account">
<field name="code">416101</field>
<field name="name">Cooperators</field>
<field name="user_type_id" ref="account.data_account_type_receivable" />
<field name="reconcile" eval="True" />
</record>

<record id="account_equity_demo" model="account.account">
<field name="name">Equity</field>
<field name="code">100910</field>
<field eval="True" name="reconcile" />
<field name="user_type_id" ref="account.data_account_type_equity" />
</record>

<record id="base.main_company" model="res.company">
<field name="property_cooperator_account" ref="account_cooperator_demo" />
</record>
<!--
this is here only to support the case where a database is initialized
without demo data, and demo data is loaded later on (because this
function is also called indirectly from the main xml data file).
-->
<function model="res.company" name="_init_cooperator_demo_data">
<value model="res.company" search="[]" />
</function>

<record id="product_template_share_type_1_demo" model="product.template">
<field name="name">Part A - Founder</field>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2023 Coop IT Easy SC
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

_logger = logging.getLogger(__name__)


def migrate(cr, version):
# the subscription journal is now set on the company instead of using the
# global cooperator.subscription_journal xml id.
cr.execute(
"""
select id, res_id
from ir_model_data
where
module = 'cooperator' and
name = 'subscription_journal'
"""
)
row = cr.fetchone()
xml_id = row[0]
journal_id = row[1]
cr.execute(
"""
update res_company as rc
set subscription_journal_id = aj.id
from account_journal as aj
where
rc.subscription_journal_id is null and
aj.company_id = rc.id and
(aj.id = %s or aj.code = 'SUBJ')
""",
(journal_id,),
)
cr.execute(
"""
select id
from res_company
where subscription_journal_id is null
order by id
"""
)
company_rows = cr.fetchall()
company_ids = [row[0] for row in company_rows]
if company_ids:
_logger.warning(
"Could not find the subscription journal for companies with ids: "
"{company_ids}. Please check and set it manually if needed.".format(
company_ids=company_ids
)
)
cr.execute(
"""
delete
from ir_model_data
where id = %s
""",
(xml_id,),
)
1 change: 1 addition & 0 deletions cooperator/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from . import company
from . import account_journal
from . import mail_template
from . import account_chart_template
12 changes: 12 additions & 0 deletions cooperator/models/account_chart_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2023 Coop IT Easy SC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import models


class AccountChartTemplate(models.Model):
_inherit = "account.chart.template"

def _load(self, sale_tax_rate, purchase_tax_rate, company):
super()._load(sale_tax_rate, purchase_tax_rate, company)
company._init_cooperator_data()
61 changes: 61 additions & 0 deletions cooperator/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def _compute_base_logo(self):
" receivable account for the"
" cooperators",
)
subscription_journal_id = fields.Many2one(
"account.journal",
"Subscription Journal",
readonly=True,
)
unmix_share_type = fields.Boolean(
string="Unmix share type",
default=True,
Expand Down Expand Up @@ -278,3 +283,59 @@ def _create_cooperator_sequences(self):
"company_id": company.id,
}
)

def _accounting_data_initialized(self):
return self.chart_template_id or self.env[
"account.chart.template"
].existing_accounting(self)

def _init_cooperator_data(self):
"""
Generate default cooperator data for the company.
"""
# this method exists to initialize data correctly for the following
# possible cases:
# 1. when a new company is created. in this case it will be called
# when the account chart template is loaded.
# 2. when a database is initialized with the cooperator module but no
# l10n module, and the l10n_generic_coa module is loaded after the
# cooperator module by the post_init_hook of the account module. in
# this case it is first called by the xml data of this module
# (cooperator) (but with no effect, as explained below) and then
# again when the account chart template is loaded.
# 3. when the cooperator module is installed on an existing database.
# in this case it is called by the xml data of this module.
subscription_request_model = self.env["subscription.request"]
for company in self:
if not company._accounting_data_initialized():
# if no account chart template has been loaded yet and no
# accounting data exists, then no accounting data should be
# created yet because all existing journals and accounts will
# be deleted when the account chart template will be loaded.
# this method will be called again after the account chart
# template has been loaded.
continue
subscription_request_model.create_journal(company)
# this is called here to support the first 2 cases explained above.
self._init_cooperator_demo_data()

def _init_cooperator_demo_data(self):
if not self.env["ir.module.module"].search([("name", "=", "cooperator")]).demo:
# demo data must not be loaded, nothing to do
return
account_account_model = self.env["account.account"]
receivable_account_type = self.env.ref("account.data_account_type_receivable")
for company in self:
if not company._accounting_data_initialized():
# same remark as in _init_cooperator_data()
continue
if not company.property_cooperator_account:
company.property_cooperator_account = account_account_model.create(
{
"code": "416101",
"name": "Cooperators",
"user_type_id": receivable_account_type.id,
"reconcile": True,
"company_id": company.id,
}
)
14 changes: 13 additions & 1 deletion cooperator/models/subscription_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,20 @@ def _prepare_invoice_line(self, product, partner, qty):
}
return res

@api.model
def create_journal(self, company):
if not company.subscription_journal_id:
company.subscription_journal_id = self.env["account.journal"].create(
{
"name": _("Subscription Journal"),
"code": _("SUBJ"),
"type": "sale",
"company_id": company.id,
}
)

def get_journal(self):
return self.env.ref("cooperator.subscription_journal")
return self.company_id.subscription_journal_id

def get_accounting_account(self):
account = self.company_id.property_cooperator_account
Expand Down
1 change: 1 addition & 0 deletions cooperator/readme/newsfragments/75.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create subscription journal per company.
46 changes: 11 additions & 35 deletions cooperator/tests/cooperator_test_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,9 @@ class CooperatorTestMixin:
@classmethod
def set_up_cooperator_test_data(cls):
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# accounting data needs to be created even if created in module data
# because when launching tests, accounting data will be deleted when odoo loads
# a test chart of account.
# cf _load in chart_template.py in account module

account_model = cls.env["account.account"]
cls.company = cls.env.user.company_id
cls.company = cls.env.company
cls.company.coop_email_contact = "coop_email@example.org"
cls.demo_partner = cls.env.ref("base.partner_demo")

receivable_account_type = cls.env.ref("account.data_account_type_receivable")
equity_account_type = cls.env.ref("account.data_account_type_equity")
cooperator_account = account_model.create(
{
"name": "Cooperator Test",
"code": "416109",
"user_type_id": receivable_account_type.id,
"reconcile": True,
}
)
cls.company.property_cooperator_account = cooperator_account
cls.equity_account = account_model.create(
{
"name": "Equity Test ",
"code": "100919",
"user_type_id": equity_account_type.id,
"reconcile": True,
}
)
cls.subscription_journal = cls.env["account.journal"].create(
{
"name": "Subscriptions Test",
"code": "SUBJT",
"type": "sale",
}
)

cls.share_x = cls.env["product.product"].create(
{
"name": "Share X - Founder",
Expand Down Expand Up @@ -208,3 +174,13 @@ def create_dummy_company_cooperator(self):
)
self.validate_subscription_request_and_pay(subscription_request)
return partner

def create_company(self, name):
company = self.env["res.company"].create(
{
"name": name,
}
)
# apply the same account chart template as the main company
self.company.chart_template_id.try_loading(company)
return company
33 changes: 33 additions & 0 deletions cooperator/tests/test_cooperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,36 @@ def test_create_subscription_with_matching_none_company_register_number(self):
subscription_request = self.env["subscription.request"].create(vals)
partner = subscription_request.partner_id
self.assertNotEqual(partner, company_partner)

def test_subscription_journal_per_company(self):
"""
Test that creating a new company creates a new subscription journal
for it.
"""
company_1 = self.company
company_2 = self.create_company("company 2")
self.assertTrue(company_2.subscription_journal_id)
self.assertEqual(company_2.subscription_journal_id.company_id, company_2)
self.assertNotEqual(
company_1.subscription_journal_id, company_2.subscription_journal_id
)

def test_subscription_request_journal_per_company(self):
company_1 = self.company
company_2 = self.create_company("company 2")
subscription_request_1 = self.env["subscription.request"].create(
self.get_dummy_subscription_requests_vals()
)
subscription_request_2 = (
self.env["subscription.request"]
.with_company(company_2)
.create(self.get_dummy_subscription_requests_vals())
)
self.assertEqual(subscription_request_1.company_id, company_1)
self.assertEqual(subscription_request_2.company_id, company_2)
self.assertEqual(
subscription_request_1.get_journal(), company_1.subscription_journal_id
)
self.assertEqual(
subscription_request_2.get_journal(), company_2.subscription_journal_id
)

0 comments on commit ac8e174

Please sign in to comment.