Files
gravity/ติดตามการจัดซื้อครุภัณ์ (พรส)/get_history.php
T
2026-09-16 23:20:08 +07:00

56 lines
1.7 KiB
PHP

<?php
require_once 'config.php';
header('Content-Type: application/json');
$id = $_GET['id'] ?? 0;
if ($id) {
$stmt = $pdo->prepare("SELECT * FROM audit_logs WHERE item_id = ? ORDER BY created_at ASC");
$stmt->execute([$id]);
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
// คำนวณระยะเวลา
$history = [];
$previousTime = null;
foreach ($logs as $log) {
$currentTime = new DateTime($log['created_at']);
$duration = '-';
if ($previousTime) {
$interval = $previousTime->diff($currentTime);
$days = $interval->days;
$hours = $interval->h;
$minutes = $interval->i;
$durationStr = [];
if ($days > 0) $durationStr[] = $days . ' วัน';
if ($hours > 0) $durationStr[] = $hours . ' ชั่วโมง';
if ($minutes > 0) $durationStr[] = $minutes . ' นาที';
if (empty($durationStr)) {
$duration = 'น้อยกว่า 1 นาที';
} else {
$duration = implode(' ', $durationStr);
}
}
$history[] = [
'id' => $log['id'],
'action' => $log['action'],
'old_status' => $log['old_status'],
'new_status' => $log['new_status'],
'details' => $log['details'],
'created_at' => $currentTime->format('d/m/Y H:i'),
'duration' => $duration
];
$previousTime = $currentTime;
}
echo json_encode(['success' => true, 'data' => $history]);
} else {
echo json_encode(['success' => false, 'message' => 'Invalid ID']);
}
?>