From f4863db8ea3cba1db0f843b0678f7ed0e938819d Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Sun, 21 Jan 2018 14:08:49 -0600 Subject: [PATCH] Fix - Issue #139 --- source/app/models/main.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/source/app/models/main.py b/source/app/models/main.py index c439a45..3bc59fd 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -2200,10 +2200,20 @@ class Productos(BaseModel): @classmethod def next_key(cls): - value = (Productos - .select(fn.Max(cast(Productos.clave, 'int')).alias('fm')) - .order_by(SQL('fm')) - .scalar()) + try: + with database_proxy.transaction(): + value = (Productos + .select(fn.Max(cast(Productos.clave, 'int')).alias('fm')) + .order_by(SQL('fm')) + .scalar()) + except Exception as e: + values = (Productos + .select(Productos.clave) + .order_by(Productos.clave) + .tuples() + ) + value = max([int(v[0]) for v in values if v[0].isdigit()]) + value = value or 0 value += 1 return {'value': value} @@ -3806,7 +3816,7 @@ class PreFacturas(BaseModel): totals_tax = {} total_trasladados = None total_retenciones = None - total_iva = 0 + # ~ total_iva = 0 for product in products: id_product = product.pop('id') @@ -3848,8 +3858,8 @@ class PreFacturas(BaseModel): continue import_tax = round(float(tax.tasa) * tax.importe, DECIMALES) total_trasladados = (total_trasladados or 0) + import_tax - if tax.name == 'IVA': - total_iva += import_tax + # ~ if tax.name == 'IVA': + # ~ total_iva += import_tax invoice_tax = { 'factura': invoice.id, @@ -3862,10 +3872,10 @@ class PreFacturas(BaseModel): for tax in totals_tax.values(): if tax.tipo == 'E' or tax.tipo == 'T': continue - if tax.tasa == round(Decimal(2/3), 6): - import_tax = round(float(tax.tasa) * total_iva, DECIMALES) - else: - import_tax = round(float(tax.tasa) * tax.importe, DECIMALES) + # ~ if tax.tasa == round(Decimal(2/3), 6): + # ~ import_tax = round(float(tax.tasa) * total_iva, DECIMALES) + # ~ else: + import_tax = round(float(tax.tasa) * tax.importe, DECIMALES) total_retenciones = (total_retenciones or 0) + import_tax invoice_tax = {