Skip to content

Commit

Permalink
[FIX] web, l10n_ch: Error Correction Level QR Bill
Browse files Browse the repository at this point in the history
l10n_ch QR bill report needs level 'M' (15% red.)
instead of default level 'L' (7% red.) as per:

https://en.wikipedia.org/wiki/QR_code#Error_correction

See specifications at:

https://www.paymentstandards.ch/dam/downloads/ig-qr-bill-en.pdf

The layout of higher level of Error Correction
are made of a thicker grid with more squares
(more information) at the cost of them being smaller
(a little harder to be read by the scanner).

opw-2584899

closes #82932

X-original-commit: 0f374b6
Signed-off-by: William André (wan) <wan@odoo.com>
Signed-off-by: Paolo Gatti (pgi) <pgi@odoo.com>
  • Loading branch information
lordkrandel committed Feb 4, 2022
1 parent 9a9d61f commit ee324e8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 2 additions & 0 deletions addons/l10n_ch/models/res_bank.py
Expand Up @@ -307,6 +307,8 @@ def _get_qr_code_generation_params(self, qr_method, amount, currency, debtor_par
'quiet': 1,
'mask': 'ch_cross',
'value': '\n'.join(self._get_qr_vals(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)),
# Swiss QR code requires Error Correction Level = 'M' by specification
'barLevel': 'M',
}
return super()._get_qr_code_generation_params(qr_method, amount, currency, debtor_partner, free_communication, structured_communication)

Expand Down
1 change: 1 addition & 0 deletions addons/l10n_ch/tests/test_swissqr.py
Expand Up @@ -147,6 +147,7 @@ def swissqr_generated(self, invoice, ref_type='NON'):

