Merge branch 'mod/facturas_trello' into 'main'

Mod/facturas trello

See merge request jjimenez/safekat!684
This commit is contained in:
2025-04-10 14:59:28 +00:00
8 changed files with 265 additions and 256 deletions

View File

@ -692,7 +692,9 @@ class Facturas extends \App\Controllers\BaseResourceController
// Se añade la linea de factura // Se añade la linea de factura
$descripcion = $model_presupuesto->generarLineaPedido($presupuesto->id, true, $linea->pedido_id); $descripcion = $model_presupuesto->generarLineaPedido($presupuesto->id, true, $linea->pedido_id);
$cantidad = intval($presupuesto->tirada) - intval($model_factura->getCantidadLineaPedidoFacturada($linea->id)); $cantidad = intval($presupuesto->tirada) - intval($model_factura->getCantidadLineaPedidoFacturada($linea->id));
$base = $cantidad * floatval($presupuesto->total_precio_unidad); $base =
floatval($presupuesto->total_aceptado_revisado && $presupuesto->total_aceptado_revisado != 0 ?
$presupuesto->total_aceptado_revisado : $presupuesto->total_aceptado);
$base = round($base, 2); $base = round($base, 2);
$total_iva = $base * ($presupuesto->iva_reducido == 1 ? 0.04 : 0.21); $total_iva = $base * ($presupuesto->iva_reducido == 1 ? 0.04 : 0.21);
// se redondea a dos decimales // se redondea a dos decimales
@ -704,7 +706,6 @@ class Facturas extends \App\Controllers\BaseResourceController
'pedido_linea_impresion_id' => $linea->pedido_id, 'pedido_linea_impresion_id' => $linea->pedido_id,
'descripcion' => $descripcion[0]->concepto, 'descripcion' => $descripcion[0]->concepto,
'cantidad' => $cantidad, 'cantidad' => $cantidad,
'precio_unidad' => $presupuesto->total_precio_unidad,
'iva' => $presupuesto->iva_reducido == 1 ? 4 : 21, 'iva' => $presupuesto->iva_reducido == 1 ? 4 : 21,
'base' => $base, 'base' => $base,
'total_iva' => $total_iva, 'total_iva' => $total_iva,

View File

