from odoo import models, fields, _ from odoo.exceptions import UserError from ..models.printer_utils import send_zpl_to_printer import logging _logger = logging.getLogger(__name__) class CitizenPrinterWizard(models.TransientModel): _name = 'citizen.printer.wizard' _description = 'Citizen ZPL Printer Wizard' picking_id = fields.Many2one('stock.picking', string="Picking", required=True) line_ids = fields.One2many('citizen.printer.wizard.line', 'wizard_id', string="Lines") def action_print(self): self.ensure_one() # Retrieve printer config ip = self.picking_id.company_id.zpl_printer_ip port = self.picking_id.company_id.zpl_printer_port or 9100 if not ip: raise UserError(_("Please configure the ZPL Printer IP in Settings.")) report = self.env.ref('citizen_zpl_printer.report_citizen_label') # Print logic try: for line in self.line_ids: if line.quantity <= 0: continue # Render ZPL for the single lot zpl_data, zpl_format = report._render_qweb_text(report.id, [line.lot_id.id]) _logger.info(f"Generated ZPL Data: {len(zpl_data)} bytes. Start: {zpl_data[:50]}") # Send to printer 'quantity' times for i in range(line.quantity): send_zpl_to_printer(ip, port, zpl_data) except Exception as e: raise UserError(_("Printing Failed: %s") % str(e)) return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Success'), 'message': _('Labels sent to printer.'), 'type': 'success', 'sticky': False, } } class CitizenPrinterWizardLine(models.TransientModel): _name = 'citizen.printer.wizard.line' _description = 'Citizen ZPL Printer Wizard Line' wizard_id = fields.Many2one('citizen.printer.wizard', string="Wizard") lot_id = fields.Many2one('stock.lot', string="Lot/Serial Number", required=True, readonly=True) product_id = fields.Many2one('product.product', string="Product", related='lot_id.product_id', readonly=True) quantity = fields.Integer(string="Copies", default=1)