Skip to content

Commit

Permalink
[IMP] product_account_multicompany_default propagate product category…
Browse files Browse the repository at this point in the history
… accounts
  • Loading branch information
santostelmo committed Nov 25, 2024
1 parent c94cf5a commit d727d23
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 35 deletions.
14 changes: 6 additions & 8 deletions product_account_multicompany_default/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
Product Account Multi-Company Default
=====================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:b354b96693fd1f8a16818ecd1fe6bfb577ac8691a6a895be3477c55e81a02481
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
Expand All @@ -23,13 +20,13 @@ Product Account Multi-Company Default
:target: https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-product_account_multicompany_default
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/multi-company&target_branch=16.0
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/multi-company&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends the functionality of products and accounting to allow you
to propagate multi-company product accounts from the current company to all
to propagate multi-company product template and product category accounts from the current company to all
other companies where you have access rights.

.. IMPORTANT::
Expand Down Expand Up @@ -77,7 +74,7 @@ Bug Tracker

Bugs are tracked on `GitHub Issues <https://github.com/OCA/multi-company/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
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/multi-company/issues/new?body=module:%20product_account_multicompany_default%0Aversion:%2016.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.
Expand All @@ -94,6 +91,7 @@ Contributors
~~~~~~~~~~~~

* Jairo Llopis (`Moduon <https://www.moduon.team/>`__)
* Telmo Santos <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions product_account_multicompany_default/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
],
"data": [
"views/product_template_view.xml",
"views/product_category_view.xml",
],
}
1 change: 1 addition & 0 deletions product_account_multicompany_default/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import product
from . import product_category
115 changes: 115 additions & 0 deletions product_account_multicompany_default/models/product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright 2023 Moduon Team S.L.
# Copyright 2024 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
import logging
from collections import defaultdict
from functools import reduce
from itertools import groupby
from operator import itemgetter, or_

from odoo import _, api, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


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

def _propagate_multicompany_account(self, field):
"""Set the same account for all companies.
Args:
field (str):
The field to propagate. E.g. "property_account_income_categ_id"
or "property_account_expense_categ_id".
"""
alien_companies = self.env.user.company_ids - self.env.company
if not alien_companies:
raise UserError(
_(
"There are no other companies to propagate to. "
"Make sure you have access to other companies."
)
)
# Gain access to all companies of current user
self = self.with_context(
allowed_company_ids=self.env.company.ids + alien_companies.ids
)
# Map alien accounts by company and code
alien_accounts = self.env["account.account"].search(
[
("company_id", "in", alien_companies.ids),
(
"code",
"in",
self[field].mapped("code"),
),
]
)
accounts_map = defaultdict(dict)
for account in alien_accounts:
accounts_map[account.company_id.id][account.code] = account.id
# Group categories by account
for good_account, categories_grouper in groupby(self, itemgetter(field)):
categories = reduce(or_, categories_grouper)
# Propagate account to alien companies if possible
target_code = good_account.code
for alien_company in alien_companies:
try:
# False is a valid value, if you want to remove the account
target_account_id = (
target_code and accounts_map[alien_company.id][target_code]
)
except KeyError:
_logger.warning(
"Not propagating account to company because it does "
"not exist there: product_categories=%s, company=%s, account=%s",
categories,
alien_company,
target_code,
)
continue
if (
categories.with_company(alien_company)[field]
and categories.with_company(alien_company)[field].id
!= target_account_id
):
categories.with_company(alien_company)[field] = target_account_id

def propagate_multicompany_account_income(self):
self._propagate_multicompany_account("property_account_income_categ_id")

def propagate_multicompany_account_expense(self):
self._propagate_multicompany_account("property_account_expense_categ_id")

def _propagate_property_fields(self):
income_categories = self.filtered("property_account_income_categ_id")
expense_categories = self.filtered("property_account_expense_categ_id")
# Skip if no account was selected
if not income_categories and not expense_categories:
return

Check warning on line 91 in product_account_multicompany_default/models/product_category.py

View check run for this annotation

Codecov / codecov/patch

product_account_multicompany_default/models/product_category.py#L91

Added line #L91 was not covered by tests
# Skip if user has access to only one company
alien_user_companies = self.env.user.company_ids - self.env.company
if not alien_user_companies:
return
# Propagate account to other companies by default
income_categories.propagate_multicompany_account_income()
expense_categories.propagate_multicompany_account_expense()

