From 6b95bb6a61bba88500af912912635df02ba3ae0d Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Wed, 7 Nov 2018 16:09:40 +0000 Subject: [PATCH 1/5] [IMP] stock: inventory loss location for each company Each company should have its own inventory location to allow specific account on it. TASK-ID: 1882269 --- addons/stock/data/stock_data.xml | 14 +++----------- addons/stock/data/stock_demo2.xml | 1 - addons/stock/models/res_company.py | 28 +++++++++++++++++++++++++++- addons/stock/tests/test_warehouse.py | 6 +++--- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/addons/stock/data/stock_data.xml b/addons/stock/data/stock_data.xml index e3487172f7c..cd79d573393 100644 --- a/addons/stock/data/stock_data.xml +++ b/addons/stock/data/stock_data.xml @@ -39,12 +39,6 @@ - - Inventory adjustment - - inventory - - Procurements @@ -103,11 +97,6 @@ - - property_stock_inventory - - - property_stock_production @@ -180,5 +169,8 @@ + + + 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..d7d4e6a1710 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -17,7 +17,6 @@ class Company(models.Model): 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({ @@ -34,11 +33,38 @@ class Company(models.Model): '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, + }) + + @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(self, vals): company = super(Company, self).create(vals) company.create_transit_location() + company.sudo()._create_inventory_loss_location() # 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, 'partner_id': company.partner_id.id}) 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({ From 5df621f66550a2832ee69e4ef23a953b5ef4724e Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Thu, 8 Nov 2018 13:42:12 +0000 Subject: [PATCH 2/5] [REF] stock: missing warehouse creation We move the code from the 'post_init_hook' into a function tag, to be consistent with the way we populate missing data when stock module is installed. TASK-ID: 1882269 --- addons/stock/__init__.py | 17 ----------------- addons/stock/__manifest__.py | 1 - addons/stock/data/stock_data.xml | 3 +++ addons/stock/models/res_company.py | 18 ++++++++++++++++-- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/addons/stock/__init__.py b/addons/stock/__init__.py index a07046e3fd4..66322ee14f3 100644 --- a/addons/stock/__init__.py +++ b/addons/stock/__init__.py @@ -17,20 +17,3 @@ def pre_init_hook(cr): ('model', 'like', '%stock%'), ('module', '=', 'stock') ]).unlink() - -def _create_warehouse(cr, registry): - """ This hook is used to add a warehouse on existing companies - when module stock is installed. - """ - env = api.Environment(cr, SUPERUSER_ID, {}) - company_ids = env['res.company'].search([]) - company_with_warehouse = env['stock.warehouse'].search([]).mapped('company_id') - company_without_warehouse = company_ids - company_with_warehouse - for company in company_without_warehouse: - company.create_transit_location() - env['stock.warehouse'].create({ - 'name': company.name, - 'code': company.name[:5], - 'company_id': company.id, - 'partner_id': company.partner_id.id - }) diff --git a/addons/stock/__manifest__.py b/addons/stock/__manifest__.py index a6c42ce4111..eacc673c9fc 100644 --- a/addons/stock/__manifest__.py +++ b/addons/stock/__manifest__.py @@ -78,5 +78,4 @@ 'application': True, 'auto_install': False, 'pre_init_hook': 'pre_init_hook', - 'post_init_hook': '_create_warehouse', } diff --git a/addons/stock/data/stock_data.xml b/addons/stock/data/stock_data.xml index cd79d573393..ecb0e11d14b 100644 --- a/addons/stock/data/stock_data.xml +++ b/addons/stock/data/stock_data.xml @@ -169,6 +169,9 @@ + + + diff --git a/addons/stock/models/res_company.py b/addons/stock/models/res_company.py index d7d4e6a1710..d44d40b5ef0 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -50,6 +50,22 @@ class Company(models.Model): 'value': 'stock.location,%d' % inventory_loss_location.id, }) + @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_inventory_loss_location(self): company_ids = self.env['res.company'].search([]) @@ -65,7 +81,5 @@ class Company(models.Model): company.create_transit_location() company.sudo()._create_inventory_loss_location() - # 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, 'partner_id': company.partner_id.id}) return company From ee1ea10c8cc35c13439ef455a0eef454b7abaf85 Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Thu, 8 Nov 2018 15:26:07 +0000 Subject: [PATCH 3/5] [IMP] stock, mrp, repair: production location for each company Each company should have its own production location to allow specific account on it. TASK-ID: 1882269 --- addons/mrp/models/stock_warehouse.py | 4 +--- addons/mrp/report/report_stock_rule.py | 2 +- addons/repair/data/repair_demo.xml | 6 +++--- addons/repair/tests/test_repair.py | 4 ++-- addons/stock/data/stock_data.xml | 14 +++---------- addons/stock/models/res_company.py | 27 ++++++++++++++++++++++++++ 6 files changed, 37 insertions(+), 20 deletions(-) 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 @@ procurement - - Production - - production - - Vendors @@ -97,11 +91,6 @@ - - property_stock_production - - - - - + + diff --git a/addons/stock/models/res_company.py b/addons/stock/models/res_company.py index 7955023918b..3fcbad0079f 100644 --- a/addons/stock/models/res_company.py +++ b/addons/stock/models/res_company.py @@ -12,7 +12,7 @@ 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. @@ -23,12 +23,12 @@ class Company(models.Model): '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, }) @@ -94,6 +94,12 @@ class Company(models.Model): '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([]) @@ -124,7 +130,7 @@ class Company(models.Model): def create(self, vals): company = super(Company, self).create(vals) - company.create_transit_location() + company.sudo()._create_transit_location() company.sudo()._create_inventory_loss_location() company.sudo()._create_production_location() company.sudo()._create_scrap_location()