新增货架一键打印所有货位条码和单个打印货位条码

This commit is contained in:
jinling.yang
2024-04-17 16:00:47 +08:00
parent 38c89a4167
commit 19b062f5ac
2 changed files with 151 additions and 49 deletions

View File

@@ -222,11 +222,13 @@ class SfLocation(models.Model):
class SfShelf(models.Model):
_name = 'sf.shelf'
_inherit = ['printing.utils']
_description = '货架'
_order = 'create_date desc'
name = fields.Char('货架名称', required=True, size=20)
barcode = fields.Char('编码', copy=False, size=15, required=True)
# 货位
location_ids = fields.One2many('sf.shelf.location', 'shelf_id', string='货位')
@@ -300,9 +302,38 @@ class SfShelf(models.Model):
j_str = str(j + 1).zfill(3) # 确保是两位数如果不足两位左侧补0
return area_type_barcode + self.channel + self.direction + '-' + self.barcode + '-' + i_str + '-' + j_str
def print_all_location_barcode(self):
"""
打印所有货位编码
"""
print('=======打印货架所有货位编码=========')
for record in self.location_ids:
print('record', record)
if not record.barcode:
continue
record.ensure_one()
# qr_code_data = record.lot_qr_code
# if not qr_code_data:
# raise UserError("没有找到二维码数据。")
barcode = record.barcode
# todo 待控制
if not barcode:
raise ValidationError("请先分配序列号")
# host = "192.168.50.110" # 可以根据实际情况修改
# port = 9100 # 可以根据实际情况修改
# 获取默认打印机配置
printer_config = self.env['printer.configuration'].sudo().search([('model', '=', self._name)], limit=1)
if not printer_config:
raise UserError('请先配置打印机')
host = printer_config.printer_id.ip_address
port = printer_config.printer_id.port
self.print_qr_code(barcode, host, port)
class ShelfLocation(models.Model):
_name = 'sf.shelf.location'
_inherit = ['printing.utils']
_description = '货位'
_order = 'id asc, create_date asc'
@@ -326,6 +357,8 @@ class ShelfLocation(models.Model):
name = fields.Char('货位名称', required=True, size=20)
barcode = fields.Char('货位编码', copy=False, size=50)
qr_code = fields.Binary(string='二维码', compute='_compute_location_qr_code', store=True)
# 货架
shelf_id = fields.Many2one('sf.shelf', string='货架')
@@ -337,6 +370,63 @@ class ShelfLocation(models.Model):
def action_check(self):
self.check_state = 'enable'
@api.depends('barcode')
def _compute_location_qr_code(self):
for record in self:
if record.barcode:
# 创建一个QRCode对象
qr = qrcode.QRCode(
version=1, # 设置版本, 1-40控制二维码的大小
error_correction=qrcode.constants.ERROR_CORRECT_L, # 设置错误校正等级
box_size=10, # 设置每个格子的像素大小
border=4, # 设置边框的格子宽度
)
# 添加数据
qr.add_data(record.barcode)
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.qr_code = data
else:
record.qr_code = False
def print_single_location_qr_code(self):
self.ensure_one()
qr_code_data = self.qr_code
if not qr_code_data:
raise UserError("没有找到二维码数据。")
barcode = self.barcode
# host = "192.168.50.110" # 可以根据实际情况修改
# port = 9100 # 可以根据实际情况修改
# 获取默认打印机配置
printer_config = self.env['printer.configuration'].sudo().search([('model', '=', self._name)], limit=1)
if not printer_config:
raise UserError('请先配置打印机')
host = printer_config.printer_id.ip_address
port = printer_config.printer_id.port
self.print_qr_code(barcode, host, port)
# 获取当前wizard的视图ID或其他标识信息
view_id = self.env.context.get('view_id')
# 构造返回wizard页面的action字典
action = {
'type': 'ir.actions.act_window',
'name': '返回 Wizard',
'res_model': 'sf.shelf', # 替换为你的wizard模型名称
'view_mode': 'form',
'view_id': view_id, # 如果需要基于特定的视图返回
'target': 'new', # 如果需要在新的窗口或标签页打开
'res_id': self.shelf_id, # 如果你想要返回当前记录的视图
}
return action
# # 仓库类别selection库区、库位、货位
# location_type = fields.Selection([
# ('货架', '货架'),
@@ -446,7 +536,7 @@ class Sf_stock_move_line(models.Model):
raise UserError(_('抱歉,只有库管人员可以执行此动作'))
# 如果用户有权限,调用父类方法
return super(CustomStockMoveLine, self).action_revert_inventory()
return super().action_revert_inventory()
@api.depends('lot_name')
def _compute_lot_qr_code(self):