PDF con json si no existe ods

This commit is contained in:
Mauricio Baeza 2017-10-25 19:46:13 -05:00
parent e7a5969656
commit a37875d063
3 changed files with 48 additions and 18 deletions

View File

@ -20,9 +20,13 @@ from io import BytesIO
from smtplib import SMTPException, SMTPAuthenticationError
from xml.etree import ElementTree as ET
#~ import uno
#~ from com.sun.star.beans import PropertyValue
#~ from com.sun.star.awt import Size
try:
import uno
from com.sun.star.beans import PropertyValue
from com.sun.star.awt import Size
APP_LIBO = True
except:
APP_LIBO = False
import pyqrcode
from dateutil import parser
@ -183,7 +187,7 @@ def get_template(name, data={}):
def get_custom_styles(name, default='plantilla_factura.json'):
path = _join(PATH_TEMPLATES, name)
path = _join(PATH_MEDIA, 'templates', name.lower())
if is_file(path):
with open(path) as fh:
return loads(fh.read())
@ -196,6 +200,18 @@ def get_custom_styles(name, default='plantilla_factura.json'):
return {}
def get_template_ods(name, default='plantilla_factura.ods'):
path = _join(PATH_MEDIA, 'templates', name.lower())
if is_file(path):
return path
path = _join(PATH_TEMPLATES, default)
if is_file(path):
return path
return ''
def dumps(data):
return json.dumps(data, default=str)
@ -725,14 +741,24 @@ class LIBO(object):
return self._read(path)
def to_pdf(styles, data):
#~ app = LIBO()
#~ if not app.is_running:
#~ return b''
#~ return app.pdf(path, data)
def to_pdf(data):
rfc = data['emisor']['rfc']
version = data['comprobante']['version']
if APP_LIBO:
app = LIBO()
if app.is_running:
name = '{}_{}.ods'.format(rfc, version)
path = get_template_ods(name)
if path:
return app.pdf(path, data)
name = '{}_{}.json'.format(rfc, version)
custom_styles = get_custom_styles(name)
path = get_path_temp()
pdf = TemplateInvoice(path)
pdf.custom_styles = styles
pdf.custom_styles = custom_styles
pdf.data = data
pdf.render()
return read_file(path)
@ -913,10 +939,7 @@ def _timbre(doc, version, values):
return data
def get_data_from_xml(invoice, rfc, values):
name = '{}_factura.json'.format(rfc.lower())
custom_styles = get_custom_styles(name)
def get_data_from_xml(invoice, values):
data = {'cancelada': invoice.cancelada}
doc = parse_xml(invoice.xml)
data['comprobante'] = _comprobante(doc.attrib.copy(), values)
@ -932,9 +955,10 @@ def get_data_from_xml(invoice, rfc, values):
'total': data['comprobante']['total'],
}
data['timbre'] = _timbre(doc, version, options)
del data['timbre']['version']
data['comprobante'].update(data['timbre'])
return custom_styles, data
return data
def to_zip(*files):

View File

@ -14,7 +14,7 @@ if __name__ == '__main__':
from controllers import util
from settings import log, VERSION, PATH_CP, COMPANIES, PRE
from settings import log, VERSION, PATH_CP, COMPANIES, PRE, CURRENT_CFDI
FORMAT = '{0:.2f}'
@ -186,6 +186,7 @@ class Emisor(BaseModel):
curp = TextField(default='')
token_timbrado = TextField(default='')
token_soporte = TextField(default='')
logo = TextField(default='')
regimenes = ManyToManyField(SATRegimenes, related_name='emisores')
def __str__(self):
@ -959,6 +960,7 @@ class Productos(BaseModel):
class Facturas(BaseModel):
cliente = ForeignKeyField(Socios)
version = TextField(default=CURRENT_CFDI)
serie = TextField(default='')
folio = IntegerField(default=0)
fecha = DateTimeField(default=util.now, formats=['%Y-%m-%d %H:%M:%S'])
@ -987,6 +989,7 @@ class Facturas(BaseModel):
notas = TextField(default='')
pagada = BooleanField(default=False)
cancelada = BooleanField(default=False)
donativo = BooleanField(default=False)
tipo_relacion = TextField(default='')
error = TextField(default='')
@ -1084,8 +1087,8 @@ class Facturas(BaseModel):
return b'', name
values = cls._get_not_in_xml(cls, obj)
custom_styles, data = util.get_data_from_xml(obj, rfc, values)
doc = util.to_pdf(custom_styles, data)
data = util.get_data_from_xml(obj, values)
doc = util.to_pdf(data)
return doc, name
@classmethod
@ -1434,6 +1437,7 @@ class Facturas(BaseModel):
obj.uuid = result['uuid']
obj.fecha_timbrado = result['fecha']
obj.estatus = 'Timbrada'
obj.error = ''
obj.save()
row = {'uuid': obj.uuid, 'estatus': 'Timbrada'}
else:

View File

@ -83,3 +83,5 @@ PRE = {
'1.2': '{http://www.sat.gob.mx/nomina12}',
}
}
CURRENT_CFDI = '3.3'