stock_add_to_wave.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, api, _
  4. from odoo.exceptions import UserError
  5. class StockPickingToWave(models.TransientModel):
  6. _name = 'stock.add.to.wave'
  7. _description = 'Wave Transfer Lines'
  8. @api.model
  9. def default_get(self, fields_list):
  10. res = super().default_get(fields_list)
  11. if self.env.context.get('active_model') == 'stock.move.line':
  12. lines = self.env['stock.move.line'].browse(self.env.context.get('active_ids'))
  13. res['line_ids'] = self.env.context.get('active_ids')
  14. picking_types = lines.picking_type_id
  15. elif self.env.context.get('active_model') == 'stock.picking':
  16. pickings = self.env['stock.picking'].browse(self.env.context.get('active_ids'))
  17. res['picking_ids'] = self.env.context.get('active_ids')
  18. picking_types = pickings.picking_type_id
  19. else:
  20. return res
  21. if len(picking_types) > 1:
  22. raise UserError(_("The selected transfers should belong to the same operation type"))
  23. return res
  24. wave_id = fields.Many2one('stock.picking.batch', string='Wave Transfer', domain="[('is_wave', '=', True), ('state', 'in', ('draft', 'in_progress'))]")
  25. picking_ids = fields.Many2many('stock.picking')
  26. line_ids = fields.Many2many('stock.move.line')
  27. mode = fields.Selection([('existing', 'an existing wave transfer'), ('new', 'a new wave transfer')], default='existing')
  28. user_id = fields.Many2one('res.users', string='Responsible')
  29. def attach_pickings(self):
  30. self.ensure_one()
  31. self = self.with_context(active_owner_id=self.user_id.id)
  32. if self.line_ids:
  33. company = self.line_ids.company_id
  34. if len(company) > 1:
  35. raise UserError(_("The selected operations should belong to a unique company."))
  36. return self.line_ids._add_to_wave(self.wave_id)
  37. if self.picking_ids:
  38. company = self.picking_ids.company_id
  39. if len(company) > 1:
  40. raise UserError(_("The selected transfers should belong to a unique company."))
  41. else:
  42. raise UserError(_('Cannot create wave transfers'))
  43. view = self.env.ref('stock_picking_batch.view_move_line_tree_detailed_wave')
  44. return {
  45. 'name': _('Add Operations'),
  46. 'type': 'ir.actions.act_window',
  47. 'view_mode': 'list',
  48. 'views': [(view.id, 'tree')],
  49. 'res_model': 'stock.move.line',
  50. 'target': 'new',
  51. 'domain': [
  52. ('picking_id', 'in', self.picking_ids.ids),
  53. ('state', '!=', 'done')
  54. ],
  55. 'context': dict(
  56. self.env.context,
  57. picking_to_wave=self.picking_ids.ids,
  58. active_wave_id=self.wave_id.id,
  59. )}