PDF con json si no existe ods
This commit is contained in:
parent
e7a5969656
commit
a37875d063
|
@ -20,9 +20,13 @@ from io import BytesIO
|
||||||
from smtplib import SMTPException, SMTPAuthenticationError
|
from smtplib import SMTPException, SMTPAuthenticationError
|
||||||
from xml.etree import ElementTree as ET
|
from xml.etree import ElementTree as ET
|
||||||
|
|
||||||
#~ import uno
|
try:
|
||||||
#~ from com.sun.star.beans import PropertyValue
|
import uno
|
||||||
#~ from com.sun.star.awt import Size
|
from com.sun.star.beans import PropertyValue
|
||||||
|
from com.sun.star.awt import Size
|
||||||
|
APP_LIBO = True
|
||||||
|
except:
|
||||||
|
APP_LIBO = False
|
||||||
|
|
||||||
import pyqrcode
|
import pyqrcode
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
@ -183,7 +187,7 @@ def get_template(name, data={}):
|
||||||
|
|
||||||
|
|
||||||
def get_custom_styles(name, default='plantilla_factura.json'):
|
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):
|
if is_file(path):
|
||||||
with open(path) as fh:
|
with open(path) as fh:
|
||||||
return loads(fh.read())
|
return loads(fh.read())
|
||||||
|
@ -196,6 +200,18 @@ def get_custom_styles(name, default='plantilla_factura.json'):
|
||||||
return {}
|
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):
|
def dumps(data):
|
||||||
return json.dumps(data, default=str)
|
return json.dumps(data, default=str)
|
||||||
|
|
||||||
|
@ -725,14 +741,24 @@ class LIBO(object):
|
||||||
return self._read(path)
|
return self._read(path)
|
||||||
|
|
||||||
|
|
||||||
def to_pdf(styles, data):
|
def to_pdf(data):
|
||||||
#~ app = LIBO()
|
rfc = data['emisor']['rfc']
|
||||||
#~ if not app.is_running:
|
version = data['comprobante']['version']
|
||||||
#~ return b''
|
|
||||||
#~ return app.pdf(path, data)
|
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()
|
path = get_path_temp()
|
||||||
pdf = TemplateInvoice(path)
|
pdf = TemplateInvoice(path)
|
||||||
pdf.custom_styles = styles
|
pdf.custom_styles = custom_styles
|
||||||
pdf.data = data
|
pdf.data = data
|
||||||
pdf.render()
|
pdf.render()
|
||||||
return read_file(path)
|
return read_file(path)
|
||||||
|
@ -913,10 +939,7 @@ def _timbre(doc, version, values):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_data_from_xml(invoice, rfc, values):
|
def get_data_from_xml(invoice, values):
|
||||||
name = '{}_factura.json'.format(rfc.lower())
|
|
||||||
custom_styles = get_custom_styles(name)
|
|
||||||
|
|
||||||
data = {'cancelada': invoice.cancelada}
|
data = {'cancelada': invoice.cancelada}
|
||||||
doc = parse_xml(invoice.xml)
|
doc = parse_xml(invoice.xml)
|
||||||
data['comprobante'] = _comprobante(doc.attrib.copy(), values)
|
data['comprobante'] = _comprobante(doc.attrib.copy(), values)
|
||||||
|
@ -932,9 +955,10 @@ def get_data_from_xml(invoice, rfc, values):
|
||||||
'total': data['comprobante']['total'],
|
'total': data['comprobante']['total'],
|
||||||
}
|
}
|
||||||
data['timbre'] = _timbre(doc, version, options)
|
data['timbre'] = _timbre(doc, version, options)
|
||||||
|
del data['timbre']['version']
|
||||||
data['comprobante'].update(data['timbre'])
|
data['comprobante'].update(data['timbre'])
|
||||||
|
|
||||||
return custom_styles, data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def to_zip(*files):
|
def to_zip(*files):
|
||||||
|
|
|
@ -14,7 +14,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
||||||
from controllers import util
|
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}'
|
FORMAT = '{0:.2f}'
|
||||||
|
@ -186,6 +186,7 @@ class Emisor(BaseModel):
|
||||||
curp = TextField(default='')
|
curp = TextField(default='')
|
||||||
token_timbrado = TextField(default='')
|
token_timbrado = TextField(default='')
|
||||||
token_soporte = TextField(default='')
|
token_soporte = TextField(default='')
|
||||||
|
logo = TextField(default='')
|
||||||
regimenes = ManyToManyField(SATRegimenes, related_name='emisores')
|
regimenes = ManyToManyField(SATRegimenes, related_name='emisores')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -959,6 +960,7 @@ class Productos(BaseModel):
|
||||||
|
|
||||||
class Facturas(BaseModel):
|
class Facturas(BaseModel):
|
||||||
cliente = ForeignKeyField(Socios)
|
cliente = ForeignKeyField(Socios)
|
||||||
|
version = TextField(default=CURRENT_CFDI)
|
||||||
serie = TextField(default='')
|
serie = TextField(default='')
|
||||||
folio = IntegerField(default=0)
|
folio = IntegerField(default=0)
|
||||||
fecha = DateTimeField(default=util.now, formats=['%Y-%m-%d %H:%M:%S'])
|
fecha = DateTimeField(default=util.now, formats=['%Y-%m-%d %H:%M:%S'])
|
||||||
|
@ -987,6 +989,7 @@ class Facturas(BaseModel):
|
||||||
notas = TextField(default='')
|
notas = TextField(default='')
|
||||||
pagada = BooleanField(default=False)
|
pagada = BooleanField(default=False)
|
||||||
cancelada = BooleanField(default=False)
|
cancelada = BooleanField(default=False)
|
||||||
|
donativo = BooleanField(default=False)
|
||||||
tipo_relacion = TextField(default='')
|
tipo_relacion = TextField(default='')
|
||||||
error = TextField(default='')
|
error = TextField(default='')
|
||||||
|
|
||||||
|
@ -1084,8 +1087,8 @@ class Facturas(BaseModel):
|
||||||
return b'', name
|
return b'', name
|
||||||
|
|
||||||
values = cls._get_not_in_xml(cls, obj)
|
values = cls._get_not_in_xml(cls, obj)
|
||||||
custom_styles, data = util.get_data_from_xml(obj, rfc, values)
|
data = util.get_data_from_xml(obj, values)
|
||||||
doc = util.to_pdf(custom_styles, data)
|
doc = util.to_pdf(data)
|
||||||
return doc, name
|
return doc, name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -1434,6 +1437,7 @@ class Facturas(BaseModel):
|
||||||
obj.uuid = result['uuid']
|
obj.uuid = result['uuid']
|
||||||
obj.fecha_timbrado = result['fecha']
|
obj.fecha_timbrado = result['fecha']
|
||||||
obj.estatus = 'Timbrada'
|
obj.estatus = 'Timbrada'
|
||||||
|
obj.error = ''
|
||||||
obj.save()
|
obj.save()
|
||||||
row = {'uuid': obj.uuid, 'estatus': 'Timbrada'}
|
row = {'uuid': obj.uuid, 'estatus': 'Timbrada'}
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -83,3 +83,5 @@ PRE = {
|
||||||
'1.2': '{http://www.sat.gob.mx/nomina12}',
|
'1.2': '{http://www.sat.gob.mx/nomina12}',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CURRENT_CFDI = '3.3'
|
||||||
|
|
Loading…
Reference in New Issue