Skip to content

Commit

Permalink
Merge PR #68 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by legalsylvain
  • Loading branch information
github-grap-bot committed Dec 19, 2024
2 parents 29e5d36 + 6ad0c58 commit 288da45
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 0 deletions.
86 changes: 86 additions & 0 deletions product_category_global_account_setting/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
==========================================
Product Category - Global Account Settings
==========================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2943b92b3c8009da4286fec63441618274008e093344ca778a43b9f4525e3b02
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-grap%2Fodoo--addons--cae-lightgray.png?logo=github
:target: https://github.com/grap/odoo-addons-cae/tree/12.0/product_category_global_account_setting
:alt: grap/odoo-addons-cae

|badge1| |badge2| |badge3|

* deprecates ``product_category_recursive_property``
* deprecates features in ``fiscal_company_account``













################### DEPRECATED

Add inheritance mechanism in product categories properties.

Features
--------

* When an user set an accounting property to a product category, the
parameter will be set to all childs

* When a user create a product category, by default all the settings will
come from parent category

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/grap/odoo-addons-cae/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/grap/odoo-addons-cae/issues/new?body=module:%20product_category_global_account_setting%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* GRAP

Contributors
~~~~~~~~~~~~

* Sylvain LE GAL (https://www.twitter.com/legalsylvain)

Maintainers
~~~~~~~~~~~

This module is part of the `grap/odoo-addons-cae <https://github.com/grap/odoo-addons-cae/tree/12.0/product_category_global_account_setting>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions product_category_global_account_setting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
17 changes: 17 additions & 0 deletions product_category_global_account_setting/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Product Category - Global Account Settings",
"version": "16.0.1.0.0",
"summary": "Propagate Accouting settings of product categories"
" for all the companies",
"category": "Accounting",
"author": "GRAP",
"website": "https://github.com/grap/odoo-addons-cae",
"license": "AGPL-3",
"depends": ["stock_account", "fiscal_company_account"],
"data": ["views/view_product_category.xml"],
"installable": True,
}
Empty file.
3 changes: 3 additions & 0 deletions product_category_global_account_setting/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import account_chart_template
from . import product_category
from . import res_company
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models


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

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

# Create properties for all the categories
# for all the new created accounts
ProductCategory = self.env["product.category"]
categories = ProductCategory.with_context(active_test=False).search([])
for category in categories:
for field_name in [
"global_property_account_expense_categ",
"global_property_account_income_categ",
]:
category._apply_global_account_property(self.env.company, field_name)
return res
124 changes: 124 additions & 0 deletions product_category_global_account_setting/models/product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
# @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 _, api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


class ProductCategory(models.Model):
_inherit = "product.category"

global_property_account_expense_categ = fields.Char(string="Expense Account Code")

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

@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

def write(self, vals):
res = super().write(vals)
field_names = []
if "global_property_account_expense_categ" in vals.keys():
field_names.append("global_property_account_expense_categ")
if "global_property_account_income_categ" in vals.keys():
field_names.append("global_property_account_income_categ")

self.propagate_global_account_properties_recursive(field_names)
return res

def propagate_global_account_properties_recursive(self, field_names):
ResCompany = self.env["res.company"]

for field_name in field_names:
for category in self:
companies = ResCompany.with_context(active_test=False).search(
[("fiscal_type", "in", ["normal", "fiscal_mother", "fiscal_child"])]
)
for company in companies:
category._apply_global_account_property(company, field_name)

childs = category.mapped("child_id")
childs.write({field_name: getattr(category, field_name)})

def _apply_global_account_property(self, company, field_name):
self.ensure_one()
IrProperty = self.env["ir.property"].sudo()
AccountAccount = self.env["account.account"].sudo()
IrModelFields = self.env["ir.model.fields"]

base_message = f"Company {company.name} - Category {self.complete_name}"

account_code = getattr(self, field_name)

if account_code:
account = AccountAccount.search(
[
("code", "=", account_code),
("company_id", "=", company.fiscal_company_id.id),
]
)
if not account:
raise UserError(
_(f"{base_message} - Account {account_code} not found.")
)

field = IrModelFields.search(
[
("name", "=", "%s_id" % field_name.replace("global_", "")),
("model", "=", "product.category"),
]
)

current_property = IrProperty.search(
[
("name", "=", field.name),
("fields_id", "=", field.id),
("company_id", "=", company.id),
("res_id", "=", "product.category,%d" % (self.id)),
]
)

if account_code:
if current_property:
# Update Existing Property
current_property.write(
{"value_reference": "account.account,%d" % (account.id)}
)
_logger.debug(
f"{base_message} - Account {account_code} : Property updated."
)
else:
# Create a new property
IrProperty.create(
{
"name": field.name,
"company_id": company.id,
"type": "many2one",
"fields_id": field.id,
"value_reference": "account.account,%d" % (account.id),
"res_id": "product.category,%d" % (self.id),
}
)
_logger.debug(
f"{base_message} - Account {account_code} : Property created."
)

elif current_property:
# Delete obsolete property
current_property.unlink()
_logger.debug("%s : Property deleted." % (base_message))
40 changes: 40 additions & 0 deletions product_category_global_account_setting/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (C) 2020 - Today: GRAP (http://www.grap.coop)
# @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 api, models

_logger = logging.getLogger(__name__)


class ResCompany(models.Model):
_inherit = "res.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

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

def _apply_global_account_settings(self):
self.ensure_one()
ProductCategory = self.env["product.category"]
categories = ProductCategory.with_context(active_test=False).search([])
for category in categories:
category._apply_global_account_property(
self, "global_property_account_expense_categ"
)
category._apply_global_account_property(
self, "global_property_account_income_categ"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Sylvain LE GAL (https://www.twitter.com/legalsylvain)
27 changes: 27 additions & 0 deletions product_category_global_account_setting/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
* deprecates ``product_category_recursive_property``
* deprecates features in ``fiscal_company_account``













################### DEPRECATED

Add inheritance mechanism in product categories properties.

Features
--------

* When an user set an accounting property to a product category, the
parameter will be set to all childs

* When a user create a product category, by default all the settings will
come from parent category
1 change: 1 addition & 0 deletions product_category_global_account_setting/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_module
Loading

0 comments on commit 288da45

Please sign in to comment.