1、序列号模型添加二维码字段,添加自动根据序列号名称生成二维码的功能,新增打印二维码功能;
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
'security/ir.model.access.csv',
|
'security/ir.model.access.csv',
|
||||||
'wizard/workpiece_delivery_views.xml',
|
'wizard/workpiece_delivery_views.xml',
|
||||||
'views/mrp_views_menus.xml',
|
'views/mrp_views_menus.xml',
|
||||||
|
'views/stock_lot_views.xml',
|
||||||
'views/mrp_production_addional_change.xml',
|
'views/mrp_production_addional_change.xml',
|
||||||
'views/mrp_routing_workcenter_view.xml',
|
'views/mrp_routing_workcenter_view.xml',
|
||||||
'views/production_line_view.xml',
|
'views/production_line_view.xml',
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import base64
|
import base64
|
||||||
|
import qrcode
|
||||||
from collections import defaultdict, namedtuple
|
from collections import defaultdict, namedtuple
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
@@ -12,6 +13,7 @@ from odoo.tools import float_compare
|
|||||||
from odoo.addons.stock.models.stock_rule import ProcurementException
|
from odoo.addons.stock.models.stock_rule import ProcurementException
|
||||||
from odoo.addons.sf_base.commons.common import Common
|
from odoo.addons.sf_base.commons.common import Common
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
|
||||||
class StockRule(models.Model):
|
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-%s-%03d" % (product.cutting_tool_model_id.code, now, 1)
|
||||||
return "%s-%03d" % (product.name, 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):
|
class StockPicking(models.Model):
|
||||||
_inherit = 'stock.picking'
|
_inherit = 'stock.picking'
|
||||||
|
|||||||
18
sf_manufacturing/views/stock_lot_views.xml
Normal file
18
sf_manufacturing/views/stock_lot_views.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<odoo>
|
||||||
|
<record id="stock_production_lot_form_generate_qr_code" model="ir.ui.view">
|
||||||
|
<field name="name">stock.lot.form.quality</field>
|
||||||
|
<field name="model">stock.lot</field>
|
||||||
|
<field name="inherit_id" ref="stock.view_production_lot_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<xpath expr="//form//sheet//group//group[2]" position="inside">
|
||||||
|
<field name="qr_code_image" widget="image"/>
|
||||||
|
</xpath>
|
||||||
|
<xpath expr="//sheet" position="before">
|
||||||
|
<header>
|
||||||
|
<button string="打印二维码" name="print_qr_code" type="object" class="btn-primary"/>
|
||||||
|
</header>
|
||||||
|
</xpath>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
</odoo>
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
import qrcode
|
|
||||||
import base64
|
|
||||||
from odoo import models, api, fields
|
from odoo import models, api, fields
|
||||||
from odoo.exceptions import ValidationError
|
from odoo.exceptions import ValidationError
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
|
|
||||||
class SfMaintenanceEquipmentTool(models.Model):
|
class SfMaintenanceEquipmentTool(models.Model):
|
||||||
@@ -82,34 +79,6 @@ class StockLot(models.Model):
|
|||||||
record.tool_material_search_id = tool_material_search
|
record.tool_material_search_id = tool_material_search
|
||||||
return records
|
return records
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
# Create an image from the QR Code instance
|
|
||||||
qr_image = qr.make_image(fill_color="black", back_color="white")
|
|
||||||
|
|
||||||
# Save the image to a BytesIO object
|
|
||||||
image_stream = BytesIO()
|
|
||||||
qr_image.save(image_stream, format="PNG")
|
|
||||||
|
|
||||||
# Store the BytesIO object in the binary field
|
|
||||||
record.qr_code_image = base64.b64decode(image_stream.getvalue())
|
|
||||||
# record.qr_code_image = image_stream.getvalue()
|
|
||||||
|
|
||||||
print(record.qr_code_image)
|
|
||||||
|
|
||||||
|
|
||||||
class ProductProduct(models.Model):
|
class ProductProduct(models.Model):
|
||||||
_inherit = 'product.product'
|
_inherit = 'product.product'
|
||||||
|
|||||||
@@ -59,7 +59,6 @@
|
|||||||
<tree>
|
<tree>
|
||||||
<field name="name"/>
|
<field name="name"/>
|
||||||
<field name="tool_material_status"/>
|
<field name="tool_material_status"/>
|
||||||
<field name="qr_code_image" widget="image"/>
|
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
</page>
|
</page>
|
||||||
|
|||||||
Reference in New Issue
Block a user