Skip to content

Commit

Permalink
[ADD] l10n_ar_afipws_fe: add qr code
Browse files Browse the repository at this point in the history
  • Loading branch information
zaoral committed Feb 3, 2021
1 parent b9a183a commit 41d9ff2
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions l10n_ar_afipws_fe/models/invoice.py
Expand Up @@ -5,14 +5,20 @@
from .pyi25 import PyI25
from odoo import fields, models, api, _
from odoo.exceptions import UserError
from odoo.tools import float_repr
import base64
from io import BytesIO
import logging
import json
import sys
import traceback
from datetime import datetime
_logger = logging.getLogger(__name__)

try:
import qrcode
except ImportError:
qrcode = None
try:
from pysimplesoap.client import SoapFault
except ImportError:
Expand Down Expand Up @@ -71,14 +77,13 @@ class AccountInvoice(models.Model):
afip_cae_due = fields.Date(
related='afip_auth_code_due'
)

afip_barcode = fields.Char(
compute='_compute_barcode',
string='AFIP Barcode'
afip_qr_code = fields.Char(
compute='_compute_qr_code',
string='AFIP QR code'
)
afip_barcode_img = fields.Binary(
compute='_compute_barcode',
string='AFIP Barcode Image'
afip_qr_code_img = fields.Binary(
compute='_compute_qr_code',
string='AFIP QR code Image'
)
afip_message = fields.Text(
string='AFIP Message',
Expand Down Expand Up @@ -137,38 +142,44 @@ def _compute_validation_type(self):

@api.multi
@api.depends('afip_auth_code')
def _compute_barcode(self):
def _compute_qr_code(self):
for rec in self:
barcode = False
if rec.afip_auth_code:
cae_due = ''.join(
[c for c in str(
rec.afip_auth_code_due or '') if c.isdigit()])
barcode = ''.join(
[str(rec.company_id.cuit),
"%03d" % int(rec.document_type_id.code),
"%05d" % int(rec.journal_id.point_of_sale_number),
str(rec.afip_auth_code), cae_due])
barcode = barcode + rec.verification_digit_modulo10(barcode)
rec.afip_barcode = barcode
rec.afip_barcode_img = rec._make_image_I25(barcode)
qr_code = False
if rec.afip_auth_code and rec.afip_auth_mode in ['CAE', 'CAEA']:
data = {
'ver': 1,
'fecha': rec.date_invoice,
'cuit': int(rec.company_id.partner_id.cuit), # TODO ensure that the cuit is only digits
'ptoVta': rec.journal_id.point_of_sale_number,
'tipoCmp': int(rec.document_type_id.code),
'nroCmp': int(rec.invoice_number),
'importe': float(float_repr(rec.amount_total, precision_digits=2)),
'moneda': rec.currency_id.afip_code,
'ctz': float(float_repr(rec.currency_rate, precision_digits=6)),
'tipoCodAut': 'E' if rec.afip_auth_mode == 'CAE' else 'A',
'codAut': int(rec.afip_auth_code),
}
if rec.commercial_partner_id.main_id_number:
data.update({'nroDocRec': int(rec.commercial_partner_id.main_id_number),
'tipoDocRec': rec.commercial_partner_id.main_id_category_id.afip_code})
qr_code = 'https://www.afip.gob.ar/fe/qr/?p=%s' % base64.encodestring(json.dumps(
data, indent=None).encode('ascii')).decode('ascii')

rec.afip_qr_code = qr_code
rec.afip_qr_code_img = rec._make_image_QR(qr_code)

@api.model
def _make_image_I25(self, barcode):
"Generate the required barcode Interleaved of 7 image using PIL"
def _make_image_QR(self, qr_code):
""" Generate the required QR code """
image = False
if barcode:
# create the helper:
pyi25 = PyI25()
if qr_code:
qr_obj = qrcode.QRCode()
output = BytesIO()
# call the helper:
bars = ''.join([c for c in barcode if c.isdigit()])
if not bars:
bars = "00"
pyi25.GenerarImagen(bars, output, extension="PNG")
# get the result and encode it for openerp binary field:
qr_obj.add_data(qr_code)
qr_obj.make(fit=True)
qr_img = qr_obj.make_image()
qr_img.save(output)
image = base64.b64encode(output.getvalue())
output.close()
return image

@api.model
Expand Down

0 comments on commit 41d9ff2

Please sign in to comment.