@api.model_create_multi
def create(self, vals_list):
"""Propagate accounts to other companies always, on creation."""
res = super().create(vals_list)
res._propagate_property_fields()
return res

def write(self, vals):
"""If forced, propagate property values on write to other companies."""
res = super().write(vals)
# Allow opt-in for progagation on write using context
# Useful for data import to update records

if self.env.context.get("force_property_propagation"):
self._propagate_property_fields()
return res
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Jairo Llopis (`Moduon <https://www.moduon.team/>`__)
* Telmo Santos <[email protected]>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This module extends the functionality of products and accounting to allow you
to propagate multi-company product accounts from the current company to all
to propagate multi-company product template and product category accounts from the current company to all
other companies where you have access rights.
51 changes: 25 additions & 26 deletions product_account_multicompany_default/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Product Account Multi-Company Default</title>
<style type="text/css">

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

Expand Down Expand Up @@ -366,12 +366,10 @@ <h1 class="title">Product Account Multi-Company Default</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:b354b96693fd1f8a16818ecd1fe6bfb577ac8691a6a895be3477c55e81a02481
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/multi-company/tree/16.0/product_account_multicompany_default"><img alt="OCA/multi-company" src="https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-product_account_multicompany_default"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/multi-company&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/multi-company/tree/16.0/product_account_multicompany_default"><img alt="OCA/multi-company" src="https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-product_account_multicompany_default"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/multi-company&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module extends the functionality of products and accounting to allow you
to propagate multi-company product accounts from the current company to all
to propagate multi-company product template and product category accounts from the current company to all
other companies where you have access rights.</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
Expand All @@ -382,28 +380,28 @@ <h1 class="title">Product Account Multi-Company Default</h1>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#configuration" id="toc-entry-1">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-2">Usage</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-3">Known issues / Roadmap</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-4">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-5">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-6">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-7">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-8">Maintainers</a></li>
<li><a class="reference internal" href="#configuration" id="id1">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="id2">Usage</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="id3">Known issues / Roadmap</a></li>
<li><a class="reference internal" href="#bug-tracker" id="id4">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id5">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id6">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id7">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id8">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<h1><a class="toc-backref" href="#id1">Configuration</a></h1>
<p>To configure this module, you need to:</p>
<ol class="arabic simple">
<li>Install some module that lets you manage products, such as sale, purchase…
(if not already installed).</li>
</ol>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<h1><a class="toc-backref" href="#id2">Usage</a></h1>
<p>To use this module, you need to:</p>
<ol class="arabic simple">
<li>Go to a product’s form view. It can be the product template or the variant.</li>
Expand All @@ -412,7 +410,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
</ol>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h1>
<h1><a class="toc-backref" href="#id3">Known issues / Roadmap</a></h1>
<p>Since this module propagates product accounts based on the account code, it
should only be used in a database where all companies belong to the same country.</p>
<p>We could add a wizard that allows the user to select multiple products and propagate
Expand All @@ -423,36 +421,37 @@ <h1><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h1>
</div>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-4">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#id4">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/multi-company/issues">GitHub Issues</a>.
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
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/multi-company/issues/new?body=module:%20product_account_multicompany_default%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-5">Credits</a></h1>
<h1><a class="toc-backref" href="#id5">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-6">Authors</a></h2>
<h2><a class="toc-backref" href="#id6">Authors</a></h2>
<ul class="simple">
<li>Moduon</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<h2><a class="toc-backref" href="#id7">Contributors</a></h2>
<ul class="simple">
<li>Jairo Llopis (<a class="reference external" href="https://www.moduon.team/">Moduon</a>)</li>
<li>Telmo Santos &lt;<a class="reference external" href="mailto:telmo.santos&#64;camptocamp.com">telmo.santos&#64;camptocamp.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<h2><a class="toc-backref" href="#id8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/yajo"><img alt="yajo" src="https://github.com/yajo.png?size=40px" /></a></p>
<p><a class="reference external" href="https://github.com/yajo"><img alt="yajo" src="https://github.com/yajo.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/multi-company/tree/16.0/product_account_multicompany_default">OCA/multi-company</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
Expand Down
1 change: 1 addition & 0 deletions product_account_multicompany_default/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_product
from . import test_product_category
Loading

0 comments on commit d727d23

Please sign in to comment.