feat: implement multi-company logic for product and stock valuation models and add stock_account dependency

This commit is contained in:
Suherdy Yacob 2026-06-04 14:12:24 +07:00
parent 9e0e5ad977
commit cfd5575451
5 changed files with 68 additions and 1 deletions

View File

@ -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',

View File

@ -3,3 +3,4 @@ from . import pos_config
from . import pos_session
from . import res_users
from . import product
from . import stock

View File

@ -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()

44
models/stock.py Normal file
View File

@ -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