# -*- coding: utf-8 -*- import logging from odoo import models, fields, api, _ _logger = logging.getLogger(__name__) class SfDemandPlanPrintWizard(models.TransientModel): _name = 'sf.demand.plan.print.wizard' _description = u'打印向导' demand_plan_id = fields.Many2one('sf.production.demand.plan', string='需求计划ID') product_id = fields.Many2one( comodel_name='product.product', related='demand_plan_id.product_id', string='产品', store=True, index=True) model_id = fields.Char('模型ID') filename_url = fields.Char('文件名/URL') type = fields.Selection([ ('1', '图纸'), ('2', '程序单'), ], string='类型') status = fields.Selection([ ('not_start', '未开始'), ('success', '成功'), ('fail', '失败'), ], string='状态', default='not_start') machining_drawings = fields.Binary('2D加工图纸', related='product_id.machining_drawings', store=True) workorder_id = fields.Many2one('mrp.workorder', string='工单') cnc_worksheet = fields.Binary('程序单') def demand_plan_print(self): for record in self: pdf_data = record.machining_drawings if record.type == '1' else record.cnc_worksheet if pdf_data: try: # 执行打印 self.env['jikimo.printing'].sudo().print_pdf(pdf_data) record.status = 'success' t_part, c_part = record.demand_plan_id.print_count.split('C') t_num = int(t_part[1:]) c_num = int(c_part) if record.type == '1': t_num += 1 elif record.type == '2': c_num += 1 record.demand_plan_id.print_count = f"T{t_num}C{c_num}" except Exception as e: record.status = 'fail' _logger.error(f"文件{record.filename_url}打印失败: {str(e)}") class MrpWorkorder(models.Model): _inherit = 'mrp.workorder' def write(self, vals): res = super(MrpWorkorder, self).write(vals) for record in self: if 'cnc_worksheet' in vals: demand_plan_print = self.env['sf.demand.plan.print.wizard'].sudo().search( [('workorder_id', '=', record.id)]) if demand_plan_print: self.env['sf.demand.plan.print.wizard'].sudo().write( {'cnc_worksheet': record.cnc_worksheet, 'filename_url': record.cnc_worksheet_name}) else: demand_plan = self.env['sf.production.demand.plan'].sudo().search( [('product_id', '=', record.product_id.id)]) if demand_plan: wizard_vals = { 'demand_plan_id': demand_plan.id, 'model_id': demand_plan.model_id, 'type': '2', 'workorder_id': record.id, 'cnc_worksheet': record.cnc_worksheet, 'filename_url': record.cnc_worksheet_name } self.env['sf.demand.plan.print.wizard'].sudo().create(wizard_vals) return res