From 971f004a2636fdd8e5d6629a95d5b0a264fb491e Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Sat, 30 May 2026 19:11:33 +0700 Subject: [PATCH] refactor: use sudo() in product archive and write methods to bypass multi-company restrictions --- models/product.py | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/models/product.py b/models/product.py index 0d54f09..9dc1df1 100644 --- a/models/product.py +++ b/models/product.py @@ -5,40 +5,24 @@ class ProductTemplate(models.Model): _inherit = 'product.template' def action_archive(self): - if not self.env.su: - if hasattr(self.env.user, '_get_company_ids'): - allowed_companies = list(self.env.user._get_company_ids()) - else: - allowed_companies = self.env.user.company_ids.filtered('active').ids - self = self.with_context(allowed_company_ids=allowed_companies) - return super().action_archive() + # Use sudo() to bypass multi-company checks during archival cascade + return super(ProductTemplate, self.sudo()).action_archive() def write(self, vals): - if not self.env.su and 'active' in vals and not vals['active']: - if hasattr(self.env.user, '_get_company_ids'): - allowed_companies = list(self.env.user._get_company_ids()) - else: - allowed_companies = self.env.user.company_ids.filtered('active').ids - self = self.with_context(allowed_company_ids=allowed_companies) + # Use sudo() during archival write to bypass multi-company checks + if 'active' in vals and not vals['active']: + return super(ProductTemplate, self.sudo()).write(vals) return super().write(vals) class ProductProduct(models.Model): _inherit = 'product.product' def action_archive(self): - if not self.env.su: - if hasattr(self.env.user, '_get_company_ids'): - allowed_companies = list(self.env.user._get_company_ids()) - else: - allowed_companies = self.env.user.company_ids.filtered('active').ids - self = self.with_context(allowed_company_ids=allowed_companies) - return super().action_archive() + # Use sudo() to bypass multi-company checks during archival cascade + return super(ProductProduct, self.sudo()).action_archive() def write(self, vals): - if not self.env.su and 'active' in vals and not vals['active']: - if hasattr(self.env.user, '_get_company_ids'): - allowed_companies = list(self.env.user._get_company_ids()) - else: - allowed_companies = self.env.user.company_ids.filtered('active').ids - self = self.with_context(allowed_company_ids=allowed_companies) + # Use sudo() during archival write to bypass multi-company checks + if 'active' in vals and not vals['active']: + return super(ProductProduct, self.sudo()).write(vals) return super().write(vals)