From 19b062f5ac5e87b43902f33af2fbe16d3b95dcc5 Mon Sep 17 00:00:00 2001 From: "jinling.yang" Date: Wed, 17 Apr 2024 16:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B4=A7=E6=9E=B6=E4=B8=80?= =?UTF-8?q?=E9=94=AE=E6=89=93=E5=8D=B0=E6=89=80=E6=9C=89=E8=B4=A7=E4=BD=8D?= =?UTF-8?q?=E6=9D=A1=E7=A0=81=E5=92=8C=E5=8D=95=E4=B8=AA=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E8=B4=A7=E4=BD=8D=E6=9D=A1=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sf_warehouse/models/model.py | 92 +++++++++++++++++++++- sf_warehouse/views/shelf_location.xml | 108 ++++++++++++++------------ 2 files changed, 151 insertions(+), 49 deletions(-) diff --git a/sf_warehouse/models/model.py b/sf_warehouse/models/model.py index 37030471..e95a902a 100644 --- a/sf_warehouse/models/model.py +++ b/sf_warehouse/models/model.py @@ -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): diff --git a/sf_warehouse/views/shelf_location.xml b/sf_warehouse/views/shelf_location.xml index 9cd4446a..cae72684 100644 --- a/sf_warehouse/views/shelf_location.xml +++ b/sf_warehouse/views/shelf_location.xml @@ -1,7 +1,7 @@ - + Sf Shelf sf.shelf @@ -9,7 +9,8 @@
-
@@ -23,12 +24,21 @@ - - - - - - + + +