40097-vm/calculo_costos.php
2026-05-10 16:22:58 +00:00

221 lines
9.4 KiB
PHP

<?php
$pageTitle = "Cálculo de Costos";
include 'db/config.php';
include 'layout_header.php';
$db = db();
// Obtener videos y sus costos asociados
$stmt = $db->query("SELECT mv.id, mv.orden, mv.foto_producto, p.nombre as nombre_producto,
mc.costo_producto, mc.costo_fijo_film, mc.comision_asesora,
mc.delivery, mc.costo_publicitario, mc.inversion_total, mc.promo_1
FROM marketing_videos mv
LEFT JOIN products p ON mv.producto_id = p.id
LEFT JOIN marketing_costos mc ON mv.id = mc.video_id
ORDER BY mv.orden ASC, mv.fecha_creacion DESC");
$costos = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<style>
.table-excel {
font-size: 0.85rem;
}
.table-excel th {
background-color: #f8f9fa;
border-bottom: 2px solid #dee2e6;
white-space: nowrap;
text-transform: uppercase;
font-weight: 600;
color: #495057;
}
.table-excel td {
vertical-align: middle;
border-bottom: 1px solid #eee;
}
.img-preview {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 4px;
}
.editable:hover {
background-color: #f1f3f5;
cursor: pointer;
}
.inline-edit-input {
width: 100%;
padding: 2px 5px;
font-size: 0.85rem;
border: 1px solid #0d6efd;
border-radius: 3px;
}
.bg-total {
background-color: #e9ecef;
font-weight: bold;
}
</style>
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h2 class="mb-0">Cálculo de Costos</h2>
<p class="text-muted small">Gestión de costos por producto de marketing</p>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover table-excel mb-0">
<thead>
<tr>
<th class="text-center">Orden</th>
<th>Producto</th>
<th class="text-center">Imagen</th>
<th class="text-center">Costo Producto</th>
<th class="text-center">Costo Fijo Film</th>
<th class="text-center">Comisión Asesora</th>
<th class="text-center">Delivery</th>
<th class="text-center">Costo Publicitario</th>
<th class="text-center bg-total">Inversión Total</th>
<th class="text-center">Promo 1</th>
</tr>
</thead>
<tbody>
<?php if (empty($costos)): ?>
<tr>
<td colspan="10" class="text-center py-4 text-muted">No hay videos registrados en producción.</td>
</tr>
<?php else: ?>
<?php foreach ($costos as $c): ?>
<?php
$inversion_total = ($c['costo_producto'] ?? 0) +
($c['costo_fijo_film'] ?? 0) +
($c['comision_asesora'] ?? 0) +
($c['delivery'] ?? 0) +
($c['costo_publicitario'] ?? 0);
?>
<tr>
<td class="text-center fw-bold text-primary"><?php echo $c['orden']; ?></td>
<td class="fw-bold"><?php echo htmlspecialchars($c['nombre_producto'] ?: 'General'); ?></td>
<td class="text-center">
<?php if ($c['foto_producto']): ?>
<img src="<?php echo $c['foto_producto']; ?>" class="img-preview" alt="Ref">
<?php else: ?>
<span class="text-muted small">Sin foto</span>
<?php endif; ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="costo_producto">
S/ <?php echo number_format($c['costo_producto'] ?? 0, 2); ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="costo_fijo_film">
S/ <?php echo number_format($c['costo_fijo_film'] ?? 0, 2); ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="comision_asesora">
S/ <?php echo number_format($c['comision_asesora'] ?? 0, 2); ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="delivery">
S/ <?php echo number_format($c['delivery'] ?? 0, 2); ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="costo_publicitario">
S/ <?php echo number_format($c['costo_publicitario'] ?? 0, 2); ?>
</td>
<td class="text-center bg-total inversion-total" id="total-<?php echo $c['id']; ?>">
S/ <?php echo number_format($inversion_total, 2); ?>
</td>
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="promo_1">
<?php echo htmlspecialchars($c['promo_1'] ?: '-'); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const editableCells = document.querySelectorAll('.editable');
editableCells.forEach(cell => {
cell.addEventListener('dblclick', function() {
if (this.querySelector('input')) return;
const id = this.getAttribute('data-id');
const field = this.getAttribute('data-field');
let currentValue = this.innerText.trim().replace('S/ ', '').replace(',', '');
if (currentValue === '-') currentValue = '';
const input = document.createElement('input');
input.type = field === 'promo_1' ? 'text' : 'number';
if (input.type === 'number') input.step = '0.01';
input.className = 'form-control form-control-sm inline-edit-input';
input.value = currentValue;
const originalContent = this.innerHTML;
this.innerHTML = '';
this.appendChild(input);
input.focus();
const saveChange = () => {
const newValue = input.value;
if (newValue === currentValue) {
this.innerHTML = originalContent;
return;
}
fetch('update_marketing_costos_field.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, field, value: newValue })
})
.then(response => response.json())
.then(data => {
if (data.success) {
if (field === 'promo_1') {
this.innerHTML = newValue || '-';
} else {
this.innerHTML = 'S/ ' + parseFloat(newValue || 0).toFixed(2);
updateTotal(id);
}
} else {
alert('Error: ' + data.error);
this.innerHTML = originalContent;
}
})
.catch(error => {
console.error('Error:', error);
this.innerHTML = originalContent;
});
};
input.addEventListener('blur', saveChange);
input.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
input.blur();
} else if (e.key === 'Escape') {
cell.innerHTML = originalContent;
}
});
});
});
function updateTotal(id) {
const row = document.querySelector(`[data-id="${id}"]`).closest('tr');
const fields = ['costo_producto', 'costo_fijo_film', 'comision_asesora', 'delivery', 'costo_publicitario'];
let total = 0;
fields.forEach(field => {
const cell = row.querySelector(`[data-field="${field}"]`);
const val = parseFloat(cell.innerText.replace('S/ ', '').replace(',', '') || 0);
total += val;
});
document.getElementById(`total-${id}`).innerText = 'S/ ' + total.toFixed(2);
}
});
</script>
<?php include 'layout_footer.php'; ?>