diff --git a/call_center_pro.php b/call_center_pro.php index 14242956..b1e48c49 100644 --- a/call_center_pro.php +++ b/call_center_pro.php @@ -233,6 +233,12 @@ try { $tracking = drive_test_fetch_tracking($pdo, array_column($orders, 'source_key')); $orders = drive_test_merge_tracking($orders, $tracking); + $orders = array_values(array_filter($orders, static function (array $order): bool { + return empty($order['eliminado']); + })); + $orders = array_values(array_filter($orders, static function (array $order): bool { + return empty($order['eliminado']); + })); if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'assign_assessor') { $sourceKey = trim((string) ($_POST['source_key'] ?? '')); @@ -1189,7 +1195,10 @@ require_once 'layout_header.php'; Los cambios se guardan en la base local del módulo de prueba. -
+
+ + +
@@ -1593,6 +1602,42 @@ document.addEventListener('DOMContentLoaded', () => { }); }); +function eliminarPedido(sourceKey, trigger) { + const confirmed = window.confirm('¿Seguro que deseas eliminar este pedido del panel? Se ocultará del Call Center y no borrará la fila original de Drive.'); + if (!confirmed) { + return; + } + + const body = new FormData(); + body.append('source_key', sourceKey); + body.append('eliminar_pedido', '1'); + + if (trigger) { + trigger.disabled = true; + } + + fetch('update_callcenter_test_tracking.php', { + method: 'POST', + body: body + }) + .then(response => response.json()) + .then(data => { + if (!data.success) { + throw new Error(data.message || 'No se pudo eliminar el pedido del panel.'); + } + alert(data.message || 'Pedido eliminado del panel correctamente.'); + window.location.reload(); + }) + .catch(error => { + alert(error.message || 'Ocurrió un error al eliminar el pedido del panel.'); + }) + .finally(() => { + if (trigger) { + trigger.disabled = false; + } + }); +} + function guardarGestion(sourceKey, trigger, options = {}) { const subirLogistica = options.subirLogistica === true || options.subirRuta === true; const moverPromoFinal = options.moverPromoFinal === true; diff --git a/gestiones_callcenter.php b/gestiones_callcenter.php index cf21d593..e5c79877 100644 --- a/gestiones_callcenter.php +++ b/gestiones_callcenter.php @@ -261,6 +261,9 @@ try { $tracking = drive_test_fetch_tracking($pdo, array_column($orders, 'source_key')); $orders = drive_test_merge_tracking($orders, $tracking); + $orders = array_values(array_filter($orders, static function (array $order): bool { + return empty($order['eliminado']); + })); if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'assign_assessor') { $sourceKey = trim((string) ($_POST['source_key'] ?? '')); @@ -303,6 +306,9 @@ try { $tracking = drive_test_fetch_tracking($pdo, array_column($orders, 'source_key')); $orders = drive_test_merge_tracking($orders, $tracking); + $orders = array_values(array_filter($orders, static function (array $order): bool { + return empty($order['eliminado']); + })); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'auto_assign_cupo') { @@ -1510,7 +1516,10 @@ require_once 'layout_header.php'; Los cambios se guardan en la base local del módulo de prueba.
-
+
+ + +
@@ -1914,6 +1923,42 @@ document.addEventListener('DOMContentLoaded', () => { }); }); +function eliminarPedido(sourceKey, trigger) { + const confirmed = window.confirm('¿Seguro que deseas eliminar este pedido del panel? Se ocultará del Call Center y no borrará la fila original de Drive.'); + if (!confirmed) { + return; + } + + const body = new FormData(); + body.append('source_key', sourceKey); + body.append('eliminar_pedido', '1'); + + if (trigger) { + trigger.disabled = true; + } + + fetch('update_callcenter_test_tracking.php', { + method: 'POST', + body: body + }) + .then(response => response.json()) + .then(data => { + if (!data.success) { + throw new Error(data.message || 'No se pudo eliminar el pedido del panel.'); + } + alert(data.message || 'Pedido eliminado del panel correctamente.'); + window.location.reload(); + }) + .catch(error => { + alert(error.message || 'Ocurrió un error al eliminar el pedido del panel.'); + }) + .finally(() => { + if (trigger) { + trigger.disabled = false; + } + }); +} + function guardarGestion(sourceKey, trigger, options = {}) { const subirLogistica = options.subirLogistica === true || options.subirRuta === true; const moverPromoFinal = options.moverPromoFinal === true; diff --git a/includes/callcenter_test_helpers.php b/includes/callcenter_test_helpers.php index 3de955c5..920f8ff0 100644 --- a/includes/callcenter_test_helpers.php +++ b/includes/callcenter_test_helpers.php @@ -61,6 +61,8 @@ function cc_test_ensure_tracking_table(PDO $pdo): void `cancelado_evidencia_path` VARCHAR(255) NULL, `cancelado_evidencia_subido_at` DATETIME NULL, `cancelado_evidencia_subido_por` INT NULL, + `eliminado_at` DATETIME NULL, + `eliminado_por` INT NULL, `ruta_contraentrega_pedido_id` INT NULL, `ruta_contraentrega_subido_at` DATETIME NULL, `ruta_contraentrega_subido_por` INT NULL, @@ -107,7 +109,9 @@ function cc_test_ensure_tracking_table(PDO $pdo): void cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'cancelado_evidencia_path', 'VARCHAR(255) NULL AFTER `promo_final_evidencia_subido_por`'); cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'cancelado_evidencia_subido_at', 'DATETIME NULL AFTER `cancelado_evidencia_path`'); cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'cancelado_evidencia_subido_por', 'INT NULL AFTER `cancelado_evidencia_subido_at`'); - cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'ruta_contraentrega_pedido_id', 'INT NULL AFTER `cancelado_evidencia_subido_por`'); + cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'eliminado_at', 'DATETIME NULL AFTER `cancelado_evidencia_subido_por`'); + cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'eliminado_por', 'INT NULL AFTER `eliminado_at`'); + cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'ruta_contraentrega_pedido_id', 'INT NULL AFTER `eliminado_por`'); cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'ruta_contraentrega_subido_at', 'DATETIME NULL AFTER `ruta_contraentrega_pedido_id`'); cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'ruta_contraentrega_subido_por', 'INT NULL AFTER `ruta_contraentrega_subido_at`'); cc_test_ensure_column($pdo, 'callcenter_test_tracking', 'pedido_rotulado_pedido_id', 'INT NULL AFTER `ruta_contraentrega_subido_por`'); diff --git a/includes/drive_test_orders.php b/includes/drive_test_orders.php index 34a9a434..c6982bfc 100644 --- a/includes/drive_test_orders.php +++ b/includes/drive_test_orders.php @@ -229,7 +229,7 @@ function drive_test_fetch_tracking(PDO $pdo, array $sourceKeys): array cc_test_ensure_tracking_table($pdo); $placeholders = implode(',', array_fill(0, count($sourceKeys), '?')); - $stmt = $pdo->prepare("SELECT source_key, estado, nota_seguimiento, user_id, direccion, referencia, agencia, sede_agencia, sede, ciudad, distrito, dni, numero_cuenta_sede_id, numero_cuenta_dni, observaciones, coordenadas, producto, cantidad, precio, monto_adelantado, confirmacion_producto, confirmacion_cantidad, confirmacion_precio, confirmacion_producto_extra, confirmacion_cantidad_extra, confirmacion_precio_extra, proxima_llamada_at, fecha_entrega_programada, numero_cuenta_enviado_at, promo_final_evidencia_path, promo_final_evidencia_subido_at, promo_final_evidencia_subido_por, cancelado_evidencia_path, cancelado_evidencia_subido_at, cancelado_evidencia_subido_por, ruta_contraentrega_pedido_id, ruta_contraentrega_subido_at, ruta_contraentrega_subido_por, pedido_rotulado_pedido_id, pedido_rotulado_subido_at, pedido_rotulado_subido_por, ultima_gestion_at, updated_at FROM callcenter_test_tracking WHERE source_key IN ($placeholders)"); + $stmt = $pdo->prepare("SELECT source_key, estado, nota_seguimiento, user_id, direccion, referencia, agencia, sede_agencia, sede, ciudad, distrito, dni, numero_cuenta_sede_id, numero_cuenta_dni, observaciones, coordenadas, producto, cantidad, precio, monto_adelantado, confirmacion_producto, confirmacion_cantidad, confirmacion_precio, confirmacion_producto_extra, confirmacion_cantidad_extra, confirmacion_precio_extra, proxima_llamada_at, fecha_entrega_programada, numero_cuenta_enviado_at, promo_final_evidencia_path, promo_final_evidencia_subido_at, promo_final_evidencia_subido_por, cancelado_evidencia_path, cancelado_evidencia_subido_at, cancelado_evidencia_subido_por, eliminado_at, eliminado_por, ruta_contraentrega_pedido_id, ruta_contraentrega_subido_at, ruta_contraentrega_subido_por, pedido_rotulado_pedido_id, pedido_rotulado_subido_at, pedido_rotulado_subido_por, ultima_gestion_at, updated_at FROM callcenter_test_tracking WHERE source_key IN ($placeholders)"); $stmt->execute($sourceKeys); $tracking = []; @@ -260,6 +260,9 @@ function drive_test_merge_tracking(array $orders, array $tracking): array $order['cancelado_evidencia_path'] = $current['cancelado_evidencia_path'] ?? null; $order['cancelado_evidencia_subido_at'] = $current['cancelado_evidencia_subido_at'] ?? null; $order['cancelado_evidencia_subido_por'] = isset($current['cancelado_evidencia_subido_por']) && (int) $current['cancelado_evidencia_subido_por'] > 0 ? (int) $current['cancelado_evidencia_subido_por'] : null; + $order['eliminado_at'] = $current['eliminado_at'] ?? null; + $order['eliminado_por'] = isset($current['eliminado_por']) && (int) $current['eliminado_por'] > 0 ? (int) $current['eliminado_por'] : null; + $order['eliminado'] = trim((string) ($order['eliminado_at'] ?? '')) !== ''; $order['ruta_contraentrega_pedido_id'] = isset($current['ruta_contraentrega_pedido_id']) && (int) $current['ruta_contraentrega_pedido_id'] > 0 ? (int) $current['ruta_contraentrega_pedido_id'] : null; $order['ruta_contraentrega_subido_at'] = $current['ruta_contraentrega_subido_at'] ?? null; $order['ruta_contraentrega_subido_por'] = isset($current['ruta_contraentrega_subido_por']) && (int) $current['ruta_contraentrega_subido_por'] > 0 ? (int) $current['ruta_contraentrega_subido_por'] : null; diff --git a/update_callcenter_test_tracking.php b/update_callcenter_test_tracking.php index c95ff1a1..75365200 100644 --- a/update_callcenter_test_tracking.php +++ b/update_callcenter_test_tracking.php @@ -777,6 +777,7 @@ $estado = cc_test_normalize_state(trim((string) ($_POST['estado'] ?? 'POR LLAMAR $validStates = cc_test_valid_states(); $subirALogistica = (string) ($_POST['subir_a_logistica'] ?? ($_POST['subir_a_ruta'] ?? '')) === '1'; $moverAPromoFinal = (string) ($_POST['mover_a_promo_final'] ?? '') === '1'; +$eliminarPedido = (string) ($_POST['eliminar_pedido'] ?? '') === '1'; if ($sourceKey === '' || !preg_match('/^[a-f0-9]{40}$/', $sourceKey)) { http_response_code(400); @@ -836,7 +837,7 @@ try { $pdo = db(); cc_test_ensure_tracking_table($pdo); - $stmtCurrent = $pdo->prepare('SELECT estado, numero_cuenta_enviado_at, numero_cuenta_sede_id, numero_cuenta_dni, user_id, promo_final_evidencia_path, promo_final_evidencia_subido_at, promo_final_evidencia_subido_por, cancelado_evidencia_path, cancelado_evidencia_subido_at, cancelado_evidencia_subido_por, ruta_contraentrega_pedido_id, pedido_rotulado_pedido_id FROM callcenter_test_tracking WHERE source_key = ? LIMIT 1'); + $stmtCurrent = $pdo->prepare('SELECT estado, numero_cuenta_enviado_at, numero_cuenta_sede_id, numero_cuenta_dni, user_id, promo_final_evidencia_path, promo_final_evidencia_subido_at, promo_final_evidencia_subido_por, cancelado_evidencia_path, cancelado_evidencia_subido_at, cancelado_evidencia_subido_por, eliminado_at, eliminado_por, ruta_contraentrega_pedido_id, pedido_rotulado_pedido_id FROM callcenter_test_tracking WHERE source_key = ? LIMIT 1'); $stmtCurrent->execute([$sourceKey]); $currentTracking = $stmtCurrent->fetch(PDO::FETCH_ASSOC) ?: null; @@ -875,6 +876,83 @@ try { } } + $deletedAtCurrent = trim((string) ($currentTracking['eliminado_at'] ?? '')); + $isDeleted = $deletedAtCurrent !== ''; + + if ($eliminarPedido) { + if (!$isAdmin) { + http_response_code(403); + echo json_encode(['success' => false, 'message' => 'Solo un administrador puede eliminar pedidos del panel.']); + exit; + } + + if ($isDeleted) { + echo json_encode(['success' => true, 'message' => 'El pedido ya estaba eliminado del panel.']); + exit; + } + + $deleteState = cc_test_normalize_state((string) ($currentTracking['estado'] ?? $estado)); + if (!in_array($deleteState, $validStates, true)) { + $deleteState = 'POR LLAMAR'; + } + + $deleteUserId = $currentTracking && (int) ($currentTracking['user_id'] ?? 0) > 0 + ? (int) $currentTracking['user_id'] + : null; + $deletedAt = (new DateTimeImmutable('now'))->format('Y-m-d H:i:s'); + $deletedBy = (int) ($_SESSION['user_id'] ?? 0); + + $stmtDelete = $pdo->prepare( + 'INSERT INTO callcenter_test_tracking ( + source_key, + estado, + user_id, + eliminado_at, + eliminado_por, + ultima_gestion_at + ) VALUES ( + :source_key, + :estado, + :user_id, + :eliminado_at, + :eliminado_por, + CURRENT_TIMESTAMP + ) + ON DUPLICATE KEY UPDATE + estado = VALUES(estado), + user_id = VALUES(user_id), + eliminado_at = VALUES(eliminado_at), + eliminado_por = VALUES(eliminado_por), + ultima_gestion_at = CURRENT_TIMESTAMP, + updated_at = CURRENT_TIMESTAMP' + ); + $stmtDelete->bindValue(':source_key', $sourceKey, PDO::PARAM_STR); + $stmtDelete->bindValue(':estado', $deleteState, PDO::PARAM_STR); + if ($deleteUserId === null) { + $stmtDelete->bindValue(':user_id', null, PDO::PARAM_NULL); + } else { + $stmtDelete->bindValue(':user_id', $deleteUserId, PDO::PARAM_INT); + } + $stmtDelete->bindValue(':eliminado_at', $deletedAt, PDO::PARAM_STR); + if ($deletedBy > 0) { + $stmtDelete->bindValue(':eliminado_por', $deletedBy, PDO::PARAM_INT); + } else { + $stmtDelete->bindValue(':eliminado_por', null, PDO::PARAM_NULL); + } + $stmtDelete->execute(); + + echo json_encode([ + 'success' => true, + 'message' => 'Pedido eliminado del panel correctamente.', + 'eliminado_at' => $deletedAt, + ]); + exit; + } + + if ($isDeleted) { + throw new RuntimeException('Este pedido fue eliminado del panel por un administrador.'); + } + $userIdToSet = $isAdmin ? ($currentTracking ? ((int) ($currentTracking['user_id'] ?? 0) > 0 ? (int) $currentTracking['user_id'] : null) : null) : (int) $_SESSION['user_id'];