[IMP] Make it possible to create sequences in a separate function + change names...
authorJosse Colpaert <jco@odoo.com>
Wed, 18 Jun 2014 10:23:02 +0000 (12:23 +0200)
committerJosse Colpaert <jco@odoo.com>
Wed, 18 Jun 2014 10:23:02 +0000 (12:23 +0200)
addons/stock/stock.py

index 9c5310d..c678b27 100644 (file)
@@ -3114,63 +3114,23 @@ class stock_warehouse(osv.osv):
         pull_obj.write(cr, uid, warehouse.mto_pull_id.id, mto_pull_vals, context=context)
         return True
 
-    def create(self, cr, uid, vals, context=None):
-        if context is None:
-            context = {}
-        if vals is None:
-            vals = {}
-        data_obj = self.pool.get('ir.model.data')
+    def create_sequences_and_picking_types(self, cr, uid, warehouse, context=None):
         seq_obj = self.pool.get('ir.sequence')
         picking_type_obj = self.pool.get('stock.picking.type')
-        location_obj = self.pool.get('stock.location')
-
-        #create view location for warehouse
-        wh_loc_id = location_obj.create(cr, uid, {
-                'name': _(vals.get('code')),
-                'usage': 'view',
-                'location_id': data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_locations')[1]
-            }, context=context)
-        vals['view_location_id'] = wh_loc_id
-        #create all location
-        def_values = self.default_get(cr, uid, {'reception_steps', 'delivery_steps'})
-        reception_steps = vals.get('reception_steps',  def_values['reception_steps'])
-        delivery_steps = vals.get('delivery_steps', def_values['delivery_steps'])
-        context_with_inactive = context.copy()
-        context_with_inactive['active_test'] = False
-        sub_locations = [
-            {'name': _('Stock'), 'active': True, 'field': 'lot_stock_id'},
-            {'name': _('Input'), 'active': reception_steps != 'one_step', 'field': 'wh_input_stock_loc_id'},
-            {'name': _('Quality Control'), 'active': reception_steps == 'three_steps', 'field': 'wh_qc_stock_loc_id'},
-            {'name': _('Output'), 'active': delivery_steps != 'ship_only', 'field': 'wh_output_stock_loc_id'},
-            {'name': _('Packing Zone'), 'active': delivery_steps == 'pick_pack_ship', 'field': 'wh_pack_stock_loc_id'},
-        ]
-        for values in sub_locations:
-            location_id = location_obj.create(cr, uid, {
-                'name': values['name'],
-                'usage': 'internal',
-                'location_id': wh_loc_id,
-                'active': values['active'],
-            }, context=context_with_inactive)
-            vals[values['field']] = location_id
-
         #create new sequences
-        in_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': vals.get('name', '') + _(' Sequence in'), 'prefix': vals.get('code', '') + '/IN/', 'padding': 5}, context=context)
-        out_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': vals.get('name', '') + _(' Sequence out'), 'prefix': vals.get('code', '') + '/OUT/', 'padding': 5}, context=context)
-        pack_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': vals.get('name', '') + _(' Sequence packing'), 'prefix': vals.get('code', '') + '/PACK/', 'padding': 5}, context=context)
-        pick_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': vals.get('name', '') + _(' Sequence picking'), 'prefix': vals.get('code', '') + '/PICK/', 'padding': 5}, context=context)
-        int_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': vals.get('name', '') + _(' Sequence internal'), 'prefix': vals.get('code', '') + '/INT/', 'padding': 5}, context=context)
-
-        #create WH
-        new_id = super(stock_warehouse, self).create(cr, uid, vals=vals, context=context)
+        in_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence in'), 'prefix': warehouse.code + '/IN/', 'padding': 5}, context=context)
+        out_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence out'), 'prefix': warehouse.code + '/OUT/', 'padding': 5}, context=context)
+        pack_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence packing'), 'prefix': warehouse.code + '/PACK/', 'padding': 5}, context=context)
+        pick_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence picking'), 'prefix': warehouse.code + '/PICK/', 'padding': 5}, context=context)
+        int_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence internal'), 'prefix': warehouse.code + '/INT/', 'padding': 5}, context=context)
 
-        warehouse = self.browse(cr, uid, new_id, context=context)
         wh_stock_loc = warehouse.lot_stock_id
         wh_input_stock_loc = warehouse.wh_input_stock_loc_id
         wh_output_stock_loc = warehouse.wh_output_stock_loc_id
         wh_pack_stock_loc = warehouse.wh_pack_stock_loc_id
 
         #fetch customer and supplier locations, for references
