# -*- 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'打印向导' 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加工图纸') cnc_worksheet = fields.Binary('程序单') @api.model def demand_plan_print(self, print_ids): plan_print_ids = self.env['sf.demand.plan.print.wizard'].sudo().search( [('id', 'in', print_ids)]) if not plan_print_ids: return {'message': '记录不存在'} success_records = [] failed_records = [] for record in plan_print_ids: 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' production_demand_plan_id = self.env['sf.production.demand.plan'].sudo().search( [('model_id', '=', record.model_id)]) for production_demand_plan in production_demand_plan_id: t_part, c_part = production_demand_plan.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 production_demand_plan.print_count = f"T{t_num}C{c_num}" success_records.append({ 'filename_url': record.filename_url, }) except Exception as e: record.status = 'fail' _logger.error(f"文件{record.filename_url}打印失败: {str(e)}") failed_records.append({ 'filename_url': record.filename_url, }) if failed_records: message = f"成功打印 {len(success_records)} 个文件,失败 {len(failed_records)} 个" else: message = f"所有 {len(success_records)} 个文件打印成功" return {'message': message} 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( [('model_id', '=', record.model_id), ('type', '=', '2')]) if demand_plan_print: demand_plan_print.write( {'cnc_worksheet': record.cnc_worksheet, 'filename_url': record.cnc_worksheet_name}) else: wizard_vals = { 'model_id': record.model_id, 'type': '2', 'cnc_worksheet': record.cnc_worksheet, 'filename_url': record.cnc_worksheet_name } self.env['sf.demand.plan.print.wizard'].sudo().create(wizard_vals) return res