stock_assign_serial_numbers.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class StockAssignSerialNumbers(models.TransientModel):
  6. _name = 'stock.assign.serial'
  7. _description = 'Stock Assign Serial Numbers'
  8. def _default_next_serial_count(self):
  9. move = self.env['stock.move'].browse(self.env.context.get('default_move_id'))
  10. if move.exists():
  11. filtered_move_lines = move.move_line_ids.filtered(lambda l: not l.lot_name and not l.lot_id)
  12. return len(filtered_move_lines)
  13. product_id = fields.Many2one('product.product', 'Product',
  14. related='move_id.product_id')
  15. move_id = fields.Many2one('stock.move')
  16. next_serial_number = fields.Char('First SN', required=True)
  17. next_serial_count = fields.Integer('Number of SN',
  18. default=_default_next_serial_count, required=True)
  19. @api.constrains('next_serial_count')
  20. def _check_next_serial_count(self):
  21. for wizard in self:
  22. if wizard.next_serial_count < 1:
  23. raise ValidationError(_("The number of Serial Numbers to generate must be greater than zero."))
  24. def generate_serial_numbers(self):
  25. self.ensure_one()
  26. self.move_id.next_serial = self.next_serial_number or ""
  27. return self.move_id._generate_serial_numbers(next_serial_count=self.next_serial_count)