From cfd55754513d06a79da0f772c1dc89661014c6af Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Thu, 4 Jun 2026 14:12:24 +0700 Subject: [PATCH] feat: implement multi-company logic for product and stock valuation models and add stock_account dependency --- __manifest__.py | 2 +- models/__init__.py | 1 + models/__pycache__/__init__.cpython-312.pyc | Bin 358 -> 387 bytes models/product.py | 22 ++++++++++ models/stock.py | 44 ++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 models/stock.py diff --git a/__manifest__.py b/__manifest__.py index ba8e071..4148e58 100644 --- a/__manifest__.py +++ b/__manifest__.py @@ -8,7 +8,7 @@ It allows an employee to be associated with multiple branch companies. """, 'author': 'Suherdy Yacob', - 'depends': ['hr', 'pos_hr', 'hr_attendance', 'product', 'stock'], + 'depends': ['hr', 'pos_hr', 'hr_attendance', 'product', 'stock', 'stock_account'], 'data': [ 'security/hr_security.xml', 'security/hr_attendance_security.xml', diff --git a/models/__init__.py b/models/__init__.py index 60833c6..e42655e 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -3,3 +3,4 @@ from . import pos_config from . import pos_session from . import res_users from . import product +from . import stock diff --git a/models/__pycache__/__init__.cpython-312.pyc b/models/__pycache__/__init__.cpython-312.pyc index 066e92c02714216825dbfa719d2884ade46e9c36..447900cb3975f2fa1b16973cc71e4c36541ab44c 100644 GIT binary patch delta 133 zcmaFH)XdC#nwOW00SNMy6|?#$@=7xLOjLIiNMT4}%VEf6i(+GBsASjVn3&_KsmXYY zwYVfdIonT@{T4$JGf-0z3y5F^5o{oW9Yke#XRmnwOW00SG)!OJ!A08d|+l|WW38Dd6z)~Op4!Q5G?{30{~1c6H5R9 diff --git a/models/product.py b/models/product.py index 9dc1df1..e3bc460 100644 --- a/models/product.py +++ b/models/product.py @@ -4,6 +4,28 @@ from odoo import api, fields, models class ProductTemplate(models.Model): _inherit = 'product.template' + @api.depends_context('company') + @api.depends('categ_id.property_cost_method') + def _compute_cost_method(self): + for product_template in self: + product_template.cost_method = ( + product_template.categ_id.sudo().with_company( + product_template.company_id + ).property_cost_method + or (product_template.company_id or self.env.company).cost_method + ) + + @api.depends_context('company') + @api.depends('categ_id.property_valuation') + def _compute_valuation(self): + for product_template in self: + product_template.valuation = ( + product_template.categ_id.sudo().with_company( + product_template.company_id + ).property_valuation + or self.env.company.inventory_valuation + ) + def action_archive(self): # Use sudo() to bypass multi-company checks during archival cascade return super(ProductTemplate, self.sudo()).action_archive() diff --git a/models/stock.py b/models/stock.py new file mode 100644 index 0000000..d79976b --- /dev/null +++ b/models/stock.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +from odoo import api, fields, models + +class StockQuant(models.Model): + _inherit = 'stock.quant' + + @api.depends_context('company') + @api.depends('product_categ_id.property_cost_method') + def _compute_cost_method(self): + for quant in self: + quant.cost_method = ( + quant.product_categ_id.sudo().with_company( + quant.company_id + ).property_cost_method + or (quant.company_id or self.env.company).cost_method + ) + + @api.depends('company_id', 'location_id', 'owner_id', 'product_id', 'quantity') + def _compute_value(self): + self.fetch(['company_id', 'location_id', 'owner_id', 'product_id', 'quantity', 'lot_id']) + self.value = 0 + for quant in self: + if not quant.location_id or not quant.product_id or\ + not quant.location_id._should_be_valued() or\ + quant._should_exclude_for_valuation() or\ + quant.product_id.uom_id.is_zero(quant.quantity): + continue + if quant.product_id.lot_valuated: + quantity = quant.lot_id.sudo().with_company(quant.company_id).product_qty + value = quant.lot_id.sudo().with_company(quant.company_id).total_value + else: + quantity = quant.product_id.sudo().with_company(quant.company_id)._with_valuation_context().qty_available + value = quant.product_id.sudo().with_company(quant.company_id).total_value + if quant.product_id.uom_id.is_zero(quantity): + continue + quant.value = quant.quantity * value / quantity + +class StockMove(models.Model): + _inherit = 'stock.move' + + @api.depends('product_id.standard_price') + def _compute_standard_price(self): + for move in self: + move.standard_price = move.product_id.sudo().with_company(move.company_id).standard_price