product_label_layout.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. _inherit = 'product.label.layout'
  7. move_line_ids = fields.Many2many('stock.move.line')
  8. picking_quantity = fields.Selection([
  9. ('picking', 'Transfer Quantities'),
  10. ('custom', 'Custom')], string="Quantity to print", required=True, default='custom')
  11. print_format = fields.Selection(selection_add=[
  12. ('zpl', 'ZPL Labels'),
  13. ('zplxprice', 'ZPL Labels with price')
  14. ], ondelete={'zpl': 'set default', 'zplxprice': 'set default'})
  15. def _prepare_report_data(self):
  16. xml_id, data = super()._prepare_report_data()
  17. if 'zpl' in self.print_format:
  18. xml_id = 'stock.label_product_product'
  19. if self.picking_quantity == 'picking' and self.move_line_ids:
  20. qties = defaultdict(int)
  21. custom_barcodes = defaultdict(list)
  22. uom_unit = self.env.ref('uom.product_uom_categ_unit', raise_if_not_found=False)
  23. for line in self.move_line_ids:
  24. if line.product_uom_id.category_id == uom_unit:
  25. if (line.lot_id or line.lot_name) and int(line.qty_done):
  26. custom_barcodes[line.product_id.id].append((line.lot_id.name or line.lot_name, int(line.qty_done)))
  27. continue
  28. qties[line.product_id.id] += line.qty_done
  29. # Pass only products with some quantity done to the report
  30. data['quantity_by_product'] = {p: int(q) for p, q in qties.items() if q}
  31. data['custom_barcodes'] = custom_barcodes
  32. return xml_id, data