613 lines
20 KiB
JavaScript
613 lines
20 KiB
JavaScript
var dtTemp;
|
|
var clienteTemp;
|
|
var lista_clientes;
|
|
var fileInput;
|
|
var file;
|
|
var cadenaB64 = '';
|
|
var textoCliente = '';
|
|
var dataTable;
|
|
|
|
|
|
var verDup=0;
|
|
|
|
const p = document.getElementById("p_errores")
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
document.getElementById('fileInput').addEventListener('change', function () {
|
|
cadenaB64 = '';
|
|
fileInput = this;
|
|
file = fileInput.files[0]; // Obtener el primer archivo seleccionado
|
|
|
|
if (file) {
|
|
if (file.type !== 'application/pdf') {
|
|
alertify.error('Solo se admiten archivos PDF');
|
|
fileInput.value = '';
|
|
return;
|
|
}
|
|
var reader = new FileReader();
|
|
reader.onload = function () {
|
|
cadenaB64 = reader.result.split(',')[1]; // Obtener la parte Base64
|
|
};
|
|
reader.readAsDataURL(file);
|
|
}
|
|
});
|
|
});
|
|
|
|
$(document).ready(function () {
|
|
document.getElementById('searchInput').addEventListener('input', function (event) {
|
|
showResults(event.target.value);
|
|
});
|
|
construyeDT();
|
|
obtenerViaPago();
|
|
$('#buttons-container').on('click', '#getSelectedRows', function () {
|
|
var dataTable = $('#pagosDT').DataTable(); // Asegúrate de obtener la instancia de DataTable por su ID
|
|
var filas = [];
|
|
dataTable.rows().every(function () {
|
|
var data = this.data();
|
|
var rowNode = this.node();
|
|
var isChecked = $(rowNode).find('.row-check').is(':checked');
|
|
if (isChecked) {
|
|
filas.push(data);
|
|
//console.log(data);
|
|
}
|
|
|
|
});
|
|
|
|
// Muestra el modal y pasa los datos seleccionados a la función facturasPagar
|
|
$('#modalPagar').modal('show');
|
|
console.log(filas);
|
|
facturasPagar(filas);
|
|
// console.log(filas); // Aquí puedes manejar los datos seleccionados como necesites
|
|
});
|
|
|
|
function construyeDT() {
|
|
var columns = [
|
|
{ data: null, defaultContent: '<input type="checkbox" class="row-check">', orderable: false, title: '--' },
|
|
{ data: 'factura_id', visible: false },
|
|
{ data: 'UUID', title: 'UUID' },
|
|
{ data: 'TOTAL', title: 'Total Factura' },
|
|
{
|
|
data: 'parcialidad', title: 'Parcialidad',
|
|
render: function (data, type, row) {
|
|
var parcialidad = parseFloat(parseFloat(row.parcialidad).toFixed(2));
|
|
var saldo_insoluto = parseFloat(parseFloat(row.saldo_insoluto).toFixed(2));
|
|
var total = parseFloat(parseFloat(row.TOTAL).toFixed(2));
|
|
if (saldo_insoluto == total) {
|
|
return '1';
|
|
} else if (saldo_insoluto < total && saldo_insoluto != 0) {
|
|
parcialidad = parcialidad + 1;
|
|
return parcialidad;
|
|
} else if (saldo_insoluto == 0) {
|
|
return parcialidad;
|
|
}
|
|
}
|
|
},
|
|
{ data: 'saldo_insoluto', title: 'Saldo insoluto' },
|
|
{ data: 'fecha_venta', title: 'Fecha' },
|
|
|
|
];
|
|
|
|
// Destruye la DataTable si ya existe
|
|
if ($.fn.DataTable.isDataTable('#pagosDT')) {
|
|
$('#pagosDT').DataTable().destroy();
|
|
}
|
|
|
|
// Construye la DataTable
|
|
dataTable = $('#pagosDT').DataTable({
|
|
destroy: true,
|
|
lengthMenu: [10, 25, 50, 100],
|
|
pageLength: 10,
|
|
searching: true,
|
|
paging: true,
|
|
ordering: true,
|
|
info: true,
|
|
responsive: true,
|
|
dom: 'Bfrtip',
|
|
buttons: [{
|
|
extend: 'pdfHtml5',
|
|
text: 'PDF',
|
|
exportOptions: { modifier: { page: 'current' } },
|
|
className: 'btn-danger'
|
|
}, {
|
|
extend: 'excelHtml5',
|
|
text: 'Excel',
|
|
exportOptions: { modifier: { page: 'current' } },
|
|
className: 'btn-success'
|
|
}],
|
|
columns: columns,
|
|
data: []
|
|
});
|
|
|
|
// Agrega un mensaje si no hay datos en la DataTable
|
|
$('#pagosDT tbody').html('<tr class="odd"><td valign="top" colspan="' + columns.length + '" class="dataTables_empty">Sin facturas</td></tr>');
|
|
|
|
// Agrega el botón #getSelectedRows al contenedor de botones
|
|
$('#buttons-container').append('<button id="getSelectedRows" style="width: -webkit-fill-available;" class="btn btn-info">Agregar facturas</button>');
|
|
}
|
|
dataTable.on('draw.dt', function () {
|
|
$('#pagosDT tbody tr').each(function () {
|
|
var estatus = $(this).find('td:eq(4)').text(); // Ajusta el índice según la posición de 'estatus'
|
|
// console.log('estatus', estatus)
|
|
|
|
if (estatus === '0') {
|
|
$(this).find('td:eq(0)').html('<span class="badge badge-success">Pagada</span>'); // Cambia el contenido de la celda
|
|
} else {
|
|
$(this).find('td:eq(36)').html('<span class="badge badge-warning">Bloqueado</span>'); // Cambia el contenido de la celda
|
|
}
|
|
});
|
|
});
|
|
|
|
dtTemp = dataTable;
|
|
traerDeudores(dataTable);
|
|
|
|
});
|
|
|
|
|
|
// Función para construir la DataTable
|
|
|
|
|
|
function inicializarDataTable(cliente, dataTable) {
|
|
$.ajax({
|
|
url: 'php/ConsultasTablas/ManipulacionConsultas.php',
|
|
type: 'POST',
|
|
data: { opcion: '7', cliente: cliente },
|
|
dataType: 'json',
|
|
success: function (data) {
|
|
if (data.length > 0) {
|
|
// console.log('Agregar filas')
|
|
dataTable.clear().rows.add(data).draw();
|
|
|
|
} else {
|
|
$('#pagosDT tbody').html('<tr class="odd"><td valign="top" colspan="' + columns.length + '" class="dataTables_empty">No hay facturas PPD</td></tr>');
|
|
}
|
|
|
|
},
|
|
error: function (error) {
|
|
console.error('Error en la llamada AJAX', error);
|
|
}
|
|
});
|
|
}
|
|
|
|
function facturasPagar(data) {
|
|
$('#formaPago option[value="4"]').hide();//agregar para eliminar el por definir
|
|
var columns = [
|
|
{ data: 'factura_id', visible: false },
|
|
{ data: 'UUID', title: 'UUID', width: '20%' }, // Ejemplo de ancho específico
|
|
{
|
|
data: 'parcialidad', title: 'Parcialidad',
|
|
render: function (data, type, row) {
|
|
var parcialidad = parseFloat(parseFloat(row.parcialidad).toFixed(2));
|
|
var saldo_insoluto = parseFloat(parseFloat(row.saldo_insoluto).toFixed(2));
|
|
var total = parseFloat(parseFloat(row.TOTAL).toFixed(2));
|
|
if (saldo_insoluto == total) {
|
|
return '1';
|
|
} else if (saldo_insoluto < total && saldo_insoluto != 0) {
|
|
parcialidad = parcialidad + 1;
|
|
return parcialidad;
|
|
} else if (saldo_insoluto == 0) {
|
|
return parcialidad;
|
|
}
|
|
}
|
|
},
|
|
{ data: 'saldo_insoluto', title: 'Saldo insoluto', width: '20%' }, // Ejemplo de ancho del 20%
|
|
{
|
|
data: null,
|
|
render: function (data, type, row) {
|
|
return `<input type="number" class="form-control custom-input" oninput="calcular('${row.saldo_insoluto}',this);" value="${row.saldo_insoluto}" >`;
|
|
}
|
|
},
|
|
|
|
];
|
|
|
|
dataTable = $('#facturasPagar').DataTable({
|
|
destroy: true,
|
|
lengthMenu: [10, 25, 50, 100],
|
|
pageLength: 10,
|
|
searching: true,
|
|
paging: true,
|
|
ordering: true,
|
|
info: true,
|
|
responsive: true,
|
|
dom: 'Bfrtip',
|
|
buttons: [{
|
|
extend: 'pdfHtml5',
|
|
text: 'PDF',
|
|
exportOptions: {
|
|
modifier: {
|
|
page: 'current'
|
|
}
|
|
},
|
|
className: 'btn-danger' // Agregar la clase btn-danger al botón
|
|
}, {
|
|
extend: 'excelHtml5',
|
|
text: 'Excel',
|
|
exportOptions: {
|
|
modifier: {
|
|
page: 'current'
|
|
},
|
|
},
|
|
className: 'btn-success' // Agregar la clase btn-danger al botón de Excel
|
|
},
|
|
|
|
],
|
|
columns: columns,
|
|
data: []
|
|
});
|
|
|
|
dataTable.clear().rows.add(data).draw();
|
|
$('#facturasPagar').css('width', '-webkit-fill-available');
|
|
recalcularTotales();
|
|
|
|
// $('#facturasPagar').on('input', '.custom-input', function () {
|
|
// console.log('Funcion');
|
|
// var inputValue = parseFloat($(this).val());
|
|
// var saldo = parseFloat($(this).data('saldo'));
|
|
// if (inputValue > saldo) {
|
|
// console.log('MUESTRA SALDO')
|
|
// mostrarDialogo(this);
|
|
// } else {
|
|
// // Ocultar el cuadro de diálogo si ya está visible
|
|
// $(this).next('.custom-dialog').remove();
|
|
// }
|
|
// });
|
|
|
|
}
|
|
|
|
let deudores = [];
|
|
|
|
function dialogoExcede(inputElement) {
|
|
$(inputElement).next('.custom-dialog-excede').remove();
|
|
var dialogHtml = `
|
|
<div class="custom-dialog-excede alert alert-danger" role="alert">
|
|
Pago excede el saldo insoluto.
|
|
</div>
|
|
`;
|
|
$(inputElement).after(dialogHtml);
|
|
}
|
|
|
|
function dialogoMenor(inputElement) {
|
|
$(inputElement).next('.custom-dialog').remove();
|
|
var dialogHtml = `
|
|
<div class="custom-dialog alert alert-warning" role="alert">
|
|
Pago no debe ser menor al saldo insoluto.
|
|
</div>
|
|
`;
|
|
$(inputElement).after(dialogHtml);
|
|
}
|
|
|
|
function eliminarDialogoMenor(inputElement) {
|
|
$(inputElement).next('.custom-dialog').remove();
|
|
}
|
|
|
|
|
|
function eliminarDialogoExcede(inputElement) {
|
|
$(inputElement).next('.custom-dialog-excede').remove();
|
|
}
|
|
|
|
function calcular(saldo, input) {
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
|
|
var pago = parseFloat(parseFloat(input.value).toFixed(2));
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
var saldo_insoluto = parseFloat(parseFloat(saldo).toFixed(2));
|
|
|
|
console.log('Saldo', saldo);
|
|
// console.log('saldo', saldo_insoluto)
|
|
if (pago > saldo_insoluto && pago != 'NaN') {
|
|
dialogoExcede(input);
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
} else if (pago == saldo_insoluto || pago < saldo_insoluto || pago == 'NaN') {
|
|
eliminarDialogoExcede(input);
|
|
}
|
|
|
|
recalcularTotales(input);
|
|
}
|
|
|
|
|
|
function recalcularTotales(input) {
|
|
var deposito = parseFloat(document.getElementById('deposito').value) || 0;
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
|
|
var textoError = document.getElementById('p-error');
|
|
if (deposito == '') {
|
|
textoError.style.display = 'none';
|
|
}
|
|
|
|
|
|
var table = $('#facturasPagar').DataTable();
|
|
var allData = table.rows().data().toArray();
|
|
|
|
var sumaTotal = 0;
|
|
|
|
|
|
var insoluto_t = 0;
|
|
|
|
var bandera = 0;
|
|
|
|
/*$('#facturasPagar tbody tr').each(function () {
|
|
pagoTemp = $(this).find('td').eq(3).find('input').val();
|
|
pagoTemp = parseFloat(pagoTemp).toFixed(2);
|
|
|
|
if (!isNaN(pagoTemp)) {
|
|
sumaTotal = (parseFloat(sumaTotal) + parseFloat(pagoTemp)).toFixed(2);
|
|
var insoluto = parseFloat($(this).find('td').eq(2).text()) || 0;
|
|
insoluto_t += insoluto;
|
|
} else {
|
|
bandera = 1;
|
|
}
|
|
|
|
});*/
|
|
|
|
//----------------------------------------------------------------
|
|
//Modificacion LASI para obtener todos los datos de la tabla Facturas a pagar
|
|
//para que se haga el calculo aun cuando no estan visisbles.
|
|
|
|
var total = table.column(3).data() // Obtiene todos los datos, no solo los visibles
|
|
.reduce(function(a, b,index) {
|
|
|
|
var pagoTemp = $(table.row(index).node()).find('td').eq(3).find('input').val();
|
|
pagoTemp = parseFloat(pagoTemp).toFixed(2);
|
|
|
|
if (!isNaN(pagoTemp)) {
|
|
sumaTotal = (parseFloat(sumaTotal) + parseFloat(pagoTemp)).toFixed(2);
|
|
var insoluto = parseFloat($(table.row(index).node()).find('td').eq(2).text()) || 0;
|
|
insoluto_t += insoluto.toFixed(2);
|
|
} else {
|
|
bandera = 1;
|
|
}
|
|
|
|
return sumaTotal;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
|
|
document.getElementById('saldo_suma').value = sumaTotal;
|
|
|
|
// console.log('DT', datos)
|
|
console.log('suma total: '+sumaTotal);
|
|
console.log('deposito: '+deposito);
|
|
if (sumaTotal > deposito || sumaTotal < deposito) {
|
|
console.log('no es igual');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
} else if (sumaTotal == deposito && bandera == 0) {
|
|
console.log('es igual');
|
|
textoError.style.display = 'none';
|
|
guardarPagosBtn.removeAttribute('disabled');
|
|
console.log('removido');
|
|
}
|
|
|
|
|
|
if (deposito > insoluto_t) {
|
|
// console.log('Deposito', deposito);
|
|
console.log('insoluto: '+insoluto_t);
|
|
console.log('Deposito: '+ deposito);
|
|
console.log('Excede el limite');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
} else if (deposito == '' || deposito == '0' || deposito == '0.00') {
|
|
console.log('Excede el limite 2');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//csubir a prd verificar duplicidad
|
|
function verDuplicidad(val)
|
|
{
|
|
$.ajax({
|
|
type: 'GET',
|
|
url: 'php/consultaRef.php?val='+val,
|
|
dataType: 'json',
|
|
success: function (data) {
|
|
lista_clientes = data.message;
|
|
console.log(lista_clientes);
|
|
if(lista_clientes==0)
|
|
{
|
|
verDup=0;
|
|
|
|
}
|
|
else
|
|
{
|
|
verDup=1;
|
|
}
|
|
|
|
}, error: function (error) {
|
|
console.log('Error ajax', error)
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
function generarSolicitud() {
|
|
var datos = [];
|
|
var acum = 0;
|
|
var table = $('#facturasPagar').DataTable();
|
|
table.search("").draw();//modificacion lasi 11/12/2024, para liberar el search y gaurde todos los registros
|
|
var allData = table.rows().data().toArray();
|
|
/*$('#facturasPagar tbody tr').each(function () {
|
|
var id = allData[acum].factura_id;
|
|
acum = acum + 1;
|
|
var fila = {};
|
|
|
|
fila['uuid'] = $(this).find('td').eq(0).text();
|
|
fila['parcialidad'] = $(this).find('td').eq(1).text();
|
|
fila['saldo_insoluto'] = $(this).find('td').eq(2).text();
|
|
fila['pago'] = $(this).find('td').eq(3).find('input').val();
|
|
fila['id_factura'] = id;
|
|
datos.push(fila);
|
|
});*/
|
|
//modificacion LASI para obtener todos los datos de la tabla, aun esten ocultos por paginacion
|
|
var allRows = table.rows({ search: 'applied' }).nodes(); // Incluye filas ocultas
|
|
|
|
$(allRows).each(function () {
|
|
var id = allData[acum].factura_id;
|
|
acum = acum + 1;
|
|
var fila = {};
|
|
|
|
fila['uuid'] = $(this).find('td').eq(0).text();
|
|
fila['parcialidad'] = $(this).find('td').eq(1).text();
|
|
fila['saldo_insoluto'] = $(this).find('td').eq(2).text();
|
|
fila['pago'] = $(this).find('td').eq(3).find('input').val();
|
|
fila['id_factura'] = id;
|
|
datos.push(fila);
|
|
});
|
|
console.log(datos);
|
|
var fechaInput = document.getElementById('fecha');
|
|
var fechaSeleccionada = fechaInput.value;
|
|
if (fechaSeleccionada == '') {
|
|
alertify.error('Selecciona una fecha');
|
|
return
|
|
}
|
|
// if (cadenaB64 == '') {
|
|
// alertify.error('Selecciona un archivo');
|
|
// return
|
|
// }
|
|
|
|
var email = document.getElementById('email').value.replace(/\s+$/, '');
|
|
if (email == '') {
|
|
alertify.error('Llene un correo');
|
|
return;
|
|
}
|
|
|
|
var folio = document.getElementById('folioP').value;
|
|
var email = document.getElementById('email').value;
|
|
var deposito = document.getElementById('deposito').value;
|
|
var formaPago = $('#formaPago option:selected').val();
|
|
var formaPago = $('#formaPago option:selected').val();
|
|
//agregar referencia de pago bancaria
|
|
var refBancaria=document.getElementById('refBancaria').value;
|
|
var unidad_negocio=document.getElementById('unidadNegocio').value;
|
|
|
|
|
|
if (formaPago == '' || formaPago == 'Forma de pago') {
|
|
alertify.error('Seleccione una forma de pago');
|
|
return;
|
|
}
|
|
|
|
if (deposito === '' || deposito === '0') {
|
|
alertify.error('Monto no debe ir vacio');
|
|
return;
|
|
}
|
|
if(refBancaria=='')
|
|
{
|
|
alertify.error('Debe agregar la referencia bancaria');
|
|
return;
|
|
}
|
|
if(verDup==1)
|
|
{
|
|
alertify.error('Ya hay un folio con la misma referencia bancaria');
|
|
return;
|
|
}
|
|
|
|
|
|
var pagos = {
|
|
folio: folio,
|
|
archivo: cadenaB64,
|
|
fecha_ppd: fechaSeleccionada,
|
|
cliente: clienteTemp,
|
|
email: email,
|
|
monto: deposito,
|
|
filas: datos,
|
|
forma_pago: formaPago,
|
|
referBancaria : refBancaria,
|
|
unidad_negocio: unidad_negocio
|
|
};
|
|
|
|
// console.log(pagos);
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'php/inserta_solicitud.php',
|
|
data: pagos,
|
|
dataType: 'json',
|
|
success: function (data) {
|
|
// console.log(data)
|
|
if (data.code == '201') {
|
|
alertify.success('Registrado con exito!');
|
|
dataTable = dtTemp;
|
|
inicializarDataTable(clienteTemp, dtTemp)
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
document.getElementById('deposito').value = '';
|
|
$('#modalPagar').modal('hide');
|
|
document.getElementById('saldo_suma').value = '';
|
|
} else {
|
|
p.innerText = data.messsage
|
|
$('#errores').modal('show')
|
|
}
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
obtenerFolio();
|
|
var table = $('#facturasPagar').DataTable();
|
|
table.clear().draw();
|
|
}, error: function (error) {
|
|
console.log('Error', error)
|
|
p.innerText = 'Error al cargar los datos, consulte a soporte'
|
|
$('#errores').modal('show')
|
|
}
|
|
});
|
|
}
|
|
|
|
function apagaBoton() {
|
|
// console.log('Btn')
|
|
var guardarPagosBtn = document.getElementById('guardarPagos');
|
|
guardarPagosBtn.setAttribute('disabled', 'disabled');
|
|
}
|
|
|
|
function traerDeudores(dataTable) {
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: 'php/ConsultasTablas/ManipulacionConsultas.php',
|
|
data: { opcion: '6' },
|
|
dataType: 'json',
|
|
success: function (data) {
|
|
lista_clientes = data;
|
|
showResults('');
|
|
console.log('deudores');
|
|
console.log(lista_clientes);
|
|
|
|
}, error: function (error) {
|
|
console.log('Error ajax', error)
|
|
}
|
|
});
|
|
}
|
|
|
|
function showResults(searchText) {
|
|
const searchResults = document.getElementById('searchResults');
|
|
searchResults.innerHTML = '';
|
|
lista_clientes.forEach(option => {
|
|
if (option.CLIENTE.toLowerCase().includes(searchText.toLowerCase())) {
|
|
const li = document.createElement('li');
|
|
|
|
li.textContent = option.CLIENTE;
|
|
li.className = 'list-group-item';
|
|
li.setAttribute('onclick', 'seleccionado(' + JSON.stringify(option) + ')');
|
|
searchResults.appendChild(li);
|
|
}
|
|
});
|
|
|
|
if (searchText === '' || searchResults.children.length === 0) {
|
|
searchResults.style.display = 'none';
|
|
} else {
|
|
searchResults.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
function seleccionado(cliente) {
|
|
var clienteInput = $("#searchInput");
|
|
clienteInput.val(cliente.CLIENTE).attr('data-custom-value', cliente.ID_CLIENTE);
|
|
textoCliente = cliente.CLIENTE;
|
|
clienteTemp = cliente.ID_CLIENTE;
|
|
searchResults.style.display = 'none';
|
|
inicializarDataTable(cliente.ID_CLIENTE, dtTemp)
|
|
}
|
|
|
|
|
|
|