diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index a73090d..a82ca4a 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -729,7 +729,9 @@ class LIBO(object): #~ Si no se encuentra, copia las celdas hacia abajo de #~ {subtotal.titulo} y {subtotal} + print (data['descuento']) if 'descuento' in data: + self._copy_cell(cell_title) self._copy_cell(cell_value) cell_title = self._set_cell(v='Descuento', cell=cell_title) diff --git a/source/app/models/main.py b/source/app/models/main.py index 2afdbc3..db6c618 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -1721,6 +1721,7 @@ class Facturas(BaseModel): def _calculate_totals(self, invoice, products): subtotal = 0 + descuento_cfdi = 0 totals_tax = {} total_trasladados = None total_retenciones = None @@ -1749,6 +1750,7 @@ class Facturas(BaseModel): product['precio_final'] = precio_final product['importe'] = round(cantidad * valor_unitario, 2) + descuento_cfdi += descuento subtotal += importe FacturasDetalle.create(**product) @@ -1800,6 +1802,7 @@ class Facturas(BaseModel): total_mn = round(total * invoice.tipo_cambio, 2) data = { 'subtotal': subtotal + descuento, + 'descuento': descuento_cfdi, 'total': total, 'total_mn': total_mn, 'total_trasladados': total_trasladados, @@ -1832,6 +1835,7 @@ class Facturas(BaseModel): totals = cls._calculate_totals(cls, obj, productos) cls._guardar_relacionados(cls, obj, relacionados) obj.subtotal = totals['subtotal'] + obj.descuento = totals['descuento'] obj.total_trasladados = totals['total_trasladados'] obj.total_retenciones = totals['total_retenciones'] obj.total = totals['total'] @@ -1879,6 +1883,9 @@ class Facturas(BaseModel): comprobante['TipoDeComprobante'] = invoice.tipo_comprobante comprobante['MetodoPago'] = invoice.metodo_pago comprobante['LugarExpedicion'] = invoice.lugar_expedicion + if invoice.descuento: + comprobante['Descuento'] = FORMAT.format(invoice.descuento) + if invoice.tipo_relacion: relacionados = { 'tipo': invoice.tipo_relacion, @@ -1897,7 +1904,7 @@ class Facturas(BaseModel): 'UsoCFDI': invoice.uso_cfdi, } - descuento = 0 + #~ descuento = 0 conceptos = [] rows = FacturasDetalle.select().where(FacturasDetalle.factura==invoice) for row in rows: @@ -1913,7 +1920,7 @@ class Facturas(BaseModel): } if row.descuento: concepto['Descuento'] = FORMAT.format(row.descuento) - descuento += row.descuento + #~ descuento += row.descuento taxes = {} traslados = [] @@ -1946,8 +1953,8 @@ class Facturas(BaseModel): concepto['impuestos'] = taxes conceptos.append(concepto) - if descuento: - comprobante['Descuento'] = FORMAT.format(descuento) + #~ if descuento: + #~ comprobante['Descuento'] = FORMAT.format(descuento) impuestos = {} traslados = [] @@ -2224,6 +2231,9 @@ class PreFacturas(BaseModel): data['totales']['subtotal'] = str(data['comprobante']['subtotal']) data['totales']['total'] = str(data['comprobante']['total']) + if obj['descuento']: + data['totales']['descuento'] = float(obj['descuento']) + taxes = PreFacturasImpuestos.get_(id) data['totales']['traslados'] = taxes['traslados'] data['totales']['retenciones'] = taxes['retenciones'] @@ -2310,6 +2320,7 @@ class PreFacturas(BaseModel): def _calculate_totals(self, invoice, products): subtotal = 0 + descuento_cfdi = 0 totals_tax = {} total_trasladados = None total_retenciones = None @@ -2318,25 +2329,36 @@ class PreFacturas(BaseModel): for product in products: id_product = product.pop('id') p = Productos.get(Productos.id==id_product) - #~ product['descripcion'] = p.descripcion + product['unidad'] = p.unidad.key product['clave'] = p.clave product['clave_sat'] = p.clave_sat product['factura'] = invoice.id product['producto'] = id_product - product['importe'] = round( - float(product['cantidad']) * float(product['valor_unitario']), 2) - subtotal += product['importe'] + + cantidad = float(product['cantidad']) + valor_unitario = float(product['valor_unitario']) + descuento = float(product['descuento']) + precio_final = valor_unitario - descuento + importe = round(cantidad * precio_final, 2) + + product['cantidad'] = cantidad + product['valor_unitario'] = valor_unitario + product['descuento'] = descuento + product['precio_final'] = precio_final + product['importe'] = round(cantidad * valor_unitario, 2) + + descuento_cfdi += descuento + subtotal += importe PreFacturasDetalle.create(**product) for tax in p.impuestos: if tax.id in totals_tax: - totals_tax[tax.id].importe += product['importe'] + totals_tax[tax.id].importe += importe else: - tax.importe = product['importe'] + tax.importe = importe totals_tax[tax.id] = tax - #~ totals_tax[tax.id]['importe'] = product['importe'] for tax in totals_tax.values(): if tax.tipo == 'E' or tax.tipo == 'R': @@ -2374,7 +2396,8 @@ class PreFacturas(BaseModel): total = subtotal + (total_trasladados or 0) - (total_retenciones or 0) total_mn = round(total * invoice.tipo_cambio, 2) data = { - 'subtotal': subtotal, + 'subtotal': subtotal + descuento, + 'descuento': descuento_cfdi, 'total': total, 'total_mn': total_mn, 'total_trasladados': total_trasladados, @@ -2384,7 +2407,6 @@ class PreFacturas(BaseModel): @classmethod def add(cls, values): - print ('VALUES', values) productos = util.loads(values.pop('productos')) emisor = Emisor.select()[0] @@ -2397,6 +2419,7 @@ class PreFacturas(BaseModel): obj = PreFacturas.create(**values) totals = cls._calculate_totals(cls, obj, productos) obj.subtotal = totals['subtotal'] + obj.descuento = totals['descuento'] obj.total_trasladados = totals['total_trasladados'] obj.total_retenciones = totals['total_retenciones'] obj.total = totals['total'] diff --git a/source/static/js/controller/invoices.js b/source/static/js/controller/invoices.js index 862b221..ddf2a1b 100644 --- a/source/static/js/controller/invoices.js +++ b/source/static/js/controller/invoices.js @@ -1045,6 +1045,7 @@ function cmd_prefactura_click(){ delete rows[i]['unidad'] delete rows[i]['importe'] rows[i]['valor_unitario'] = parseFloat(rows[i]['valor_unitario']) + rows[i]['descuento'] = parseFloat(rows[i]['descuento']) } var data = new Object()