expected_params = {
'barcode_type': 'QR',
'barLevel': 'M',
'width': 256,
'height': 256,
'quiet': 1,
Expand Down
22 changes: 13 additions & 9 deletions addons/web/controllers/main.py
Expand Up @@ -1975,28 +1975,32 @@ def report_routes(self, reportname, docids=None, converter=None, **data):
#------------------------------------------------------
# Misc. route utils
#------------------------------------------------------
@http.route(['/report/barcode', '/report/barcode/<type>/<path:value>'], type='http', auth="public")
def report_barcode(self, type, value, width=600, height=100, humanreadable=0, quiet=1, mask=None):
@http.route(['/report/barcode', '/report/barcode/<barcode_type>/<path:value>'], type='http', auth="public")
def report_barcode(self, barcode_type, value, **kwargs):
"""Contoller able to render barcode images thanks to reportlab.
Samples:
Samples::
<img t-att-src="'/report/barcode/QR/%s' % o.name"/>
<img t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' %
<img t-att-src="'/report/barcode/?barcode_type=%s&amp;value=%s&amp;width=%s&amp;height=%s' %
('QR', o.name, 200, 200)"/>
:param type: Accepted types: 'Codabar', 'Code11', 'Code128', 'EAN13', 'EAN8', 'Extended39',
'Extended93', 'FIM', 'I2of5', 'MSI', 'POSTNET', 'QR', 'Standard39', 'Standard93',
'UPCA', 'USPS_4State'
:param barcode_type: Accepted types: 'Codabar', 'Code11', 'Code128', 'EAN13', 'EAN8',
'Extended39', 'Extended93', 'FIM', 'I2of5', 'MSI', 'POSTNET', 'QR', 'Standard39',
'Standard93', 'UPCA', 'USPS_4State'
:param width: Pixel width of the barcode
:param height: Pixel height of the barcode
:param humanreadable: Accepted values: 0 (default) or 1. 1 will insert the readable value
at the bottom of the output image
:param quiet: Accepted values: 0 (default) or 1. 1 will display white
margins on left and right.
:param mask: The mask code to be used when rendering this QR-code.
Masks allow adding elements on top of the generated image,
such as the Swiss cross in the center of QR-bill codes.
:param barLevel: QR code Error Correction Levels. Default is 'L'.
ref: https://hg.reportlab.com/hg-public/reportlab/file/830157489e00/src/reportlab/graphics/barcode/qr.py#l101
"""
try:
barcode = request.env['ir.actions.report'].barcode(type, value, width=width,
height=height, humanreadable=humanreadable, quiet=quiet, mask=mask)
barcode = request.env['ir.actions.report'].barcode(barcode_type, value, **kwargs)
except (ValueError, AttributeError):
raise werkzeug.exceptions.HTTPException(description='Cannot convert into barcode.')

Expand Down
45 changes: 30 additions & 15 deletions odoo/addons/base/models/ir_actions_report.py
Expand Up @@ -533,7 +533,25 @@ def _get_report_from_name(self, report_name):
return report_obj.with_context(context).sudo().search(conditions, limit=1)

@api.model
def barcode(self, barcode_type, value, width=600, height=100, humanreadable=0, quiet=1, mask=None):
def barcode(self, barcode_type, value, **kwargs):
defaults = {
'width': (600, int),
'height': (100, int),
'humanreadable': (False, lambda x: bool(int(x))),
'quiet': (True, lambda x: bool(int(x))),
'mask': (None, lambda x: x),
'barBorder': (4, int),
# The QR code can have different layouts depending on the Error Correction Level
# See: https://en.wikipedia.org/wiki/QR_code#Error_correction
# Level 'L' – up to 7% damage (default)
# Level 'M' – up to 15% damage (i.e. required by l10n_ch QR bill)
# Level 'Q' – up to 25% damage
# Level 'H' – up to 30% damage
'barLevel': ('L', lambda x: x in ('L', 'M', 'Q', 'H') and x or 'L'),
}
kwargs = {k: validator(kwargs.get(k, v)) for k, (v, validator) in defaults.items()}
kwargs['humanReadable'] = kwargs.pop('humanreadable')

if barcode_type == 'UPCA' and len(value) in (11, 12, 13):
barcode_type = 'EAN13'
if len(value) in (11, 12):
Expand All @@ -544,34 +562,31 @@ def barcode(self, barcode_type, value, width=600, height=100, humanreadable=0, q
elif barcode_type == 'DataMatrix' and not self.datamatrix_available():
# fallback to avoid stacktrack because reportlab won't recognize the type and error message isn't useful/will be blocking
barcode_type = 'Code128'
try:
width, height, humanreadable, quiet = int(width), int(height), bool(int(humanreadable)), bool(int(quiet))
elif barcode_type == 'QR':
# for `QR` type, `quiet` is not supported. And is simply ignored.
# But we can use `barBorder` to get a similar behaviour.
bar_border = 4
if barcode_type == 'QR' and quiet:
bar_border = 0
if kwargs['quiet']:
kwargs['barBorder'] = 0

barcode = createBarcodeDrawing(
barcode_type, value=value, format='png', width=width, height=height,
humanReadable=humanreadable, quiet=quiet, barBorder=bar_border
)
try:
barcode = createBarcodeDrawing(barcode_type, value=value, format='png', **kwargs)

# If a mask is asked and it is available, call its function to
# post-process the generated QR-code image
if mask:
if kwargs['mask']:
available_masks = self.get_available_barcode_masks()
mask_to_apply = available_masks.get(mask)
mask_to_apply = available_masks.get(kwargs['mask'])
if mask_to_apply:
mask_to_apply(width, height, barcode)
mask_to_apply(kwargs['width'], kwargs['height'], barcode)

return barcode.asString('png')
except (ValueError, AttributeError):
if barcode_type == 'Code128':
raise ValueError("Cannot convert into barcode.")
elif barcode_type == 'QR':
raise ValueError("Cannot convert into QR code.")
else:
return self.barcode('Code128', value, width=width, height=height,
humanreadable=humanreadable, quiet=quiet)
return self.barcode('Code128', value, **kwargs)

@api.model
def get_available_barcode_masks(self):
Expand Down

0 comments on commit ee324e8

Please sign in to comment.