'Confirms stock move or put it in waiting if it\'s linked to another move.'

@api.multi
def action_confirm(self):

    move_create_proc = self.env['stock.move']
    move_to_confirm = self.env['stock.move']
    move_waiting = self.env['stock.move']
    to_assign = {}
    self.set_default_price_unit_from_product()
    for move in self:
        if move.move_orig_ids:
            move_waiting |= move
        else:
            inner_move = move.split_from
            while inner_move:
                if inner_move.move_orig_ids:
                    move_waiting |= move
                    break
                inner_move = inner_move.split_from
            else:
                if (move.procure_method == 'make_to_order'):
                    move_create_proc |= move
                else:
                    move_to_confirm |= move
        if ((not move.picking_id) and move.picking_type_id):
            key = (move.group_id.id, move.location_id.id, move.location_dest_id.id)
            if (key not in to_assign):
                to_assign[key] = self.env['stock.move']
            to_assign[key] |= move
    procurements = self.env['procurement.order']
    for move in move_create_proc:
        procurements |= procurements.create(move._prepare_procurement_from_move())
    if procurements:
        procurements.run()
    move_to_confirm.write({'state': 'confirmed'})
    (move_waiting | move_create_proc).write({'state': 'waiting'})
    for (key, moves) in to_assign.items():
        moves.assign_picking()
    self._push_apply()
    return self
