diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index ffb2b7c..a203455 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -740,6 +740,7 @@ class LIBO(object): def _conceptos(self, data): first = True + copy = False for concepto in data: key = concepto.get('noidentificacion', '') description = concepto['descripcion'] @@ -757,18 +758,14 @@ class LIBO(object): cell_6 = self._set_cell('{importe}', importe, value=True) else: row = cell_2.getCellAddress().Row + 1 - self._sheet.getRows().insertByIndex(row, 1) - if cell_1: - self._copy_cell(cell_1) - cell_1 = self._set_cell(v=key, cell=cell_1) - if cell_3: - self._copy_cell(cell_3) - cell_3 = self._set_cell(v=unidad, cell=cell_3) - self._copy_cell(cell_2) - self._copy_cell(cell_4) - self._copy_cell(cell_5) - self._copy_cell(cell_6) + if not copy: + self._sheet.getRows().insertByIndex(row, len(data)-1) + copy = True + source = self._sheet.getRows().getByIndex(row - 1) + cell_1 = self._set_cell(v=key, cell=cell_1) + self._sheet.copyRange(cell_1.getCellAddress(), source.getRangeAddress()) cell_2 = self._set_cell(v=description, cell=cell_2) + cell_3 = self._set_cell(v=unidad, cell=cell_3) cell_4 = self._set_cell(v=cantidad, cell=cell_4, value=True) cell_5 = self._set_cell(v=valor_unitario, cell=cell_5, value=True) cell_6 = self._set_cell(v=importe, cell=cell_6, value=True) diff --git a/source/app/main.ini.example b/source/app/main.ini.example index 77a5d54..9f37e02 100644 --- a/source/app/main.ini.example +++ b/source/app/main.ini.example @@ -15,4 +15,5 @@ thunder-lock = true #~ stats = 127.0.0.1:9191 #~ Establece una ruta accesible para el servidor web logger = file:/srv/log/empresalibre-uwsgi.log -log-maxsize = 1000000 \ No newline at end of file +log-maxsize = 1000000 +http-timeout = 180 \ No newline at end of file diff --git a/source/app/main_debug.ini b/source/app/main_debug.ini index 043ea43..94481fe 100644 --- a/source/app/main_debug.ini +++ b/source/app/main_debug.ini @@ -8,3 +8,4 @@ threads = 4 py-autoreload = 1 thunder-lock = true static-map = /static=../static +http-timeout = 120 \ No newline at end of file diff --git a/source/app/models/db.py b/source/app/models/db.py index cf12c63..302db1a 100644 --- a/source/app/models/db.py +++ b/source/app/models/db.py @@ -25,6 +25,9 @@ class StorageEngine(object): return getattr(self, '_get_{}'.format(table))(values, session) return getattr(self, '_get_{}'.format(table))(values) + def _get_canopenpre(self, values): + return main.PreFacturasDetalle.can_open(values['id']) + def _get_importinvoice(self, values): return main.import_invoice() diff --git a/source/app/models/main.py b/source/app/models/main.py index cc064b1..c439a45 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -3534,6 +3534,44 @@ class PreFacturas(BaseModel): class Meta: order_by = ('fecha',) + @util.run_in_thread + def _send_in_thread(self, id, obj, values): + log.info('Generando PDF...') + files = (self.get_pdf(id),) + log.info('PDF Generado...') + + invoice = PreFacturas.select().where(PreFacturas.id==id).dicts()[0] + fields = { + 'receptor_nombre': obj.cliente.nombre, + 'receptor_rfc': obj.cliente.rfc, + } + fields.update(invoice) + + asunto = 'Enviamos la prefactura: PRE-{}'.format(obj.folio) + server = { + 'servidor': values['correo_servidor'], + 'puerto': values['correo_puerto'], + 'ssl': bool(int(values['correo_ssl'])), + 'usuario': values['correo_usuario'], + 'contra': values['correo_contra'], + } + options = { + 'para': obj.cliente.correo_facturas, + 'copia': values['correo_copia'], + 'confirmar': util.get_bool(values.get('correo_confirmacion', '0')), + 'asunto': asunto, + 'mensaje': util.make_info_mail(values['correo_mensaje'], fields), + 'files': files, + } + data= { + 'server': server, + 'options': options, + } + log.info('Enviando prefactura...') + result = util.send_mail(data) + log.info('Prefactura enviada...') + return + @classmethod def enviar(cls, id): values = Configuracion.get_({'fields': 'correo'}) @@ -3546,6 +3584,12 @@ class PreFacturas(BaseModel): msg = 'El cliente no tiene configurado el correo para facturas' return {'ok': False, 'msg': msg} + rows = PreFacturasDetalle.count(id) + if rows > 300: + cls._send_in_thread(cls, id, obj, values) + msg = 'Enviando correo...' + return {'ok': True, 'msg': msg} + files = (cls.get_pdf(id),) invoice = PreFacturas.select().where(PreFacturas.id==id).dicts()[0] @@ -3661,6 +3705,25 @@ class PreFacturas(BaseModel): return data + @util.run_in_thread + def _get_pdf_in_thread(self, id): + obj = PreFacturas.get(PreFacturas.id==id) + name = '{}{}_{}.pdf'.format(obj.serie, obj.folio, obj.cliente.rfc) + data = self._get_info_to_pdf(self, id) + doc = util.to_pdf(data, data['emisor']['rfc']) + + emisor = Emisor.select()[0] + target = emisor.rfc + '/Prefacturas/' + files = ( + (doc, name, target), + ) + util.sync_cfdi({'REPO': False}, files) + return + + @classmethod + def get_pdf_in_thread(cls, id): + return cls._get_pdf_in_thread(cls, id) + @classmethod def get_pdf(cls, id): obj = PreFacturas.get(PreFacturas.id==id) @@ -4008,6 +4071,21 @@ class PreFacturasDetalle(BaseModel): return {'rows': data, 'receptor': receptor} + @classmethod + def count(cls, id): + c = PreFacturasDetalle.select().where( + PreFacturasDetalle.factura==id).count() + return c + + @classmethod + def can_open(cls, id): + c = cls.count(id) + # ~ result = c < 300 + # ~ if result: + # ~ return True + PreFacturas.get_pdf_in_thread(id) + return c < 300 + @classmethod def get_(cls, id): data = [] diff --git a/source/static/js/controller/invoices.js b/source/static/js/controller/invoices.js index 4bc2f6f..db54fe8 100644 --- a/source/static/js/controller/invoices.js +++ b/source/static/js/controller/invoices.js @@ -1543,12 +1543,26 @@ function enviar_prefactura(id){ } -function grid_preinvoices_click(id, e, node){ - //~ var row = this.getItem(id) +function generate_pdf(id){ + webix.ajax().get('/values/canopenpre', {id: id}, { + error: function(text, data, xhr) { + }, + success: function(text, data, xhr) { + var value = data.json(); + //~ if(value){ + window.open('/doc/pre/' + id, '_blank') + //~ }else{ + //~ msg_ok('Generando prefactura...') + //~ } + } + }) +} + + +function grid_preinvoices_click(id, e, node){ if(id.column == 'pdf'){ - //~ location = '/doc/pre/' + row.id - window.open('/doc/pre/' + id, '_blank') + generate_pdf(id.row) }else if(id.column == 'email'){ enviar_prefactura(id) } @@ -1895,21 +1909,6 @@ function cmd_upload_invoice_click(){ function add_import_product_taxes(taxes){ - //~ var taxes = values.taxes - //~ var row = grid.getItem(values.id) - - //~ values['delete'] = '-' - //~ if (row == undefined){ - //~ grid.add(values) - //~ } else { - //~ values['cantidad'] = parseFloat(row.cantidad) + parseFloat(values['cantidad']) - //~ values['valor_unitario'] = parseFloat(row.valor_unitario) - //~ values['descuento'] = parseFloat(row.descuento) - //~ var precio_final = values['valor_unitario'] - values['descuento'] - //~ values['importe'] = (precio_final * values['cantidad']).round(DECIMALES) - //~ grid.updateItem(row.id, values) - //~ } - for(var v of taxes){ var pt = table_pt.findOne(v) if(pt === null){ diff --git a/source/templates/plantilla_factura.ods b/source/templates/plantilla_factura.ods index e439792..3e50e6b 100644 Binary files a/source/templates/plantilla_factura.ods and b/source/templates/plantilla_factura.ods differ