[FIX] product,stock: create a company for non-admin users

The typical multi-company record rule is
['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]

As the company is just created, the current user does not belong to the company.
Create first without company (to still check the access rights) and after add
the company in sudo

Can not apply the same patch for stock as locations and sublocations are created
based on the warehouse company.

Fixes #16807
This commit is contained in:
Martin Trigaux
2017-05-08 14:41:14 +02:00
parent 6513658957
commit 5c009b5f31
2 changed files with 8 additions and 4 deletions
+3 -2
View File
@@ -18,12 +18,13 @@ class ResCompany(models.Model):
'currency_id': new_company.currency_id.id,
})
field = self.env['ir.model.fields'].search([('model', '=', 'res.partner'), ('name', '=', 'property_product_pricelist')])
self.env['ir.property'].create({
product_property = self.env['ir.property'].create({
'name': 'property_product_pricelist',
'company_id': new_company.id,
'value_reference': 'product.pricelist,%s' % pricelist.id,
'fields_id': field.id
})
# multi-company security rules prevents access
product_property.sudo().write({'company_id': new_company.id})
return new_company
@api.multi
+5 -2
View File
@@ -23,14 +23,17 @@ class Company(models.Model):
location = self.env['stock.location'].create({
'name': _('%s: Transit Location') % self.name,
'usage': 'transit',
'company_id': self.id,
'location_id': parent_location and parent_location.id or False,
})
location.sudo().write({'company_id': self.id})
self.write({'internal_transit_location_id': location.id})
@api.model
def create(self, vals):
company = super(Company, self).create(vals)
self.env['stock.warehouse'].create({'name': company.name, 'code': company.name[:5], 'company_id': company.id})
# mutli-company rules prevents creating warehouse and sub-locations
self.env['stock.warehouse'].check_access_rights('create')
self.env['stock.warehouse'].sudo().create({'name': company.name, 'code': company.name[:5], 'company_id': company.id})
company.create_transit_location()
return company