Major Refactor: Refactor the codebase to improve readability and maintainability

This commit is contained in:
2026-06-21 23:38:49 -04:00
parent 9c4753cd1f
commit 801b0b97fc
46 changed files with 2378 additions and 2031 deletions

View File

@@ -1,19 +1,14 @@
{% extends "macros/base.html" %}
{% from 'macros/modals.html' import confirm_modal, alert_modal %}
{% from "macros/ui.html" import flashed_messages, back_link %}
{% block title %}Rendición de Caja{% endblock %}
{% block head %}
<!-- HEAD -->
{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<a href="{{ url_for('worker_dashboard') }}" class="btn btn-outline-secondary btn-sm mb-2">
<i class="bi bi-arrow-left"></i> Volver al Historial
</a>
{{ back_link(url_for('worker.worker_dashboard'), 'Volver al Historial') }}
<h2>Nueva Rendición de Caja</h2>
</div>
<div class="text-end text-muted">
@@ -22,13 +17,7 @@
</div>
</div>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">{{ message|safe }}</div>
{% endfor %}
{% endif %}
{% endwith %}
{{ flashed_messages() }}
<form method="POST">
<div class="card mb-4 shadow-sm">
@@ -241,228 +230,5 @@
{% endblock %}
{% block scripts %}
<script>
document.getElementById('companion_select').addEventListener('change', function() {
const timeDiv = document.getElementById('companion_times_div');
const compIn = document.getElementById('comp_in');
const compOut = document.getElementById('comp_out');
if (this.value) {
timeDiv.style.display = 'block';
compIn.required = true;
compOut.required = true;
} else {
timeDiv.style.display = 'none';
compIn.required = false;
compOut.required = false;
compIn.value = '';
compOut.value = '';
}
});
</script>
<script>
const inputsCantidad = document.querySelectorAll('input[name^="qty_"]');
const displayTotalProductos = document.getElementById('total_productos_calc');
function calcularVentaProductos() {
let granTotal = 0;
const filas = document.querySelectorAll('tbody tr');
filas.forEach(fila => {
const inputQty = fila.querySelector('input[name^="qty_"]');
if (inputQty) {
if (parseInt(inputQty.value) < 0) {
inputQty.value = 0;
}
const cantidad = parseInt(inputQty.value) || 0;
const precioTexto = fila.cells[1].innerText.replace(/\D/g, '');
const precio = parseInt(precioTexto) || 0;
granTotal += (cantidad * precio);
}
});
displayTotalProductos.value = granTotal.toLocaleString('es-CL');
checkWarnings();
}
inputsCantidad.forEach(input => {
input.addEventListener('keydown', function(e) {
if (['Backspace', 'Tab', 'ArrowLeft', 'ArrowRight', 'Delete', 'Enter'].includes(e.key) || e.ctrlKey || e.metaKey) return;
if (e.key < '0' || e.key > '9') {
e.preventDefault();
}
});
input.addEventListener('input', function() {
this.value = this.value.replace(/\D/g, '');
calcularVentaProductos();
});
});
function checkWarnings() {
const getVal = (id) => {
const el = document.getElementById(id);
if (!el || !el.value.trim() || el.value === '0') return 0;
return parseInt(el.value.replace(/\D/g, '')) || 0;
};
const totalProductos = getVal('total_productos_calc');
const totalDeclarado = getVal('total_general');
const gastos = getVal('gastos');
const efectivo = getVal('venta_efectivo');
let warnings = [];
// Comprobar diferencias en totales (solo si se ha ingresado algo para no mostrar el error de entrada)
if ((totalProductos > 0 || totalDeclarado > 0) && totalProductos !== totalDeclarado) {
warnings.push("El <strong>Total Venta por Productos</strong> no coincide con el <strong>Total Ventas Declaradas</strong>.");
}
// Comprobar si los gastos superan el efectivo
if (gastos > efectivo) {
warnings.push("El <strong>Monto de Gastos</strong> es mayor que el <strong>Efectivo</strong> declarado.");
}
const warningContainer = document.getElementById('discrepancy_warning');
const warningText = document.getElementById('discrepancy_text');
if (warnings.length > 0) {
warningText.innerHTML = warnings.join("<br>");
warningContainer.style.display = 'block';
} else {
warningContainer.style.display = 'none';
}
}
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const submitModal = document.getElementById('confirmSubmitModal');
const mainForm = document.querySelector('form');
const alertModalEl = document.getElementById('globalAlertModal');
const alertModal = new bootstrap.Modal(alertModalEl);
const confirmBtn = submitModal.querySelector('button[type="submit"]');
function mostrarError(mensaje) {
document.getElementById('globalAlertModalBody').textContent = mensaje;
alertModal.show();
}
function validarFormulario() {
const requiredInputs = mainForm.querySelectorAll('[required]');
let valid = true;
requiredInputs.forEach(input => {
const isMoney = input.classList.contains('money-input');
if (!input.value.trim() || (isMoney && input.value === '')) {
input.classList.add('is-invalid');
valid = false;
} else {
input.classList.remove('is-invalid');
}
});
return valid;
}
confirmBtn.addEventListener('click', function(e) {
e.preventDefault();
if (validarFormulario()) {
mainForm.submit();
} else {
bootstrap.Modal.getInstance(submitModal).hide();
mostrarError("Por favor, rellena los campos obligatorios (Fecha y Hora) antes de enviar.");
}
});
document.querySelectorAll('.money-input').forEach(input => {
if (!input.value.trim()) input.value = '0';
});
});
</script>
<script>
const inputsVenta = document.querySelectorAll('.sale-input');
const displayDigital = document.getElementById('total_digital');
const displayGeneral = document.getElementById('total_general');
function calcularTotales() {
const getVal = (id) => {
const el = document.getElementById(id);
if (!el || !el.value.trim() || el.value === '0') return 0;
return parseInt(el.value.replace(/\D/g, '')) || 0;
};
const debito = getVal('venta_debito');
const credito = getVal('venta_credito');
const mp = getVal('venta_mp');
const efectivo = getVal('venta_efectivo');
const totalDigital = debito + credito + mp;
const totalGeneral = totalDigital + efectivo; // Ya no restamos los gastos aquí
document.getElementById('total_digital').value = totalDigital.toLocaleString('es-CL');
document.getElementById('total_general').value = totalGeneral.toLocaleString('es-CL');
checkWarnings();
}
inputsVenta.forEach(input => {
input.addEventListener('input', calcularTotales);
});
document.querySelector('form').addEventListener('submit', function(e) {
const requiredInputs = this.querySelectorAll('[required]');
let valid = true;
requiredInputs.forEach(input => {
const isMoney = input.classList.contains('money-input');
if (!input.value.trim() || (isMoney && input.value === '')) {
input.classList.add('is-invalid');
valid = false;
} else {
input.classList.remove('is-invalid');
}
});
if (!valid) {
e.preventDefault();
const alertModalEl = document.getElementById('globalAlertModal');
if (alertModalEl) {
const alertModal = bootstrap.Modal.getOrCreateInstance(alertModalEl);
document.getElementById('globalAlertModalBody').textContent = "Por favor, rellena todos los campos obligatorios antes de enviar.";
alertModal.show();
} else {
alert("Por favor, rellena todos los campos obligatorios.");
}
}
});
document.querySelectorAll('.money-input').forEach(function(input) {
input.addEventListener('keydown', function(e) {
if (['Backspace', 'Tab', 'ArrowLeft', 'ArrowRight', 'Delete', 'Enter'].includes(e.key) || e.ctrlKey || e.metaKey) return;
if (e.key < '0' || e.key > '9') {
e.preventDefault();
}
});
input.addEventListener('focus', function() {
if (this.value === '0') this.value = '';
});
input.addEventListener('blur', function() {
if (this.value.trim() === '' || this.value.trim() === '0') {
this.value = '0';
}
calcularTotales();
});
input.addEventListener('input', function() {
let value = this.value.replace(/\D/g, '');
if (value !== '') {
this.value = parseInt(value, 10).toLocaleString('es-CL');
}
calcularTotales();
});
});
</script>
<script src="{{ url_for('static', filename='js/worker_dashboard.js') }}"></script>
{% endblock %}