93 lines
3.5 KiB
PHP
93 lines
3.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
// Silenciar errores de PHP para asegurar una respuesta JSON limpia
|
|
ini_set('display_errors', 0);
|
|
error_reporting(0);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$response = [];
|
|
$error = '';
|
|
$message = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
|
|
$sede_id = filter_input(INPUT_POST, 'sede_id', FILTER_VALIDATE_INT);
|
|
$movement_date = filter_input(INPUT_POST, 'movement_date');
|
|
$quantity = 1;
|
|
|
|
if ($product_id && $sede_id && $movement_date) {
|
|
try {
|
|
$pdo = db();
|
|
if (!$pdo) {
|
|
throw new PDOException("No se pudo conectar a la base de datos.");
|
|
}
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM stock_sedes WHERE product_id = :product_id AND sede_id = :sede_id FOR UPDATE");
|
|
$stmt->execute(['product_id' => $product_id, 'sede_id' => $sede_id]);
|
|
$existing_stock = $stmt->fetch();
|
|
|
|
if ($existing_stock) {
|
|
$new_quantity = $existing_stock['quantity'] - $quantity;
|
|
if ($new_quantity < 0) {
|
|
$error = "No hay stock para registrar la salida. Stock actual: " . $existing_stock['quantity'];
|
|
$pdo->rollBack();
|
|
} else {
|
|
$update_stmt = $pdo->prepare("UPDATE stock_sedes SET quantity = :quantity WHERE id = :id");
|
|
$update_stmt->execute(['quantity' => $new_quantity, 'id' => $existing_stock['id']]);
|
|
|
|
$history_stmt = $pdo->prepare(
|
|
"INSERT INTO stock_movements (product_id, sede_id, quantity, type, movement_date)
|
|
VALUES (:product_id, :sede_id, :quantity, 'salida', :movement_date)"
|
|
);
|
|
$history_stmt->execute([
|
|
'product_id' => $product_id,
|
|
'sede_id' => $sede_id,
|
|
'quantity' => $quantity,
|
|
'movement_date' => $movement_date
|
|
]);
|
|
|
|
$pdo->commit();
|
|
|
|
$stmt_prod_name = $pdo->prepare("SELECT nombre FROM products WHERE id = :id");
|
|
$stmt_prod_name->execute(['id' => $product_id]);
|
|
$product_name = $stmt_prod_name->fetchColumn();
|
|
|
|
$message = "Salida de 1 unidad de '{$product_name}' registrada. Stock restante: {$new_quantity}.";
|
|
}
|
|
} else {
|
|
$error = "No hay stock registrado para este producto en la sede seleccionada.";
|
|
$pdo->rollBack();
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
if (isset($pdo) && $pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
// No exponer detalles del error de la DB al cliente
|
|
$error = "Error al actualizar el inventario. Verifique la conexión o los datos.";
|
|
// Loguear el error real para depuración
|
|
error_log("PDOException en registrar_salida_api.php: " . $e->getMessage());
|
|
}
|
|
} else {
|
|
$error = "Faltan datos para registrar la salida (producto, sede o fecha).";
|
|
}
|
|
} else {
|
|
$error = "Método de solicitud no válido.";
|
|
}
|
|
|
|
if ($error) {
|
|
$response['success'] = false;
|
|
$response['message'] = $error;
|
|
} else {
|
|
$response['success'] = true;
|
|
$response['message'] = $message;
|
|
}
|
|
|
|
echo json_encode($response);
|
|
exit;
|
|
?>
|