| Server IP : 172.67.187.206 / 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 : /Inetpub/www/supply_system/requisitions/ |
Upload File : |
<?php
include_once __DIR__ . '/../config.php';
include_once __DIR__ . '/../functions.php'; // Needed for formatThaiDate
include_once __DIR__ . '/../includes/auth_check.php';
// --- FPDF Library (Make sure the path is correct) ---
require_once __DIR__ . '/../libs/fpdf/fpdf.php'; // Adjust path if necessary
// --- Function for Thai PDF (Needs THSarabun files in font directory) ---
function iconv_helper($text) {
// Converts UTF-8 to TIS-620 (Thai Windows codepage used by FPDF with Thai fonts)
return iconv('utf-8', 'cp874', $text); // cp874 is often used for TIS-620 in iconv
}
$req_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$action = isset($_GET['action']) ? $_GET['action'] : '';
$requisition = null;
$details = []; // Array to store detail rows
$transactions = [];
$error_message = '';
$user_id = getUserData('user_id');
$user_type = getUserData('user_type');
$user_dept_id = getUserData('department_id');
// --- Fetch Data ---
if ($req_id > 0) {
// --- Fetch Requisition Header ---
$sql_req = "SELECT r.*, d.dept_name, d.dept_code, u_req.full_name as requester_name,
u_head.full_name as head_approver_name, u_app.full_name as approver_name
FROM requisitions r
JOIN departments d ON r.department_id = d.id
JOIN users u_req ON r.requested_by = u_req.id
LEFT JOIN users u_head ON r.head_approved_by = u_head.id
LEFT JOIN users u_app ON r.approved_by = u_app.id
WHERE r.id = $req_id";
$result_req = mysqli_query($conn, $sql_req);
if ($result_req && mysqli_num_rows($result_req) > 0) {
$requisition = mysqli_fetch_assoc($result_req);
mysqli_free_result($result_req);
// --- Permission Check ---
$can_view = false;
if ($user_type == 'admin' || $user_type == 'supply_manager') {
$can_view = true;
} elseif ($user_type == 'head_of_department' && $requisition['department_id'] == $user_dept_id) {
$can_view = true;
} elseif ($user_type == 'department' && $requisition['requested_by'] == $user_id) {
$can_view = true;
}
if (!$can_view) {
$_SESSION['message'] = "คุณไม่มีสิทธิ์เข้าดูคำขอเบิกนี้";
$_SESSION['message_type'] = 'warning';
redirect('../index.php');
}
// --- Fetch Requisition Details ---
$sql_details = "SELECT rd.*, s.supply_code, s.supply_name, s.unit, c.category_name
FROM requisition_details rd
JOIN supplies s ON rd.supply_id = s.id
JOIN categories c ON s.category_id = c.id
WHERE rd.requisition_id = $req_id
ORDER BY s.supply_code ASC";
$result_details = mysqli_query($conn, $sql_details); // This is the mysqli_result object
if ($result_details) {
while ($row = mysqli_fetch_assoc($result_details)) {
$details[] = $row; // Populate the $details array
}
mysqli_free_result($result_details); // <-- (FIX 1) Free the result *here*
} else {
$error_message .= "เกิดข้อผิดพลาดในการดึงรายการพัสดุ: " . mysqli_error($conn) . "<br>";
}
// --- Fetch Supply Transactions if Approved ---
if ($requisition['status'] == 'approved') {
$sql_trans = "SELECT st.*, s.supply_code, s.supply_name, b.batch_no
FROM supply_transactions st
JOIN supplies s ON st.supply_id = s.id
LEFT JOIN supply_batches b ON st.batch_id = b.id
WHERE st.reference_type = 'requisition' AND st.reference_id = $req_id AND st.transaction_type = 'out'";
$result_trans = mysqli_query($conn, $sql_trans);
if($result_trans){
while($row = mysqli_fetch_assoc($result_trans)){
$transactions[] = $row;
}
mysqli_free_result($result_trans);
} else {
$error_message .= "เกิดข้อผิดพลาดในการดึงข้อมูลการตัดจ่าย: " . mysqli_error($conn) . "<br>";
}
}
} else {
$error_message = "ไม่พบข้อมูลคำขอเบิก ID: $req_id";
}
} else {
redirect('../index.php'); // Redirect if no ID provided
}
// --- PDF Generation Logic ---
if ($action == 'print_pdf' && $requisition && !$error_message) {
// --- Calculate Fiscal Year and Running Number ---
$running_number_display = $requisition['id']; // Default to ID
$fiscal_year_be = date('Y') + 543; // Default to current BE year
if ($requisition['status'] == 'approved' && !empty($requisition['approved_date'])) {
$current_approved_date = $requisition['approved_date'];
$current_timestamp = strtotime($current_approved_date);
$current_year = (int)date('Y', $current_timestamp);
$current_month = (int)date('n', $current_timestamp);
if ($current_month >= 10) { // Fiscal year starts Oct 1
$fiscal_year_start_sql = "'$current_year-10-01 00:00:00'";
$fiscal_year_end_sql = "'" . ($current_year + 1) . "-09-30 23:59:59'";
$fiscal_year_be = ($current_year + 543) + 1;
} else {
$fiscal_year_start_sql = "'" . ($current_year - 1) . "-10-01 00:00:00'";
$fiscal_year_end_sql = "'$current_year-09-30 23:59:59'";
$fiscal_year_be = ($current_year + 543);
}
$sql_running_num = "SELECT COUNT(id) as running_count
FROM requisitions
WHERE status = 'approved'
AND approved_date >= $fiscal_year_start_sql
AND approved_date <= $fiscal_year_end_sql
AND (approved_date < '$current_approved_date' OR (approved_date = '$current_approved_date' AND id <= $req_id))";
$res_running_num = mysqli_query($conn, $sql_running_num);
if ($res_running_num && $row_num = mysqli_fetch_assoc($res_running_num)) {
$running_number_display = $row_num['running_count'];
mysqli_free_result($res_running_num);
} else {
$running_number_display = $requisition['id'];
}
} else {
$request_date_obj = date_create($requisition['request_date']);
$request_year = (int)date_format($request_date_obj, 'Y');
$request_month = (int)date_format($request_date_obj, 'n');
$fiscal_year_be = ($request_month >= 10) ? ($request_year + 543 + 1) : ($request_year + 543);
$running_number_display = 'N/A';
}
$header_ref = $running_number_display . '/' . $fiscal_year_be;
// --- Initialize PDF ---
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
// --- Add Thai Fonts ---
$fontpath = __DIR__ . '/../libs/fpdf/font/';
$pdf->AddFont('sarabun', '', 'THSarabun.php');
$pdf->AddFont('sarabunb', '', 'THSarabunB.php');
// --- Header ---
$pdf->SetFont('sarabunb', '', 16);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 8, iconv_helper('รายงานคำขอเบิกพัสดุ'), 0, 0, 'C');
$pdf->SetX($pdf->GetPageWidth() - 10 - 30);
$pdf->SetFont('sarabunb', '', 12);
$pdf->Cell(30, 8, iconv_helper($header_ref), 0, 1, 'R');
$pdf->Ln(5);
// --- Requisition Info ---
$pdf->SetFont('sarabunb', '', 14);
$pdf->Cell(0, 8, iconv_helper('ข้อมูลคำขอ'), 0, 1, 'L');
$pdf->Line(10, $pdf->GetY(), $pdf->GetPageWidth() - 10, $pdf->GetY());
$pdf->Ln(2);
$pdf->SetFont('sarabun', '', 11);
$col1_width = 40;
$col2_width = 0;
$pdf->Cell($col1_width, 6, iconv_helper('เลขที่คำขอ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper($requisition['requisition_no']), 0, 1, 'L');
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('กลุ่มงาน:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper($requisition['dept_name'] . ' (' . $requisition['dept_code'] . ')'), 0, 1, 'L');
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('ผู้ขอ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper($requisition['requester_name']), 0, 1, 'L');
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('วันที่ขอ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper(formatThaiDate($requisition['request_date'], false)), 0, 1, 'L');
if ($requisition['head_approved_by']) {
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('ผอ.กลุ่มรับทราบ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper($requisition['head_approver_name']), 0, 1, 'L');
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('วันที่ ผอ.กลุ่มรับทราบ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper(formatThaiDate($requisition['head_approved_date'], false)), 0, 1, 'L');
}
$pdf->Ln(2);
// --- Approval Info ---
if ($requisition['approved_by'] || $requisition['status'] == 'approved' || $requisition['status'] == 'rejected') {
$pdf->SetFont('sarabunb', '', 14);
$pdf->Cell(0, 8, iconv_helper('ข้อมูลการอนุมัติ'), 0, 1, 'L');
$pdf->Line(10, $pdf->GetY(), $pdf->GetPageWidth() - 10, $pdf->GetY());
$pdf->Ln(2);
if ($requisition['approved_by']) {
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('ผู้ดูแลพัสดุอนุมัติ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper($requisition['approver_name']), 0, 1, 'L');
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('วันที่อนุมัติ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$pdf->Cell($col2_width, 6, iconv_helper(formatThaiDate($requisition['approved_date'], false)), 0, 1, 'L');
}
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell($col1_width, 6, iconv_helper('สถานะ:'), 0, 0, 'L');
$pdf->SetFont('sarabunb', '', 11);
$status_text = 'ไม่ทราบสถานะ';
switch ($requisition['status']) {
case 'approved': $status_text = 'อนุมัติแล้ว'; break;
case 'rejected': $status_text = 'ไม่อนุมัติ (พัสดุ)'; break;
case 'head_rejected': $status_text = 'ไม่อนุมัติ (ผอ. กลุ่ม)'; break;
case 'head_approved': $status_text = 'รอฝ่ายพัสดุอนุมัติ'; break;
case 'pending': $status_text = 'รอ ผอ.กลุ่มรับทราบ'; break;
}
$pdf->Cell(0, 6, iconv_helper($status_text), 0, 1, 'L');
$pdf->Ln(2);
}
// --- Details Table ---
$pdf->Ln(5);
$pdf->SetFont('sarabunb', '', 14);
$pdf->Cell(0, 8, iconv_helper('รายการพัสดุที่ขอเบิก'), 0, 1, 'L');
$pdf->Line(10, $pdf->GetY(), $pdf->GetPageWidth() - 10, $pdf->GetY());
$pdf->Ln(2);
$pdf->SetFont('sarabunb', '', 10);
$pdf->SetFillColor(230, 230, 230);
$pdf->SetDrawColor(150, 150, 150);
$pdf->SetLineWidth(0.1);
$w_code = 25;
$w_name = 80;
$w_unit = 15;
$w_req = 15;
$w_app = 15;
$w_val = 25;
$pdf->Cell($w_code, 7, iconv_helper('รหัสพัสดุ'), 1, 0, 'C', true);
$pdf->Cell($w_name, 7, iconv_helper('ชื่อพัสดุ'), 1, 0, 'C', true);
$pdf->Cell($w_unit, 7, iconv_helper('หน่วย'), 1, 0, 'C', true);
$pdf->Cell($w_req, 7, iconv_helper('จำนวนขอ'), 1, 0, 'C', true);
$pdf->Cell($w_app, 7, iconv_helper('จำนวนได้'), 1, 0, 'C', true);
$pdf->Cell($w_val, 7, iconv_helper('มูลค่ารวม'), 1, 1, 'C', true);
// Table Data
$pdf->SetFont('sarabun', '', 10);
$pdf->SetFillColor(255, 255, 255);
$total_requested = 0;
$total_approved = 0;
// --- (FIX 2) Loop through the $details *array* instead of mysqli_fetch_assoc ---
foreach ($details as $detail) {
$total_requested += $detail['requested_quantity'];
$total_approved += $detail['approved_quantity'];
$cellHeight = 6;
$currentY = $pdf->GetY();
$currentX = $pdf->GetX();
$name_width = $pdf->GetStringWidth(iconv_helper($detail['supply_name']));
$lines = ceil($name_width / ($w_name - 2));
$maxHeight = $lines * ($cellHeight - 1);
if($maxHeight < $cellHeight) $maxHeight = $cellHeight;
$pdf->MultiCell($w_code, $maxHeight, iconv_helper($detail['supply_code']), 1, 'C', false);
$pdf->SetXY($currentX + $w_code, $currentY);
$pdf->MultiCell($w_name, $maxHeight, iconv_helper($detail['supply_name']), 1, 'L', false);
$pdf->SetXY($currentX + $w_code + $w_name, $currentY);
$pdf->MultiCell($w_unit, $maxHeight, iconv_helper($detail['unit']), 1, 'C', false);
$pdf->SetXY($currentX + $w_code + $w_name + $w_unit, $currentY);
$pdf->MultiCell($w_req, $maxHeight, iconv_helper(number_format($detail['requested_quantity'])), 1, 'C', false);
$pdf->SetXY($currentX + $w_code + $w_name + $w_unit + $w_req, $currentY);
$pdf->MultiCell($w_app, $maxHeight, iconv_helper(number_format($detail['approved_quantity'])), 1, 'C', false);
$pdf->SetXY($currentX + $w_code + $w_name + $w_unit + $w_req + $w_app, $currentY);
$pdf->MultiCell($w_val, $maxHeight, iconv_helper(number_format($detail['total_value'], 2)), 1, 'R', false);
}
// --- (FIX 2) End of loop change ---
// Table Footer (Totals)
$pdf->SetFont('sarabunb', '', 10);
$pdf->Cell($w_code + $w_name + $w_unit, 7, iconv_helper('รวมทั้งหมด'), 1, 0, 'C');
$pdf->Cell($w_req, 7, iconv_helper(number_format($total_requested)), 1, 0, 'C');
$pdf->Cell($w_app, 7, iconv_helper(number_format($total_approved)), 1, 0, 'C');
$pdf->Cell($w_val, 7, iconv_helper(number_format($requisition['total_value'], 2)), 1, 1, 'R');
// --- Signatures ---
$pdf->Ln(20);
$pdf->SetFont('sarabun', '', 11);
$pdf->Cell(95, 6, iconv_helper('ลายเซ็นผู้ขอ'), 0, 0, 'C');
$pdf->Cell(95, 6, iconv_helper('ลายเซ็นผู้อนุมัติ'), 0, 1, 'C');
$pdf->Ln(15);
$pdf->Cell(95, 6, iconv_helper('(............................................)'), 0, 0, 'C');
$pdf->Cell(95, 6, iconv_helper('(............................................)'), 0, 1, 'C');
$pdf->Cell(95, 6, iconv_helper($requisition['requester_name']), 0, 0, 'C');
$pdf->Cell(95, 6, iconv_helper(isset($requisition['approver_name']) ? $requisition['approver_name'] : ''), 0, 1, 'C');
// --- Footer: Print Date ---
$pdf->SetY(-20);
$pdf->SetFont('sarabun', '', 9);
$pdf->SetTextColor(100, 100, 100);
$pdf->Cell(0, 6, iconv_helper('พิมพ์เมื่อ: ' . formatThaiDate(date('Y-m-d H:i:s'), true)), 0, 1, 'R');
// --- Output PDF ---
$filename = 'requisition_' . $requisition['requisition_no'] . '.pdf';
$pdf->Output('I', $filename);
mysqli_close($conn); // Close connection *inside* PDF block
exit(); // Stop script execution after sending PDF
} // --- End PDF Generation Logic ---
// --- HTML Display Logic ---
$page_title = "รายละเอียดคำขอเบิก " . ($requisition ? htmlspecialchars($requisition['requisition_no']) : '');
include_once __DIR__ . '/../includes/header.php';
// Include Sidebar based on user type
switch ($user_type) {
case 'admin':
case 'supply_manager':
include_once __DIR__ . '/../includes/sidebar_supply.php';
break;
case 'head_of_department':
include_once __DIR__ . '/../includes/sidebar_head.php';
break;
case 'department':
include_once __DIR__ . '/../includes/sidebar_dept.php';
break;
default:
include_once __DIR__ . '/../includes/sidebar_dept.php';
break;
}
// DO NOT CLOSE CONNECTION HERE
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mt-4">
<h1><?php echo $page_title; ?></h1>
<?php if ($requisition && !$error_message): ?>
<a href="view.php?id=<?php echo $req_id; ?>&action=print_pdf" target="_blank" class="btn btn-danger">
<i class="bi bi-file-earmark-pdf-fill"></i> พิมพ์ PDF
</a>
<?php endif; ?>
</div>
<?php if ($error_message): ?>
<div class="alert alert-danger mt-3" 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 ($requisition): ?>
<div class="card my-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span><i class="bi bi-file-earmark-text me-1"></i> ข้อมูลคำขอเบิก</span>
<span>สถานะ:
<?php
$status_badge = 'secondary';
$status_text = $requisition['status'];
switch ($requisition['status']) {
case 'pending': $status_badge = 'warning text-dark'; $status_text = 'รอ ผอ. กลุ่มรับทราบ'; break;
case 'head_approved': $status_badge = 'info text-dark'; $status_text = 'รอฝ่ายพัสดุอนุมัติ'; break;
case 'approved': $status_badge = 'success'; $status_text = 'อนุมัติแล้ว'; break;
case 'rejected': $status_badge = 'danger'; $status_text = 'ไม่อนุมัติ (พัสดุ)'; break;
case 'head_rejected': $status_badge = 'danger'; $status_text = 'ไม่อนุมัติ (ผอ. กลุ่ม)'; break;
}
?>
<span class="badge bg-<?php echo $status_badge; ?>"><?php echo $status_text; ?></span>
</span>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>เลขที่คำขอ:</strong> <?php echo htmlspecialchars($requisition['requisition_no']); ?></p>
<p><strong>กลุ่มงาน:</strong> <?php echo htmlspecialchars($requisition['dept_name']); ?></p>
<p><strong>ผู้ขอเบิก:</strong> <?php echo htmlspecialchars($requisition['requester_name']); ?></p>
<p><strong>วันที่ขอ:</strong> <?php echo formatThaiDate($requisition['request_date'], false); ?></p>
</div>
<div class="col-md-6">
<p><strong>ผอ. กลุ่มรับทราบ:</strong> <?php echo $requisition['head_approver_name'] ? htmlspecialchars($requisition['head_approver_name']) . ' (' . formatThaiDate($requisition['head_approved_date'], false) . ')' : '-'; ?></p>
<p><strong>ผู้อนุมัติ (พัสดุ):</strong> <?php echo $requisition['approver_name'] ? htmlspecialchars($requisition['approver_name']) . ' (' . formatThaiDate($requisition['approved_date'], false) . ')' : '-'; ?></p>
<p><strong>หมายเหตุ (ผู้ขอ):</strong> <?php echo nl2br(htmlspecialchars(isset($requisition['remarks']) ? $requisition['remarks'] : '-')); ?></p>
<?php if($requisition['status'] == 'rejected' || $requisition['status'] == 'head_rejected'): ?>
<p class="text-danger"><strong>เหตุผลไม่อนุมัติ (ผอ.กลุ่ม):</strong> <?php echo nl2br(htmlspecialchars(isset($requisition['head_remarks']) ? $requisition['head_remarks'] : '-')); ?></p>
<p class="text-danger"><strong>เหตุผลไม่อนุมัติ (พัสดุ):</strong> <?php echo nl2br(htmlspecialchars(isset($requisition['remarks']) ? $requisition['remarks'] : '-')); ?></p>
<?php endif; ?>
<?php if($requisition['status'] == 'approved'): ?>
<p class="fw-bold"><strong>มูลค่ารวมที่อนุมัติ:</strong> <?php echo number_format($requisition['total_value'], 2); ?> บาท</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="card mb-4">
<div class="card-header"><i class="bi bi-list-ul me-1"></i>รายการพัสดุที่ขอเบิก</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-sm table-bordered">
<thead class="table-light">
<tr>
<th>#</th>
<th>รหัส</th>
<th>ชื่อพัสดุ</th>
<th>หน่วยนับ</th>
<th class="text-end">จำนวนขอเบิก</th>
<th class="text-end">จำนวนอนุมัติ</th>
<?php if($requisition['status'] == 'approved'): ?>
<th class="text-end">ราคา/หน่วย (ที่ตัดจ่าย)</th>
<th class="text-end">มูลค่ารวม (บาท)</th>
<?php endif; ?>
<th>หมายเหตุ (รายการ)</th>
</tr>
</thead>
<tbody>
<?php if (!empty($details)): ?>
<?php $detail_counter = 1; ?>
<?php foreach ($details as $item): ?>
<tr>
<td><?php echo $detail_counter++; ?></td>
<td><?php echo htmlspecialchars($item['supply_code']); ?></td>
<td><?php echo htmlspecialchars($item['supply_name']); ?></td>
<td><?php echo htmlspecialchars($item['unit']); ?></td>
<td class="text-end"><?php echo number_format($item['requested_quantity']); ?></td>
<td class="text-end fw-bold <?php echo ($requisition['status'] != 'pending' && $item['approved_quantity'] < $item['requested_quantity']) ? 'text-warning' : ''; ?>">
<?php echo ($requisition['status'] == 'pending' || $requisition['status'] == 'head_rejected') ? '-' : number_format($item['approved_quantity']); ?>
</td>
<?php if($requisition['status'] == 'approved'): ?>
<td class="text-end"><?php echo ($item['approved_quantity'] > 0 && $item['total_value'] > 0) ? number_format($item['total_value'] / $item['approved_quantity'], 2) : '0.00'; ?></td>
<td class="text-end fw-bold"><?php echo number_format($item['total_value'], 2); ?></td>
<?php endif; ?>
<td><?php echo htmlspecialchars(isset($item['remarks']) ? $item['remarks'] : ''); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="<?php echo ($requisition['status'] == 'approved') ? '9' : '7'; ?>" class="text-center text-muted">ไม่พบรายการพัสดุ</td>
</tr>
<?php endif; ?>
</tbody>
<?php if($requisition['status'] == 'approved' && !empty($details)): ?>
<tfoot>
<tr>
<td colspan="7" class="text-end fw-bold">มูลค่ารวมทั้งสิ้น:</td>
<td class="text-end fw-bold fs-5"><?php echo number_format($requisition['total_value'], 2); ?></td>
<td></td>
</tr>
</tfoot>
<?php endif; ?>
</table>
</div>
</div>
</div>
<?php if ($requisition['status'] == 'approved' && !empty($transactions)): ?>
<div class="card mb-4">
<div class="card-header"><i class="bi bi-box-arrow-up me-1"></i>รายการตัดจ่ายจากสต็อก (ตาม FIFO)</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-sm table-bordered">
<thead class="table-light">
<tr>
<th>#</th>
<th>ชื่อพัสดุ</th>
<th>Batch No.</th>
<th class="text-end">จำนวนที่ตัด</th>
<th class="text-end">ราคา/หน่วย (บาท)</th>
<th class="text-end">มูลค่า (บาท)</th>
<th>วันที่ตัดจ่าย</th>
</tr>
</thead>
<tbody>
<?php $trans_counter = 1; ?>
<?php foreach ($transactions as $tran): ?>
<tr>
<td><?php echo $trans_counter++; ?></td>
<td><?php echo htmlspecialchars($tran['supply_name']); ?> (<?php echo htmlspecialchars($tran['supply_code']); ?>)</td>
<td><?php echo htmlspecialchars(isset($tran['batch_no']) ? $tran['batch_no'] : 'N/A'); ?></td>
<td class="text-end"><?php echo number_format($tran['quantity']); ?></td>
<td class="text-end"><?php echo number_format($tran['unit_price'], 2); ?></td>
<td class="text-end fw-bold"><?php echo number_format($tran['total_value'], 2); ?></td>
<td><?php echo formatThaiDate($tran['transaction_date']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<div class="mt-4">
<?php if ($user_type == 'head_of_department' && $requisition['status'] == 'pending'): ?>
<form action="action_head_ack.php" method="POST" class="d-inline">
<input type="hidden" name="requisition_id" value="<?php echo $req_id; ?>">
<button type="submit" class="btn btn-success"><i class="bi bi-check-lg"></i> รับทราบคำขอ</button>
</form>
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#rejectHeadModal">
<i class="bi bi-x-lg"></i> ไม่อนุมัติ / ตีกลับ
</button>
<?php endif; ?>
<?php if ($user_type == 'supply_manager' && $requisition['status'] == 'head_approved'): ?>
<a href="approve_form.php?id=<?php echo $req_id; ?>" class="btn btn-success"><i class="bi bi-pencil-square"></i> ดำเนินการอนุมัติ/จ่ายพัสดุ</a>
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#rejectSupplyModal">
<i class="bi bi-x-lg"></i> ไม่อนุมัติคำขอ
</button>
<?php endif; ?>
<?php
$back_link = '../index.php'; // Default back link
if ($user_type == 'department') $back_link = 'list_my.php';
if ($user_type == 'head_of_department') $back_link = ($requisition['status'] == 'pending') ? 'list_pending_head.php' : 'history_dept.php';
if ($user_type == 'supply_manager') $back_link = ($requisition['status'] == 'head_approved') ? 'list_pending_supply.php' : 'history_all.php';
?>
<a href="<?php echo $back_link; ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> กลับ</a>
</div>
<div class="modal fade" id="rejectHeadModal" tabindex="-1" aria-labelledby="rejectHeadModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="rejectHeadModalLabel">ไม่อนุมัติ / ตีกลับคำขอ</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="action_head_reject.php" method="POST">
<div class="modal-body">
<input type="hidden" name="requisition_id" value="<?php echo $req_id; ?>">
<div class="mb-3">
<label for="head_remarks" class="form-label">เหตุผล (ถ้ามี):</label>
<textarea class="form-control" id="head_remarks" name="head_remarks" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-danger">ยืนยันไม่อนุมัติ</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="rejectSupplyModal" tabindex="-1" aria-labelledby="rejectSupplyModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="rejectSupplyModalLabel">ไม่อนุมัติคำขอเบิก</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="action_supply_reject.php" method="POST">
<div class="modal-body">
<input type="hidden" name="requisition_id" value="<?php echo $req_id; ?>">
<div class="mb-3">
<label for="supply_remarks" class="form-label">เหตุผลที่ไม่อนุมัติ:</label>
<textarea class="form-control" id="supply_remarks" name="supply_remarks" rows="3" required></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-danger">ยืนยันไม่อนุมัติ</button>
</div>
</form>
</div>
</div>
</div>
<?php endif; // End check for $requisition ?>
</div>
<?php
// --- Include Footer ---
include_once __DIR__ . '/../includes/footer.php';
// --- Close connection *after* all HTML is rendered ---
if (isset($conn) && $conn) {
mysqli_close($conn);
}
?>