-        customer_loc, supplier_loc = self._get_partner_locations(cr, uid, new_id, context=context)
+        customer_loc, supplier_loc = self._get_partner_locations(cr, uid, warehouse.id, context=context)
 
         #create in, out, internal picking types for warehouse
         input_loc = wh_input_stock_loc
@@ -3197,7 +3157,7 @@ class stock_warehouse(osv.osv):
 
         in_type_id = picking_type_obj.create(cr, uid, vals={
             'name': _('Receptions'),
-            'warehouse_id': new_id,
+            'warehouse_id': warehouse.id,
             'code': 'incoming',
             'sequence_id': in_seq_id,
             'default_location_src_id': supplier_loc.id,
@@ -3206,7 +3166,7 @@ class stock_warehouse(osv.osv):
             'color': color}, context=context)
         out_type_id = picking_type_obj.create(cr, uid, vals={
             'name': _('Delivery Orders'),
-            'warehouse_id': new_id,
+            'warehouse_id': warehouse.id,
             'code': 'outgoing',
             'sequence_id': out_seq_id,
             'return_picking_type_id': in_type_id,
@@ -3217,7 +3177,7 @@ class stock_warehouse(osv.osv):
         picking_type_obj.write(cr, uid, [in_type_id], {'return_picking_type_id': out_type_id}, context=context)
         int_type_id = picking_type_obj.create(cr, uid, vals={
             'name': _('Internal Transfers'),
-            'warehouse_id': new_id,
+            'warehouse_id': warehouse.id,
             'code': 'internal',
             'sequence_id': int_seq_id,
             'default_location_src_id': wh_stock_loc.id,
@@ -3227,22 +3187,22 @@ class stock_warehouse(osv.osv):
             'color': color}, context=context)
         pack_type_id = picking_type_obj.create(cr, uid, vals={
             'name': _('Pack'),
-            'warehouse_id': new_id,
+            'warehouse_id': warehouse.id,
             'code': 'internal',
             'sequence_id': pack_seq_id,
             'default_location_src_id': wh_pack_stock_loc.id,
             'default_location_dest_id': output_loc.id,
-            'active': delivery_steps == 'pick_pack_ship',
+            'active': warehouse.delivery_steps == 'pick_pack_ship',
             'sequence': max_sequence + 3,
             'color': color}, context=context)
         pick_type_id = picking_type_obj.create(cr, uid, vals={
             'name': _('Pick'),
-            'warehouse_id': new_id,
+            'warehouse_id': warehouse.id,
             'code': 'internal',
             'sequence_id': pick_seq_id,
             'default_location_src_id': wh_stock_loc.id,
             'default_location_dest_id': wh_pack_stock_loc.id,
-            'active': delivery_steps != 'ship_only',
+            'active': warehouse.delivery_steps != 'ship_only',
             'sequence': max_sequence + 2,
             'color': color}, context=context)
 
@@ -3254,7 +3214,52 @@ class stock_warehouse(osv.osv):
             'pick_type_id': pick_type_id,
             'int_type_id': int_type_id,
         }
-        super(stock_warehouse, self).write(cr, uid, new_id, vals=vals, context=context)
+        super(stock_warehouse, self).write(cr, uid, warehouse.id, vals=vals, context=context)
+
+
+    def create(self, cr, uid, vals, context=None):
+        if context is None:
+            context = {}
+        if vals is None:
+            vals = {}
+        data_obj = self.pool.get('ir.model.data')
+        seq_obj = self.pool.get('ir.sequence')
+        picking_type_obj = self.pool.get('stock.picking.type')
+        location_obj = self.pool.get('stock.location')
+
+        #create view location for warehouse
+        wh_loc_id = location_obj.create(cr, uid, {
+                'name': _(vals.get('code')),
+                'usage': 'view',
+                'location_id': data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_locations')[1]
+            }, context=context)
+        vals['view_location_id'] = wh_loc_id
+        #create all location
+        def_values = self.default_get(cr, uid, {'reception_steps', 'delivery_steps'})
+        reception_steps = vals.get('reception_steps',  def_values['reception_steps'])
+        delivery_steps = vals.get('delivery_steps', def_values['delivery_steps'])
+        context_with_inactive = context.copy()
+        context_with_inactive['active_test'] = False
+        sub_locations = [
+            {'name': _('Stock'), 'active': True, 'field': 'lot_stock_id'},
+            {'name': _('Input'), 'active': reception_steps != 'one_step', 'field': 'wh_input_stock_loc_id'},
+            {'name': _('Quality Control'), 'active': reception_steps == 'three_steps', 'field': 'wh_qc_stock_loc_id'},
+            {'name': _('Output'), 'active': delivery_steps != 'ship_only', 'field': 'wh_output_stock_loc_id'},
+            {'name': _('Packing Zone'), 'active': delivery_steps == 'pick_pack_ship', 'field': 'wh_pack_stock_loc_id'},
+        ]
+        for values in sub_locations:
+            location_id = location_obj.create(cr, uid, {
+                'name': values['name'],
+                'usage': 'internal',
+                'location_id': wh_loc_id,
+                'active': values['active'],
+            }, context=context_with_inactive)
+            vals[values['field']] = location_id
+
+        #create WH
+        new_id = super(stock_warehouse, self).create(cr, uid, vals=vals, context=context)
+        warehouse = self.browse(cr, uid, new_id, context=context)
+        self.create_sequences_and_picking_types(cr, uid, warehouse, context=context)
         warehouse.refresh()
 
         #create routes and push/pull rules
@@ -3298,7 +3303,8 @@ class stock_warehouse(osv.osv):
             for push in route.push_ids:
                 push_obj.write(cr, uid, push.id, {'name': pull.name.replace(warehouse.name, name, 1)}, context=context)
         #change the mto pull rule name
-        pull_obj.write(cr, uid, warehouse.mto_pull_id.id, {'name': warehouse.mto_pull_id.name.replace(warehouse.name, name, 1)}, context=context)
+        if warehouse.mto_pull_id.id:
+            pull_obj.write(cr, uid, warehouse.mto_pull_id.id, {'name': warehouse.mto_pull_id.name.replace(warehouse.name, name, 1)}, context=context)
 
     def _check_delivery_resupply(self, cr, uid, warehouse, new_location, change_to_multiple, context=None):
         """ Will check if the resupply routes from this warehouse follow the changes of number of delivery steps """
@@ -3380,11 +3386,12 @@ class stock_warehouse(osv.osv):
                 if vals.get('name'):
                     name = vals.get('name', warehouse.name)
                 self._handle_renaming(cr, uid, warehouse, name, vals.get('code', warehouse.code), context=context_with_inactive)
-                seq_obj.write(cr, uid, warehouse.in_type_id.sequence_id.id, {'name': name + _(' Sequence in'), 'prefix': vals.get('code', warehouse.code) + '\IN\\'}, context=context)
-                seq_obj.write(cr, uid, warehouse.out_type_id.sequence_id.id, {'name': name + _(' Sequence out'), 'prefix': vals.get('code', warehouse.code) + '\OUT\\'}, context=context)
-                seq_obj.write(cr, uid, warehouse.pack_type_id.sequence_id.id, {'name': name + _(' Sequence packing'), 'prefix': vals.get('code', warehouse.code) + '\PACK\\'}, context=context)
-                seq_obj.write(cr, uid, warehouse.pick_type_id.sequence_id.id, {'name': name + _(' Sequence picking'), 'prefix': vals.get('code', warehouse.code) + '\PICK\\'}, context=context)
-                seq_obj.write(cr, uid, warehouse.int_type_id.sequence_id.id, {'name': name + _(' Sequence internal'), 'prefix': vals.get('code', warehouse.code) + '\INT\\'}, context=context)
+                if warehouse.in_type_id:
+                    seq_obj.write(cr, uid, warehouse.in_type_id.sequence_id.id, {'name': name + _(' Sequence in'), 'prefix': vals.get('code', warehouse.code) + '\IN\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.out_type_id.sequence_id.id, {'name': name + _(' Sequence out'), 'prefix': vals.get('code', warehouse.code) + '\OUT\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.pack_type_id.sequence_id.id, {'name': name + _(' Sequence packing'), 'prefix': vals.get('code', warehouse.code) + '\PACK\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.pick_type_id.sequence_id.id, {'name': name + _(' Sequence picking'), 'prefix': vals.get('code', warehouse.code) + '\PICK\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.int_type_id.sequence_id.id, {'name': name + _(' Sequence internal'), 'prefix': vals.get('code', warehouse.code) + '\INT\\'}, context=context)
         if vals.get('resupply_wh_ids') and not vals.get('resupply_route_ids'):
             for cmd in vals.get('resupply_wh_ids'):
                 if cmd[0] == 6: