diff --git a/addons/mrp/models/stock_warehouse.py b/addons/mrp/models/stock_warehouse.py index 7496236882b..a03ab6ddc36 100644 --- a/addons/mrp/models/stock_warehouse.py +++ b/addons/mrp/models/stock_warehouse.py @@ -59,9 +59,7 @@ class StockWarehouse(models.Model): @api.model def _get_production_location(self): - location = self.env.ref('stock.location_production', raise_if_not_found=False) - if not location: - location = self.env['stock.location'].search([('usage', '=', 'production')], limit=1) + location = self.env['stock.location'].with_context(force_company=self.company_id.id).search([('usage', '=', 'production'), ('company_id', '=', self.company_id.id)], limit=1) if not location: raise UserError(_('Can\'t find any production location.')) return location diff --git a/addons/mrp/report/report_stock_rule.py b/addons/mrp/report/report_stock_rule.py index d25960d1183..2f25ca6a6bd 100644 --- a/addons/mrp/report/report_stock_rule.py +++ b/addons/mrp/report/report_stock_rule.py @@ -13,5 +13,5 @@ class ReportStockRule(models.AbstractModel): """ res = super(ReportStockRule, self)._get_rule_loc(rule, product_id) if rule.action == 'manufacture': - res['source'] = product_id.property_stock_production or self.env.ref('stock.location_production') + res['source'] = product_id.property_stock_production return res diff --git a/addons/repair/data/repair_demo.xml b/addons/repair/data/repair_demo.xml index 60616df3fb2..173c86f863b 100644 --- a/addons/repair/data/repair_demo.xml +++ b/addons/repair/data/repair_demo.xml @@ -9,7 +9,7 @@ 1 - - Scrapped - - True - inventory - - - - Inventory adjustment - - inventory - - Procurements procurement - - Production - - production - - Vendors @@ -103,16 +84,6 @@ - - property_stock_inventory - - - - - property_stock_production - - - - - + + + + + + + + + + + + + + diff --git a/addons/stock/data/stock_demo2.xml b/addons/stock/data/stock_demo2.xml index e5f23bb647d..0b8ff734243 100644 --- a/addons/stock/data/stock_demo2.xml +++ b/addons/stock/data/stock_demo2.xml @@ -34,7 +34,6 @@ lot - diff --git a/addons/stock/models/res_company.py b/addons/stock/models/res_company.py index 28a10509d52..3fcbad0079f 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -12,34 +12,127 @@ class Company(models.Model): 'stock.location', 'Internal Transit Location', on_delete="restrict", help="Technical field used for resupply routes between warehouses that belong to this company") - def create_transit_location(self): + def _create_transit_location(self): '''Create a transit location with company_id being the given company_id. This is needed in case of resuply routes between warehouses belonging to the same company, because we don't want to create accounting entries at that time. ''' - # TDE FIXME: called in data - should be done in init ?? parent_location = self.env.ref('stock.stock_location_locations', raise_if_not_found=False) for company in self: location = self.env['stock.location'].create({ 'name': _('%s: Transit Location') % company.name, 'usage': 'transit', 'location_id': parent_location and parent_location.id or False, + 'company_id': company.id, }) - location.sudo().write({'company_id': company.id}) + company.write({'internal_transit_location_id': location.id}) - warehouses = self.env['stock.warehouse'].search([('partner_id', '=', company.partner_id.id)]) - warehouses.mapped('partner_id').with_context(force_company=company.id).write({ + company.partner_id.with_context(force_company=company.id).write({ 'property_stock_customer': location.id, 'property_stock_supplier': location.id, }) + def _create_inventory_loss_location(self): + parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False) + inventory_loss_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_inventory')]) + for company in self: + inventory_loss_location = self.env['stock.location'].create({ + 'name': '%s: Inventory adjustment' % company.name, + 'usage': 'inventory', + 'location_id': parent_location.id, + 'company_id': company.id, + }) + self.env['ir.property'].create({ + 'name': 'property_stock_inventory_%s' % company.name, + 'fields_id': inventory_loss_product_template_field.id, + 'company_id': company.id, + 'value': 'stock.location,%d' % inventory_loss_location.id, + }) + + def _create_production_location(self): + parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False) + production_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_production')]) + for company in self: + production_location = self.env['stock.location'].create({ + 'name': '%s: Production' % company.name, + 'usage': 'production', + 'location_id': parent_location.id, + 'company_id': company.id, + }) + self.env['ir.property'].create({ + 'name': 'property_stock_inventory_%s' % company.name, + 'fields_id': production_product_template_field.id, + 'company_id': company.id, + 'value': 'stock.location,%d' % production_location.id, + }) + + def _create_scrap_location(self): + for company in self: + parent_location = self.env.ref('stock.stock_location_locations_virtual', raise_if_not_found=False) + scrap_location = self.env['stock.location'].create({ + 'name': '%s: Scrap' % company.name, + 'usage': 'inventory', + 'location_id': parent_location.id, + 'company_id': company.id, + 'scrap_location': True, + }) + + @api.model + def create_missing_warehouse(self): + """ This hook is used to add a warehouse on existing companies + when module stock is installed. + """ + company_ids = self.env['res.company'].search([]) + company_with_warehouse = self.env['stock.warehouse'].search([]).mapped('company_id') + company_without_warehouse = company_ids - company_with_warehouse + for company in company_without_warehouse: + self.env['stock.warehouse'].create({ + 'name': company.name, + 'code': company.name[:5], + 'company_id': company.id, + 'partner_id': company.partner_id.id, + }) + + @api.model + def create_missing_transit_location(self): + company_without_transit = self.env['res.company'].search([('internal_transit_location_id', '=', False)]) + for company in company_without_transit: + company._create_transit_location() + + @api.model + def create_missing_inventory_loss_location(self): + company_ids = self.env['res.company'].search([]) + inventory_loss_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_inventory')]) + companies_having_property = self.env['ir.property'].search([('fields_id', '=', inventory_loss_product_template_field.id)]).mapped('company_id') + company_without_property = company_ids - companies_having_property + for company in company_without_property: + company._create_inventory_loss_location() + + @api.model + def create_missing_production_location(self): + company_ids = self.env['res.company'].search([]) + production_product_template_field = self.env['ir.model.fields'].search([('model','=','product.template'),('name','=','property_stock_production')]) + companies_having_property = self.env['ir.property'].search([('fields_id', '=', production_product_template_field.id)]).mapped('company_id') + company_without_property = company_ids - companies_having_property + for company in company_without_property: + company._create_production_location() + + @api.model + def create_missing_scrap_location(self): + company_ids = self.env['res.company'].search([]) + companies_having_scrap_loc = self.env['stock.location'].search([('scrap_location', '=', True)]).mapped('company_id') + company_without_property = company_ids - companies_having_scrap_loc + for company in company_without_property: + company._create_scrap_location() + @api.model def create(self, vals): company = super(Company, self).create(vals) - company.create_transit_location() - # mutli-company rules prevents creating warehouse and sub-locations - self.env['stock.warehouse'].check_access_rights('create') + company.sudo()._create_transit_location() + company.sudo()._create_inventory_loss_location() + company.sudo()._create_production_location() + company.sudo()._create_scrap_location() self.env['stock.warehouse'].sudo().create({'name': company.name, 'code': company.name[:5], 'company_id': company.id, 'partner_id': company.partner_id.id}) return company diff --git a/addons/stock/tests/test_warehouse.py b/addons/stock/tests/test_warehouse.py index 0b9fe1e6d20..9a93da5c939 100644 --- a/addons/stock/tests/test_warehouse.py +++ b/addons/stock/tests/test_warehouse.py @@ -34,9 +34,9 @@ class TestWarehouse(TestStockCommon): self.assertIn(inventory.name, inventory.move_ids.name) self.assertEqual(inventory.move_ids.product_qty, 15.0) self.assertEqual(inventory.move_ids.location_id, self.warehouse_1.lot_stock_id) - self.assertEqual(inventory.move_ids.location_dest_id, self.env.ref('stock.location_inventory')) # Inventory loss + self.assertEqual(inventory.move_ids.location_dest_id, self.product_1.property_stock_inventory) # Inventory loss self.assertEqual(inventory.move_ids.state, 'done') - quants = self.env['stock.quant']._gather(self.product_1, self.env.ref('stock.location_inventory')) + quants = self.env['stock.quant']._gather(self.product_1, self.product_1.property_stock_inventory) self.assertEqual(len(quants), 1) # One quant created for inventory loss # Check quantity of product in various locations: current, its parent, brother and other @@ -176,7 +176,7 @@ class TestWarehouse(TestStockCommon): productA = self.env['product.product'].create({'name': 'Product A', 'type': 'product'}) stock_location = self.env.ref('stock.stock_location_stock') customer_location = self.env.ref('stock.stock_location_customers') - location_loss = self.env.ref('stock.location_inventory') + location_loss = productA.property_stock_inventory # Create a picking out and force availability picking_out = self.env['stock.picking'].create({