forked from OCA/l10n-spain
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Primera version QR Verifactu (#62)
- Loading branch information
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,12 @@ | |
# Copyright 2024 Aures TIC - Almudena de La Puente <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
import base64 | ||
import io | ||
import json | ||
import logging | ||
from hashlib import sha256 | ||
from urllib.parse import urlencode | ||
|
||
from requests import Session | ||
|
||
|
@@ -15,6 +18,14 @@ | |
|
||
from odoo.addons.l10n_es_aeat.models.aeat_mixin import round_by_keys | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import qrcode | ||
except (ImportError, IOError) as err: | ||
qrcode = None | ||
_logger.error(err) | ||
|
||
########################################### | ||
# revisar los imports que no hagan falta | ||
# cuando funcione bien el _connect_aeat sin tener que poner | ||
|
@@ -85,6 +96,8 @@ class VerifactuMixin(models.AbstractModel): | |
readonly=True, | ||
string="Verifactu Code", | ||
) | ||
verifactu_qr_url = fields.Char("URL", compute="_compute_verifactu_qr_url") | ||
verifactu_qr = fields.Binary(string="QR", compute="_compute_verifactu_qr") | ||
|
||
def _compute_verifactu_enabled(self): | ||
raise NotImplementedError | ||
|
@@ -100,6 +113,56 @@ def _compute_verifactu_macrodata(self): | |
>= 0 | ||
) | ||
|
||
def _compute_verifactu_qr_url(self): | ||
"""Returns the URL to be used in the QR code. A sample URL would be (urlencoded): | ||
https://prewww2.aeat.es/wlpl/TIKECONT/ValidarQR?nif=89890001K&numserie=12345678%26G33&fecha=01-01-2024&importe=241.4 | ||
""" | ||
for record in self: | ||
agency = self.env.ref("l10n_es_aeat.aeat_tax_agency_spain") | ||
if record.company_id.verifactu_test: | ||
qr_base_url = agency.verifactu_qr_base_url_test_address | ||
else: | ||
qr_base_url = agency.verifactu_qr_base_url | ||
|
||
qr_values = record._get_verifactu_qr_values() | ||
|
||
# Check all values are ASCII between 32 and 126 | ||
for value in qr_values.values(): | ||
try: | ||
str(value).encode("ascii") | ||
except UnicodeEncodeError: | ||
raise UserError(_("QR URL value '{}' is not ASCII").format(value)) | ||
|
||
# Build QR URL | ||
qr_url = "{}?{}".format( | ||
qr_base_url, | ||
urlencode(qr_values, encoding="utf-8"), | ||
) | ||
|
||
record.verifactu_qr_url = qr_url | ||
|
||
def _compute_verifactu_qr(self): | ||
# If qrcode module is not available, we can't generate QR codes | ||
if not qrcode: | ||
_logger.error("qrcode module is not available") | ||
return | ||
for record in self: | ||
if record.state != "posted" or not record.verifactu_enabled: | ||
record.verifactu_qr = False | ||
continue | ||
qr = qrcode.QRCode( | ||
border=0, error_correction=qrcode.constants.ERROR_CORRECT_M | ||
) | ||
qr.add_data(record.verifactu_qr_url) | ||
qr.make() | ||
img = qr.make_image() | ||
with io.BytesIO() as temp: | ||
img.save(temp, format="PNG") | ||
record.verifactu_qr = base64.b64encode(temp.getvalue()) | ||
|
||
def _get_verifactu_qr_values(self): | ||
raise NotImplementedError | ||
|
||
@api.model | ||
def _get_verifactu_tax_keys(self): | ||
return self.env["account.fiscal.position"]._get_verifactu_tax_keys() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Almudena de La Puente <[email protected]> | ||
* Laura Cazorla <[email protected]> | ||
* Andreu Orensanz <[email protected]> | ||
* Iván Antón <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<odoo> | ||
<template id="report_invoice_document" inherit_id="account.report_invoice_document" | ||
priority="1000"> | ||
<div class="page" position="inside"> | ||
<div id="verifactu" style="padding-top:50px;page-break-inside:avoid;" | ||
class="text-center" t-if="o.verifactu_enabled and o.verifactu_qr"> | ||
<ul class="list-inline mb4">QR Tributario</ul> | ||
<ul id="verifactu_qr" class="list-inline mb4"> | ||
<img t-attf-src="data:image/png;base64,{{o.verifactu_qr}}" | ||
style="min-width: 30mm; max-width: 40mm" /> | ||
</ul> | ||
<ul class="list-inline mb4">VERI*FACTU</ul> | ||
</div> | ||
</div> | ||
</template> | ||
</odoo> |