1、序列号模型添加二维码字段,添加自动根据序列号名称生成二维码的功能,新增打印二维码功能;

This commit is contained in:
yuxianghui
2024-02-20 15:59:58 +08:00
parent bb519b8ab8
commit cf3ec1b6ae
5 changed files with 75 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import base64
import qrcode
from collections import defaultdict, namedtuple
import logging
import json
@@ -12,6 +13,7 @@ from odoo.tools import float_compare
from odoo.addons.stock.models.stock_rule import ProcurementException
from odoo.addons.sf_base.commons.common import Common
from odoo.exceptions import UserError
from io import BytesIO
class StockRule(models.Model):
@@ -264,6 +266,60 @@ class ProductionLot(models.Model):
return "%s-%s-%03d" % (product.cutting_tool_model_id.code, now, 1)
return "%s-%03d" % (product.name, 1)
qr_code_image = fields.Binary(string='二维码', compute='_generate_qr_code')
@api.depends('name')
def _generate_qr_code(self):
for record in self:
# Generate QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(record.name)
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
# Encode the image data in base64
image_stream = BytesIO()
qr_image.save(image_stream, format="PNG")
encoded_image = base64.b64encode(image_stream.getvalue())
record.qr_code_image = encoded_image
def print_qr_code(self):
self.ensure_one() # 确保这个方法只为一个记录调用
# if not self.lot_id:
# raise UserError("没有找到序列号。")
# 假设_lot_qr_code方法已经生成了二维码并保存在字段中
qr_code_data = self.qr_code_image
if not qr_code_data:
raise UserError("没有找到二维码数据。")
# 生成下载链接或直接触发下载
# 此处的实现依赖于你的具体需求,以下是触发下载的一种示例
attachment = self.env['ir.attachment'].sudo().create({
'datas': self.qr_code_image,
'type': 'binary',
'description': '二维码图片',
'name': self.name + '.png',
# 'res_id': invoice.id,
# 'res_model': 'stock.picking',
'public': True,
'mimetype': 'application/x-png',
# 'model_name': 'stock.picking',
})
# 返回附件的下载链接
download_url = '/web/content/%s?download=true' % attachment.id
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
return {
'type': 'ir.actions.act_url',
'url': str(base_url) + download_url,
'target': 'self',
}
class StockPicking(models.Model):
_inherit = 'stock.picking'