[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) <sig@openerp.com>
This commit is contained in:
Goffin Simon
2019-04-23 13:37:18 +00:00
parent 4b0b00dbd9
commit 1da058f8ed
2 changed files with 16 additions and 7 deletions
+3 -2
View File
@@ -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
+13 -5
View File
@@ -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.