314 lines
15 KiB
PHP
314 lines
15 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, mc.promo_2, mc.promo_3
|
|
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 {
|
|
background-color: #ffffff;
|
|
transition: all 0.2s;
|
|
position: relative;
|
|
}
|
|
.editable:hover {
|
|
background-color: #f0f7ff !important;
|
|
cursor: pointer;
|
|
box-shadow: inset 0 0 0 1px #0d6efd;
|
|
z-index: 1;
|
|
}
|
|
.recaudo-cell {
|
|
background-color: #fdfdfd;
|
|
cursor: default;
|
|
color: #6c757d;
|
|
}
|
|
.inline-edit-input {
|
|
width: 100%;
|
|
padding: 2px 5px;
|
|
font-size: 0.85rem;
|
|
border: 2px solid #0d6efd;
|
|
border-radius: 3px;
|
|
text-align: center;
|
|
}
|
|
.bg-total {
|
|
background-color: #f1f3f5;
|
|
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. <span class="badge bg-info text-dark">Doble clic en las celdas blancas para editar</span></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" id="costosTable">
|
|
<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>
|
|
<th class="text-center recaudo-cell">Recaudo 1 (Auto)</th>
|
|
<th class="text-center">Promo 2</th>
|
|
<th class="text-center recaudo-cell">Recaudo 2 (Auto)</th>
|
|
<th class="text-center">Promo 3</th>
|
|
<th class="text-center recaudo-cell">Recaudo 3 (Auto)</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($costos)): ?>
|
|
<tr>
|
|
<td colspan="15" 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);
|
|
|
|
$costos_operativos = ($c['costo_fijo_film'] ?? 0) +
|
|
($c['comision_asesora'] ?? 0) +
|
|
($c['delivery'] ?? 0) +
|
|
($c['costo_publicitario'] ?? 0);
|
|
|
|
function calculateRecaudo($promo, $costos) {
|
|
if (!$promo || $promo == '-') return null;
|
|
preg_match('/[\d.]+/', $promo, $matches);
|
|
$val = isset($matches[0]) ? floatval($matches[0]) : 0;
|
|
return $val > 0 ? $val - $costos : null;
|
|
}
|
|
|
|
$recaudo1 = calculateRecaudo($c['promo_1'], $costos_operativos);
|
|
$recaudo2 = calculateRecaudo($c['promo_2'], $costos_operativos);
|
|
$recaudo3 = calculateRecaudo($c['promo_3'], $costos_operativos);
|
|
?>
|
|
<tr data-row-id="<?php echo $c['id']; ?>">
|
|
<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>
|
|
<td class="text-center recaudo-cell recaudo-1 <?php echo $recaudo1 !== null ? ($recaudo1 >= 0 ? 'text-success fw-bold' : 'text-danger fw-bold') : 'text-muted'; ?>" data-id="<?php echo $c['id']; ?>" title="Calculado automáticamente (Promo - Costos)">
|
|
<?php echo $recaudo1 !== null ? 'S/ ' . number_format($recaudo1, 2) : '-'; ?>
|
|
</td>
|
|
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="promo_2">
|
|
<?php echo htmlspecialchars($c['promo_2'] ?: '-'); ?>
|
|
</td>
|
|
<td class="text-center recaudo-cell recaudo-2 <?php echo $recaudo2 !== null ? ($recaudo2 >= 0 ? 'text-success fw-bold' : 'text-danger fw-bold') : 'text-muted'; ?>" data-id="<?php echo $c['id']; ?>" title="Calculado automáticamente (Promo - Costos)">
|
|
<?php echo $recaudo2 !== null ? 'S/ ' . number_format($recaudo2, 2) : '-'; ?>
|
|
</td>
|
|
<td class="text-center editable" data-id="<?php echo $c['id']; ?>" data-field="promo_3">
|
|
<?php echo htmlspecialchars($c['promo_3'] ?: '-'); ?>
|
|
</td>
|
|
<td class="text-center recaudo-cell recaudo-3 <?php echo $recaudo3 !== null ? ($recaudo3 >= 0 ? 'text-success fw-bold' : 'text-danger fw-bold') : 'text-muted'; ?>" data-id="<?php echo $c['id']; ?>" title="Calculado automáticamente (Promo - Costos)">
|
|
<?php echo $recaudo3 !== null ? 'S/ ' . number_format($recaudo3, 2) : '-'; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const tableBody = document.querySelector('#costosTable tbody');
|
|
|
|
tableBody.addEventListener('dblclick', function(e) {
|
|
const cell = e.target.closest('.editable');
|
|
if (!cell || cell.querySelector('input')) return;
|
|
|
|
const id = cell.getAttribute('data-id');
|
|
const field = cell.getAttribute('data-field');
|
|
let currentValue = cell.innerText.trim().replace('S/ ', '').replace(',', '');
|
|
if (currentValue === '-') currentValue = '';
|
|
|
|
const input = document.createElement('input');
|
|
const isPromo = field.startsWith('promo_');
|
|
input.type = isPromo ? '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 = cell.innerHTML;
|
|
cell.innerHTML = '';
|
|
cell.appendChild(input);
|
|
input.focus();
|
|
input.select();
|
|
|
|
const saveChange = () => {
|
|
const newValue = input.value;
|
|
if (newValue === currentValue) {
|
|
cell.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 (isPromo) {
|
|
cell.innerHTML = newValue || '-';
|
|
} else {
|
|
cell.innerHTML = 'S/ ' + parseFloat(newValue || 0).toFixed(2);
|
|
}
|
|
updateTotal(id);
|
|
} else {
|
|
alert('Error: ' + data.error);
|
|
cell.innerHTML = originalContent;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
cell.innerHTML = originalContent;
|
|
});
|
|
};
|
|
|
|
input.addEventListener('blur', saveChange);
|
|
input.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Enter') {
|
|
input.blur();
|
|
} else if (e.key === 'Escape') {
|
|
input.removeEventListener('blur', saveChange);
|
|
cell.innerHTML = originalContent;
|
|
}
|
|
});
|
|
});
|
|
|
|
function updateTotal(id) {
|
|
const row = document.querySelector(`tr[data-row-id="${id}"]`);
|
|
if (!row) return;
|
|
|
|
const costFields = ['costo_fijo_film', 'comision_asesora', 'delivery', 'costo_publicitario'];
|
|
const allFields = ['costo_producto', ...costFields];
|
|
|
|
let totalInversion = 0;
|
|
let totalCostosOperativos = 0;
|
|
|
|
allFields.forEach(field => {
|
|
const cell = row.querySelector(`[data-field="${field}"]`);
|
|
if (cell) {
|
|
const val = parseFloat(cell.innerText.replace('S/ ', '').replace(',', '') || 0);
|
|
totalInversion += val;
|
|
if (costFields.includes(field)) {
|
|
totalCostosOperativos += val;
|
|
}
|
|
}
|
|
});
|
|
|
|
const totalEl = document.getElementById(`total-${id}`);
|
|
if (totalEl) totalEl.innerText = 'S/ ' + totalInversion.toFixed(2);
|
|
|
|
// Update Recaudos
|
|
[1, 2, 3].forEach(num => {
|
|
const promoCell = row.querySelector(`[data-field="promo_${num}"]`);
|
|
const recaudoCell = row.querySelector(`.recaudo-${num}`);
|
|
|
|
if (promoCell && recaudoCell) {
|
|
const promoText = promoCell.innerText.trim();
|
|
const promoValMatch = promoText.match(/[\d.]+/);
|
|
const promoVal = promoValMatch ? parseFloat(promoValMatch[0]) : 0;
|
|
|
|
if (promoVal > 0) {
|
|
const recaudo = promoVal - totalCostosOperativos;
|
|
recaudoCell.innerText = 'S/ ' + recaudo.toFixed(2);
|
|
recaudoCell.classList.remove('text-muted');
|
|
recaudoCell.classList.add('fw-bold');
|
|
if (recaudo >= 0) {
|
|
recaudoCell.classList.add('text-success');
|
|
recaudoCell.classList.remove('text-danger');
|
|
} else {
|
|
recaudoCell.classList.add('text-danger');
|
|
recaudoCell.classList.remove('text-success');
|
|
}
|
|
} else {
|
|
recaudoCell.innerText = '-';
|
|
recaudoCell.classList.add('text-muted');
|
|
recaudoCell.classList.remove('fw-bold', 'text-success', 'text-danger');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<?php include 'layout_footer.php'; ?>
|