40097-vm/save_marketing_video_v3.php
2026-05-19 01:22:09 +00:00

77 lines
2.4 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
include 'db/config.php';
if (!isset($_SESSION['user_role']) || !in_array($_SESSION['user_role'], ['Administrador', 'admin'])) {
header('Location: dashboard.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$db = db();
$producto_id = !empty($_POST['producto_id']) ? $_POST['producto_id'] : null;
$orden = !empty($_POST['orden']) ? $_POST['orden'] : 0;
$costo_producto = !empty($_POST['costo_producto']) ? $_POST['costo_producto'] : 0;
$foto_path = null;
if (isset($_FILES['foto_producto']) && $_FILES['foto_producto']['error'] == 0) {
$target_dir = "assets/uploads/marketing_images/";
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_extension = pathinfo($_FILES["foto_producto"]["name"], PATHINFO_EXTENSION);
$file_name = uniqid() . '_v3.' . $file_extension;
$target_file = $target_dir . $file_name;
if (move_uploaded_file($_FILES["foto_producto"]["tmp_name"], $target_file)) {
$foto_path = $target_file;
}
}
try {
$db->beginTransaction();
$stmt = $db->prepare("INSERT INTO marketing_videos_v3 (
producto_id,
foto_producto,
estado,
orden,
fecha_creacion
) VALUES (?, ?, 'PENDIENTE', ?, CURRENT_TIMESTAMP)");
$stmt->execute([
$producto_id,
$foto_path,
$orden
]);
$video_id = $db->lastInsertId();
if (!$video_id) {
throw new Exception("No se pudo obtener el ID del video insertado.");
}
// Guardar costo inicial en la tabla de costos V3
$stmt_costo = $db->prepare("INSERT INTO marketing_costos_v3 (video_id, costo_producto, inversion_total) VALUES (?, ?, ?)");
$stmt_costo->execute([$video_id, $costo_producto, $costo_producto]);
$db->commit();
$redirect = $_POST['redirect'] ?? 'calculo_costos_v3.php?success=1';
header('Location: ' . $redirect);
exit();
} catch (Exception $e) {
if ($db->inTransaction()) {
$db->rollBack();
}
error_log("Error en save_marketing_video_v3.php: " . $e->getMessage());
header('Location: calculo_costos_v3.php?error=' . urlencode($e->getMessage()));
exit();
}
} else {
header('Location: calculo_costos_v3.php');
exit();
}
?>