# -*- coding: utf-8 -*-
##############################################################################
#
#    Author: Alexandre Fayolle
#    Copyright 2013 Camptocamp SA
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################


from openerp.osv import orm, fields
from openerp import netsvc

class StockPicking(orm.Model):
    _inherit = "stock.picking"
    _columns = {
        'priority': fields.selection([('0', 'Normal'),
                                      ('1', 'Urgent'),
                                      ('2', 'Very Urgent')],
                                     'Priority',
                                     required=True,
                                     help='The priority of the picking'),
        }
    _defaults = {
        'priority': '0',
        }
    def retry_assign_all(self, cr, uid, ids, context=None):
        domain = [('type', '!=', 'in'),
                  ('state', 'in', ('confirmed', 'assigned'))]
        if ids:
            domain += [('ids', 'in', ids)]
        picking_ids = self.search(cr, uid, domain, order='priority desc, min_date', context=context)
        cancelled_ids = self.cancel_assign(cr, uid, picking_ids, context)
        assigned_ids = self.action_assign(cr, uid, picking_ids, context)
        return cancelled_ids, assigned_ids

class StockPickingOut(orm.Model):
    _inherit = 'stock.picking.out'
    _columns = {
        'priority': fields.selection([('0', 'Normal'),
                                      ('1', 'Urgent'),
                                      ('2', 'Very Urgent')],
                                     'Priority',
                                     required=True,
                                     help='The priority of the picking'),
        }
    _defaults = {
        'priority': '0',
        }

    def retry_assign_all(self, cr, uid, ids, context=None):
        return self.pool.get('stock.picking').retry_assign_all(cr, uid, ids, context=context)

class StockPickingIn(orm.Model):
    _inherit = 'stock.picking.in'
    _columns = {
        'priority': fields.selection([('0', 'Normal'),
                                      ('1', 'Urgent'),
                                      ('2', 'Very Urgent')],
                                     'Priority',
                                     required=True,
                                     help='The priority of the picking'),
        }
    _defaults = {
        'priority': '0',
        }

    def retry_assign_all(self, cr, uid, ids, context=None):
        return self.pool.get('stock.picking').retry_assign_all(cr, uid, ids, context=context)




class StockPickingRetryAvailability(orm.TransientModel):
    _name = "stock.picking.retry.availability"
    _columns = {
        }

    def action_retry_assign(self, cr, uid, ids, context=None):
        pick_obj = self.pool['stock.picking']

        pick_obj.retry_assign_all(cr, uid, [], context=context)

        return {'type': 'ir.actions.act_window_close'}

@@ -930,16 +930,30 @@
 #######%0A
+import logging
 %0A%0Afrom o
@@ -974,16 +974,21 @@
 ort orm,
+ osv,
  fields%0A
@@ -1003,22 +1003,98 @@
 nerp
- import netsvc
+.tools.translate import _%0Afrom openerp import netsvc%0A_logger = logging.getLogger(__name__)
 %0A%0Acl
@@ -1893,32 +1893,76 @@
 ontext=context)%0A
+        _logger.info('cancelling pickings')%0A
         cancelle
@@ -2037,24 +2037,178 @@
 signed_ids =
+ %5B%5D%0A        errors = %5B%5D%0A        _logger.info('reassigning pickings')%0A        for picking_id in picking_ids:%0A            try:%0A                assigned_id =
  self.action
@@ -2216,32 +2216,33 @@
 assign(cr, uid, 
+%5B
 picking_ids, con
@@ -2231,34 +2231,564 @@
  %5Bpicking_id
-s, context
+%5D, context)%0A                assigned_ids.append(assigned_id)%0A            except osv.except_osv, exc:%0A                name = self.read(cr, uid, picking_id, %5B'name'%5D, context=context)%5B'name'%5D%0A                errors.append(u'%25s: %25s' %25 (name, exc.args%5B-1%5D))%0A                _logger.info('error in action_assign for picking %25s: %25s' %25 (name, exc))%0A        if errors:%0A            message = '%5Cn'.join(errors)%0A            raise orm.except_orm(_(u'Warning'),%0A                                 _(u'The following errors were encountered:%5Cn%25s') %25 message
 )%0A        re

allow to continue processing pickings even if some of them have no stock at all by handing the exception