| Server IP : 104.21.80.248 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : E:/Inetpub/www/supply_system/supplies/ |
Upload File : |
<?php
include_once __DIR__ . '/../config.php'; // ../ goes up one level from supplies/ to the root
include_once __DIR__ . '/../functions.php';
include_once __DIR__ . '/../includes/auth_check.php'; // Ensure user is logged in
// Optional: Add role check if needed, e.g., only supply managers can view details
// requireRole(['admin', 'supply_manager']);
$supply_id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Get supply ID from URL
$supply_info = null;
$batches = []; // Active batches
$inactive_batches = []; // Inactive/Consumed/Expired batches for history
$error_message = '';
if ($supply_id > 0) {
// --- Fetch supply details ---
$sql_supply = "SELECT s.*, c.category_name
FROM supplies s
JOIN categories c ON s.category_id = c.id
WHERE s.id = $supply_id";
$result_supply = mysqli_query($conn, $sql_supply);
if ($result_supply && mysqli_num_rows($result_supply) > 0) {
$supply_info = mysqli_fetch_assoc($result_supply);
mysqli_free_result($result_supply);
// --- Fetch ALL batches for this supply, order by status then date ---
$sql_batches = "SELECT sb.*, u.full_name as created_by_name
FROM supply_batches sb
LEFT JOIN users u ON sb.created_by = u.id
WHERE sb.supply_id = $supply_id
ORDER BY
CASE sb.status
WHEN 'active' THEN 1
WHEN 'consumed' THEN 2
WHEN 'expired' THEN 3
ELSE 4
END,
sb.received_date ASC, sb.id ASC"; // Active first (FIFO), then others
$result_batches = mysqli_query($conn, $sql_batches);
if ($result_batches) {
while ($row = mysqli_fetch_assoc($result_batches)) {
if ($row['status'] == 'active' && $row['quantity_remaining'] > 0) {
$batches[] = $row; // Active and remaining batches
} else {
$inactive_batches[] = $row; // Consumed, expired, or remaining = 0
}
}
mysqli_free_result($result_batches);
} else {
$error_message = "เกิดข้อผิดพลาดในการดึงข้อมูล Batch: " . mysqli_error($conn);
}
} else {
$error_message = "ไม่พบข้อมูลพัสดุรหัส ID: $supply_id";
}
} else {
// If ID is invalid or not provided, redirect back to the list
$_SESSION['message'] = 'ไม่ได้ระบุรหัสพัสดุ';
$_SESSION['message_type'] = 'warning';
redirect('index.php');
}
$page_title = isset($supply_info['supply_name']) ? "รายละเอียดพัสดุ: " . htmlspecialchars($supply_info['supply_name']) : "รายละเอียดพัสดุ";
// --- Include Header and Sidebar ---
include_once __DIR__ . '/../includes/header.php';
// Assuming supply managers and admins view this page
include_once __DIR__ . '/../includes/sidebar_supply.php';
// mysqli_close($conn); // Close connection later, after displaying messages if any
?>
<div class="container-fluid">
<h1 class="mt-4"><?php echo $page_title; ?></h1>
<?php if (isset($_SESSION['message'])): ?>
<div class="alert alert-<?php echo $_SESSION['message_type']; ?> alert-dismissible fade show" role="alert">
<?php echo $_SESSION['message']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['message'], $_SESSION['message_type']); ?>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger" role="alert">
<?php echo $error_message; ?>
</div>
<a href="index.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> กลับไปรายการพัสดุ</a>
<?php elseif ($supply_info): ?>
<div class="card mb-4">
<div class="card-header">
<i class="bi bi-info-circle me-1"></i>
ข้อมูลพัสดุ
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>รหัสพัสดุ:</strong> <?php echo htmlspecialchars($supply_info['supply_code']); ?></p>
<p><strong>ชื่อพัสดุ:</strong> <?php echo htmlspecialchars($supply_info['supply_name']); ?></p>
<p><strong>หมวดหมู่:</strong> <?php echo htmlspecialchars($supply_info['category_name']); ?></p>
<p><strong>หน่วยนับ:</strong> <?php echo htmlspecialchars($supply_info['unit']); ?></p>
<p><strong>คำอธิบาย:</strong> <?php echo nl2br(htmlspecialchars(isset($supply_info['description']) ? $supply_info['description'] : '-')); ?></p>
</div>
<div class="col-md-6">
<p><strong>จำนวนคงคลังรวม:</strong> <span class="fw-bold fs-5"><?php echo number_format($supply_info['quantity_in_stock']); ?></span></p>
<p><strong>จุดสั่งซื้อขั้นต่ำ:</strong> <?php echo number_format($supply_info['min_stock_level']); ?></p>
<p><strong>มูลค่าเฉลี่ยต่อหน่วย:</strong> <?php echo number_format($supply_info['average_unit_price'], 2); ?> บาท</p>
<p><strong>มูลค่ารวมในคลัง (ประมาณ):</strong> <?php echo number_format($supply_info['total_value'], 2); ?> บาท</p>
<p><strong>สถานะพัสดุ:</strong>
<?php if ($supply_info['status'] == 'active'): ?>
<span class="badge bg-success">ใช้งาน</span>
<?php else: ?>
<span class="badge bg-secondary">ไม่ใช้งาน</span>
<?php endif; ?>
<?php if ($supply_info['quantity_in_stock'] <= $supply_info['min_stock_level'] && $supply_info['min_stock_level'] > 0): ?>
<span class="badge bg-warning text-dark ms-1">ต่ำกว่าจุดสั่งซื้อ</span>
<?php endif; ?>
</p>
</div>
</div>
</div>
</div>
<div class="card mb-4">
<div class="card-header">
<i class="bi bi-layers-fill me-1"></i>
รายการ Batch ที่ยังคงเหลือ (เรียงตามลำดับ FIFO ที่จะถูกตัดจ่ายก่อน)
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-sm table-hover">
<thead class="table-light">
<tr>
<th>#</th>
<th>หมายเลข Batch</th>
<th class="text-end">จำนวนรับเข้า</th>
<th class="text-end">จำนวนคงเหลือ</th>
<th class="text-end">ราคา/หน่วย (บาท)</th>
<th class="text-end">มูลค่าคงเหลือ (บาท)</th>
<th>วันที่รับเข้า</th>
<th>วันที่หมดอายุ</th>
<th>ผู้จำหน่าย</th>
<th>เลขที่ใบส่งของ</th>
<th>ผู้บันทึก</th>
<th>ดำเนินการ</th> </tr>
</thead>
<tbody>
<?php if (!empty($batches)): ?>
<?php $batch_counter = 1; ?>
<?php foreach ($batches as $batch):
// Check if deletable (nothing consumed)
$can_delete = ($batch['quantity_remaining'] == $batch['quantity_received']);
?>
<tr <?php echo ($batch === $batches[0]) ? 'class="table-info"' : ''; // Highlight next batch ?>>
<td><?php echo $batch_counter++; ?></td>
<td><?php echo htmlspecialchars($batch['batch_no']); ?></td>
<td class="text-end"><?php echo number_format($batch['quantity_received']); ?></td>
<td class="text-end fw-bold"><?php echo number_format($batch['quantity_remaining']); ?></td>
<td class="text-end"><?php echo number_format($batch['unit_price'], 2); ?></td>
<td class="text-end"><?php echo number_format($batch['quantity_remaining'] * $batch['unit_price'], 2); ?></td>
<td><?php echo formatThaiDate($batch['received_date'], true, true); ?></td>
<td><?php echo $batch['expiry_date'] ? formatThaiDate($batch['expiry_date'], false, true) : '-'; ?></td>
<td><?php echo htmlspecialchars(isset($batch['supplier']) ? $batch['supplier'] : '-'); ?></td>
<td><?php echo htmlspecialchars(isset($batch['invoice_no']) ? $batch['invoice_no'] : '-'); ?></td>
<td><?php echo htmlspecialchars(isset($batch['created_by_name']) ? $batch['created_by_name'] : 'N/A'); ?></td>
<td class="text-center"> <a href="batch_edit.php?id=<?php echo $batch['id']; ?>&supply_id=<?php echo $supply_id; ?>" class="btn btn-warning btn-sm" title="แก้ไขข้อมูล Batch (ยกเว้นจำนวน)">
<i class="bi bi-pencil-fill"></i>
</a>
<?php if ($can_delete): ?>
<a href="action_batch_delete.php?id=<?php echo $batch['id']; ?>&supply_id=<?php echo $supply_id; ?>"
class="btn btn-danger btn-sm"
title="ลบ Batch (เฉพาะที่ยังไม่ถูกเบิก)"
onclick="return confirm('ต้องการลบ Batch นี้ใช่หรือไม่? (จะลบได้เฉพาะ Batch ที่ยังไม่มีการเบิกจ่ายเท่านั้น)');">
<i class="bi bi-trash-fill"></i>
</a>
<?php else: ?>
<button class="btn btn-danger btn-sm" title="ไม่สามารถลบได้ เนื่องจากมีการเบิกจ่ายจาก Batch นี้ไปแล้ว" disabled>
<i class="bi bi-trash-fill"></i>
</button>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="12" class="text-center text-muted">ไม่พบ Batch ที่ยังคงเหลือสำหรับพัสดุนี้</td> </tr>
<?php endif; ?>
</tbody>
</table>
<?php if (!empty($batches)): ?>
<small class="text-muted"><span class="table-info p-1 rounded">แถบสีฟ้า</span> คือ Batch ที่จะถูกเบิกจ่ายก่อนตามหลัก FIFO</small>
<?php endif; ?>
</div>
</div>
</div>
<?php if (!empty($inactive_batches)): ?>
<div class="card mb-4">
<div class="card-header">
<i class="bi bi-clock-history me-1"></i>
ประวัติ Batch ที่ใช้หมดแล้ว / หมดอายุ / ไม่ใช้งาน
</div>
<div class="card-body">
<div class="table-responsive" style="max-height: 300px; overflow-y: auto;">
<table class="table table-bordered table-sm table-secondary text-muted">
<thead class="table-light">
<tr>
<th>#</th>
<th>หมายเลข Batch</th>
<th class="text-end">จำนวนรับเข้า</th>
<th class="text-end">จำนวนคงเหลือ</th>
<th class="text-end">ราคา/หน่วย</th>
<th>วันที่รับเข้า</th>
<th>สถานะ</th>
<th>ผู้บันทึก</th>
</tr>
</thead>
<tbody>
<?php $inactive_counter = 1; ?>
<?php foreach ($inactive_batches as $batch): ?>
<tr>
<td><?php echo $inactive_counter++; ?></td>
<td><?php echo htmlspecialchars($batch['batch_no']); ?></td>
<td class="text-end"><?php echo number_format($batch['quantity_received']); ?></td>
<td class="text-end"><?php echo number_format($batch['quantity_remaining']); ?></td>
<td class="text-end"><?php echo number_format($batch['unit_price'], 2); ?></td>
<td><?php echo formatThaiDate($batch['received_date'], false, true); ?></td>
<td>
<span class="badge bg-<?php echo ($batch['status'] == 'consumed' ? 'secondary' : ($batch['status'] == 'expired' ? 'warning' : 'light text-dark')); ?>">
<?php echo htmlspecialchars($batch['status']); ?>
</span>
</td>
<td><?php echo htmlspecialchars(isset($batch['created_by_name']) ? $batch['created_by_name'] : 'N/A'); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<a href="index.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> กลับไปรายการพัสดุ</a>
<?php endif; // End check for $supply_info ?>
</div>
<?php
// --- Include Footer ---
include_once __DIR__ . '/../includes/footer.php';
// Close connection here if it wasn't closed before
if (isset($conn) && $conn) {
mysqli_close($conn);
}
?>