Commit e66d2178 authored by agb80's avatar agb80

Fixed problems with not standard characters

As reported on #11 when generating an invoice with no ascii characters
on product description there where errors generating the XML file.
With this patch we create the XML file with proper encoding and
provent errors to appear.

Close #11
parent e6f14ff5
......@@ -571,11 +571,11 @@ class account_invoice(osv.Model):
node.appendChild(child)
else:
if isinstance(attribute, str) or isinstance(attribute, unicode):
# TODO: Remove conv_ascii function
attribute = conv_ascii(attribute)
else:
attribute = str(attribute)
attribute = str(attribute)
node.setAttribute(element, attribute)
# print "attribute",unicode( attribute, 'UTF-8')
if not parent:
doc.appendChild(node)
return doc
......
# -*- encoding: utf-8 -*-
#
###########################################################################
# Module Writen to OpenERP, Open Source Management Solution
#
# Copyright (c) 2011 Vauxoo - http://www.vauxoo.com
# All Rights Reserved.
# info Vauxoo (info@vauxoo.com)
# Authors: Vaouxoo (<http://www.vauxoo.com/>)
# Openpyme (<http://openpyme.mx>)
#
#
# Coded by: Luis Torres (luis_t260@vauxoo.com)
# Financed by: http://www.sfsoluciones.com (aef@sfsoluciones.com)
# Agustín Cruz Lozano (agustin.cruz@openpyme.mx)
#
# Fist version financed by: http://www.sfsoluciones.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
......@@ -20,10 +21,10 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You should have received a copy of the GNU Affero General Public Licens
#
##############################################################################
from openerp.osv import osv
import tempfile
import os
......@@ -137,7 +138,6 @@ class account_invoice(osv.Model):
else:
comprobante = 'Comprobante'
emisor = 'Emisor'
regimenFiscal = 'RegimenFiscal'
receptor = 'Receptor'
concepto = 'Conceptos'
......@@ -148,11 +148,12 @@ class account_invoice(osv.Model):
(fileno_xml, fname_xml) = tempfile.mkstemp(
'.xml', 'openerp_' + (invoice_number or '') + '__facturae__')
fname_txt = fname_xml + '.txt'
f = open(fname_xml, 'w')
doc_xml.writexml(
f, indent=' ', addindent=' ', newl='\r\n', encoding='UTF-8')
f.close()
os.close(fileno_xml)
# Write xml file with proper encoding
with codecs.open(fname_xml, 'w', encoding='UTF-8') as f:
doc_xml.writexml(
f, indent=' ', addindent=' ', newl='\r\n'
)
(fileno_sign, fname_sign) = tempfile.mkstemp('.txt', 'openerp_' + (
invoice_number or '') + '__facturae_txt_md5__')
......@@ -167,22 +168,31 @@ class account_invoice(osv.Model):
fname_txt, txt_str = self._xml2cad_orig(
cr=False, uid=False, ids=False, context=context)
data_dict['cadena_original'] = txt_str
msg2 = ''
if not txt_str:
raise osv.except_osv(_('Error in Original String!'), _(
"Can't get the string original of the voucher.\nCkeck your configuration.\n%s" % (msg2)))
raise osv.except_osv(
_('Error in Original String!'),
_("Can't get the string original of the voucher.\n"
"Ckeck your configuration.")
)
# TODO: Is this validation needed?
if not data_dict[comprobante].get('folio', ''):
raise osv.except_osv(_('Error in Folio!'), _(
"Can't get the folio of the voucher.\nBefore generating the XML, click on the button, generate invoice.\nCkeck your configuration.\n%s" % (msg2)))
raise osv.except_osv(
_('Error in Folio!'),
_("Can't get the folio of the voucher.\n"
"Before generating the XML, click on the button, "
"generate invoice.\nCkeck your configuration.\n")
)
context.update({'fecha': data_dict[comprobante]['fecha']})
sign_str = self._get_sello(
cr=False, uid=False, ids=False, context=context)
if not sign_str:
raise osv.except_osv(_('Error in Stamp !'), _(
"Can't generate the stamp of the voucher.\nCkeck your configuration.\ns%s") % (msg2))
raise osv.except_osv(
_('Error in Stamp !'),
_("Can't generate the stamp of the voucher.\n"
"Ckeck your configuration.")
)
nodeComprobante = doc_xml.getElementsByTagName(comprobante)[0]
nodeComprobante.setAttribute("sello", sign_str)
......@@ -190,15 +200,21 @@ class account_invoice(osv.Model):
noCertificado = self._get_noCertificado(context['fname_cer'])
if not noCertificado:
raise osv.except_osv(_('Error in No. Certificate !'), _(
"Can't get the Certificate Number of the voucher.\nCkeck your configuration.\n%s") % (msg2))
raise osv.except_osv(
_('Error in No. Certificate !'),
_("Can't get the Certificate Number of the voucher.\n"
"Ckeck your configuration.")
)
nodeComprobante.setAttribute("noCertificado", noCertificado)
data_dict[comprobante]['noCertificado'] = noCertificado
cert_str = self._get_certificate_str(context['fname_cer'])
if not cert_str:
raise osv.except_osv(_('Error in Certificate!'), _(
"Can't get the Certificate Number of the voucher.\nCkeck your configuration.\n%s") % (msg2))
raise osv.except_osv(
_('Error in Certificate!'),
_("Can't get the Certificate Number of the voucher.\n"
"Ckeck your configuration.")
)
cert_str = cert_str.replace(' ', '').replace('\n', '')
nodeComprobante.setAttribute("certificado", cert_str)
data_dict[comprobante]['certificado'] = cert_str
......
......@@ -182,7 +182,7 @@ class ir_attachment_facturae_mx(osv.Model):
)
# Codifica el XML para almacenarlo
f = base64.encodestring(timbre or '')
f = base64.encodestring(timbre.encode('UTF-8') or '')
msg += string.join([mensaje, ". Folio Fiscal ", uuid, "."])
else:
......
......@@ -193,9 +193,10 @@ class ir_attachment_facturae_mx(osv.Model):
)
msj = tools.ustr(res.get('msg', False))
index_xml = res.get('cfdi_xml', False)
xml_file = res.get('cfdi_xml', False).encode('UTF-8')
data_attach = {
'name': fname_invoice,
'datas': base64.encodestring(res.get('cfdi_xml', False)),
'datas': base64.encodestring(xml_file),
'datas_fname': fname_invoice,
'description': 'Factura-E XML CFD-I SIGN',
'res_model': 'account.invoice',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment