# -*- 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) 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' 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}" 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: 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', 'cnc_worksheet': record.cnc_worksheet, 'filename_url': record.cnc_worksheet_name } self.env['sf.demand.plan.print.wizard'].sudo().create(wizard_vals) return res