From 1da058f8edd10aff27bc572874080eda430771a8 Mon Sep 17 00:00:00 2001 From: Goffin Simon Date: Tue, 23 Apr 2019 13:33:01 +0000 Subject: [PATCH] [FIX] stock: Error when re-installing manufacturing application Steps to reproduce the bug: - Install MRP - Uninstall MRP - Install MRP Bug: An error was raised by sql constraint 'barcode_company_uniq' which ensures that a barcode for a location is unique per company. But when uninstalling MRP, some locations such as pbm_loc_id or sam_loc_id are set to active = False and when re-installing MRP, the function _create_missing_locations tried to re-create these locations and the sql constraint was raised. opw:1967534 closes odoo/odoo#32882 Signed-off-by: Simon Goffin (sig) --- addons/mrp/models/stock_warehouse.py | 5 +++-- addons/stock/models/stock_warehouse.py | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/addons/mrp/models/stock_warehouse.py b/addons/mrp/models/stock_warehouse.py index dd9a41f174f..7a838cbe2b3 100644 --- a/addons/mrp/models/stock_warehouse.py +++ b/addons/mrp/models/stock_warehouse.py @@ -172,18 +172,19 @@ class StockWarehouse(models.Model): manufacture_steps = vals.get('manufacture_steps', def_values['manufacture_steps']) code = vals.get('code') or self.code code = code.replace(' ', '').upper() + company_id = vals.get('company_id', self.company_id.id) values.update({ 'pbm_loc_id': { 'name': _('Pre-Production'), 'active': manufacture_steps in ('pbm', 'pbm_sam'), 'usage': 'internal', - 'barcode': code + '-PREPRODUCTION' + 'barcode': self._valid_barcode(code + '-PREPRODUCTION', company_id) }, 'sam_loc_id': { 'name': _('Post-Production'), 'active': manufacture_steps == 'pbm_sam', 'usage': 'internal', - 'barcode': code + '-POSTPRODUCTION' + 'barcode': self._valid_barcode(code + '-POSTPRODUCTION', company_id) }, }) return values diff --git a/addons/stock/models/stock_warehouse.py b/addons/stock/models/stock_warehouse.py index 913b94bb5a5..b85a34a60c6 100644 --- a/addons/stock/models/stock_warehouse.py +++ b/addons/stock/models/stock_warehouse.py @@ -441,40 +441,48 @@ class Warehouse(models.Model): delivery_steps = vals.get('delivery_steps', def_values['delivery_steps']) code = vals.get('code') or self.code code = code.replace(' ', '').upper() + company_id = vals.get('company_id', self.company_id.id) sub_locations = { 'lot_stock_id': { 'name': _('Stock'), 'active': True, 'usage': 'internal', - 'barcode': code + '-STOCK' + 'barcode': self._valid_barcode(code + '-STOCK', company_id) }, 'wh_input_stock_loc_id': { 'name': _('Input'), 'active': reception_steps != 'one_step', 'usage': 'internal', - 'barcode': code + '-INPUT' + 'barcode': self._valid_barcode(code + '-INPUT', company_id) }, 'wh_qc_stock_loc_id': { 'name': _('Quality Control'), 'active': reception_steps == 'three_steps', 'usage': 'internal', - 'barcode': code + '-QUALITY' + 'barcode': self._valid_barcode(code + '-QUALITY', company_id) }, 'wh_output_stock_loc_id': { 'name': _('Output'), 'active': delivery_steps != 'ship_only', 'usage': 'internal', - 'barcode': code + '-OUTPUT' + 'barcode': self._valid_barcode(code + '-OUTPUT', company_id) }, 'wh_pack_stock_loc_id': { 'name': _('Packing Zone'), 'active': delivery_steps == 'pick_pack_ship', 'usage': 'internal', - 'barcode': code + '-PACKING' + 'barcode': self._valid_barcode(code + '-PACKING', company_id) }, } return sub_locations + def _valid_barcode(self, barcode, company_id): + location = self.env['stock.location'].with_context(active_test=False).search([ + ('barcode', '=', barcode), + ('company_id', '=', company_id) + ]) + return not location and barcode + def _create_missing_locations(self, vals): """ It could happen that the user delete a mandatory location or a module with new locations was installed after some warehouses creation.