136 lines
4.8 KiB
PHP
136 lines
4.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
mysqli_report(MYSQLI_REPORT_OFF);
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
require_once 'conexion.php';
|
|
|
|
if ($conexion->connect_error) {
|
|
echo json_encode(['success' => false, 'error' => 'No hay conexión con la base de datos.']);
|
|
exit;
|
|
}
|
|
|
|
if (!is_array($data) || !isset($data['id'])) {
|
|
echo json_encode(['success' => false, 'error' => 'Faltan datos requeridos.']);
|
|
exit;
|
|
}
|
|
|
|
$idOrden = intval($data['id']);
|
|
$estado = isset($data['estado']) ? intval($data['estado']) : (isset($data['aprobada']) ? intval($data['aprobada']) : 0);
|
|
|
|
if (!in_array($estado, [0, 1, 2, 3], true)) {
|
|
echo json_encode(['success' => false, 'error' => 'Estado no válido.']);
|
|
exit;
|
|
}
|
|
|
|
function tableHasColumn($conexion, $table, $column)
|
|
{
|
|
$stmt = $conexion->prepare(
|
|
"SELECT COUNT(*) AS count FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?"
|
|
);
|
|
if (!$stmt) {
|
|
return false;
|
|
}
|
|
$stmt->bind_param('ss', $table, $column);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$row = $result ? $result->fetch_assoc() : null;
|
|
$stmt->close();
|
|
return $row && intval($row['count']) > 0;
|
|
}
|
|
|
|
$hasEstado = tableHasColumn($conexion, 'ordenes_compra', 'Estado');
|
|
if (!$hasEstado) {
|
|
$alterQuery = "ALTER TABLE ordenes_compra ADD COLUMN Estado TINYINT(1) DEFAULT 0";
|
|
if (!$conexion->query($alterQuery)) {
|
|
echo json_encode(['success' => false, 'error' => 'No se pudo corregir la estructura de la tabla: ' . $conexion->error]);
|
|
exit;
|
|
}
|
|
$hasEstado = true;
|
|
}
|
|
|
|
$hasAprobada = tableHasColumn($conexion, 'ordenes_compra', 'Aprobada');
|
|
if (!$hasAprobada) {
|
|
$alterQuery = "ALTER TABLE ordenes_compra ADD COLUMN Aprobada TINYINT(1) DEFAULT 0";
|
|
if (!$conexion->query($alterQuery)) {
|
|
echo json_encode(['success' => false, 'error' => 'No se pudo corregir la estructura de la tabla: ' . $conexion->error]);
|
|
exit;
|
|
}
|
|
$hasAprobada = true;
|
|
}
|
|
|
|
$aprobada = in_array($estado, [1, 3], true) ? 1 : 0;
|
|
|
|
$conexion->begin_transaction();
|
|
|
|
try {
|
|
if ($hasEstado && $hasAprobada) {
|
|
$stmt = $conexion->prepare("UPDATE ordenes_compra SET Estado = ?, Aprobada = ? WHERE ID_Orden = ?");
|
|
if (!$stmt) {
|
|
throw new Exception('Error en la consulta SQL: ' . $conexion->error);
|
|
}
|
|
$stmt->bind_param('iii', $estado, $aprobada, $idOrden);
|
|
} elseif ($hasEstado) {
|
|
$stmt = $conexion->prepare("UPDATE ordenes_compra SET Estado = ? WHERE ID_Orden = ?");
|
|
if (!$stmt) {
|
|
throw new Exception('Error en la consulta SQL: ' . $conexion->error);
|
|
}
|
|
$stmt->bind_param('ii', $estado, $idOrden);
|
|
} else {
|
|
$stmt = $conexion->prepare("UPDATE ordenes_compra SET Aprobada = ? WHERE ID_Orden = ?");
|
|
if (!$stmt) {
|
|
throw new Exception('Error en la consulta SQL: ' . $conexion->error);
|
|
}
|
|
$stmt->bind_param('ii', $aprobada, $idOrden);
|
|
}
|
|
|
|
if (!$stmt->execute()) {
|
|
throw new Exception('No se pudo actualizar el estado: ' . $stmt->error);
|
|
}
|
|
$stmt->close();
|
|
|
|
if ($estado === 3) {
|
|
$productosStmt = $conexion->prepare("SELECT ID_Producto, Cantidad FROM ordenes_productos WHERE ID_Orden = ?");
|
|
if (!$productosStmt) {
|
|
throw new Exception('No se pudieron obtener los productos de la orden.');
|
|
}
|
|
$productosStmt->bind_param('i', $idOrden);
|
|
if (!$productosStmt->execute()) {
|
|
$productosStmt->close();
|
|
throw new Exception('No se pudieron leer los productos de la orden.');
|
|
}
|
|
|
|
$productosResult = $productosStmt->get_result();
|
|
$stockStmt = $conexion->prepare("UPDATE productos SET Stock_Disponible = Stock_Disponible + ? WHERE ID_Producto = ?");
|
|
if (!$stockStmt) {
|
|
$productosStmt->close();
|
|
throw new Exception('No se pudo preparar la actualización de stock.');
|
|
}
|
|
|
|
while ($producto = $productosResult->fetch_assoc()) {
|
|
$cantidad = intval($producto['Cantidad']);
|
|
$idProducto = intval($producto['ID_Producto']);
|
|
|
|
if ($cantidad > 0 && $idProducto > 0) {
|
|
$stockStmt->bind_param('ii', $cantidad, $idProducto);
|
|
if (!$stockStmt->execute()) {
|
|
$stockStmt->close();
|
|
$productosStmt->close();
|
|
throw new Exception('No se pudo actualizar el stock de los productos.');
|
|
}
|
|
}
|
|
}
|
|
|
|
$stockStmt->close();
|
|
$productosStmt->close();
|
|
}
|
|
|
|
$conexion->commit();
|
|
echo json_encode(['success' => true, 'message' => 'Estado de la orden actualizado correctamente.']);
|
|
} catch (Exception $e) {
|
|
$conexion->rollback();
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|
|
|
|
$conexion->close();
|
|
?>
|