Skip to content

Commit

Permalink
[IMP] x_x_multicompany_default context flag to force on write
Browse files Browse the repository at this point in the history
Add the possibility to propagate properties on write if a context key is set.
This can be useful in case of data import
  • Loading branch information
yvaucher committed Jun 12, 2024
1 parent 2d8ee68 commit 7304502
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 57 deletions.
35 changes: 12 additions & 23 deletions partner_account_multicompany_default/models/partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2023 Moduon Team S.L.
# Copyright 2024 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
from odoo import api, models
from odoo import models


class Partner(models.Model):
Expand Down Expand Up @@ -31,25 +31,15 @@ def propagate_multicompany_payment_term_id(self):
def propagate_multicompany_supplier_payment_term_id(self):
self._propagate_multicompany_field("property_supplier_payment_term_id")

@api.model_create_multi
def create(self, vals_list):
"""Propagate accounts to other companies always, on creation."""
res = super().create(vals_list)
multicompany_partners = res - res.filtered("company_id")
payable_partners = multicompany_partners.filtered("property_account_payable_id")
receivable_partners = multicompany_partners.filtered(
"property_account_receivable_id"
)
position_partners = multicompany_partners.filtered(
"property_account_position_id"
)
payment_term_partners = multicompany_partners.filtered(
"property_payment_term_id"
)
payment_term_partners = multicompany_partners.filtered(
"property_payment_term_id"
)
supplier_payment_term_partners = multicompany_partners.filtered(
def _propagate_property_fields(self):
"""Propagate accounts to other companies always."""
super()._propagate_property_fields()
payable_partners = self.filtered("property_account_payable_id")
receivable_partners = self.filtered("property_account_receivable_id")
position_partners = self.filtered("property_account_position_id")
payment_term_partners = self.filtered("property_payment_term_id")
payment_term_partners = self.filtered("property_payment_term_id")
supplier_payment_term_partners = self.filtered(
"property_supplier_payment_term_id"
)
if (
Expand All @@ -60,15 +50,14 @@ def create(self, vals_list):
and not payment_term_partners
and not supplier_payment_term_partners
):
return res
return

Check warning on line 53 in partner_account_multicompany_default/models/partner.py

View check run for this annotation

Codecov / codecov/patch

partner_account_multicompany_default/models/partner.py#L53

Added line #L53 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 res
return
# Propagate account to other companies by default
payable_partners.propagate_multicompany_account_payable()
receivable_partners.propagate_multicompany_account_receivable()
position_partners.propagate_multicompany_account_position()
payment_term_partners.propagate_multicompany_payment_term_id()
supplier_payment_term_partners.propagate_multicompany_supplier_payment_term_id()
return res
23 changes: 22 additions & 1 deletion partner_base_multicompany_default/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from itertools import groupby
from operator import itemgetter, or_

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

Expand Down Expand Up @@ -145,3 +145,24 @@ def _propagate_multicompany_field(self, field):
sorted_partners._propagate_multicompany_m2o(field)
else:
sorted_partners._propagate_multicompany_value(field)

def _propagate_property_fields(self):
return

@api.model_create_multi
def create(self, vals_list):
"""Propagate property values on creation to other companies."""
res = super().create(vals_list)
multicompany_partners = res - res.filtered("company_id")
multicompany_partners._propagate_property_fields()
return res

def write(self, vals):
"""If forced, propagate property values on update 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"):
multicompany_partners = self - self.filtered("company_id")
multicompany_partners._propagate_property_fields()

Check warning on line 167 in partner_base_multicompany_default/models/partner.py

View check run for this annotation

Codecov / codecov/patch

partner_base_multicompany_default/models/partner.py#L166-L167

Added lines #L166 - L167 were not covered by tests
return res
14 changes: 6 additions & 8 deletions partner_delivery_multicompany_default/models/partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2023 Moduon Team S.L.
# Copyright 2024 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
from odoo import api, models
from odoo import models


class Partner(models.Model):
Expand All @@ -13,21 +13,19 @@ def _get_multicompany_delivery_carrier_key(self):
def propagate_multicompany_delivery_carrier(self):
self._propagate_multicompany_field("property_delivery_carrier_id")

@api.model_create_multi
def create(self, vals_list):
def _propagate_property_fields(self):
"""Propagate delivery carrier to other companies always, on creation."""
res = super().create(vals_list)
multicompany_partners = res - res.filtered("company_id")
super()._propagate_property_fields()
multicompany_partners = self - self.filtered("company_id")
delivery_partners = multicompany_partners.filtered(
"property_delivery_carrier_id"
)
# Skip if no delivery was selected
if not delivery_partners:
return res
return
# 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 res
return
# Propagate delivery carrier to other companies by default
delivery_partners.propagate_multicompany_delivery_carrier()
return res
14 changes: 6 additions & 8 deletions partner_product_multicompany_default/models/partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2023 Moduon Team S.L.
# Copyright 2024 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
from odoo import api, models
from odoo import models


class Partner(models.Model):
Expand All @@ -13,21 +13,19 @@ def _get_multicompany_product_pricelist_key(self):
def propagate_multicompany_product_pricelist(self):
self._propagate_multicompany_field("property_product_pricelist")

@api.model_create_multi
def create(self, vals_list):
def _propagate_property_fields(self):
"""Propagate product pricelist to other companies always, on creation."""
res = super().create(vals_list)
multicompany_partners = res - res.filtered("company_id")
super()._propagate_property_fields()
multicompany_partners = self - self.filtered("company_id")
pricelist_partners = multicompany_partners.filtered(
"property_product_pricelist"
)
# Skip if no pricelist was selected
if not pricelist_partners:
return res
return
# 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 res
return
# Propagate pricelist to other companies by default
pricelist_partners.propagate_multicompany_product_pricelist()
return res
14 changes: 6 additions & 8 deletions partner_purchase_multicompany_default/models/partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2023 Moduon Team S.L.
# Copyright 2024 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
from odoo import api, models
from odoo import models


class Partner(models.Model):
Expand All @@ -11,21 +11,19 @@ def propagate_multicompany_purchase_currency(self):
# currency are shared acrosse companies
self._propagate_multicompany_field("property_purchase_currency_id")

@api.model_create_multi
def create(self, vals_list):
def _propagate_property_fields(self):
"""Propagate accounts to other companies always, on creation."""
res = super().create(vals_list)
multicompany_partners = res - res.filtered("company_id")
super()._propagate_property_fields()
multicompany_partners = self - self.filtered("company_id")
purchase_currency_partners = multicompany_partners.filtered(
"property_purchase_currency_id"
)
# Skip if no currency was selected
if not purchase_currency_partners:
return res
return
# 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 res
return

Check warning on line 27 in partner_purchase_multicompany_default/models/partner.py

View check run for this annotation

Codecov / codecov/patch

partner_purchase_multicompany_default/models/partner.py#L27

Added line #L27 was not covered by tests
# Propagate account to other companies by default
purchase_currency_partners.propagate_multicompany_purchase_currency()
return res
31 changes: 22 additions & 9 deletions product_account_multicompany_default/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,36 @@ def propagate_multicompany_account_income(self):
def propagate_multicompany_account_expense(self):
self._propagate_multicompany_account("property_account_expense_id")

@api.model_create_multi
def create(self, vals_list):
"""Propagate accounts to other companies always, on creation."""
res = super().create(vals_list)
multicompany_products = res - res.filtered("company_id")
income_products = multicompany_products.filtered("property_account_income_id")
expense_products = multicompany_products.filtered("property_account_expense_id")
def _propagate_property_fields(self):
income_products = self.filtered("property_account_income_id")
expense_products = self.filtered("property_account_expense_id")
# Skip if no account was selected
if not income_products and not expense_products:
return res
return
# 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 res
return

Check warning on line 96 in product_account_multicompany_default/models/product.py

View check run for this annotation

Codecov / codecov/patch

product_account_multicompany_default/models/product.py#L96

Added line #L96 was not covered by tests
# Propagate account to other companies by default
income_products.propagate_multicompany_account_income()
expense_products.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)
multicompany_products = res - res.filtered("company_id")
multicompany_products._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"):
multicompany_products = self - self.filtered("company_id")
multicompany_products._propagate_property_fields()

Check warning on line 116 in product_account_multicompany_default/models/product.py

View check run for this annotation

Codecov / codecov/patch

product_account_multicompany_default/models/product.py#L115-L116

Added lines #L115 - L116 were not covered by tests
return res


Expand Down

0 comments on commit 7304502

Please sign in to comment.