Skip to content

Instantly share code, notes, and snippets.

@odony
Last active August 27, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odony/5e49af0cb7b2e8510e673fa0f3e6144f to your computer and use it in GitHub Desktop.
Save odony/5e49af0cb7b2e8510e673fa0f3e6144f to your computer and use it in GitHub Desktop.
multi-company check
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api, _
from odoo.exceptions import UserError
class StockPutawayRule(models.Model):
_name = 'stock.putaway.rule'
_order = 'sequence,product_id'
_description = 'Putaway Rule'
_company_consistency_check = True
def _domain_product_id(self):
domain = "[('type', '!=', 'service'), '|', ('company_id', '=', False), ('company_id', '=', company_id)]"
if self.env.context.get('active_model') == 'product.template':
return [('product_tmpl_id', '=', self.env.context.get('active_id'))]
return domain
product_id = fields.Many2one(
'product.product', 'Product',
default=_default_product_id, domain=_domain_product_id,
company_restrict=True, ondelete='cascade')
category_id = fields.Many2one('product.category', 'Product Category',
default=_default_category_id, domain=_domain_category_id,
ondelete='cascade')
location_in_id = fields.Many2one(
'stock.location', 'When product arrives in',
domain="[('child_ids', '!=', False), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
company_restrict=True, default=_default_location_id, required=True, ondelete='cascade')
location_out_id = fields.Many2one(
'stock.location', 'Store to',
domain="[('id', 'child_of', location_in_id), ('id', '!=', location_in_id), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
company_restrict=True, required=True, ondelete='cascade')
sequence = fields.Integer('Priority', help="Give to the more specialized category, a higher priority to have them in top of the list.")
company_id = fields.Many2one(
'res.company', 'Company', required=True,
default=lambda s: s.env.company.id, index=True)
def write(self, vals):
if 'company_id' in vals:
for rule in self:
if rule.company_id.id != vals['company_id']:
raise UserError(_("Changing the company of this record is forbidden at this point, you should rather archive it and create a new one."))
res = super(StockPutawayRule, self).write(vals)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment