diff --git a/quality_control/models/quality.py b/quality_control/models/quality.py index e7ad73ba..de67a092 100644 --- a/quality_control/models/quality.py +++ b/quality_control/models/quality.py @@ -13,6 +13,9 @@ from odoo.osv.expression import OR from odoo.exceptions import UserError from odoo.tools import image_data_uri from base64 import b64encode +import requests +import json +import base64 class QualityPoint(models.Model): @@ -449,6 +452,72 @@ class QualityCheck(models.Model): print(f"{report_url}/{latest_attachment.id}/datas") return f"{report_url}/{latest_attachment.id}/datas" return False + + def upload_factory_report(self): + """ + 上传出厂检验报告到加工订单明细中 + 将当前质检单的出厂检验报告上传到对应的加工订单明细中 + """ + self.ensure_one() + if not self.report_content: + raise UserError(_('当前质检单没有出厂检验报告,请先发布报告')) + + if not self.part_number: + raise UserError(_('零件图号不能为空')) + + if not self.picking_id or not self.picking_id.origin: + raise UserError(_('无法找到相关的调拨单或来源单据')) + + # 获取订单号(从调拨单的来源字段获取) + order_ref = self.picking_id.retrospect_ref + + try: + # 准备请求数据 + payload = { + "order_ref": order_ref, + "part_number": self.part_number, + "report_file": self.report_content.decode('utf-8') if isinstance(self.report_content, bytes) else self.report_content + } + + # 将Python字典转换为JSON字符串 + json_data = json.dumps(payload) + + # 获取服务器URL + base_url = self.env['ir.config_parameter'].sudo().get_param('bfm_url_new') + api_url = f"{base_url}/api/report/create" + + # 设置请求头 + headers = { + 'Content-Type': 'application/json', + } + + # 发送POST请求 + response = requests.post(api_url, data=json_data, headers=headers) + + # 处理响应 + if response.status_code == 200: + result = response.json() + if result.get('success'): + # 上传成功,显示成功消息 + return { + 'type': 'ir.actions.client', + 'tag': 'display_notification', + 'params': { + 'title': _('上传成功'), + 'message': _('出厂检验报告已成功上传到加工订单明细'), + 'type': 'success', + 'sticky': False, + } + } + else: + # API返回失败信息 + raise UserError(_('上传失败: %s') % result.get('message', '未知错误')) + else: + # HTTP请求失败 + raise UserError(_('请求失败,状态码: %s') % response.status_code) + + except Exception as e: + raise UserError(_('上传过程中发生错误: %s') % str(e)) @depends('product_id') def _compute_material_name(self):