Skip to content

Commit

Permalink
[MIG] product_category_global_account_setting: Migration to 16.0 (fro…
Browse files Browse the repository at this point in the history
…m 12.0)

- Remove chart of account test. Wait for real framework in V18
- adapt view
- improve tests
  • Loading branch information
legalsylvain committed Dec 16, 2024
1 parent 39e10f2 commit 6ad0c58
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 130 deletions.
3 changes: 1 addition & 2 deletions product_category_global_account_setting/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
"name": "Product Category - Global Account Settings",
"version": "12.0.1.1.1",
"version": "16.0.1.0.0",
"summary": "Propagate Accouting settings of product categories"
" for all the companies",
"category": "Accounting",
Expand All @@ -13,6 +13,5 @@
"license": "AGPL-3",
"depends": ["stock_account", "fiscal_company_account"],
"data": ["views/view_product_category.xml"],
"demo": ["demo/account_chart_template.xml", "demo/account_account_template.xml"],
"installable": True,
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import models

_logger = logging.getLogger(__name__)


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

def load_for_current_company(self, sale_tax_rate, purchase_tax_rate):
res = super().load_for_current_company(sale_tax_rate, purchase_tax_rate)
def _load(self, company):
res = super()._load(company)

# Create properties for all the categories
# for all the new created accounts
Expand All @@ -24,7 +20,5 @@ def load_for_current_company(self, sale_tax_rate, purchase_tax_rate):
"global_property_account_expense_categ",
"global_property_account_income_categ",
]:
category._apply_global_account_property(
self.env.user.company_id, field_name
)
category._apply_global_account_property(self.env.company, field_name)
return res
25 changes: 12 additions & 13 deletions product_category_global_account_setting/models/product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class ProductCategory(models.Model):

global_property_account_income_categ = fields.Char(string="Income Account Code")

@api.model
def create(self, vals):
category = super().create(vals)
field_names = []
if vals.get("global_property_account_expense_categ"):
field_names.append("global_property_account_expense_categ")
if vals.get("global_property_account_income_categ"):
field_names.append("global_property_account_income_categ")

category.propagate_global_account_properties_recursive(field_names)
return category
@api.model_create_multi
def create(self, vals_list):
categories = super().create(vals_list)
for category, vals in zip(categories, vals_list, strict=True):
field_names = []
if vals.get("global_property_account_expense_categ"):
field_names.append("global_property_account_expense_categ")
if vals.get("global_property_account_income_categ"):
field_names.append("global_property_account_income_categ")

category.propagate_global_account_properties_recursive(field_names)
return categories

@api.multi
def write(self, vals):
res = super().write(vals)
field_names = []
Expand All @@ -55,7 +55,6 @@ def propagate_global_account_properties_recursive(self, field_names):
childs = category.mapped("child_id")
childs.write({field_name: getattr(category, field_name)})

@api.multi
def _apply_global_account_property(self, company, field_name):
self.ensure_one()
IrProperty = self.env["ir.property"].sudo()
Expand Down
15 changes: 7 additions & 8 deletions product_category_global_account_setting/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
class ResCompany(models.Model):
_inherit = "res.company"

@api.model
def create(self, vals):
company = super().create(vals)
if vals.get("fiscal_type") == "fiscal_child":
company._apply_global_account_settings()
return company
@api.model_create_multi
def create(self, vals_list):
companies = super().create(vals_list)
for company, vals in zip(companies, vals_list, strict=True):
if vals.get("fiscal_type") == "fiscal_child":
company._apply_global_account_settings()
return companies

@api.multi
def write(self, vals):
res = super().write(vals)
if vals.get("fiscal_type") == "fiscal_child":
for company in self:
company._apply_global_account_settings()
return res

@api.multi
def _apply_global_account_settings(self):
self.ensure_one()
ProductCategory = self.env["product.category"]
Expand Down
74 changes: 16 additions & 58 deletions product_category_global_account_setting/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,28 @@


class TestModule(TransactionCase):
def setUp(self):
super().setUp()
self.ResCompany = self.env["res.company"]
self.AccountAccount = self.env["account.account"]
self.IrProperty = self.env["ir.property"].sudo()

