Simulador de Créditos
/* Plugin Name: Simulador de Crédito Cooperativo Description: Simulador con múltiples líneas de crédito, configuración administrativa, tabla de amortización, envío por correo, descarga en PDF y Excel. Compatible con Elementor. Version: 2.2 Author: JUAN PALACIO */ if (!defined('ABSPATH')) exit; // Crear opciones en el administrador add_action('admin_menu', function() { add_options_page('Configuración del Simulador de Crédito', 'Simulador Crédito', 'manage_options', 'simulador_credito_config', 'simulador_credito_config_page'); }); function simulador_credito_config_page() { if (isset($_POST['guardar_config'])) { update_option('lineas_credito', stripslashes($_POST['lineas_credito'])); echo '

Configuración guardada.

'; } $lineas_credito = get_option('lineas_credito', '[{"nombre":"Libre Inversión","interes":1.5,"min":1000000,"max":50000000,"plazo_min":6,"plazo_max":84}]'); echo '

Simulador de Crédito


'; } add_shortcode('simulador_credito', 'simulador_credito_shortcode'); function simulador_credito_shortcode() { $lineas = json_decode(get_option('lineas_credito'), true); ob_start(); ?> #simulador-creditos { font-family: 'Segoe UI', sans-serif; background: #f9f9f9; padding: 20px; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); max-width: 800px; margin: 0 auto; } #simulador-creditos label { display: block; margin-top: 15px; font-weight: bold; } #simulador-creditos .valor-visible { font-size: 1.2em; font-weight: bold; margin-top: 5px; display: block; text-align: right; color: #004080; } #simulador-creditos input[type="range"] { width: 100%; } #simulador-creditos button { margin-top: 15px; margin-right: 10px; padding: 10px 15px; border: none; background: #004080; color: #fff; border-radius: 5px; cursor: pointer; } #simulador-creditos table { margin-top: 20px; width: 100%; border-collapse: collapse; background: #fff; border: 1px solid #ccc; } #simulador-creditos th, td { padding: 6px; text-align: center; }
<option value=''>

Cuota mensual estimada:

MesCuotaInterésCapitalSaldo
function actualizarParametros() { const linea = JSON.parse(document.getElementById('linea_credito').value); document.getElementById('monto').min = linea.min; document.getElementById('monto').max = linea.max; document.getElementById('monto').step = 500000; document.getElementById('monto').value = linea.min; document.getElementById('plazo').min = linea.plazo_min; document.getElementById('plazo').max = linea.plazo_max; document.getElementById('plazo').value = linea.plazo_min; document.getElementById('interes').value = linea.interes; actualizarTexto(); } function actualizarTexto() { const monto = parseInt(document.getElementById('monto').value); const plazo = parseInt(document.getElementById('plazo').value); document.getElementById('monto_valor').innerText = '$' + monto.toLocaleString(); document.getElementById('plazo_valor').innerText = plazo + ' meses'; } document.addEventListener('DOMContentLoaded', () => { actualizarParametros(); }); function calcularCredito() { let monto = parseInt(document.getElementById('monto').value); let plazo = parseInt(document.getElementById('plazo').value); let interes = parseFloat(document.getElementById('interes').value) / 100; let tasaMensual = interes; let cuota = (monto * tasaMensual) / (1 - Math.pow(1 + tasaMensual, -plazo)); document.getElementById('cuota').innerText = '$' + Math.round(cuota).toLocaleString(); let saldo = monto; let tbody = document.querySelector('#tabla-amortizacion tbody'); tbody.innerHTML = ''; for (let i = 1; i <= plazo; i++) { let interesMes = saldo * tasaMensual; let abonoCapital = cuota - interesMes; saldo -= abonoCapital; let row = ` ${i} $${Math.round(cuota).toLocaleString()} $${Math.round(interesMes).toLocaleString()} $${Math.round(abonoCapital).toLocaleString()} $${Math.round(saldo).toLocaleString()} `; tbody.innerHTML += row; } } function enviarSimulacionPorCorreo() { const email = document.getElementById('correo').value; const tabla = document.getElementById('tabla-amortizacion').outerHTML; const cuota = document.getElementById('cuota').innerText; fetch('', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ action: 'enviar_simulacion_credito', email: email, cuota: cuota, tabla: tabla }) }).then(r => r.text()).then(alert); } function descargarPDF() { const doc = new jspdf.jsPDF(); const cuota = document.getElementById('cuota').innerText; doc.setFontSize(12); doc.text("Simulación de Crédito", 14, 20); doc.text("Cuota mensual estimada: " + cuota, 14, 30); doc.html(document.getElementById('tabla-amortizacion'), { callback: function (doc) { doc.save('simulacion_credito.pdf'); }, x: 14, y: 40 }); } function descargarExcel() { let wb = XLSX.utils.book_new(); let ws_data = [["Mes", "Cuota", "Interés", "Capital", "Saldo"]]; document.querySelectorAll("#tabla-amortizacion tbody tr").forEach(tr => { let row = Array.from(tr.children).map(td => td.textContent.replace(/[$,]/g, '')); ws_data.push(row); }); let ws = XLSX.utils.aoa_to_sheet(ws_data); XLSX.utils.book_append_sheet(wb, ws, "Amortización"); XLSX.writeFile(wb, "simulacion_credito.xlsx"); } <?php return ob_get_clean(); } add_action('wp_ajax_enviar_simulacion_credito', 'enviar_simulacion_credito'); add_action('wp_ajax_nopriv_enviar_simulacion_credito', 'enviar_simulacion_credito'); function enviar_simulacion_credito() { $email = sanitize_email($_POST['email']); $cuota = sanitize_text_field($_POST['cuota']); $tabla = wp_kses_post($_POST['tabla']); $mensaje = "

Simulación de Crédito

Cuota mensual estimada: {$cuota}

{$tabla}"; wp_mail($email, 'Resultado de tu simulador de crédito', $mensaje, ['Content-Type: text/html; charset=UTF-8']); echo 'Simulación enviada correctamente a ' . esc_html($email); wp_die(); }