Mini Shell
<!doctype html>
<html lang="en">
<!-- นำเข้า Select2 CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<!-- นำเข้า SweetAlert CSS (เวอร์ชัน 1) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<style>
/* บังคับให้ Select2 เป็น Block เต็มความกว้าง */
.select2-container { display: block !important; width: 100% !important; }
.select2-container .select2-selection--single {
height: 36px !important;
border: none !important;
border-bottom: 1px solid #D2D2D2 !important;
border-radius: 0 !important;
padding-left: 0 !important;
background-color: transparent !important;
box-shadow: none !important;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 36px !important; padding-left: 0 !important; color: #555555; font-size: 14px;
}
.select2-container--default .select2-selection--single .select2-selection__arrow { height: 36px !important; }
.select2-container--default.select2-container--open .select2-selection--single,
.select2-container--default.select2-container--focus .select2-selection--single {
border-bottom: 2px solid #9c27b0 !important;
}
.select2-dropdown {
border: 1px solid #ccc !important; border-radius: 4px !important; box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
background-color: #9c27b0 !important; color: white !important;
}
/* ปรับ Modal Z-index ให้แสดงผลทับ Select2 ได้ถูกต้อง */
.modal { z-index: 1050; }
.select2-container--open { z-index: 9999999 !important; }
</style>
<?php include 'head.php'; ?>
<body>
<div class="wrapper">
<?php include 'leftside.php'; ?>
<div class="main-panel">
<?php include 'header.php'; ?>
<?php include 'db/database.php'; ?>
<div class="content">
<div class="container-fluid">
<?php
// =========================================================================
// ส่วนจัดการคำสั่ง (Add, Edit, Delete) ก่อนที่จะคิวรีข้อมูลมาแสดงตาราง
// =========================================================================
$search_sahakornID = isset($_POST['search_sahakornID']) ? $_POST['search_sahakornID'] : '';
$search_year = isset($_POST['search_year']) ? $_POST['search_year'] : '';
$alert_msg = "";
$alert_type = ""; // success, error, warning
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
$sahakornID = $_POST['search_sahakornID'];
$year = $_POST['search_year'];
$formulaID = isset($_POST['formulaID']) ? $_POST['formulaID'] : '';
$sack = isset($_POST['sack']) ? $_POST['sack'] : 0;
// ฟังก์ชันช่วยในการคำนวณและ Insert (ใช้ซ้ำใน Add และ Edit)
function insertProcessData($link, $sahakornID, $year, $formulaID, $sack) {
$stmt_insert = mysqli_prepare($link, "INSERT INTO sahakorn_process (sahakornID, year, formulaID, sack, S_mother_pui, sum) VALUES (?, ?, ?, ?, ?, ?)");
// แม่ปุ๋ย
$stmt_m = mysqli_prepare($link, "SELECT mn.mother_name, mf.mother_quantity FROM mother_fertilizer mf JOIN mother_name mn ON mf.mother_fertilizer_name_id = mn.mother_name_id WHERE mf.formulaID = ?");
mysqli_stmt_bind_param($stmt_m, "i", $formulaID); mysqli_stmt_execute($stmt_m); $res_m = mysqli_stmt_get_result($stmt_m);
while ($row = mysqli_fetch_assoc($res_m)) {
$kg_sum = ($row['mother_quantity'] / 100) * $sack * 50;
mysqli_stmt_bind_param($stmt_insert, "iiidsd", $sahakornID, $year, $formulaID, $sack, $row['mother_name'], $kg_sum);
mysqli_stmt_execute($stmt_insert);
}
mysqli_stmt_close($stmt_m);
// สารเติมแต่ง
$stmt_a = mysqli_prepare($link, "SELECT a.additives_name, af.additives_quantity FROM additives_fertilizer af JOIN additives a ON af.additives_fertilizer_name_id = a.additives_id WHERE af.formulaID = ?");
mysqli_stmt_bind_param($stmt_a, "i", $formulaID); mysqli_stmt_execute($stmt_a); $res_a = mysqli_stmt_get_result($stmt_a);
while ($row = mysqli_fetch_assoc($res_a)) {
$kg_sum = ($row['additives_quantity'] / 100) * $sack * 50;
mysqli_stmt_bind_param($stmt_insert, "iiidsd", $sahakornID, $year, $formulaID, $sack, $row['additives_name'], $kg_sum);
mysqli_stmt_execute($stmt_insert);
}
mysqli_stmt_close($stmt_a); mysqli_stmt_close($stmt_insert);
}
if ($action == 'add') {
// เช็คข้อมูลซ้ำ ถ้าซ้ำให้ลบของเก่าทิ้ง (ถือเป็นการอัปเดต)
mysqli_query($link, "DELETE FROM sahakorn_process WHERE sahakornID='$sahakornID' AND year='$year' AND formulaID='$formulaID'");
insertProcessData($link, $sahakornID, $year, $formulaID, $sack);
$alert_msg = "เพิ่มข้อมูลการผสมปุ๋ยสำเร็จ";
$alert_type = "success";
} elseif ($action == 'edit') {
$old_formulaID = $_POST['old_formulaID'];
// ลบข้อมูลสูตรเก่าออกก่อน
mysqli_query($link, "DELETE FROM sahakorn_process WHERE sahakornID='$sahakornID' AND year='$year' AND formulaID='$old_formulaID'");
// คำนวณและบันทึกสูตรใหม่
insertProcessData($link, $sahakornID, $year, $formulaID, $sack);
$alert_msg = "อัปเดตข้อมูลการผสมปุ๋ยสำเร็จ";
$alert_type = "success";
} elseif ($action == 'delete') {
$del_formulaID = $_POST['del_formulaID'];
// 1. ลบตารางหลัก
mysqli_query($link, "DELETE FROM sahakorn_process WHERE sahakornID='$sahakornID' AND year='$year' AND formulaID='$del_formulaID'");
// 2. ลบตารางที่เชื่อมโยงตามเงื่อนไข
@mysqli_query($link, "DELETE FROM calculate_fertilizer WHERE sahakornID='$sahakornID' AND year='$year' AND formulaID='$del_formulaID'");
@mysqli_query($link, "DELETE FROM calculate_remaining WHERE sahakornID='$sahakornID' AND year='$year' AND formulaID='$del_formulaID'");
$alert_msg = "ลบข้อมูลเรียบร้อยแล้ว";
$alert_type = "success";
}
}
?>
<!-- ส่วนฟอร์มค้นหา -->
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title" style="color: white;">กรุณาเลือกสหกรณ์และปีบัญชี</h4>
</div>
<div class="card-content">
<form method="POST" action="">
<input type="hidden" name="action" value="search">
<div class="row">
<div class="col-md-5">
<div class="form-group label-floating">
<label class="control-label">กรุณาเลือกสหกรณ์</label>
<select name="search_sahakornID" id="searchSahakorn" class="form-control" required>
<option value=""></option>
<?php
$sql_sahakorn = "SELECT sahakornID, name FROM sahakorn ORDER BY sahakornID ASC";
$result_sahakorn = mysqli_query($link, $sql_sahakorn);
while($row_sahakorn = mysqli_fetch_assoc($result_sahakorn)) {
$sel = ($row_sahakorn['sahakornID'] == $search_sahakornID) ? "selected" : "";
echo "<option value='".$row_sahakorn['sahakornID']."' $sel>".$row_sahakorn['name']."</option>";
}
?>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">กรุณาเลือกปีบัญชี(ปีพ.ศ.)</label>
<select name="search_year" id="searchYear" class="form-control" required>
<option value=""></option>
<?php
$current_year_be = (int)date('Y') + 543;
for ($y = $current_year_be; $y >= $current_year_be - 4; $y--) {
$sel = ($y == $search_year) ? "selected" : "";
echo "<option value='".$y."' $sel>".$y."</option>";
}
?>
</select>
</div>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary" style="margin-top: 25px;">
<i class="material-icons">search</i> ค้นหาข้อมูล
</button>
</div>
</div>
<div class="clearfix"></div>
</form>
</div>
</div>
<!-- แสดงตารางประวัติ ก็ต่อเมื่อมีการค้นหา หรือ มีการทำรายการ (Add/Edit/Delete) -->
<?php
if ($search_sahakornID != '' && $search_year != '') {
// ดึงชื่อสหกรณ์
$stmt_sh_name = mysqli_prepare($link, "SELECT name FROM sahakorn WHERE sahakornID = ?");
mysqli_stmt_bind_param($stmt_sh_name, "i", $search_sahakornID); mysqli_stmt_execute($stmt_sh_name);
$res_sh = mysqli_stmt_get_result($stmt_sh_name); $row_sh = mysqli_fetch_assoc($res_sh);
$sh_name = $row_sh['name']; mysqli_stmt_close($stmt_sh_name);
// หาวัตถุดิบทั้งหมดที่ใช้ในปีนี้
$sql_all_ingredients = "SELECT DISTINCT S_mother_pui FROM sahakorn_process WHERE sahakornID = ? AND year = ? ORDER BY S_mother_pui ASC";
$stmt_all_ing = mysqli_prepare($link, $sql_all_ingredients);
mysqli_stmt_bind_param($stmt_all_ing, "ii", $search_sahakornID, $search_year); mysqli_stmt_execute($stmt_all_ing);
$res_all_ing = mysqli_stmt_get_result($stmt_all_ing);
$all_ingredients = array();
$sum_ingredients_cols = array();
while($row_ing = mysqli_fetch_assoc($res_all_ing)){
$all_ingredients[] = $row_ing['S_mother_pui'];
$sum_ingredients_cols[$row_ing['S_mother_pui']] = 0;
}
mysqli_stmt_close($stmt_all_ing);
// ดึงข้อมูลประวัติ
$sql_history = "SELECT sp.formulaID, f.name_formula, sp.sack, sp.S_mother_pui, sp.sum
FROM sahakorn_process sp
LEFT JOIN formula f ON sp.formulaID = f.formulaID
WHERE sp.sahakornID = ? AND sp.year = ?
ORDER BY sp.formulaID ASC";
$stmt_hist = mysqli_prepare($link, $sql_history);
mysqli_stmt_bind_param($stmt_hist, "ii", $search_sahakornID, $search_year); mysqli_stmt_execute($stmt_hist);
$res_hist = mysqli_stmt_get_result($stmt_hist);
$history_data = array();
while($row_h = mysqli_fetch_assoc($res_hist)){
$fid = $row_h['formulaID'];
if(!isset($history_data[$fid])) {
$history_data[$fid] = array(
'formulaID' => $fid,
'formula_name' => $row_h['name_formula'],
'sack' => $row_h['sack'],
'total_sum' => 0,
'ingredients' => array()
);
}
$history_data[$fid]['ingredients'][$row_h['S_mother_pui']] = $row_h['sum'];
$history_data[$fid]['total_sum'] += $row_h['sum'];
}
mysqli_stmt_close($stmt_hist);
// ดึงตัวเลือกสูตรปุ๋ยมาเก็บไว้ในตัวแปรสำหรับใช้ใน Modal
$formula_options = "";
$res_f = mysqli_query($link, "SELECT formulaID, name_formula FROM formula");
while($rf = mysqli_fetch_assoc($res_f)){
$formula_options .= "<option value='".$rf['formulaID']."'>".$rf['name_formula']."</option>";
}
?>
<!-- ตารางประวัติการบันทึกข้อมูล -->
<div class="card">
<div class="card-header" data-background-color="blue">
<h4 class="title" style="color: white;">ประวัติการบันทึกข้อมูลของ: <?php echo htmlspecialchars($sh_name); ?></h4>
<p class="category">แสดงรายละเอียดการผสมปุ๋ยสำหรับปีบัญชี <?php echo $search_year; ?></p>
</div>
<div class="card-content table-responsive">
<!-- ปุ่มเพิ่มข้อมูล -->
<div style="margin-bottom: 15px;">
<button class="btn btn-success" data-toggle="modal" data-target="#addModal">
<i class="material-icons">add_box</i> เพิ่มข้อมูลปุ๋ยผสม
</button>
</div>
<table class="table table-bordered table-hover">
<thead class="text-info">
<tr>
<th class="text-center" width="5%">ลำดับ</th>
<th class="text-center">สูตรปุ๋ย</th>
<th class="text-right">ที่ผสม (กระสอบ)</th>
<?php foreach($all_ingredients as $ing) { ?>
<th class="text-right"><?php echo htmlspecialchars($ing); ?></th>
<?php } ?>
<th class="text-right" style="font-weight:bold;">รวม (ก.ก.)</th>
<!-- ย้ายเครื่องมือมาไว้ด้านขวา -->
<th class="text-center" width="10%">เครื่องมือ</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$grand_total_sack = 0;
$grand_total_sum = 0;
if (count($history_data) > 0) {
foreach($history_data as $fid => $data) {
$grand_total_sack += $data['sack'];
$grand_total_sum += $data['total_sum'];
?>
<tr>
<td class="text-center"><?php echo $i++; ?></td>
<td class="text-center"><?php echo htmlspecialchars($data['formula_name']); ?></td>
<td class="text-right"><?php echo number_format($data['sack'], 2); ?></td>
<?php
foreach($all_ingredients as $ing) {
$val = isset($data['ingredients'][$ing]) ? $data['ingredients'][$ing] : 0;
$sum_ingredients_cols[$ing] += $val;
?>
<td class="text-right"><?php echo ($val > 0) ? number_format($val, 2) : '-'; ?></td>
<?php } ?>
<td class="text-right" style="color:black; font-weight:bold;"><?php echo number_format($data['total_sum'], 2); ?></td>
<!-- ย้ายเครื่องมือมาไว้ด้านขวา -->
<td class="text-center">
<!-- ปุ่มแก้ไข -->
<button type="button" class="btn btn-warning btn-simple btn-xs" title="แก้ไข"
onclick="openEditModal('<?php echo $fid; ?>', '<?php echo $data['sack']; ?>')">
<i class="material-icons">edit</i>
</button>
<!-- ฟอร์มสำหรับปุ่มลบ (class ผูกกับ SweetAlert 1) -->
<form method="POST" action="" style="display:inline;" class="delete-form">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="search_sahakornID" value="<?php echo $search_sahakornID; ?>">
<input type="hidden" name="search_year" value="<?php echo $search_year; ?>">
<input type="hidden" name="del_formulaID" value="<?php echo $fid; ?>">
<button type="button" class="btn btn-danger btn-simple btn-xs btn-delete" title="ลบ">
<i class="material-icons">close</i>
</button>
</form>
</td>
</tr>
<?php
}
?>
<tr style="background-color: #fafafa;">
<td colspan="2" class="text-center" style="font-weight: bold; color: black;">รวมทั้งหมด</td>
<td class="text-right" style="font-weight: bold; color: black;"><?php echo number_format($grand_total_sack, 2); ?></td>
<?php foreach($all_ingredients as $ing) { ?>
<td class="text-right" style="font-weight: bold; color: black;"><?php echo number_format($sum_ingredients_cols[$ing], 2); ?></td>
<?php } ?>
<td class="text-right" style="font-weight: bold; color: black;"><?php echo number_format($grand_total_sum, 2); ?></td>
<td></td> <!-- ช่องว่างสำหรับใต้คอลัมน์เครื่องมือ -->
</tr>
<?php
} else {
$colspan = 5 + count($all_ingredients);
echo "<tr><td colspan='".$colspan."' class='text-center text-danger'>--- ไม่พบข้อมูลการแปรรูปปุ๋ยในปีบัญชีนี้ ---</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
<!-- ============================================== -->
<!-- Modal: เพิ่มข้อมูล -->
<!-- ============================================== -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="addModalLabel">เพิ่มข้อมูลการผสมปุ๋ย</h4>
</div>
<form method="POST" action="">
<div class="modal-body">
<!-- ส่งค่าสหกรณ์และปีกลับไปเงียบๆ เพื่อให้ระบบจำสถานะค้นหาได้ -->
<input type="hidden" name="action" value="add">
<input type="hidden" name="search_sahakornID" value="<?php echo $search_sahakornID; ?>">
<input type="hidden" name="search_year" value="<?php echo $search_year; ?>">
<div class="form-group label-floating is-empty">
<label class="control-label">กรุณาเลือกสูตรปุ๋ยผสม</label>
<select name="formulaID" id="addFormulaSelect" class="form-control" required style="width: 100%;">
<option value=""></option>
<?php echo $formula_options; ?>
</select>
</div>
<div class="form-group label-floating">
<label class="control-label">จำนวนปุ๋ยที่ผสม (กระสอบ)</label>
<input type="number" name="sack" class="form-control" step="0.01" min="0.01" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-success">บันทึกข้อมูล</button>
</div>
</form>
</div>
</div>
</div>
<!-- ============================================== -->
<!-- Modal: แก้ไขข้อมูล -->
<!-- ============================================== -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="editModalLabel">แก้ไขข้อมูลการผสมปุ๋ย</h4>
</div>
<form method="POST" action="">
<div class="modal-body">
<!-- ข้อมูลที่จำเป็นสำหรับการแก้ไข/อัปเดต -->
<input type="hidden" name="action" value="edit">
<input type="hidden" name="search_sahakornID" value="<?php echo $search_sahakornID; ?>">
<input type="hidden" name="search_year" value="<?php echo $search_year; ?>">
<input type="hidden" name="old_formulaID" id="editOldFormulaID" value="">
<div class="form-group label-floating is-focused">
<label class="control-label">สูตรปุ๋ยผสม (เลือกใหม่ได้)</label>
<select name="formulaID" id="editFormulaSelect" class="form-control" required style="width: 100%;">
<option value=""></option>
<?php echo $formula_options; ?>
</select>
</div>
<div class="form-group label-floating is-focused">
<label class="control-label">จำนวนปุ๋ยที่ผสม (กระสอบ)</label>
<input type="number" name="sack" id="editSackInput" class="form-control" step="0.01" min="0.01" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-warning">บันทึกการแก้ไข</button>
</div>
</form>
</div>
</div>
</div>
<?php
} // สิ้นสุดบล็อกแสดงตารางประวัติ
?>
</div>
</div>
<?php include 'footer1.php'; ?>
</div>
</div>
<?php include 'footer2.php'; ?>
<!-- นำเข้า JS ของ Select2 และ SweetAlert 1 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script>
$(document).ready(function() {
// ==========================================
// จัดการ Select2
// ==========================================
function initFloatingSelect2(selector, dropdownParentModal) {
var options = {
allowClear: true,
placeholder: "" // ทำให้เป็นค่าว่างเปล่า จะได้ไม่ซ้อนทับกับ Label-floating
};
if(dropdownParentModal) {
options.dropdownParent = $(dropdownParentModal);
}
var $select = $(selector).select2(options);
$select.on('select2:open', function () {
$(this).closest('.form-group').removeClass('is-empty').addClass('is-focused');
}).on('select2:close', function () {
$(this).closest('.form-group').removeClass('is-focused');
if (!$(this).val()) {
$(this).closest('.form-group').addClass('is-empty');
}
}).on('change', function () {
if ($(this).val()) {
$(this).closest('.form-group').removeClass('is-empty');
} else {
$(this).closest('.form-group').addClass('is-empty');
}
});
// ตรวจสอบค่าเริ่มต้น
$select.each(function() {
if ($(this).val()) {
$(this).closest('.form-group').removeClass('is-empty');
}
});
}
// เรียกใช้งาน Select2
initFloatingSelect2('#searchSahakorn, #searchYear', null);
initFloatingSelect2('#addFormulaSelect', '#addModal');
initFloatingSelect2('#editFormulaSelect', '#editModal');
// ==========================================
// จัดการปุ่มลบข้อมูล ด้วย SweetAlert (เวอร์ชัน 1)
// ==========================================
$('.btn-delete').on('click', function(e) {
e.preventDefault();
var form = $(this).closest('form');
swal({
title: "ยืนยันการลบข้อมูล?",
text: "หากลบแล้วจะไม่สามารถกู้คืนได้ และข้อมูลที่เชื่อมโยงจะถูกลบด้วย",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#f44336", // สีแดง
confirmButtonText: "ใช่, ลบเลย!",
cancelButtonText: "ยกเลิก",
closeOnConfirm: false
}, function(isConfirm){
if (isConfirm) {
form.submit();
}
});
});
});
// ฟังก์ชันเปิด Modal แก้ไข
function openEditModal(formulaID, sack) {
$('#editOldFormulaID').val(formulaID);
$('#editFormulaSelect').val(formulaID).trigger('change');
$('#editSackInput').val(sack);
$('#editSackInput').closest('.form-group').removeClass('is-empty').addClass('is-focused');
$('#editModal').modal('show');
}
</script>
<?php
// ==========================================
// การแสดงผลแจ้งเตือน Alert เมื่อบันทึก/แก้ไข/ลบสำเร็จด้วย SweetAlert 1
// ==========================================
if ($alert_msg != "") {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
swal({
title: "สำเร็จ",
text: "<?php echo $alert_msg; ?>",
type: "<?php echo $alert_type; ?>",
timer: 2500,
showConfirmButton: false
});
});
</script>
<?php } ?>
</body>
</html>
Zerion Mini Shell 1.0