self.expense_type = self.env.ref("account.data_account_type_expenses")
self.mother_company = self.env.ref("fiscal_company_base.company_fiscal_mother")
self.income_type = self.env.ref("account.data_account_type_revenue")
self.saleable_categ = self.env.ref("product.product_category_1")
self.saleable_service_categ = self.env.ref("product.product_category_3")
self.expense_field = self.env.ref(
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.ResCompany = cls.env["res.company"]
cls.AccountAccount = cls.env["account.account"]
cls.IrProperty = cls.env["ir.property"].sudo()
cls.mother_company = cls.env.ref("fiscal_company_base.company_fiscal_mother")
cls.saleable_categ = cls.env.ref("product.product_category_1")
cls.saleable_service_categ = cls.env.ref("product.product_category_3")
cls.expense_field = cls.env.ref(
"account." "field_product_category__property_account_expense_categ_id"
)
self.income_field = self.env.ref(
"account." "field_product_category__property_account_income_categ_id"
)
self.chart_template = self.env.ref(
"product_category_global_account_setting.chart_template"
)
self.account_template = self.env.ref(
"product_category_global_account_setting.account_template"
)

def _create_account_for_all_companies(self, code, name, user_type_id):
def _create_account_for_all_companies(self, code, name, account_type):
for company in self.ResCompany.with_context(active_test=False).search(
[("fiscal_type", "in", ["normal", "fiscal_mother"])]
):
self.AccountAccount.sudo().create(
{
"code": code,
"name": name,
"user_type_id": user_type_id,
"account_type": account_type,
"company_id": company.id,
}
)
Expand All @@ -58,12 +47,10 @@ def test_01_propagate_recursively(self):

# Try to affect properties should success
# if account exists
self._create_account_for_all_companies(
"607TEST", "Purchase", self.expense_type.id
)
self._create_account_for_all_companies("607TEST", "Purchase", "expense")
self.saleable_categ.write({"global_property_account_expense_categ": "607TEST"})

self._create_account_for_all_companies("707TEST", "Sale", self.income_type.id)
self._create_account_for_all_companies("707TEST", "Sale", "income")
self.saleable_categ.write({"global_property_account_income_categ": "707TEST"})

companies = self.ResCompany.with_context(active_test=False).search(
Expand Down Expand Up @@ -104,9 +91,7 @@ def test_01_propagate_recursively(self):
)

def test_02_fiscal_child_company(self):
self._create_account_for_all_companies(
"607TEST", "Purchase", self.expense_type.id
)
self._create_account_for_all_companies("607TEST", "Purchase", "expense")

self.saleable_categ.write({"global_property_account_expense_categ": "607TEST"})

Expand All @@ -115,7 +100,7 @@ def test_02_fiscal_child_company(self):
{
"name": "Test Fiscal Child Company (Global Account)",
"fiscal_type": "fiscal_child",
"fiscal_company_id": self.mother_company.id,
"parent_id": self.mother_company.id,
}
)

Expand All @@ -130,30 +115,3 @@ def test_02_fiscal_child_company(self):
self.assertEqual(
1, len(properties), "Create a new fiscal company should create properties"
)

def test_03_fiscal_mother_company(self):
self._create_account_for_all_companies(
"607TEST", "Purchase", self.expense_type.id
)

self.saleable_categ.write({"global_property_account_expense_categ": "607TEST"})

# Create a fiscal mother
new_company = self.ResCompany.create(
{
"name": "Test Fiscal Mother Company (Global Account)",
"fiscal_type": "fiscal_mother",
}
)

self.env.user.write({"company_id": new_company.id})

# Try to install a chart of account without 607TEST
# should fail
with self.assertRaises(UserError):
self.chart_template.load_for_current_company(0, 0)

# Affect the 607TEST account template to the chart template
# and try to install again chart of account, should success
self.account_template.chart_template_id = self.chart_template.id
self.chart_template.load_for_current_company(0, 0)
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ Copyright (C) 2020-Today GRAP (http://www.grap.coop)
<field name="model">product.category</field>
<field name="inherit_id" ref="product.product_category_list_view"/>
<field name="arch" type="xml">
<field name="display_name" position="after">
<field name="global_property_account_expense_categ"/>
<field name="global_property_account_income_categ"/>
</field>
<xpath expr="//tree" position="attributes">
<attribute name="multi_edit">1</attribute>
</xpath>
<field name="display_name" position="after">
<field name="global_property_account_expense_categ" optional="show"/>
<field name="global_property_account_income_categ" optional="show"/>
</field>
</field>
</record>

<record id="view_product_category_form" model="ir.ui.view">
<field name="model">product.category</field>
<field name="inherit_id" ref="account.view_category_property_form"/>
<field name="arch" type="xml">
<group name="account_property" position="inside">

<group name="global_account_property" string="Global Account Properties" groups="account.group_account_manager" col="3">
<xpath expr="//group[@name='account_property']/group" position="before">
<group name="global_account_property" string="Global Account Properties" groups="account.group_account_manager">
<field name="global_property_account_income_categ"/>
<field name="global_property_account_expense_categ"/>
</group>
</group>
</xpath>
</field>
</record>

Expand Down

0 comments on commit 6ad0c58

Please sign in to comment.