@ -101,15 +101,6 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
->validator('Validate::notEmpty', array( ->validator('Validate::notEmpty', array(
'message' => lang('Facturas.validation.requerido')) 'message' => lang('Facturas.validation.requerido'))
), ),
Field::inst( 'precio_unidad' )
->getFormatter( 'Format::toDecimalChar')->setFormatter( 'Format::fromDecimalChar')
->validator('Validate::numeric', array(
"decimal" => ',',
'message' => lang('Facturas.validation.decimal'))
)
->validator('Validate::notEmpty', array(
'message' => lang('Facturas.validation.requerido'))
),
Field::inst( 'iva' ) Field::inst( 'iva' )
->validator('Validate::numeric', array( ->validator('Validate::numeric', array(
'message' => lang('Facturas.validation.numerico')) 'message' => lang('Facturas.validation.numerico'))
@ -128,7 +119,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$totales = $this->generate_totales( $totales = $this->generate_totales(
$values['factura_id'], $values['factura_id'],
$values['pedido_linea_impresion_id'], $values['pedido_linea_impresion_id'],
$values['precio_unidad'], $values['total'],
$values['iva'], $values['iva'],
$values['cantidad'], $values['cantidad'],
$values['old_cantidad']); $values['old_cantidad']);
@ -152,7 +143,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$totales = $this->generate_totales( $totales = $this->generate_totales(
$values['factura_id'], $values['factura_id'],
$values['pedido_linea_impresion_id'], $values['pedido_linea_impresion_id'],
$values['precio_unidad'], $values['total'],
$values['iva'], $values['iva'],
$values['cantidad'], $values['cantidad'],
$values['old_cantidad']); $values['old_cantidad']);
@ -203,7 +194,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$model->updateTotales($factura_id); $model->updateTotales($factura_id);
} }
private function generate_totales($factura_id, $pedido_linea_id, $precio_unidad, $iva, $cantidad, $old_cantidad) private function generate_totales($factura_id, $pedido_linea_id, $total, $iva, $cantidad, $old_cantidad)
{ {
// si es una linea que se refiere a pedido // si es una linea que se refiere a pedido
@ -214,6 +205,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
// se calcula y se actualiza el subtotal, total_iva y total // se calcula y se actualiza el subtotal, total_iva y total
// redondeando a 4 decimales el precio_unidad y a dos el resto // redondeando a 4 decimales el precio_unidad y a dos el resto
$precio_unidad = round($total / $old_cantidad, 4);
$base = round($precio_unidad * $cantidad, 2); $base = round($precio_unidad * $cantidad, 2);
$total_iva = round($base * $iva / 100, 2); $total_iva = round($base * $iva / 100, 2);
$total = round($base + $total_iva, 2); $total = round($base + $total_iva, 2);

View File

@ -57,10 +57,7 @@ class FacturasPagos extends \App\Controllers\BaseResourceController
->fields( ->fields(
Field::inst( 'id' ), Field::inst( 'id' ),
Field::inst( 'forma_pago_id' ), Field::inst( 'forma_pago_id' ),
Field::inst( 'notes' ) Field::inst( 'notes' ),
->validator('Validate::notEmpty', array(
'message' => lang('Facturas.validation.requerido'))
),
Field::inst( 'fecha_pago_at' ) Field::inst( 'fecha_pago_at' )
->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) ) ->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d H:i:s' ) ) ->getFormatter( Format::dateSqlToFormat( 'Y-m-d H:i:s' ) )

View File

@ -0,0 +1,24 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class RemovePrecioUnidadFacturas extends Migration
{
public function up()
{
$this->forge->dropColumn('facturas_lineas', 'precio_unidad');
}
public function down()
{
$fields = [
'precio_unidad' => [
'type' => 'DOUBLE',
'null' => false,
],
];
$this->forge->addColumn('facturas_lineas', $fields);
}
}

View File

@ -12,7 +12,6 @@ class FacturaLineaEntity extends \CodeIgniter\Entity\Entity
'pedido_maquetacion_id' => null, 'pedido_maquetacion_id' => null,
'descripcion' => null, 'descripcion' => null,
'cantidad' => null, 'cantidad' => null,
'precio_unidad' => null,
'iva' => null, 'iva' => null,
'base' => null, 'base' => null,
'total_iva' => null, 'total_iva' => null,
@ -29,7 +28,6 @@ class FacturaLineaEntity extends \CodeIgniter\Entity\Entity
'pedido_linea_impresion_id' => 'int', 'pedido_linea_impresion_id' => 'int',
'pedido_maquetacion_id' => 'int', 'pedido_maquetacion_id' => 'int',
'cantidad' => 'float', 'cantidad' => 'float',
'precio_unidad' => 'float',
'iva' => 'float', 'iva' => 'float',
'base' => 'float', 'base' => 'float',
'total_iva' => 'float', 'total_iva' => 'float',

View File

@ -14,7 +14,6 @@ class FacturaLineaModel extends \App\Models\BaseModel {
'pedido_maquetacion_id', 'pedido_maquetacion_id',
'descripcion', 'descripcion',
'cantidad', 'cantidad',
'precio_unidad',
'iva', 'iva',
'base', 'base',
'total_iva', 'total_iva',
@ -38,7 +37,7 @@ class FacturaLineaModel extends \App\Models\BaseModel {
->select( ->select(
"t1.id AS id, t1.factura_id AS factura_id, "t1.id AS id, t1.factura_id AS factura_id,
t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id, t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id,
t1.descripcion AS descripcion, t1.cantidad as cantidad, t1.precio_unidad AS precio_unidad, t1.iva AS iva, t1.descripcion AS descripcion, t1.cantidad as cantidad, t1.iva AS iva,
t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data, t2.pedido_id AS pedido_id, t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data, t2.pedido_id AS pedido_id,
t3.total_aceptado AS total_aceptado, t4.tirada_flexible AS tirada_flexible, t4.descuento_tirada_flexible AS descuento_tirada_flexible, t3.total_aceptado AS total_aceptado, t4.tirada_flexible AS tirada_flexible, t4.descuento_tirada_flexible AS descuento_tirada_flexible,
t6.cantidad AS cantidad_albaran" t6.cantidad AS cantidad_albaran"
@ -89,7 +88,7 @@ class FacturaLineaModel extends \App\Models\BaseModel {
->select( ->select(
"t1.id AS id, t1.factura_id AS factura_id, "t1.id AS id, t1.factura_id AS factura_id,
t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id, t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id,
t1.descripcion AS descripcion, t1.cantidad as cantidad, t1.precio_unidad AS precio_unidad, t1.iva AS iva, t1.descripcion AS descripcion, t1.cantidad as cantidad, t1.iva AS iva,
t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data" t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data"
) )
->where("t1.factura_id", $factura_id) ->where("t1.factura_id", $factura_id)

View File

@ -20,7 +20,6 @@
<th ></th> <th ></th>
<th style="width: 10%;"><?= lang('Facturas.unidades') ?></th> <th style="width: 10%;"><?= lang('Facturas.unidades') ?></th>
<th ><?= lang('Facturas.concepto') ?></th> <th ><?= lang('Facturas.concepto') ?></th>
<th style="width: 10%;"><?= lang('Facturas.precioUnidad') ?></th>
<th style="width: 10%;"><?= lang('Facturas.iva') ?></th> <th style="width: 10%;"><?= lang('Facturas.iva') ?></th>
<th style="width: 10%;"><?= lang('Facturas.subtotal') ?></th> <th style="width: 10%;"><?= lang('Facturas.subtotal') ?></th>
<th></th> <th></th>
@ -31,26 +30,26 @@
</tbody> </tbody>
<tfoot> <tfoot>
<tr> <tr>
<td colspan="9" style="text-align:right">Subtotal:</td> <td colspan="8" style="text-align:right">Subtotal:</td>
<td id="subtotal-sum"></td> <td class="autonumeric" id="subtotal-sum"></td>
<td></td> <td></td>
<td></td> <td></td>
</tr> </tr>
<tr> <tr>
<td colspan="9" style="text-align:right">I.V.A.:</td> <td colspan="8" style="text-align:right">I.V.A.:</td>
<td id="total-iva-sum"></td> <td class="autonumeric" id="total-iva-sum"></td>
<td></td> <td></td>
<td></td> <td></td>
</tr> </tr>
<tr> <tr>
<td colspan="9" style="text-align:right">Total:</td> <td colspan="8" style="text-align:right">Total:</td>
<td id="total-sum"></td> <td class="autonumeric" id="total-sum"></td>
<td></td> <td></td>
<td></td> <td></td>
</tr> </tr>
<tr> <tr>
<td <?= ($facturaEntity->serie_id == 7 || $facturaEntity->serie_id == 9)? "style='display:none;'":"" ?> colspan="9" style="text-align:right">Pendiente de pago:</td> <td <?= ($facturaEntity->serie_id == 7 || $facturaEntity->serie_id == 9)? "style='display:none;'":"" ?> colspan="8" style="text-align:right">Pendiente de pago:</td>
<td <?= ($facturaEntity->serie_id == 7 || $facturaEntity->serie_id == 9)? "style='display:none;'":"" ?> id="pendiente-pago"></td> <td class="autonumeric" <?= ($facturaEntity->serie_id == 7 || $facturaEntity->serie_id == 9)? "style='display:none;'":"" ?> id="pendiente-pago"></td>
<td></td> <td></td>
<td></td> <td></td>
</tr> </tr>
@ -79,55 +78,77 @@
<?=$this->section('additionalInlineJs') ?> <?=$this->section('additionalInlineJs') ?>
const actionBtns = function(data) { const actionBtns = function(data) {
<?php if ($facturaEntity->estado != 'borrador') : ?>
// se comprueba si data es null if (data.pedido_id != null) {
<?php if($facturaEntity->estado != 'borrador') :?> return `
if (data.pedido_id != null) { <div class="text-right py-0 align-middle">
return ` <div class="row">
<div class="text-right py-0 align-middle"> <button type="button" class="btn btn-sm btn-primary btn-view_pedido" data-id="${data.pedido_id}">Ver pedido</button>
<div class="row"> </div>
<button type="button" class="btn btn-sm btn-primary btn-view_pedido" data-id="${data.pedido_id}">Ver pedido</button> </div>
</div> `;
</div> } else {
`; return ``;
} }
else
{
return ``;
}
<?php else: ?> <?php else: ?>
if (data.pedido_id === null) { if (data.pedido_id === null) {
return ` return `
<td class="text-right py-0 align-middle"> <div class="text-right py-0 align-middle">
<div class="row mb-2"> <div class="row mb-2">
<div class="btn-group btn-group-sm"> <div class="btn-group btn-group-sm">
<a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}"></i></a> <a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}"></i></a>
<span class="edit"><a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></a></span> <span class="edit"><a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></a></span>
<span class="cancel"></span> <span class="cancel"></span>
</div>
</div>
</div> </div>
</div> `;
</td>`; } else {
} return `
else{ <div class="text-right py-0 align-middle">
return ` <div class="row mb-2 align-middle">
<div class="text-right py-0 align-middle"> <div class="btn-group btn-group-sm">
<div class="row mb-2 align-middle"> <a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}"></i></a>
<div class="btn-group btn-group-sm"> <span class="edit"><a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></a></span>
<a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete mx-2" data-id="${data.id}"></i></a> <span class="cancel"></span>
<span class="edit"><a href="javascript:void(0);"><i class="ti ti-pencil ti-sm btn-edit mx-2" data-id="${data.id}"></i></a></span> </div>
<span class="cancel"></span> </div>
<div class="row">
<button type="button" class="btn btn-sm btn-primary btn-view_pedido" data-id="${data.pedido_id}">Ver pedido</button>
</div>
</div> </div>
</div> `;
<div class="row"> }
<button type="button" class="btn btn-sm btn-primary btn-view_pedido" data-id="${data.pedido_id}">Ver pedido</button>
</div>
</div>
`;
}
<?php endif; ?> <?php endif; ?>
}; };
const autoNumericSubtotal = new AutoNumeric('#subtotal-sum', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
const autoNumericIVA = new AutoNumeric('#total-iva-sum', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
const autoNumericTotal = new AutoNumeric('#total-sum', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
const autoNumericPendientePago = new AutoNumeric('#pendiente-pago', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
var editor_lineas = new $.fn.dataTable.Editor( { var editor_lineas = new $.fn.dataTable.Editor( {
ajax: { ajax: {
@ -153,14 +174,6 @@ var editor_lineas = new $.fn.dataTable.Editor( {
rows: 5, rows: 5,
style: "height: 120px;" style: "height: 120px;"
} }
}, {
name: "precio_unidad",
attr: {
type: "text",
name : "precio_unidad",
class :"autonumeric",
style: "min-width: 65px;"
}
}, { }, {
name: "iva", name: "iva",
attr: { attr: {
@ -187,56 +200,52 @@ var editor_lineas = new $.fn.dataTable.Editor( {
var old_cantidad = 0; var old_cantidad = 0;
editor_lineas.on('open', (event) => { editor_lineas.on('open', (event) => {
$("input.autonumeric").each(function () { $("input.autonumeric").each(function () {
let autoNumericInstance = AutoNumeric.getAutoNumericElement(this) let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
if (autoNumericInstance) { if (autoNumericInstance) {
autoNumericInstance.remove() autoNumericInstance.remove()
} }
new AutoNumeric(this, { new AutoNumeric(this, {
decimalCharacter: ",", decimalCharacter: ",",
digitGroupSeparator: ".", digitGroupSeparator: ".",
allowDecimalPadding : 'floats', allowDecimalPadding : 'floats',
decimalPlaces: 2, decimalPlaces: 2,
unformatOnSubmit: true, unformatOnSubmit: true,
}); });
})
}) })
editor_lineas.on('preSubmit', (e, d, type) => { })
if (d.data) { editor_lineas.on('preSubmit', (e, d, type) => {
Object.keys(d.data).forEach(function (key) { if (d.data) {
// Find all elements with class .autonumeric if ( type === 'create'){
$("input.autonumeric").each(function () { d.data[0]['factura_id'] = <?= $facturaEntity->id ?>;
let autoNumericInstance = AutoNumeric.getAutoNumericElement(this) d.data[0]['old_cantidad'] = null;
if (autoNumericInstance) { }
// Get raw value and update the corresponding field else if(type === 'edit' ) {
let rawValue = autoNumericInstance.getNumericString(); for (v in d.data){
d.data[key][this.name] = rawValue; // Ensure the correct name attribute is used d.data[v]['factura_id'] = <?= $facturaEntity->id ?>;
} d.data[v]['old_cantidad'] = old_cantidad;
});
});
} }
}) }
Object.keys(d.data).forEach(function (key) {
// Find all elements with class .autonumeric
$("input.autonumeric").each(function () {
let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
if (autoNumericInstance) {
// Get raw value and update the corresponding field
let rawValue = autoNumericInstance.getNumericString();
d.data[key][this.name] = rawValue; // Ensure the correct name attribute is used
}
});
});
}
})
editor_lineas.on( 'preEdit', function ( e, json, data, id ) { editor_lineas.on( 'preEdit', function ( e, json, data, id ) {
old_cantidad = data.cantidad; old_cantidad = data.cantidad;
}); });
editor_lineas.on( 'preSubmit', function ( e, d, type ) {
if ( type === 'create'){
d.data[0]['factura_id'] = <?= $facturaEntity->id ?>;
d.data[0]['old_cantidad'] = null;
}
else if(type === 'edit' ) {
for (v in d.data){
d.data[v]['factura_id'] = <?= $facturaEntity->id ?>;
d.data[v]['old_cantidad'] = old_cantidad;
}
}
});
editor_lineas.on( 'postSubmit', function ( e, json, data, action ) { editor_lineas.on( 'postSubmit', function ( e, json, data, action ) {
@ -310,7 +319,7 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
<div class="row mt-2"> <div class="row mt-2">
<div style="margin-top: auto;" class="col-md-12 col-lg-2 px-4"> <div style="margin-top: auto;" class="col-md-12 col-lg-2 px-4">
<label for="total-aceptado_-${meta.row}">Total aceptado</label> <label for="total-aceptado_-${meta.row}">Total aceptado</label>
<input readonly style="max-width: 110px;" type="text" id="total-aceptado_${meta.row}" value="${row.total_aceptado}"> <input readonly class="autonumeric-datatable-lineas" style="max-width: 110px;" type="text" id="total-aceptado_${meta.row}" value="${row.total_aceptado}">
</div> </div>
`; `;
@ -366,7 +375,6 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
} }
} }
}, },
{data: "precio_unidad",render : (d) => `<span class="autonumeric">${d}</span>`},
{data: "iva",render : (d) => `<span class="autonumeric">${d}</span>`}, {data: "iva",render : (d) => `<span class="autonumeric">${d}</span>`},
{data: "base",render : (d) => `<span class="autonumeric">${d}</span>`}, {data: "base",render : (d) => `<span class="autonumeric">${d}</span>`},
{data: "total_iva",render : (d) => `<span class="autonumeric">${d}</span>`}, {data: "total_iva",render : (d) => `<span class="autonumeric">${d}</span>`},
@ -391,94 +399,75 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
}, },
{ {
visible: false, visible: false,
targets: [1, 2, 3, 4, 10, 11] targets: [1, 2, 3, 4, 9, 10]
} }
], ],
drawCallback: function() { drawCallback: function() {
$(this.api().table().container()).find('table').css('width', '100%'); $(this.api().table().container()).find('table').css('width', '100%');
this.api().columns.adjust(); this.api().columns.adjust();
$('.autonumeric-datatable-lineas').each(function() {
if (!$(this).data('autoNumeric')) {
new AutoNumeric(this, {
decimalPlaces: 2,
digitGroupSeparator: '.',
decimalCharacter: ',',
unformatOnSubmit: true
});
}
});
}, },
footerCallback: function (row, data, start, end, display) { footerCallback: function (row, data, start, end, display) {
updateFooterLineas(this.api()); const table = this.api();
const intVal = i => typeof i === 'string' ? parseFloat(i.replace(/\./g, '').replace(',', '.')) || 0 : i || 0;
const totalSubtotal = table.column(8).data().reduce((a, b) => intVal(a) + intVal(b), 0);
const totalIVA = table.column(9).data().reduce((a, b) => intVal(a) + intVal(b), 0);
const totalTotal = table.column(10).data().reduce((a, b) => intVal(a) + intVal(b), 0);
autoNumericSubtotal.set(totalSubtotal);
autoNumericIVA.set(totalIVA);
autoNumericTotal.set(totalTotal);
<?php if($facturaEntity->estado == 'borrador') :?>
var pendientePago = totalTotal;
var total_pagos = 0;
<?php else: ?>
var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = totalTotal - total_pagos;
<?php endif; ?>
if (isNaN(pendientePago)) pendientePago = 0;
autoNumericPendientePago.set(pendientePago);
$.ajax({
url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>',
method: 'POST',
data: {
base: totalSubtotal,
total: totalTotal,
total_pagos: total_pagos,
pendiente: pendientePago,
<?= csrf_token() ?? "token" ?> : <?= csrf_token() ?>v
}
}).done(function (data) {
if (data.estado_pago === 'pagada') {
$('#estado_pago_text').text('<?= lang('Facturas.pagada') ?>').css('color', 'green');
} else {
$('#estado_pago_text').text('<?= lang('Facturas.pendiente') ?>').css('color', 'red');
}
}).fail(function (jqXHR) {
popErrorAlert(jqXHR.responseJSON.messages.error);
});
} }
}); });
function updateFooterLineas(table){
// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
var totalSubtotal = table
.column(9)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var totalIVA = table
.column(10)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var totalTotal = table
.column(11)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
$('#subtotal-sum').html(totalSubtotal.toFixed(2));
$('#total-iva-sum').html(totalIVA.toFixed(2));
$('#total-sum').html(totalTotal.toFixed(2));
// Assuming pendiente de pago is totalTotal - totalIVA for this example
<?php if($facturaEntity->estado == 'borrador') :?>
var pendientePago = totalTotal ;
var total_pagos = 0;
<?php else: ?>
var total_pagos = parseFloat($('#totalCobrado-sum').html()).toFixed(2);
var pendientePago = totalTotal - total_pagos;
<?php endif; ?>
if(isNaN(pendientePago)){
pendientePago = 0;
}
$('#pendiente-pago').html(pendientePago.toFixed(2));
$.ajax({
url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>',
method: 'POST',
data: {
base: totalSubtotal,
total: totalTotal,
total_pagos: total_pagos,
pendiente: pendientePago,
<?= csrf_token() ?? "token" ?> : <?= csrf_token() ?>v
}
}).done((data, textStatus, jqXHR) => {
if(data.estado_pago == 'pagada'){
$('#estado_pago_text').text('<?= lang('Facturas.pagada') ?>');
$('#estado_pago_text').css('color', 'green');
}
else{
$('#estado_pago_text').text('<?= lang('Facturas.pendiente') ?>');
$('#estado_pago_text').css('color', 'red');
}
}).fail((jqXHR, textStatus, errorThrown) => {
popErrorAlert(jqXHR.responseJSON.messages.error)
})
}
// Delete row // Delete row
$(document).on('click', '.btn-delete', function(e) { $(document).on('click', '.btn-delete', function(e) {
@ -577,7 +566,6 @@ $(document).on('click', '.btn-excedentes', function(e) {
pedido_id: pedido_id, pedido_id: pedido_id,
pedido_linea_impresion_id: pedido_linea_impresion_id, pedido_linea_impresion_id: pedido_linea_impresion_id,
pedido_linea_maquetacion_id: pedido_linea_maquetacion_id, pedido_linea_maquetacion_id: pedido_linea_maquetacion_id,
precio_unidad: row_data.precio_unidad,
descripcion: row_data.descripcion, descripcion: row_data.descripcion,
iva: row_data.iva, iva: row_data.iva,
descuento: row_data.descuento_tirada_flexible, descuento: row_data.descuento_tirada_flexible,

View File

@ -73,7 +73,7 @@ const actionBtns_pagos = function(data) {
<?php endif; ?> <?php endif; ?>
} }
var editor_pagos = new $.fn.dataTable.Editor( { const editor_pagos = new $.fn.dataTable.Editor( {
ajax: { ajax: {
url: "<?= route_to('editorOfPagosFacturas') ?>", url: "<?= route_to('editorOfPagosFacturas') ?>",
headers: { headers: {
@ -99,8 +99,10 @@ var editor_pagos = new $.fn.dataTable.Editor( {
{ {
name: "total", name: "total",
attr: { attr: {
style: "min-width: 65px;" style: "min-width: 65px;",
} class :"autonumeric",
name: 'total'
},
}, },
{ {
name: "fecha_pago_at", name: "fecha_pago_at",
@ -132,17 +134,46 @@ var editor_pagos = new $.fn.dataTable.Editor( {
] ]
}); });
editor_pagos.on('open',(event)=>{
$("input.autonumeric").each(function () {
let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
if(autoNumericInstance){
autoNumericInstance.remove()
}
new AutoNumeric(this, {
digitGroupSeparator: ".",
decimalCharacter: ",",
allowDecimalPadding : 'floats',
decimalPlaces: 2,
unformatOnSubmit: true,
});
})
})
editor_pagos.on( 'preSubmit', function ( e, d, type ) { editor_pagos.on( 'preSubmit', function ( e, d, type ) {
if ( type === 'create'){ if (d.data) {
d.data[0]['factura_id'] = <?= $facturaEntity->id ?>; if ( type === 'create'){
} d.data[0]['factura_id'] = <?= $facturaEntity->id ?>;
else if(type === 'edit' ) {
for (v in d.data){
d.data[v]['factura_id'] = <?= $facturaEntity->id ?>;
} }
else if(type === 'edit' ) {
for (v in d.data){
d.data[v]['factura_id'] = <?= $facturaEntity->id ?>;
}
}
Object.keys(d.data).forEach(function (key) {
// Find all elements with class .autonumeric
$("input.autonumeric").each(function () {
let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
if (autoNumericInstance) {
// Get raw value and update the corresponding field
let rawValue = autoNumericInstance.getNumericString();
d.data[key][this.name] = rawValue; // Ensure the correct name attribute is used
}
});
});
} }
}); });
@ -154,6 +185,19 @@ editor_pagos.on( 'postSubmit', function ( e, json, data, action ) {
tablePagos.draw(); tablePagos.draw();
}); });
const autoNumericTotalCobrado = new AutoNumeric('#totalCobrado-sum', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
const autoNumericPendienteCobro = new AutoNumeric('#pendienteCobro-sum', 0, {
decimalPlaces: 2,
digitGroupSeparator: ',',
decimalCharacter: '.',
unformatOnSubmit: true
});
var tablePagos = $('#tableOfLineasPagos').DataTable({ var tablePagos = $('#tableOfLineasPagos').DataTable({
processing: true, processing: true,
@ -193,7 +237,7 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
return data!='0000-00-00 00:00:00' ? moment(data).format('DD/MM/YYYY') : ''; return data!='0000-00-00 00:00:00' ? moment(data).format('DD/MM/YYYY') : '';
} }
}, },
{data: "total"}, {data: "total",render : (d) => `<span class="autonumeric">${d}</span>`},
], ],
order: [[1, "asc"]], order: [[1, "asc"]],
dom: 't', dom: 't',
@ -259,8 +303,8 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
}, 0); }, 0);
// Update footer // Update footer
$('#totalCobrado-sum').html(totalPagos.toFixed(2)); autoNumericTotalCobrado.set(totalPagos);
$('#pendienteCobro-sum').html(totalPendiente.toFixed(2)); autoNumericPendienteCobro.set(totalPendiente);
// call footerCallback of the other table // call footerCallback of the other table
if (typeof tableLineas !== 'undefined') { if (typeof tableLineas !== 'undefined') {
@ -269,63 +313,29 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
} }
}); });
function updateFooterLineas(table){ function updateFooterLineas(table){
// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
var totalSubtotal = table
.column(9)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var totalIVA = table
.column(10)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var totalTotal = table
.column(11)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
$('#subtotal-sum').html(totalSubtotal.toFixed(2));
$('#total-iva-sum').html(totalIVA.toFixed(2));
$('#total-sum').html(totalTotal.toFixed(2));
// Assuming pendiente de pago is totalTotal - totalIVA for this example // Assuming pendiente de pago is totalTotal - totalIVA for this example
<?php if($facturaEntity->estado == 'borrador') :?> <?php if($facturaEntity->estado == 'borrador') :?>
var pendientePago = totalTotal ; var pendientePago = autoNumericTotal.getNumber();
var total_pagos = 0; var total_pagos = 0;
<?php else: ?> <?php else: ?>
var total_pagos = parseFloat($('#totalCobrado-sum').html()).toFixed(2); var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = totalTotal - total_pagos; var pendientePago = autoNumericTotal.getNumber() - total_pagos;
<?php endif; ?> <?php endif; ?>
// Se comprueba si pendientePago es un numero o NAN // Se comprueba si pendientePago es un numero o NAN
if(isNaN(pendientePago)){ if(isNaN(pendientePago)){
pendientePago = 0; pendientePago = 0;
} }
$('#pendiente-pago').html(pendientePago.toFixed(2)); autoNumericPendientePago.set(pendientePago);
$.ajax({ $.ajax({
url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>', url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>',
method: 'POST', method: 'POST',
data: { data: {
base: totalSubtotal, base: autoNumericSubtotal.getNumber(),
total: totalTotal, total: autoNumericTotal.getNumber(),
total_pagos: total_pagos, total_pagos: total_pagos,
pendiente: pendientePago pendiente: pendientePago
} }