321 lines
15 KiB
PHP
321 lines
15 KiB
PHP
<?php
|
|
$pageTitle = "Cálculo de Costos V2";
|
|
include 'db/config.php';
|
|
include 'layout_header.php';
|
|
|
|
$db = db();
|
|
|
|
// Obtener productos para el select del modal
|
|
$stmt_products = $db->query("SELECT id, nombre FROM products ORDER BY nombre ASC");
|
|
$productos_select = $stmt_products->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// 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-v2 {
|
|
font-size: 0.85rem;
|
|
}
|
|
.table-v2 th {
|
|
background-color: #f1f3f5;
|
|
border-bottom: 2px solid #dee2e6;
|
|
white-space: nowrap;
|
|
padding: 12px 8px;
|
|
}
|
|
.table-v2 td {
|
|
vertical-align: middle;
|
|
padding: 4px;
|
|
}
|
|
.input-cell {
|
|
width: 100%;
|
|
min-width: 80px;
|
|
padding: 6px;
|
|
border: 1px solid #ced4da;
|
|
border-radius: 4px;
|
|
text-align: center;
|
|
font-size: 0.85rem;
|
|
}
|
|
.input-cell:focus {
|
|
border-color: #0d6efd;
|
|
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.25);
|
|
outline: 0;
|
|
}
|
|
.input-cell.saving {
|
|
background-color: #fff3cd;
|
|
border-color: #ffc107;
|
|
}
|
|
.input-cell.saved {
|
|
background-color: #d1e7dd;
|
|
border-color: #198754;
|
|
}
|
|
.recaudo-v2 {
|
|
background-color: #f8f9fa;
|
|
font-weight: bold;
|
|
text-align: center;
|
|
padding: 8px;
|
|
border-radius: 4px;
|
|
min-width: 90px;
|
|
transition: all 0.3s;
|
|
}
|
|
.img-v2 {
|
|
width: 40px;
|
|
height: 40px;
|
|
object-fit: cover;
|
|
border-radius: 4px;
|
|
}
|
|
.btn-save-row {
|
|
padding: 6px 12px;
|
|
}
|
|
</style>
|
|
|
|
<div class="container-fluid">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<div>
|
|
<h2 class="mb-0">Cálculo de Costos <span class="badge bg-warning text-dark">Versión 2</span></h2>
|
|
<p class="text-muted">Edición directa mediante cuadros de texto. Escribe y presiona "Guardar".</p>
|
|
</div>
|
|
<div>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalNuevoV2">
|
|
<i class="fas fa-plus me-2"></i> Nuevo Producto
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (isset($_GET['success'])): ?>
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
¡Cambios guardados correctamente!
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-v2 mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th class="text-center">Orden</th>
|
|
<th>Producto</th>
|
|
<th class="text-center">Imagen</th>
|
|
<th class="text-center">Costo Prod.</th>
|
|
<th class="text-center">Film</th>
|
|
<th class="text-center">Asesora</th>
|
|
<th class="text-center">Delivery</th>
|
|
<th class="text-center">Publicidad</th>
|
|
<th class="text-center bg-light">Inversión Total</th>
|
|
<th class="text-center">Promo 1</th>
|
|
<th class="text-center">Recaudo 1</th>
|
|
<th class="text-center">Promo 2</th>
|
|
<th class="text-center">Recaudo 2</th>
|
|
<th class="text-center">Promo 3</th>
|
|
<th class="text-center">Recaudo 3</th>
|
|
<th class="text-center">Acción</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?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);
|
|
|
|
function getVal($promo) {
|
|
preg_match('/[\d.]+/', $promo, $matches);
|
|
return isset($matches[0]) ? floatval($matches[0]) : 0;
|
|
}
|
|
|
|
$p1 = getVal($c['promo_1']);
|
|
$p2 = getVal($c['promo_2']);
|
|
$p3 = getVal($c['promo_3']);
|
|
|
|
$r1 = $p1 > 0 ? $p1 - $inversion_total : null;
|
|
$r2 = $p2 > 0 ? $p2 - $inversion_total : null;
|
|
$r3 = $p3 > 0 ? $p3 - $inversion_total : null;
|
|
?>
|
|
<tr id="row-<?php echo $c['id']; ?>" data-id="<?php echo $c['id']; ?>">
|
|
<td class="text-center fw-bold"><?php echo $c['orden']; ?></td>
|
|
<td style="max-width: 150px;" class="text-truncate" title="<?php echo htmlspecialchars($c['nombre_producto']); ?>">
|
|
<?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-v2">
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><input type="number" step="0.01" class="input-cell" data-field="costo_producto" value="<?php echo $c['costo_producto']; ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'costo_producto', this.value)"></td>
|
|
<td><input type="number" step="0.01" class="input-cell" data-field="costo_fijo_film" value="<?php echo $c['costo_fijo_film']; ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'costo_fijo_film', this.value)"></td>
|
|
<td><input type="number" step="0.01" class="input-cell" data-field="comision_asesora" value="<?php echo $c['comision_asesora']; ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'comision_asesora', this.value)"></td>
|
|
<td><input type="number" step="0.01" class="input-cell" data-field="delivery" value="<?php echo $c['delivery']; ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'delivery', this.value)"></td>
|
|
<td><input type="number" step="0.01" class="input-cell" data-field="costo_publicitario" value="<?php echo $c['costo_publicitario']; ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'costo_publicitario', this.value)"></td>
|
|
<td class="text-center">
|
|
<div class="recaudo-v2 bg-light fw-bold" id="total-<?php echo $c['id']; ?>">
|
|
S/ <?php echo number_format($inversion_total, 2); ?>
|
|
</div>
|
|
</td>
|
|
|
|
<td><input type="text" class="input-cell" data-field="promo_1" value="<?php echo htmlspecialchars($c['promo_1']); ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'promo_1', this.value)"></td>
|
|
<td><div class="recaudo-v2 recaudo-1 <?php echo $r1 >= 0 ? 'text-success' : 'text-danger'; ?>" id="r1-<?php echo $c['id']; ?>"><?php echo $r1 !== null ? 'S/ '.number_format($r1, 2) : '-'; ?></div></td>
|
|
|
|
<td><input type="text" class="input-cell" data-field="promo_2" value="<?php echo htmlspecialchars($c['promo_2']); ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'promo_2', this.value)"></td>
|
|
<td><div class="recaudo-v2 recaudo-2 <?php echo $r2 >= 0 ? 'text-success' : 'text-danger'; ?>" id="r2-<?php echo $c['id']; ?>"><?php echo $r2 !== null ? 'S/ '.number_format($r2, 2) : '-'; ?></div></td>
|
|
|
|
<td><input type="text" class="input-cell" data-field="promo_3" value="<?php echo htmlspecialchars($c['promo_3']); ?>" onchange="updateRow(<?php echo $c['id']; ?>, 'promo_3', this.value)"></td>
|
|
<td><div class="recaudo-v2 recaudo-3 <?php echo $r3 >= 0 ? 'text-success' : 'text-danger'; ?>" id="r3-<?php echo $c['id']; ?>"><?php echo $r3 !== null ? 'S/ '.number_format($r3, 2) : '-'; ?></div></td>
|
|
|
|
<td class="text-center">
|
|
<div class="d-flex gap-1">
|
|
<button class="btn btn-success btn-sm btn-save-row" onclick="location.reload()" title="Refrescar cálculos">
|
|
<i class="fas fa-sync-alt"></i>
|
|
</button>
|
|
<button class="btn btn-danger btn-sm" onclick="eliminarRow(<?php echo $c['id']; ?>)">
|
|
<i class="fas fa-trash"></i>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Nuevo -->
|
|
<div class="modal fade" id="modalNuevoV2" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form action="save_marketing_video.php" method="POST" enctype="multipart/form-data">
|
|
<input type="hidden" name="redirect" value="calculo_costos_v2.php?success=1">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Nuevo Producto</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">Producto</label>
|
|
<select name="producto_id" id="select_producto_nuevo" class="form-select" required onchange="fetchProductCost(this.value)">
|
|
<option value="">Seleccionar...</option>
|
|
<?php foreach ($productos_select as $p): ?>
|
|
<option value="<?php echo $p['id']; ?>"><?php echo htmlspecialchars($p['nombre']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Costo del Producto (S/)</label>
|
|
<input type="number" name="costo_producto" id="costo_producto_nuevo" class="form-control" step="0.01" placeholder="0.00">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Orden</label>
|
|
<input type="number" name="orden" class="form-control" value="1">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Imagen</label>
|
|
<input type="file" name="foto_producto" class="form-control">
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
|
<button type="submit" class="btn btn-primary">Guardar</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function fetchProductCost(productId) {
|
|
if (!productId) return;
|
|
|
|
fetch(`get_product_details.php?id=${productId}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success && data.product.costo) {
|
|
document.getElementById('costo_producto_nuevo').value = data.product.costo;
|
|
}
|
|
})
|
|
.catch(error => console.error('Error fetching product cost:', error));
|
|
}
|
|
|
|
function updateRow(id, field, value) {
|
|
const row = document.getElementById(`row-${id}`);
|
|
const input = row.querySelector(`[data-field="${field}"]`);
|
|
|
|
input.classList.add('saving');
|
|
console.log(`Actualizando ${field} para ID ${id} con valor ${value}`);
|
|
|
|
fetch('update_marketing_costos_field.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ id, field, value })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
input.classList.remove('saving');
|
|
if (data.success) {
|
|
input.classList.add('saved');
|
|
setTimeout(() => input.classList.remove('saved'), 1000);
|
|
|
|
// Recalcular todo en la fila
|
|
recalculateRow(id);
|
|
} else {
|
|
alert('Error al guardar: ' + data.error);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
input.classList.remove('saving');
|
|
console.error('Error:', error);
|
|
alert('Error de conexión');
|
|
});
|
|
}
|
|
|
|
function recalculateRow(id) {
|
|
const row = document.getElementById(`row-${id}`);
|
|
const fields = ['costo_producto', 'costo_fijo_film', 'comision_asesora', 'delivery', 'costo_publicitario'];
|
|
|
|
let total = 0;
|
|
fields.forEach(f => {
|
|
const val = parseFloat(row.querySelector(`[data-field="${f}"]`).value) || 0;
|
|
total += val;
|
|
});
|
|
|
|
document.getElementById(`total-${id}`).innerText = 'S/ ' + total.toFixed(2);
|
|
|
|
// Recalcular recaudos
|
|
for (let i = 1; i <= 3; i++) {
|
|
const promoVal = row.querySelector(`[data-field="promo_${i}"]`).value;
|
|
const recaudoEl = document.getElementById(`r${i}-${id}`);
|
|
|
|
const match = promoVal.match(/[\d.]+/);
|
|
const pVal = match ? parseFloat(match[0]) : 0;
|
|
|
|
if (pVal > 0) {
|
|
const recaudo = pVal - total;
|
|
recaudoEl.innerText = 'S/ ' + recaudo.toFixed(2);
|
|
recaudoEl.className = 'recaudo-v2 ' + (recaudo >= 0 ? 'text-success' : 'text-danger');
|
|
} else {
|
|
recaudoEl.innerText = '-';
|
|
recaudoEl.className = 'recaudo-v2 text-muted';
|
|
}
|
|
}
|
|
}
|
|
|
|
function eliminarRow(id) {
|
|
if (confirm('¿Eliminar este producto?')) {
|
|
window.location.href = 'delete_marketing_video.php?id=' + id + '&redirect=calculo_costos_v2.php';
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<?php include 'layout_footer.php'; ?>
|