diff --git a/addons/stock_picking_batch/wizard/stock_picking_to_batch.py b/addons/stock_picking_batch/wizard/stock_picking_to_batch.py
index 6f310faf129..f4f50f02c81 100644
--- a/addons/stock_picking_batch/wizard/stock_picking_to_batch.py
+++ b/addons/stock_picking_batch/wizard/stock_picking_to_batch.py
@@ -1,17 +1,31 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from odoo import api, fields, models
+from odoo import fields, models, _
+from odoo.exceptions import UserError
class StockPickingToBatch(models.TransientModel):
_name = 'stock.picking.to.batch'
_description = 'Batch Transfer Lines'
- batch_id = fields.Many2one('stock.picking.batch', string='Batch Transfer')
+ batch_id = fields.Many2one('stock.picking.batch', string='Batch Transfer', domain="[('state', '=', 'draft')]")
+ mode = fields.Selection([('existing', 'an existing batch transfer'), ('new', 'a new batch transfer')], default='existing')
+ user_id = fields.Many2one('res.users', string='Responsible', help='Person responsible for this batch transfer')
def attach_pickings(self):
- # use active_ids to add picking line to the selected batch
self.ensure_one()
- picking_ids = self.env.context.get('active_ids')
- return self.env['stock.picking'].browse(picking_ids).write({'batch_id': self.batch_id.id})
+ pickings = self.env['stock.picking'].browse(self.env.context.get('active_ids'))
+ if self.mode == 'new':
+ company = pickings.company_id
+ if len(company) > 1:
+ raise UserError(_("The selected pickings should belong to an unique company."))
+ batch = self.env['stock.picking.batch'].create({
+ 'user_id': self.user_id.id,
+ 'company_id': company.id,
+ 'picking_type_id': pickings[0].picking_type_id.id,
+ })
+ else:
+ batch = self.batch_id
+
+ pickings.write({'batch_id': batch.id})
diff --git a/addons/stock_picking_batch/wizard/stock_picking_to_batch_views.xml b/addons/stock_picking_batch/wizard/stock_picking_to_batch_views.xml
index 5997a49c80a..d021f984031 100644
--- a/addons/stock_picking_batch/wizard/stock_picking_to_batch_views.xml
+++ b/addons/stock_picking_batch/wizard/stock_picking_to_batch_views.xml
@@ -6,11 +6,18 @@
stock.picking.to.batch.form
stock.picking.to.batch
-