| Server IP : 172.67.187.206 / Your IP : 172.71.28.155 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';
include_once __DIR__ . '/../includes/auth_check.php';
// requireRole(['admin', 'supply_manager']); // Ensure only authorized users can access
$req_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$requisition = null;
$details = [];
$error_message = '';
$user_id = getUserData('user_id');
if ($req_id > 0) {
// --- Fetch Requisition Header ---
$sql_req = "SELECT r.*, d.dept_name, u_req.full_name as requester_name,
u_head.full_name as head_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
WHERE r.id = $req_id AND r.status = 'head_approved'"; // Must be in 'head_approved' status
$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);
// --- Fetch Requisition Details with Current Stock AND Image ---
$sql_details = "SELECT rd.*, s.supply_code, s.supply_name, s.unit, s.quantity_in_stock, s.image_filename
FROM requisition_details rd
JOIN supplies s ON rd.supply_id = s.id
WHERE rd.requisition_id = $req_id
ORDER BY s.supply_code ASC"; // Make sure image_filename is selected
$result_details = mysqli_query($conn, $sql_details);
if ($result_details) {
while ($row = mysqli_fetch_assoc($result_details)) {
$details[] = $row;
}
mysqli_free_result($result_details);
} else {
$error_message .= "เกิดข้อผิดพลาดในการดึงรายการพัสดุ: " . mysqli_error($conn) . "<br>";
}
} else {
// Check if the requisition exists but has a different status
$sql_check_status = "SELECT status FROM requisitions WHERE id = $req_id";
$res_check = mysqli_query($conn, $sql_check_status);
if ($res_check && mysqli_num_rows($res_check) > 0) {
$current_status = mysqli_fetch_assoc($res_check)['status'];
$error_message = "ไม่สามารถดำเนินการได้ สถานะปัจจุบันของคำขอคือ '" . $current_status . "' ไม่ใช่ 'รอฝ่ายพัสดุอนุมัติ'";
mysqli_free_result($res_check);
} else {
$error_message = "ไม่พบข้อมูลคำขอเบิก ID: $req_id หรือสถานะไม่ถูกต้อง";
}
}
} else {
redirect('list_pending_supply.php'); // Redirect if no ID provided
}
$page_title = "ดำเนินการอนุมัติคำขอเบิก " . ($requisition ? htmlspecialchars($requisition['requisition_no']) : '');
// --- Include Header and Sidebar ---
include_once __DIR__ . '/../includes/header.php';
?>
<style>
/* Thumbnail Styling */
.approve-img-thumbnail {
max-width: 45px;
max-height: 45px;
object-fit: cover;
cursor: pointer;
}
.image-placeholder-approve {
font-size: 1.8rem;
color: #adb5bd;
cursor: default;
}
/* Modal Image Styling */
.modal-body img {
max-width: 100%;
max-height: 70vh;
display: block;
margin: 0 auto;
}
</style>
<?php
include_once __DIR__ . '/../includes/sidebar_supply.php';
// mysqli_close($conn); // Close connection later
?>
<div class="container-fluid">
<h1 class="mt-4"><?php echo $page_title; ?></h1>
<?php if ($error_message): ?>
<div class="alert alert-danger" role="alert">
<?php echo $error_message; ?>
</div>
<a href="list_pending_supply.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> กลับไปรายการรออนุมัติ</a>
<?php elseif ($requisition): ?>
<div class="card mb-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 class="badge bg-info text-dark">รอฝ่ายพัสดุอนุมัติ</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 htmlspecialchars(isset($requisition['head_approver_name']) ? $requisition['head_approver_name'] : '-'); ?> (<?php echo formatThaiDate($requisition['head_approved_date']); ?>)</p>
<p><strong>หมายเหตุ (ผู้ขอ):</strong> <?php echo nl2br(htmlspecialchars(isset($requisition['remarks']) ? $requisition['remarks'] : '-')); ?></p>
</div>
</div>
</div>
</div>
<form action="action_supply_approve.php" method="POST" id="approveForm">
<input type="hidden" name="requisition_id" value="<?php echo $req_id; ?>">
<div class="card mb-4">
<div class="card-header"><i class="bi bi-pencil-square me-1"></i>ระบุจำนวนที่อนุมัติจ่าย</div>
<div class="card-body">
<p class="text-muted"><small>ระบบจะตัดสต็อกตามจำนวนที่อนุมัติ โดยใช้หลักการ FIFO (เข้าก่อน-ออกก่อน)</small></p>
<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>หน่วยนับ</th>
<th class="text-end">จำนวนขอเบิก</th>
<th class="text-end">คงคลังปัจจุบัน</th>
<th class="text-center" style="width: 15%;">จำนวนอนุมัติจ่าย</th>
<th>หมายเหตุ (รายการ)</th>
</tr>
</thead>
<tbody>
<?php if (!empty($details)): ?>
<?php $detail_counter = 1; ?>
<?php foreach ($details as $item):
$max_approve = min($item['requested_quantity'], $item['quantity_in_stock']);
$default_approve = $max_approve;
$image_url = BASE_URL . '/uploads/supplies/' . $item['image_filename'];
$image_path = __DIR__ . '/../uploads/supplies/' . $item['image_filename'];
$image_exists = !empty($item['image_filename']) && file_exists($image_path);
?>
<tr>
<td><?php echo $detail_counter++; ?></td>
<td class="text-center align-middle">
<?php if ($image_exists): ?>
<img src="<?php echo $image_url; ?>"
alt="<?php echo htmlspecialchars($item['supply_name']); ?>"
class="img-thumbnail approve-img-thumbnail"
data-bs-toggle="modal"
data-bs-target="#imageModalApprove"
data-image-url="<?php echo $image_url; ?>"
data-image-title="<?php echo htmlspecialchars($item['supply_name'] . ' (' . $item['supply_code'] . ')'); ?>">
<?php else: ?>
<i class="bi bi-image image-placeholder-approve"></i>
<?php endif; ?>
</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 <?php echo ($item['quantity_in_stock'] < $item['requested_quantity']) ? 'text-danger fw-bold' : ''; ?>">
<?php echo number_format($item['quantity_in_stock']); ?>
<?php if ($item['quantity_in_stock'] < $item['requested_quantity']) echo ' <i class="bi bi-exclamation-triangle-fill text-warning" title="สต็อกไม่พอจ่ายตามจำนวนที่ขอ"></i>'; ?>
</td>
<td class="text-center">
<input type="number"
class="form-control form-control-sm text-end approved-qty"
name="approved_quantity[<?php echo $item['id']; ?>]"
value="<?php echo $default_approve; ?>"
min="0"
max="<?php echo $max_approve; ?>"
step="1" required
data-detail-id="<?php echo $item['id']; ?>"
>
<input type="hidden" name="requested_quantity[<?php echo $item['id']; ?>]" value="<?php echo $item['requested_quantity']; ?>">
</td>
<td>
<input type="text" class="form-control form-control-sm" name="detail_remarks[<?php echo $item['id']; ?>]" placeholder="หมายเหตุ...">
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="9" class="text-center text-muted">ไม่พบรายการพัสดุในคำขอนี้</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="mb-3">
<label for="supply_remarks" class="form-label">หมายเหตุเพิ่มเติม (ถ้ามี):</label>
<textarea class="form-control" id="supply_remarks" name="supply_remarks" rows="2"></textarea>
</div>
</div> <div class="card-footer d-flex justify-content-between"> <div>
<a href="list_pending_supply.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> ยกเลิก</a>
</div>
<div>
<button type="button" class="btn btn-danger me-2" data-bs-toggle="modal" data-bs-target="#rejectSupplyModal">
<i class="bi bi-x-lg"></i> ไม่อนุมัติคำขอ
</button>
<button type="submit" class="btn btn-success"><i class="bi bi-check-circle-fill"></i> ยืนยันการอนุมัติและตัดจ่าย</button>
</div>
</div>
</div> </form>
<?php endif; // End check for $requisition ?>
</div>
<div class="modal fade" id="imageModalApprove" tabindex="-1" aria-labelledby="imageModalApproveLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalApproveLabel">รูปภาพพัสดุ</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center">
<img src="" id="modalApproveImage" alt="Supply Image">
</div>
</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; // Ensure req_id is available ?>">
<div class="mb-3">
<label for="supply_remarks_reject" class="form-label">เหตุผลที่ไม่อนุมัติ:</label>
<textarea class="form-control" id="supply_remarks_reject" 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
// --- Include Footer ---
include_once __DIR__ . '/../includes/footer.php';
// Close connection here if it wasn't closed before
if (isset($conn) && $conn) {
mysqli_close($conn);
}
?>
<script>
$(document).ready(function() {
// --- Image Modal Handler (Approve Form) ---
var imageModalApprove = document.getElementById('imageModalApprove');
if (imageModalApprove) {
imageModalApprove.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var imageUrl = button.getAttribute('data-image-url');
var imageTitle = button.getAttribute('data-image-title');
var modalTitle = imageModalApprove.querySelector('.modal-title');
var modalImage = imageModalApprove.querySelector('#modalApproveImage');
modalTitle.textContent = imageTitle;
modalImage.src = imageUrl;
modalImage.alt = imageTitle;
});
}
// Optional: Add JavaScript validation if needed (remains the same)
// $('#approveForm').on('submit', function(e) { ... });
});
</script>