product_print_zpl_barcode: fix warning and improve code
This commit is contained in:
@@ -52,10 +52,10 @@ class ProductProduct(models.Model):
|
|||||||
# Not useful for ZPL, but it is often useful to have a barcode image field
|
# Not useful for ZPL, but it is often useful to have a barcode image field
|
||||||
barcode_image_png = fields.Binary(
|
barcode_image_png = fields.Binary(
|
||||||
compute='_compute_barcode_image_png',
|
compute='_compute_barcode_image_png',
|
||||||
string='Barcode Image')
|
string='PNG Barcode Image')
|
||||||
barcode_image_svg = fields.Binary(
|
barcode_image_svg = fields.Binary(
|
||||||
compute='_compute_barcode_image_svg',
|
compute='_compute_barcode_image_svg',
|
||||||
string='Barcode Image')
|
string='SVG Barcode Image')
|
||||||
|
|
||||||
def _get_barcode_image(self, img_format):
|
def _get_barcode_image(self, img_format):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
from odoo import api, fields, models, _
|
from odoo import api, fields, models, _
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
from odoo.tools import float_compare, float_is_zero
|
from odoo.tools import float_compare, float_is_zero
|
||||||
from stdnum.ean import is_valid
|
from stdnum.ean import is_valid, calc_check_digit
|
||||||
import base64
|
import base64
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@@ -55,8 +55,7 @@ class ProductPrintZplBarcode(models.TransientModel):
|
|||||||
|
|
||||||
product_id = fields.Many2one(
|
product_id = fields.Many2one(
|
||||||
'product.product', string='Product', required=True, readonly=True)
|
'product.product', string='Product', required=True, readonly=True)
|
||||||
uom_id = fields.Many2one(
|
uom_id = fields.Many2one(related='product_id.uom_id')
|
||||||
related='product_id.uom_id', readonly=True)
|
|
||||||
# 1 line = un peu moins de 30
|
# 1 line = un peu moins de 30
|
||||||
product_name = fields.Char('Product Label', required=True, size=56)
|
product_name = fields.Char('Product Label', required=True, size=56)
|
||||||
nomenclature_id = fields.Many2one(
|
nomenclature_id = fields.Many2one(
|
||||||
@@ -64,15 +63,13 @@ class ProductPrintZplBarcode(models.TransientModel):
|
|||||||
rule_id = fields.Many2one(
|
rule_id = fields.Many2one(
|
||||||
'barcode.rule', string='Barcode Rule', readonly=True,
|
'barcode.rule', string='Barcode Rule', readonly=True,
|
||||||
compute='_compute_rule_id')
|
compute='_compute_rule_id')
|
||||||
barcode_type = fields.Selection(
|
barcode_type = fields.Selection(related='rule_id.type', string="Barcode Type")
|
||||||
related='rule_id.type', readonly=True, string="Barcode Type")
|
|
||||||
label_size = fields.Selection([
|
label_size = fields.Selection([
|
||||||
('38x25', '38x25 mm'),
|
('38x25', '38x25 mm'),
|
||||||
], required=True, default='38x25', string='Label Size')
|
], required=True, default='38x25')
|
||||||
pricelist_id = fields.Many2one(
|
pricelist_id = fields.Many2one(
|
||||||
'product.pricelist', string='Pricelist', required=True)
|
'product.pricelist', string='Pricelist', required=True)
|
||||||
currency_id = fields.Many2one(
|
currency_id = fields.Many2one(related='pricelist_id.currency_id')
|
||||||
related='pricelist_id.currency_id', readonly=True)
|
|
||||||
# TODO: for the moment, we only support weight, but...
|
# TODO: for the moment, we only support weight, but...
|
||||||
quantity = fields.Float(digits='Stock Weight')
|
quantity = fields.Float(digits='Stock Weight')
|
||||||
price_uom = fields.Monetary(
|
price_uom = fields.Monetary(
|
||||||
@@ -140,14 +137,14 @@ class ProductPrintZplBarcode(models.TransientModel):
|
|||||||
"of the barcode pattern (%s).")
|
"of the barcode pattern (%s).")
|
||||||
% (pbarcode, len(pbarcode), len(prefix), prefix))
|
% (pbarcode, len(pbarcode), len(prefix), prefix))
|
||||||
barcode = pbarcode[0:len(prefix)]
|
barcode = pbarcode[0:len(prefix)]
|
||||||
# print "barcode=", barcode
|
# print("barcode=", barcode)
|
||||||
# print "pattern=", pattern
|
# print("pattern=", pattern)
|
||||||
m = re.search('\{N+D+\}', pattern)
|
m = re.search('\{N+D+\}', pattern)
|
||||||
# print "m=", m
|
# print("m=", m)
|
||||||
assert m
|
assert m
|
||||||
pattern_val = m.group(0)
|
pattern_val = m.group(0)
|
||||||
pattern_val = pattern_val[1:-1]
|
pattern_val = pattern_val[1:-1]
|
||||||
# print "pattern_val=", pattern_val
|
# print("pattern_val=", pattern_val)
|
||||||
max_value = 10**pattern_val.count('N')
|
max_value = 10**pattern_val.count('N')
|
||||||
if float_compare(value, max_value, precision_digits=prec) != -1:
|
if float_compare(value, max_value, precision_digits=prec) != -1:
|
||||||
raise UserError(_(
|
raise UserError(_(
|
||||||
@@ -166,11 +163,15 @@ class ProductPrintZplBarcode(models.TransientModel):
|
|||||||
value_d_ext = value_d + '0' * pattern_val.count('D')
|
value_d_ext = value_d + '0' * pattern_val.count('D')
|
||||||
# 2) cut at the right size
|
# 2) cut at the right size
|
||||||
barcode += value_d_ext[0:pattern_val.count('D')]
|
barcode += value_d_ext[0:pattern_val.count('D')]
|
||||||
# print "barcode=", barcode
|
# print("barcode=", barcode)
|
||||||
# Add checksum
|
# Add checksum
|
||||||
if self.rule_id.encoding == 'ean13':
|
if self.rule_id.encoding == 'ean13':
|
||||||
barcode = bno.sanitize_ean(barcode)
|
# I don't call bno.sanitize_ean() due to this bug:
|
||||||
# print "barcode FINAL=", barcode
|
# https://github.com/odoo/odoo/pull/114112
|
||||||
|
barcode = barcode + calc_check_digit(barcode)
|
||||||
|
assert len(barcode) == 13
|
||||||
|
assert is_valid(barcode)
|
||||||
|
# print("barcode FINAL=", barcode)
|
||||||
zpl_unicode = self._price_weight_barcode_type_zpl() % {
|
zpl_unicode = self._price_weight_barcode_type_zpl() % {
|
||||||
'product_name': self.product_name,
|
'product_name': self.product_name,
|
||||||
'ean_zpl_command': len(self.barcode) == 8 and 'B8' or 'BE',
|
'ean_zpl_command': len(self.barcode) == 8 and 'B8' or 'BE',
|
||||||
|
|||||||
Reference in New Issue
Block a user