stock_picking_to_batch.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, _
  4. from odoo.exceptions import UserError
  5. class StockPickingToBatch(models.TransientModel):
  6. _name = 'stock.picking.to.batch'
  7. _description = 'Batch Transfer Lines'
  8. batch_id = fields.Many2one('stock.picking.batch', string='Batch Transfer', domain="[('is_wave', '=', False), ('state', 'in', ('draft', 'in_progress'))]")
  9. mode = fields.Selection([('existing', 'an existing batch transfer'), ('new', 'a new batch transfer')], default='new')
  10. user_id = fields.Many2one('res.users', string='Responsible')
  11. is_create_draft = fields.Boolean(string="Draft", help='When checked, create the batch in draft status')
  12. def attach_pickings(self):
  13. self.ensure_one()
  14. pickings = self.env['stock.picking'].browse(self.env.context.get('active_ids'))
  15. if self.mode == 'new':
  16. company = pickings.company_id
  17. if len(company) > 1:
  18. raise UserError(_("The selected pickings should belong to an unique company."))
  19. batch = self.env['stock.picking.batch'].create({
  20. 'user_id': self.user_id.id,
  21. 'company_id': company.id,
  22. 'picking_type_id': pickings[0].picking_type_id.id,
  23. })
  24. else:
  25. batch = self.batch_id
  26. pickings.write({'batch_id': batch.id})
  27. # you have to set some pickings to batch before confirm it.
  28. if self.mode == 'new' and not self.is_create_draft:
  29. batch.action_confirm()