This commit is contained in:
amazuecos
2025-04-11 03:34:10 +02:00
9 changed files with 427 additions and 413 deletions

View File

@ -692,7 +692,9 @@ class Facturas extends \App\Controllers\BaseResourceController
// Se añade la linea de factura
$descripcion = $model_presupuesto->generarLineaPedido($presupuesto->id, true, $linea->pedido_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);
$total_iva = $base * ($presupuesto->iva_reducido == 1 ? 0.04 : 0.21);
// se redondea a dos decimales
@ -704,7 +706,6 @@ class Facturas extends \App\Controllers\BaseResourceController
'pedido_linea_impresion_id' => $linea->pedido_id,
'descripcion' => $descripcion[0]->concepto,
'cantidad' => $cantidad,
'precio_unidad' => $presupuesto->total_precio_unidad,
'iva' => $presupuesto->iva_reducido == 1 ? 4 : 21,
'base' => $base,
'total_iva' => $total_iva,

View File

@ -101,15 +101,6 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
->validator('Validate::notEmpty', array(
'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' )
->validator('Validate::numeric', array(
'message' => lang('Facturas.validation.numerico'))
@ -128,7 +119,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$totales = $this->generate_totales(
$values['factura_id'],
$values['pedido_linea_impresion_id'],
$values['precio_unidad'],
$values['total'],
$values['iva'],
$values['cantidad'],
$values['old_cantidad']);
@ -152,7 +143,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$totales = $this->generate_totales(
$values['factura_id'],
$values['pedido_linea_impresion_id'],
$values['precio_unidad'],
$values['total'],
$values['iva'],
$values['cantidad'],
$values['old_cantidad']);
@ -203,7 +194,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
$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
@ -214,6 +205,7 @@ class FacturasLineas extends \App\Controllers\BaseResourceController
// se calcula y se actualiza el subtotal, total_iva y total
// 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);
$total_iva = round($base * $iva / 100, 2);
$total = round($base + $total_iva, 2);

View File

@ -57,10 +57,7 @@ class FacturasPagos extends \App\Controllers\BaseResourceController
->fields(
Field::inst( 'id' ),
Field::inst( 'forma_pago_id' ),
Field::inst( 'notes' )
->validator('Validate::notEmpty', array(
'message' => lang('Facturas.validation.requerido'))
),
Field::inst( 'notes' ),
Field::inst( 'fecha_pago_at' )
->validator( Validate::dateFormat( '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,
'descripcion' => null,
'cantidad' => null,
'precio_unidad' => null,
'iva' => null,
'base' => null,
'total_iva' => null,
@ -29,7 +28,6 @@ class FacturaLineaEntity extends \CodeIgniter\Entity\Entity
'pedido_linea_impresion_id' => 'int',
'pedido_maquetacion_id' => 'int',
'cantidad' => 'float',
'precio_unidad' => 'float',
'iva' => 'float',
'base' => 'float',
'total_iva' => 'float',

View File

@ -14,7 +14,6 @@ class FacturaLineaModel extends \App\Models\BaseModel {
'pedido_maquetacion_id',
'descripcion',
'cantidad',
'precio_unidad',
'iva',
'base',
'total_iva',
@ -38,7 +37,7 @@ class FacturaLineaModel extends \App\Models\BaseModel {
->select(
"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.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,
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"
@ -89,7 +88,7 @@ class FacturaLineaModel extends \App\Models\BaseModel {
->select(
"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.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"
)
->where("t1.factura_id", $factura_id)

View File

@ -20,7 +20,6 @@
<th ></th>
<th style="width: 10%;"><?= lang('Facturas.unidades') ?></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.subtotal') ?></th>
<th></th>
@ -31,26 +30,26 @@
</tbody>
<tfoot>
<tr>
<td colspan="9" style="text-align:right">Subtotal:</td>
<td id="subtotal-sum"></td>
<td colspan="8" style="text-align:right">Subtotal:</td>
<td class="autonumeric" id="subtotal-sum"></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="9" style="text-align:right">I.V.A.:</td>
<td id="total-iva-sum"></td>
<td colspan="8" style="text-align:right">I.V.A.:</td>
<td class="autonumeric" id="total-iva-sum"></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="9" style="text-align:right">Total:</td>
<td id="total-sum"></td>
<td colspan="8" style="text-align:right">Total:</td>
<td class="autonumeric" id="total-sum"></td>
<td></td>
<td></td>
</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;'":"" ?> id="pendiente-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 class="autonumeric" <?= ($facturaEntity->serie_id == 7 || $facturaEntity->serie_id == 9)? "style='display:none;'":"" ?> id="pendiente-pago"></td>
<td></td>
<td></td>
</tr>
@ -79,9 +78,7 @@
<?=$this->section('additionalInlineJs') ?>
const actionBtns = function(data) {
// se comprueba si data es null
<?php if($facturaEntity->estado != 'borrador') :?>
<?php if ($facturaEntity->estado != 'borrador') : ?>
if (data.pedido_id != null) {
return `
<div class="text-right py-0 align-middle">
@ -90,16 +87,13 @@ const actionBtns = function(data) {
</div>
</div>
`;
}
else
{
} else {
return ``;
}
<?php else: ?>
if (data.pedido_id === null) {
return `
<td class="text-right py-0 align-middle">
<div class="text-right py-0 align-middle">
<div class="row mb-2">
<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>
@ -107,9 +101,9 @@ const actionBtns = function(data) {
<span class="cancel"></span>
</div>
</div>
</td>`;
}
else{
</div>
`;
} else {
return `
<div class="text-right py-0 align-middle">
<div class="row mb-2 align-middle">
@ -128,6 +122,33 @@ const actionBtns = function(data) {
<?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( {
ajax: {
@ -153,14 +174,6 @@ var editor_lineas = new $.fn.dataTable.Editor( {
rows: 5,
style: "height: 120px;"
}
}, {
name: "precio_unidad",
attr: {
type: "text",
name : "precio_unidad",
class :"autonumeric",
style: "min-width: 65px;"
}
}, {
name: "iva",
attr: {
@ -202,8 +215,18 @@ editor_lineas.on('open', (event) => {
});
})
})
editor_lineas.on('preSubmit', (e, d, type) => {
editor_lineas.on('preSubmit', (e, d, type) => {
if (d.data) {
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;
}
}
Object.keys(d.data).forEach(function (key) {
// Find all elements with class .autonumeric
$("input.autonumeric").each(function () {
@ -216,27 +239,13 @@ editor_lineas.on('open', (event) => {
});
});
}
})
})
editor_lineas.on( 'preEdit', function ( e, json, data, id ) {
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 ) {
@ -310,7 +319,7 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
<div class="row mt-2">
<div style="margin-top: auto;" class="col-md-12 col-lg-2 px-4">
<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>
`;
@ -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: "base",render : (d) => `<span class="autonumeric">${d}</span>`},
{data: "total_iva",render : (d) => `<span class="autonumeric">${d}</span>`},
@ -391,69 +399,50 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
},
{
visible: false,
targets: [1, 2, 3, 4, 10, 11]
targets: [1, 2, 3, 4, 9, 10]
}
],
drawCallback: function() {
$(this.api().table().container()).find('table').css('width', '100%');
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) {
updateFooterLineas(this.api());
}
});
const table = this.api();
const intVal = i => typeof i === 'string' ? parseFloat(i.replace(/\./g, '').replace(',', '.')) || 0 : i || 0;
function updateFooterLineas(table){
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);
// 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;
};
autoNumericSubtotal.set(totalSubtotal);
autoNumericIVA.set(totalIVA);
autoNumericTotal.set(totalTotal);
// 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 pendientePago = totalTotal;
var total_pagos = 0;
<?php else: ?>
var total_pagos = parseFloat($('#totalCobrado-sum').html()).toFixed(2);
var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = totalTotal - total_pagos;
<?php endif; ?>
if(isNaN(pendientePago)){
pendientePago = 0;
}
$('#pendiente-pago').html(pendientePago.toFixed(2));
if (isNaN(pendientePago)) pendientePago = 0;
autoNumericPendientePago.set(pendientePago);
$.ajax({
url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>',
@ -465,19 +454,19 @@ function updateFooterLineas(table){
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');
}).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');
}
else{
$('#estado_pago_text').text('<?= lang('Facturas.pendiente') ?>');
$('#estado_pago_text').css('color', 'red');
}).fail(function (jqXHR) {
popErrorAlert(jqXHR.responseJSON.messages.error);
});
}
}).fail((jqXHR, textStatus, errorThrown) => {
popErrorAlert(jqXHR.responseJSON.messages.error)
})
}
});
// Delete row
$(document).on('click', '.btn-delete', function(e) {
@ -577,7 +566,6 @@ $(document).on('click', '.btn-excedentes', function(e) {
pedido_id: pedido_id,
pedido_linea_impresion_id: pedido_linea_impresion_id,
pedido_linea_maquetacion_id: pedido_linea_maquetacion_id,
precio_unidad: row_data.precio_unidad,
descripcion: row_data.descripcion,
iva: row_data.iva,
descuento: row_data.descuento_tirada_flexible,

View File

@ -73,7 +73,7 @@ const actionBtns_pagos = function(data) {
<?php endif; ?>
}
var editor_pagos = new $.fn.dataTable.Editor( {
const editor_pagos = new $.fn.dataTable.Editor( {
ajax: {
url: "<?= route_to('editorOfPagosFacturas') ?>",
headers: {
@ -99,8 +99,10 @@ var editor_pagos = new $.fn.dataTable.Editor( {
{
name: "total",
attr: {
style: "min-width: 65px;"
}
style: "min-width: 65px;",
class :"autonumeric",
name: 'total'
},
},
{
name: "fecha_pago_at",
@ -132,10 +134,27 @@ 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 ) {
if (d.data) {
if ( type === 'create'){
d.data[0]['factura_id'] = <?= $facturaEntity->id ?>;
}
@ -144,6 +163,18 @@ editor_pagos.on( 'preSubmit', function ( e, d, type ) {
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();
});
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({
processing: true,
@ -193,7 +237,7 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
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"]],
dom: 't',
@ -259,8 +303,8 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
}, 0);
// Update footer
$('#totalCobrado-sum').html(totalPagos.toFixed(2));
$('#pendienteCobro-sum').html(totalPendiente.toFixed(2));
autoNumericTotalCobrado.set(totalPagos);
autoNumericPendienteCobro.set(totalPendiente);
// call footerCallback of the other table
if (typeof tableLineas !== 'undefined') {
@ -269,63 +313,29 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({
}
});
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 pendientePago = autoNumericTotal.getNumber();
var total_pagos = 0;
<?php else: ?>
var total_pagos = parseFloat($('#totalCobrado-sum').html()).toFixed(2);
var pendientePago = totalTotal - total_pagos;
var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = autoNumericTotal.getNumber() - total_pagos;
<?php endif; ?>
// Se comprueba si pendientePago es un numero o NAN
if(isNaN(pendientePago)){
pendientePago = 0;
}
$('#pendiente-pago').html(pendientePago.toFixed(2));
autoNumericPendientePago.set(pendientePago);
$.ajax({
url: '<?= route_to('updateFacturaTotales', $facturaEntity->id) ?>',
method: 'POST',
data: {
base: totalSubtotal,
total: totalTotal,
base: autoNumericSubtotal.getNumber(),
total: autoNumericTotal.getNumber(),
total_pagos: total_pagos,
pendiente: pendientePago
}

File diff suppressed because one or more lines are too long