51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
from odoo import models, _
|
|
from odoo.exceptions import UserError
|
|
from .printer_utils import send_zpl_to_printer
|
|
|
|
class MrpProduction(models.Model):
|
|
_inherit = 'mrp.production'
|
|
|
|
def action_print_citizen_label(self):
|
|
self.ensure_one()
|
|
if self.state != 'done':
|
|
raise UserError(_("You can only print labels for done manufacturing orders."))
|
|
|
|
# For MRP, we usually have lot_producing_id
|
|
lots = self.lot_producing_id
|
|
|
|
# Also check if there are other lots produced in case of multi-step or whatever (usually lot_producing_id is the main one)
|
|
# If we want all produced lots (e.g. byproducts or split production?), we might look at finished_move_line_ids
|
|
|
|
if not lots:
|
|
# Try to find from finished moves
|
|
lots = self.finished_move_line_ids.mapped('lot_id')
|
|
|
|
if not lots:
|
|
raise UserError(_("No lots found to print."))
|
|
|
|
# Retrieve printer config
|
|
ip = self.company_id.zpl_printer_ip
|
|
port = self.company_id.zpl_printer_port or 9100
|
|
|
|
if not ip:
|
|
raise UserError(_("Please configure the ZPL Printer IP in Settings."))
|
|
|
|
report = self.env.ref('stock.label_lot_template')
|
|
|
|
try:
|
|
zpl_data, _ = report._render_qweb_text(lots.ids)
|
|
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,
|
|
}
|
|
}
|