[MERGE]: Merged with lp:~openerp-dev/openobject-addons/rpa-dev-addons2
[odoo/odoo.git] / addons / stock / stock.py
index 6478a5f..ec281ca 100644 (file)
@@ -1762,7 +1762,8 @@ class stock_move(osv.osv):
                     result.setdefault(m.picking_id, [])
                     result[m.picking_id].append( (m, dest) )
         return result
-    def _create_chained_picking(self, cr, uid, pick_name,picking,ptype,move, context=None):
+
+    def _create_chained_picking(self, cr, uid, pick_name, picking, ptype, move, context=None):
         res_obj = self.pool.get('res.company')
         picking_obj = self.pool.get('stock.picking')
         pick_id= picking_obj.create(cr, uid, {
@@ -1779,6 +1780,7 @@ class stock_move(osv.osv):
                                 'date': picking.date,
                             })
         return pick_id
+
     def action_confirm(self, cr, uid, ids, context=None):
         """ Confirms stock move.
         @return: List of ids.
@@ -1794,11 +1796,18 @@ class stock_move(osv.osv):
             new_moves = []
             if context is None:
                 context = {}
+            seq_obj = self.pool.get('ir.sequence')
             for picking, todo in self._chain_compute(cr, uid, moves, context=context).items():
                 ptype = todo[0][1][5] and todo[0][1][5] or location_obj.picking_type_get(cr, uid, todo[0][0].location_dest_id, todo[0][1][0])
-                pick_name = picking.name or ''
                 if picking:
-                    pickid = self._create_chained_picking(cr, uid, pick_name,picking,ptype,todo,context)
+                    # name of new picking according to its type
+                    new_pick_name = seq_obj.get(cr, uid, 'stock.picking.' + ptype)
+                    pickid = self._create_chained_picking(cr, uid, new_pick_name, picking, ptype, todo, context=context)
+                    # Need to check name of old picking because it always considers picking as "OUT" when created from Sale Order
+                    old_ptype = location_obj.picking_type_get(cr, uid, picking.move_lines[0].location_id, picking.move_lines[0].location_dest_id)
+                    if old_ptype != picking.type:
+                        old_pick_name = seq_obj.get(cr, uid, 'stock.picking.' + old_ptype)
+                        self.pool.get('stock.picking').write(cr, uid, picking.id, {'name': old_pick_name}, context=context)
                 else:
                     pickid = False
                 for move, (loc, dummy, delay, dummy, company_id, ptype) in todo:
@@ -1821,9 +1830,10 @@ class stock_move(osv.osv):
                 if pickid:
                     wf_service.trg_validate(uid, 'stock.picking', pickid, 'button_confirm', cr)
             if new_moves:
-                create_chained_picking(self, cr, uid, new_moves, context)
-        create_chained_picking(self, cr, uid, moves, context)
-        return []
+                new_moves += create_chained_picking(self, cr, uid, new_moves, context)
+            return new_moves
+        all_moves = create_chained_picking(self, cr, uid, moves, context)
+        return all_moves
 
     def action_assign(self, cr, uid, ids, *args):
         """ Changes state to confirmed or waiting.
@@ -2076,10 +2086,9 @@ class stock_move(osv.osv):
             if move.picking_id:
                 picking_ids.append(move.picking_id.id)
             if move.move_dest_id.id and (move.state != 'done'):
-                self.write(cr, uid, [move.id], {'move_history_ids': [(4, move.move_dest_id.id)]})
-                #cr.execute('insert into stock_move_history_ids (parent_id,child_id) values (%s,%s)', (move.id, move.move_dest_id.id))
+                self.write(cr, uid, [move.id], {'stock_move_history_ids': [(4, move.move_dest_id.id)]}, context=context)
                 if move.move_dest_id.state in ('waiting', 'confirmed'):
-                    self.write(cr, uid, [move.move_dest_id.id], {'state': 'assigned'})
+                    self.action_assign(cr, uid, [move.move_dest_id.id])
                     if move.move_dest_id.picking_id:
                         wf_service.trg_write(uid, 'stock.picking', move.move_dest_id.picking_id.id, cr)
                     if move.move_dest_id.auto_validate:
@@ -2265,7 +2274,18 @@ class stock_move(osv.osv):
                 self.write(cr, uid, [current_move], update_val)
         return res
 
-    def action_consume(self, cr, uid, ids, quantity, location_id=False, context=None):
+    def trigger_move_state(self, cr, uid, move, state, context=None):
+        if isinstance(move, (int, long)):
+            move = [move]
+        res = []
+        if state == 'confirm':
+            res = self.action_confirm(cr, uid, move, context=context)
+        if state == 'assigned':
+            self.check_assign(cr, uid, move, context=context)
+            self.force_assign(cr, uid, move, context=context)
+        return res
+
+    def action_consume(self, cr, uid, ids, quantity, location_id=False,  context=None):
         """ Consumed product with specific quatity from specific source location
         @param cr: the database cursor
         @param uid: the user id
@@ -2293,12 +2313,12 @@ class stock_move(osv.osv):
                 quantity = move.product_qty
 
             uos_qty = quantity / move_qty * move.product_uos_qty
+            state = (move.state in ('confirm', 'assign') and move.state) or 'confirm'
 
             if quantity_rest > 0:
                 default_val = {
                     'product_qty': quantity,
                     'product_uos_qty': uos_qty,
-                    'state': move.state,
                     'location_id': location_id or move.location_id.id,
                 }
                 if (not move.prodlot_id.id) and (move.product_id.track_production and location_id):
@@ -2331,6 +2351,8 @@ class stock_move(osv.osv):
                 for (id, name) in product_obj.name_get(cr, uid, [new_move.product_id.id]):
                     message = _('Product ') + " '" + name + "' "+ _("is consumed with") + " '" + str(new_move.product_qty) + "' "+ _("quantity.")
                     self.log(cr, uid, new_move.id, message)
+
+            self.trigger_move_state(cr, uid, res, state, context=context)
         self.action_done(cr, uid, res)
 
         return res