diff --git a/quality_control/controllers/main.py b/quality_control/controllers/main.py index 5bbe71a9..39d9457b 100644 --- a/quality_control/controllers/main.py +++ b/quality_control/controllers/main.py @@ -90,25 +90,25 @@ class QualityController(http.Controller): ('Access-Control-Allow-Headers', 'Content-Type, Authorization') ] ) - + + class QualityReportController(http.Controller): - - @http.route('/quality/report/', type='http', auth='public') - def get_public_report(self, attachment_id, **kw): + @http.route('/quality/report/', type='http', auth='public') + def get_public_report(self, document_id, **kw): """提供公开访问PDF报告的控制器""" - attachment = request.env['ir.attachment'].sudo().browse(int(attachment_id)) - - # 安全检查:确保只有质检报告附件可以被访问 - if attachment.exists() and 'QC-' in attachment.name: - # 解码Base64数据为二进制数据 - pdf_content = base64.b64decode(attachment.datas) - - # 返回解码后的PDF内容 + document = request.env['documents.document'].sudo().browse(int(document_id)) + + # 安全检查:确保只有质检报告文档可以被访问 + if document.exists() and document.res_model == 'quality.check': + # 获取PDF内容 + pdf_content = document.raw + + # 返回PDF内容 return request.make_response( pdf_content, headers=[ ('Content-Type', 'application/pdf'), - ('Content-Disposition', f'inline; filename={attachment.name}') + ('Content-Disposition', f'inline; filename={document.name}.pdf') ] ) - return request.not_found() \ No newline at end of file + return request.not_found() diff --git a/quality_control/models/quality.py b/quality_control/models/quality.py index 022dc6b7..010c5d95 100644 --- a/quality_control/models/quality.py +++ b/quality_control/models/quality.py @@ -183,10 +183,12 @@ class QualityCheck(models.Model): report_number_name = fields.Char('出厂检验报告编号名称', compute='_compute_report_number_name') old_report_name = fields.Char('旧出厂检验报告编号', default='') + @api.depends('serial_number', 'part_number') def _compute_report_number_name(self): for record in self: - str_serial_number = '0' + str(record.serial_number) if record.serial_number < 10 else str(record.serial_number) + str_serial_number = '0' + str(record.serial_number) if record.serial_number < 10 else str( + record.serial_number) str_part_number = record.part_number if record.part_number else '' record.report_number_name = f'FQC{str_part_number}{str_serial_number}' @@ -284,25 +286,7 @@ class QualityCheck(models.Model): """实际执行发布操作的方法""" self.ensure_one() - # 1. 获取报告动作 - report_action = self.env.ref('sf_quality.action_report_quality_inspection') - - # 2. 生成PDF报告 - 修改这里的调用方式 - pdf_content, _ = report_action._render_qweb_pdf( - report_ref=report_action.report_name, # 添加report_ref参数 - res_ids=self.ids - ) - - # attachment = self.env['ir.attachment'].create({ - # 'name': f'{self.name}.pdf', - # 'type': 'binary', - # 'datas': b64encode(pdf_content), - # 'res_model': self._name, - # 'res_id': self.id, - # 'mimetype': 'application/pdf', - # }) - - # 获取已发布的文档文件夹 + # 1. 获取已发布的文档文件夹 workspace = self.env['documents.folder'].search( [('parent_folder_id', '=', self.env.ref('sf_quality.documents_purchase_contracts_folder').id), ('name', '=', '已发布')], limit=1) @@ -310,11 +294,9 @@ class QualityCheck(models.Model): if self.serial_number > 99: raise UserError(_('流水号不能大于99')) - # 3. 创建文档记录 + # 2. 先创建空文档记录 doc_vals = { 'name': self.report_number_name, - 'raw': pdf_content, - # 'attachment_id': attachment.id, 'mimetype': 'application/pdf', 'res_id': self.id, 'folder_id': workspace.id, @@ -322,13 +304,26 @@ class QualityCheck(models.Model): } doc = self.env['documents.document'].create(doc_vals) - # 关联到当前质检记录 + + # 3. 关联文档到质检记录 self.write({ 'report_number_id': doc.id, 'quality_state': 'pass' }) - # 记录发布历史 + # 4. 获取报告动作并生成PDF(此时二维码将包含正确的文档ID) + report_action = self.env.ref('sf_quality.action_report_quality_inspection') + pdf_content, _ = report_action._render_qweb_pdf( + report_ref=report_action.report_name, + res_ids=self.ids + ) + + # 5. 更新文档内容 + doc.write({ + 'raw': pdf_content + }) + + # 6. 记录发布历史 self.env['quality.check.report.history'].create({ 'check_id': self.id, 'report_number_id': doc.id, @@ -339,18 +334,17 @@ class QualityCheck(models.Model): 'sequence': len(self.report_history_ids) + 1 }) - # 更新流水号 + # 7. 更新其他信息 self.serial_number += 1 self.quality_manager = self.env.user.id if self.publish_status == 'canceled' and self.picking_id.state == 'done': self.upload_factory_report() - + self.write({ 'publish_status': 'published', }) - # 返回成功消息 return True # 发布前检验零件图号、操机员、质检员 @@ -397,7 +391,7 @@ class QualityCheck(models.Model): self.report_number_id.write({ 'folder_id': self.env.ref('sf_quality.documents_purchase_contracts_folder_canceled').id, }) - + # 3. 记录发布历史 self.env['quality.check.report.history'].create({ 'check_id': self.id, @@ -408,7 +402,7 @@ class QualityCheck(models.Model): 'document_status': 'canceled', 'sequence': len(self.report_history_ids) + 1 }) - + self.write({ 'old_report_name': self.report_number_id.name }) @@ -442,7 +436,7 @@ class QualityCheck(models.Model): width=140, height=140) ) ) - + def get_latest_report_attachment(self, check_id): """获取指定质检记录的最新报告附件,并删除旧的报告附件""" # 查找特定质检记录的所有附件 @@ -459,20 +453,14 @@ class QualityCheck(models.Model): # 返回最新的附件(如果存在) return attachments and attachments[0] or False - + def get_report_url(self): - """生成报告访问URL,确保获取最新版本""" - base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') - report_url = f"{base_url}/web/content/ir.attachment" - - # 获取最新附件的ID - latest_attachment = self.get_latest_report_attachment(self.id) - if latest_attachment: - # 生成包含附件ID的URL - print(f"{base_url}/quality/report/{latest_attachment.id}") - return f"{base_url}/quality/report/{latest_attachment.id}" + """生成报告访问URL""" + self.ensure_one() + if self.report_number_id: + return f'/quality/report/{self.report_number_id.id}' return False - + def upload_factory_report(self): """ 上传出厂检验报告到加工订单明细中 @@ -481,39 +469,40 @@ class QualityCheck(models.Model): self.ensure_one() if not self.report_content: raise UserError(_('当前质检单没有出厂检验报告,请先发布报告')) - + if not self.product_id.model_name: 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, "model_name": self.product_id.model_name, - "report_file": self.report_content.decode('utf-8') if isinstance(self.report_content, bytes) else self.report_content + "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() @@ -536,10 +525,10 @@ class QualityCheck(models.Model): else: # HTTP请求失败 raise UserError(_('请求失败,状态码: %s') % response.status_code) - + except Exception as e: raise UserError(_('上传过程中发生错误: %s') % str(e)) - + def delete_factory_report(self): """ 删除加工订单明细中的出厂检验报告 @@ -549,7 +538,7 @@ class QualityCheck(models.Model): if not order_ref: raise UserError(_('无法找到相关的调拨单或来源单据')) - + if not self.product_id.model_name: raise UserError(_('产品模型名称为空')) @@ -558,11 +547,11 @@ class QualityCheck(models.Model): payload = { "order_ref": order_ref, "model_name": self.product_id.model_name - } - + } + # 将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/delete" @@ -597,10 +586,9 @@ class QualityCheck(models.Model): else: # HTTP请求失败 raise UserError(_('请求失败,状态码: %s') % response.status_code) - + except Exception as e: - raise UserError(_('删除过程中发生错误: %s') % str(e)) - + raise UserError(_('删除过程中发生错误: %s') % str(e)) @depends('product_id') def _compute_material_name(self):