分配序列号时增加二维码生成及编码打印
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
import logging
|
||||
import base64
|
||||
import qrcode
|
||||
import io
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.osv import expression
|
||||
from odoo.exceptions import UserError
|
||||
@@ -375,6 +378,112 @@ class Sf_stock_move_line(models.Model):
|
||||
# location_dest_id = fields.Many2one('stock.location', string='目标库位')
|
||||
location_dest_id_product_type = fields.Many2many(related='location_dest_id.product_type')
|
||||
location_dest_id_value = fields.Integer(compute='_compute_location_dest_id_value', store=True)
|
||||
# lot_qr_code = fields.Binary(string='二维码', compute='_compute_lot_qr_code', store=True)
|
||||
lot_qr_code = fields.Binary(string='二维码', compute='_compute_lot_qr_code', store=True)
|
||||
|
||||
@api.depends('lot_name')
|
||||
def _compute_lot_qr_code(self):
|
||||
for record in self:
|
||||
if record.lot_id:
|
||||
# record.lot_qr_code = record.lot_id.lot_qr_code
|
||||
# 创建一个QRCode对象
|
||||
qr = qrcode.QRCode(
|
||||
version=1, # 设置版本, 1-40,控制二维码的大小
|
||||
error_correction=qrcode.constants.ERROR_CORRECT_L, # 设置错误校正等级
|
||||
box_size=10, # 设置每个格子的像素大小
|
||||
border=4, # 设置边框的格子宽度
|
||||
)
|
||||
|
||||
# 添加数据
|
||||
qr.add_data(self.lot_id.name)
|
||||
qr.make(fit=True)
|
||||
|
||||
# 创建二维码图像
|
||||
img = qr.make_image(fill_color="black", back_color="white")
|
||||
|
||||
# 创建一个内存文件
|
||||
buffer = io.BytesIO()
|
||||
img.save(buffer, format="PNG") # 将图像保存到内存文件中
|
||||
|
||||
# 获取二进制数据
|
||||
binary_data = buffer.getvalue()
|
||||
|
||||
# 使用Base64编码这些二进制数据
|
||||
data = base64.b64encode(binary_data)
|
||||
self.lot_qr_code = data
|
||||
else:
|
||||
record.lot_qr_code = False
|
||||
|
||||
def print_qr_code(self):
|
||||
self.ensure_one() # 确保这个方法只为一个记录调用
|
||||
# if not self.lot_id:
|
||||
# raise UserError("没有找到序列号。")
|
||||
# 假设_lot_qr_code方法已经生成了二维码并保存在字段中
|
||||
qr_code_data = self.lot_qr_code
|
||||
if not qr_code_data:
|
||||
raise UserError("没有找到二维码数据。")
|
||||
|
||||
# 生成下载链接或直接触发下载
|
||||
# 此处的实现依赖于你的具体需求,以下是触发下载的一种示例
|
||||
attachment = self.env['ir.attachment'].sudo().create({
|
||||
'datas': self.lot_qr_code,
|
||||
'type': 'binary',
|
||||
'description': '二维码图片',
|
||||
'name': self.lot_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',
|
||||
}
|
||||
|
||||
# # # 定义一个方法,用于根据序列号生成二维码
|
||||
# # @api.depends('lot_id')
|
||||
# def generate_lot_qr_code(self):
|
||||
# # 创建一个QRCode对象
|
||||
# qr = qrcode.QRCode(
|
||||
# version=1, # 设置版本, 1-40,控制二维码的大小
|
||||
# error_correction=qrcode.constants.ERROR_CORRECT_L, # 设置错误校正等级
|
||||
# box_size=10, # 设置每个格子的像素大小
|
||||
# border=4, # 设置边框的格子宽度
|
||||
# )
|
||||
#
|
||||
# # 添加数据
|
||||
# qr.add_data(self.lot_id.name)
|
||||
# qr.make(fit=True)
|
||||
#
|
||||
# # 创建二维码图像
|
||||
# img = qr.make_image(fill_color="black", back_color="white")
|
||||
#
|
||||
# # 创建一个内存文件
|
||||
# buffer = io.BytesIO()
|
||||
# img.save(buffer, format="PNG") # 将图像保存到内存文件中
|
||||
#
|
||||
# # 获取二进制数据
|
||||
# binary_data = buffer.getvalue()
|
||||
#
|
||||
# # 使用Base64编码这些二进制数据
|
||||
# data = base64.b64encode(binary_data)
|
||||
# self.lot_qr_code = data
|
||||
# attachment = self.env['ir.attachment'].sudo().create({
|
||||
# 'datas': data,
|
||||
# 'type': 'binary',
|
||||
# 'description': '二维码图片',
|
||||
# 'name': self.lot_id.name + '.png',
|
||||
# # 'res_id': invoice.id,
|
||||
# # 'res_model': 'stock.picking',
|
||||
# 'public': True,
|
||||
# 'mimetype': 'application/pdf',
|
||||
# # 'model_name': 'stock.picking',
|
||||
# })
|
||||
|
||||
# def button_test(self):
|
||||
# print(self.picking_id.name)
|
||||
@@ -652,3 +761,35 @@ class SfStockScrap(models.Model):
|
||||
|
||||
def action_check(self):
|
||||
self.check_state = 'enable'
|
||||
|
||||
|
||||
class CustomStockMove(models.Model):
|
||||
_inherit = 'stock.move'
|
||||
|
||||
def action_assign_serial_show_details(self):
|
||||
# 首先执行原有逻辑
|
||||
result = super(CustomStockMove, self).action_assign_serial_show_details()
|
||||
# 接着为每个 lot_name 生成二维码
|
||||
move_lines = self.move_line_ids # 获取当前 stock.move 对应的所有 stock.move.line 记录
|
||||
for line in move_lines:
|
||||
if line.lot_name: # 确保 lot_name 存在
|
||||
qr_data = self.compute_lot_qr_code(line.lot_name)
|
||||
# 假设 stock.move.line 模型中有一个字段叫做 lot_qr_code 用于存储二维码数据
|
||||
line.lot_qr_code = qr_data
|
||||
return result
|
||||
|
||||
def compute_lot_qr_code(self, lot_name):
|
||||
qr = qrcode.QRCode(
|
||||
version=1,
|
||||
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
||||
box_size=10,
|
||||
border=4,
|
||||
)
|
||||
qr.add_data(lot_name)
|
||||
qr.make(fit=True)
|
||||
img = qr.make_image(fill_color="black", back_color="white")
|
||||
buffer = io.BytesIO()
|
||||
img.save(buffer, format="PNG")
|
||||
binary_data = buffer.getvalue()
|
||||
data = base64.b64encode(binary_data).decode() # 确保返回的是字符串形式的数据
|
||||
return data
|
||||
|
||||
@@ -51,6 +51,22 @@
|
||||
</xpath>
|
||||
<xpath expr="//form//sheet//group//group//field[@name='location_dest_id']" position="after">
|
||||
<field name="destination_location_id" options="{'no_create': False}"/>
|
||||
|
||||
</xpath>
|
||||
<xpath expr="//form//sheet//group//group//field[@name='create_uid']" position="after">
|
||||
<field name="lot_qr_code" widget="image"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="sf_view_stock_move_line_operation_tree" model="ir.ui.view">
|
||||
<field name="name">sf.stock.move.line.operation.tree</field>
|
||||
<field name="model">stock.move.line</field>
|
||||
<field name="inherit_id" ref="stock.view_stock_move_line_operation_tree"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='product_uom_id']" position="after">
|
||||
<field name="lot_qr_code" widget="image"/>
|
||||
<button name="print_qr_code" string="打印编码" type="object" class="oe_highlight"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
Reference in New Issue
Block a user