They provide me with an XML to download that information and convert it into a QR code that redirects to that information. For Odoo version 13, this code worked for me
from odoo import api, fields, models, tools, _
import io, base64, qrcode
from odoo.exceptions import UserError
import zipfile
from xml.dom.minidom import parseString
class QrCodeAccountMove(models.Model):
_inherit = 'account.move'
qr_code = fields.Binary("QR Code", attachment=True, store=True)
def l10n_co_edi_download_electronic_invoice(self):
invoice_download_msg, attachments = super(QrCodeAccountMove, self).l10n_co_edi_download_electronic_invoice()
if attachments:
with tools.osutil.tempdir() as file_dir:
with zipfile.ZipFile(io.BytesIO(attachments[0][1])) as zip_ref:
zip_ref.extractall(file_dir)
xml_file = [f for f in listdir(file_dir) if f.endswith('.xml')]
if xml_file:
content = parseString(zip_ref.read(xml_file[0]))
uuid_element = content.getElementsByTagName('cbc:UUID')
description_element = content.getElementsByTagName('cbc:Description')
if uuid_element:
self.l10n_co_edi_cufe_cude_ref = uuid_element[0].childNodes[0].nodeValue
if description_element:
desc = description_element[0].childNodes[0].nodeValue
parsed_desc = parseString(desc)
tag_xml = parsed_desc.getElementsByTagName('cbc:Note')
if tag_xml and self.move_type == 'in_invoice':
qr_txt = tag_xml[1].childNodes[0].nodeValue
qr_lst = qr_txt.split()
text = self.split_text(qr_lst, 2)
self.generate_qr_code(text)
if tag_xml and self.move_type not in ['in_refund', 'in_invoice']:
qr_txt = tag_xml[9].childNodes[0].nodeValue
qr_lst = qr_txt.split()
text = self.split_text(qr_lst, 2)
self.generate_qr_code(text)
return invoice_download_msg, attachments
def split_text(self, arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return '\n'.join([' '.join(chunk) for chunk in arrs])
def generate_qr_code(self, text):
qr = qrcode.QRCode(
version=4,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=4,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image()
temp = BytesIO()
img.save(temp, format="PNG")
qr_image = base64.b64encode(temp.getvalue())
self.qr_code = qr_image.decode('utf-8')
I've tried to do the same for version 16, but I believe some functions or code have changed because I'm not able to generate a QR code, despite making several modifications.
I would be very grateful if you could help me with this problem. Thank you very much.