From 1cc7d1f1b8b27cc1e0b3b9cca8f418ff83a2528e Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Thu, 18 Jan 2018 00:15:14 -0600 Subject: [PATCH] Descargar ODS --- source/app/controllers/util.py | 10 ++++++---- source/app/models/main.py | 20 ++++++++++++++++++++ source/static/js/controller/invoices.js | 2 ++ source/static/js/controller/util.js | 1 + source/static/js/ui/invoices.js | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index a7bee29..3141d54 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -891,7 +891,7 @@ class LIBO(object): self._clean() return - def pdf(self, path, data): + def pdf(self, path, data, ods=False): options = {'AsTemplate': True, 'Hidden': True} self._template = self._doc_open(path, options) if self._template is None: @@ -902,8 +902,10 @@ class LIBO(object): path = '{}.ods'.format(tempfile.mkstemp()[1]) self._template.storeToURL(self._path_url(path), ()) - doc = self._doc_open(path, {'Hidden': True}) + if ods: + return self._read(path) + doc = self._doc_open(path, {'Hidden': True}) options = {'FilterName': 'calc_pdf_Export'} path = tempfile.mkstemp()[1] doc.storeToURL(self._path_url(path), self._set_properties(options)) @@ -913,7 +915,7 @@ class LIBO(object): return self._read(path) -def to_pdf(data, emisor_rfc): +def to_pdf(data, emisor_rfc, ods=False): rfc = data['emisor']['rfc'] if DEBUG: rfc = emisor_rfc @@ -928,7 +930,7 @@ def to_pdf(data, emisor_rfc): name = '{}_{}{}.ods'.format(rfc.lower(), version, donativo) path = get_template_ods(name) if path: - return app.pdf(path, data) + return app.pdf(path, data, ods) name = '{}_{}.json'.format(rfc, version) custom_styles = get_custom_styles(name) diff --git a/source/app/models/main.py b/source/app/models/main.py index 3c5231c..e760cde 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -107,6 +107,7 @@ def validar_timbrar(): def get_doc(type_doc, id, rfc): types = { 'xml': 'application/xml', + 'ods': 'application/octet-stream', 'zip': 'application/octet-stream', } content_type = types.get(type_doc, 'application/pdf') @@ -114,6 +115,8 @@ def get_doc(type_doc, id, rfc): data, file_name = Facturas.get_xml(id) elif type_doc == 'pdf': data, file_name = Facturas.get_pdf(id, rfc) + elif type_doc == 'ods': + data, file_name = Facturas.get_ods(id, rfc) elif type_doc == 'zip': data, file_name = Facturas.get_zip(id, rfc) elif type_doc == 'pre': @@ -2594,6 +2597,23 @@ class Facturas(BaseModel): return doc, name + @classmethod + def get_ods(cls, id, rfc): + try: + emisor = Emisor.select()[0] + except IndexError: + return b'', 'sin_datos_de_emisor.pdf' + + obj = Facturas.get(Facturas.id==id) + name = '{}{}_{}.ods'.format(obj.serie, obj.folio, obj.cliente.rfc) + if obj.uuid is None: + return b'', name + + values = cls._get_not_in_xml(cls, obj, emisor) + data = util.get_data_from_xml(obj, values) + doc = util.to_pdf(data, emisor.rfc, True) + return doc, name + @classmethod def get_zip(cls, id, rfc): obj = Facturas.get(Facturas.id==id) diff --git a/source/static/js/controller/invoices.js b/source/static/js/controller/invoices.js index b9eabbd..36c1733 100644 --- a/source/static/js/controller/invoices.js +++ b/source/static/js/controller/invoices.js @@ -1131,6 +1131,8 @@ function grid_invoices_click(id, e, node){ location = '/doc/xml/' + row.id }else if(id.column == 'pdf'){ get_pdf(row.id) + }else if(id.column == 'ods'){ + location = '/doc/ods/' + row.id }else if(id.column == 'zip'){ location = '/doc/zip/' + row.id }else if(id.column == 'email'){ diff --git a/source/static/js/controller/util.js b/source/static/js/controller/util.js index 4e29cd1..0373ee7 100644 --- a/source/static/js/controller/util.js +++ b/source/static/js/controller/util.js @@ -40,6 +40,7 @@ function get_icon(tipo){ zip: 'fa-file-zip-o', email: 'fa-envelope-o', print: 'fa-print', + table: 'fa-table', } return "" } diff --git a/source/static/js/ui/invoices.js b/source/static/js/ui/invoices.js index 83f5cf3..eb39161 100644 --- a/source/static/js/ui/invoices.js +++ b/source/static/js/ui/invoices.js @@ -241,6 +241,7 @@ var grid_invoices_cols = [ fillspace: true, sort: 'string', footer: '$ 0.00'}, {id: 'xml', header: 'XML', adjust: 'data', template: get_icon('xml')}, {id: 'pdf', header: 'PDF', adjust: 'data', template: get_icon('pdf')}, + {id: 'ods', header: 'ODS', adjust: 'data', template: get_icon('table')}, {id: 'zip', header: 'ZIP', adjust: 'data', template: get_icon('zip')}, {id: 'email', header: '', adjust: 'data', template: get_icon('email')} ]