34849-vm/flujo_de_caja.php
2026-02-03 05:29:28 +00:00

151 lines
6.0 KiB
PHP

<?php
require_once 'layout_header.php';
require_once 'db/config.php';
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$start_date = "$year-$month-01";
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$end_date = "$year-$month-$days_in_month";
$flujo_data = [];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM flujo_caja WHERE fecha BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$flujo_data[$row['fecha']] = $row;
}
} catch (PDOException $e) {
// Handle error
}
$columns = [
'bcp_yape', 'b_nacion', 'interbank', 'bbva', 'otros_ingresos',
'tu1', 'tu2', 'tu3', 'fl1', 'fl2', 'fl3',
'rc_envio', 'rc_contraent', 'total_inversion_publicitaria'
];
?>
<div class="container-fluid mt-4">
<h2>Flujo de Caja</h2>
<p>Registro y control de los movimientos de ingresos y gastos.</p>
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover" style="width: 100%;">
<thead class="thead-dark">
<tr>
<th rowspan="2" style="vertical-align: middle; text-align: center;">Fecha</th>
<th colspan="5" style="text-align: center;">Ingresos</th>
<th colspan="6" style="text-align: center;">Inversion Publicitaria</th>
<th rowspan="2" style="vertical-align: middle; text-align: center;">RC ENVIO</th>
<th rowspan="2" style="vertical-align: middle; text-align: center;">RC CONTRAENT</th>
<th rowspan="2" style="vertical-align: middle; text-align: center;">Total Ingresos</th>
<th rowspan="2" style="vertical-align: middle; text-align: center;">Total Inversion Publicitaria</th>
<th rowspan="2" style="vertical-align: middle; text-align: center;">Recaudo final</th>
</tr>
<tr>
<th>BCP/YAPE</th>
<th>B. NACION</th>
<th>INTERBANK</th>
<th>BBVA</th>
<th>Otros Ingresos</th>
<th>TU 1</th>
<th>TU 2</th>
<th>TU 3</th>
<th>FL1</th>
<th>FL2</th>
<th>FL3</th>
</tr>
</thead>
<tbody>
<?php for ($day = 1; $day <= $days_in_month; $day++):
$date = date('Y-m-d', strtotime("$year-$month-$day"));
$day_data = isset($flujo_data[$date]) ? $flujo_data[$date] : array_fill_keys($columns, '0.00');
?>
<tr data-date="<?php echo $date; ?>">
<td><?php echo $date; ?></td>
<?php foreach ($columns as $col): ?>
<td contenteditable="true" data-column="<?php echo $col; ?>"><?php echo htmlspecialchars($day_data[$col]); ?></td>
<?php endforeach; ?>
<td class="total-ingresos">0.00</td>
<td class="total-inversion">0.00</td>
<td class="recaudo-final">0.00</td>
</tr>
<?php endfor; ?>
</tbody>
</table>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const table = document.querySelector('.table');
// Function to update totals for a specific row
function updateTotals(row) {
const getVal = (selector) => {
const cell = row.querySelector(`[data-column="${selector}"]`);
return parseFloat(cell.textContent) || 0;
};
const ingresos = getVal('bcp_yape') + getVal('b_nacion') + getVal('interbank') + getVal('bbva') + getVal('otros_ingresos') + getVal('rc_envio') + getVal('rc_contraent');
const inversionManual = getVal('total_inversion_publicitaria');
const inversionAuto = getVal('tu1') + getVal('tu2') + getVal('tu3') + getVal('fl1') + getVal('fl2') + getVal('fl3');
const totalInversion = inversionManual > 0 ? inversionManual : inversionAuto;
const recaudoFinal = ingresos - totalInversion;
row.querySelector('.total-ingresos').textContent = ingresos.toFixed(2);
row.querySelector('.total-inversion').textContent = totalInversion.toFixed(2);
row.querySelector('.recaudo-final').textContent = recaudoFinal.toFixed(2);
}
// Initial calculation for all rows
table.querySelectorAll('tbody tr').forEach(updateTotals);
// Event listener for cell edits
table.addEventListener('blur', function(e) {
if (e.target.hasAttribute('contenteditable')) {
const cell = e.target;
const row = cell.closest('tr');
const date = row.dataset.date;
const column = cell.dataset.column;
let value = parseFloat(cell.textContent);
if (isNaN(value)) {
value = 0;
cell.textContent = '0.00';
}
cell.textContent = value.toFixed(2);
// Save data to server
fetch('save_flujo_caja.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ fecha: date, columna: column, valor: value })
})
.then(response => response.json())
.then(data => {
if (!data.success) {
console.error('Error saving data:', data.message);
// Optional: Add user feedback for save failure
}
})
.catch(error => console.error('Fetch error:', error));
// Update totals for the edited row
updateTotals(row);
}
}, true); // Use capturing to ensure blur event is handled properly
});
</script>
<?php
require_once 'layout_footer.php';
?>