diff --git a/source/app/models/main.py b/source/app/models/main.py index bc631ec..8a8046d 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -5461,7 +5461,6 @@ class Facturas(BaseModel): retenciones = [] if invoice.tipo_comprobante != 'T': - if is_global: ticket = (Tickets .get(fn.Concat(Tickets.serie, Tickets.folio)==row.clave) @@ -5474,9 +5473,11 @@ class Facturas(BaseModel): product_taxes = row.producto.impuestos for impuesto in product_taxes: - if is_global: - impuesto = impuesto.impuesto base = float(row.importe - row.descuento) + if is_global: + base = float(impuesto.base) + impuesto = impuesto.impuesto + if impuesto.tipo == 'E': tax = { 'Base': FORMAT.format(base), @@ -5518,6 +5519,7 @@ class Facturas(BaseModel): taxes['traslados'] = traslados if retenciones: taxes['retenciones'] = retenciones + concepto['impuestos'] = taxes conceptos.append(concepto) @@ -5612,6 +5614,7 @@ class Facturas(BaseModel): 'edu': is_edu, 'complementos': complementos, } + return utils.make_xml(data, certificado) @classmethod @@ -7898,6 +7901,8 @@ class Tickets(BaseModel): rows = Tickets.select().where(Tickets.id.in_(ids)) + tax_sum = {} + for row in rows: details = DEFAULT_GLOBAL.copy() details['clave'] = row.serie + str(row.folio) @@ -7918,28 +7923,44 @@ class Tickets(BaseModel): FacturasDetalle.create(**details) - rows = (TicketsImpuestos - .select( - TicketsImpuestos.impuesto, - fn.Sum(TicketsImpuestos.base), - fn.Sum(TicketsImpuestos.importe)) - .where(TicketsImpuestos.ticket.in_(ids)) - .group_by(TicketsImpuestos.impuesto) - .order_by(TicketsImpuestos.impuesto) - ) - for tax in rows: + taxes = (TicketsImpuestos + .select( + TicketsImpuestos.impuesto, + TicketsImpuestos.base, + TicketsImpuestos.importe) + .where(TicketsImpuestos.ticket == row.id) + ) + + for r in taxes: + tax_id = r.impuesto.id + tasa = r.impuesto.tasa + tax_importe = round(tasa * r.base, DECIMALES) + + if tax_id in tax_sum: + tax_sum[tax_id]['base'] += r.base + tax_sum[tax_id]['importe'] += tax_importe + else: + values = { + 'tipo': r.impuesto.tipo, + 'key': r.impuesto.key, + 'base': r.base, + 'importe': tax_importe} + tax_sum[tax_id] = values + + for i, tax in tax_sum.items(): + tax_importe = round(tax['importe'], DECIMALES) invoice_tax = { 'factura': invoice.id, - 'impuesto': tax.impuesto.id, - 'base': tax.base, - 'importe': tax.importe, + 'impuesto': i, + 'base': tax['base'], + 'importe': tax_importe } FacturasImpuestos.create(**invoice_tax) - if tax.impuesto.tipo == 'T' and tax.impuesto.key != '000': - total_trasladados = (total_trasladados or 0) + tax.importe - elif tax.impuesto.tipo == 'R' and tax.impuesto.key != '000': - total_retenciones = (total_retenciones or 0) + tax.importe + if tax['tipo'] == 'T' and tax['key'] != '000': + total_trasladados = (total_trasladados or 0) + tax_importe + elif tax['tipo'] == 'R' and tax['key'] != '000': + total_retenciones = (total_retenciones or 0) + tax_importe total = subtotal - descuento_cfdi + \ (total_trasladados or 0) - (total_retenciones or 0) @@ -7955,6 +7976,7 @@ class Tickets(BaseModel): 'total_retenciones': total_retenciones, 'notas': notes, } + return data @classmethod