stock_lot_label_layout.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from odoo import fields, models
  5. class ProductLabelLayout(models.TransientModel):
  6. _name = 'lot.label.layout'
  7. _description = 'Choose the sheet layout to print lot labels'
  8. picking_ids = fields.Many2many('stock.picking')
  9. label_quantity = fields.Selection([
  10. ('lots', 'One per lot/SN'),
  11. ('units', 'One per unit')], string="Quantity to print", required=True, default='lots', help="If the UoM of a lot is not 'units', the lot will be considered as a unit and only one label will be printed for this lot.")
  12. print_format = fields.Selection([
  13. ('4x12', '4 x 12'),
  14. ('zpl', 'ZPL Labels')], string="Format", default='4x12', required=True)
  15. def process(self):
  16. self.ensure_one()
  17. xml_id = 'stock.action_report_lot_label'
  18. if self.print_format == 'zpl':
  19. xml_id = 'stock.label_lot_template'
  20. if self.label_quantity == 'lots':
  21. docids = self.picking_ids.move_line_ids.lot_id.ids
  22. else:
  23. uom_categ_unit = self.env.ref('uom.product_uom_categ_unit')
  24. quantity_by_lot = defaultdict(int)
  25. for move_line in self.picking_ids.move_line_ids:
  26. if not move_line.lot_id:
  27. continue
  28. if move_line.product_uom_id.category_id == uom_categ_unit:
  29. quantity_by_lot[move_line.lot_id.id] += int(move_line.qty_done)
  30. else:
  31. quantity_by_lot[move_line.lot_id.id] += 1
  32. docids = []
  33. for lot_id, qty in quantity_by_lot.items():
  34. docids.append([lot_id] * qty)
  35. report_action = self.env.ref(xml_id).report_action(docids)
  36. report_action.update({'close_on_report_download': True})
  37. return report_action