Mini Shell

Direktori : /var/www/vhosts/ccp.ac.th/backup_full/httpdocs/fertilizer/
Upload File :
Current File : /var/www/vhosts/ccp.ac.th/backup_full/httpdocs/fertilizer/report1.php

<!doctype html>
<html lang="en">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
    
    <style>
        /* บังคับให้ Select2 เป็น Block เต็มความกว้าง เพื่อรักษาระยะให้ตรงกับฟอร์มอื่นๆ */
        .select2-container {
            display: block !important;
            width: 100% !important;
        }
        /* ปรับแต่ง Select2 ให้ไม่มีกรอบและมีแค่เส้นใต้เหมือน Input ของ Material */
        .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; 
        }
        /* ปรับแต่งหน้าต่าง Dropdown ที่เด้งลงมาให้สวยงามเข้ากับธีม */
        .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;
        }
    </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">            

                        <div class="card">
                            <div class="card-header" data-background-color="purple">
                                <h4 class="title" style="color: white;">ออกรายงานการแปรรูปปุ๋ยผสม</h4>
                            </div>
                            <div class="card-content">
                                <?php
                                // เก็บค่าที่เคยเลือกไว้ เพื่อนำมาแสดงซ้ำตอนโหลดหน้า (Sticky values)
                                $selected_sahakorn = isset($_POST['search_sahakornID']) ? $_POST['search_sahakornID'] : '';
                                $selected_year = isset($_POST['search_year']) ? $_POST['search_year'] : '';
                                ?>
                                <form method="POST" action="">
                                    <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'] == $selected_sahakorn) ? "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
                                                    // สร้างตัวเลือกปีปัจจุบันย้อนหลังไป 4 ปี
                                                    $current_year_be = (int)date('Y') + 543; 
                                                    for ($y = $current_year_be; $y >= $current_year_be - 4; $y--) {
                                                        // เช็คว่าตรงกับที่เคยเลือกไหม
                                                        $sel = ($y == $selected_year) ? "selected" : "";
                                                        echo "<option value='".$y."' $sel>".$y."</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <button type="submit" name="btn_search" class="btn btn-primary" style="margin-top: 25px;">
                                                <i class="material-icons">search</i> ดูรายงาน
                                            </button>
                                        </div>
                                    </div>
                                    <div class="clearfix"></div>
                                </form>
                            </div>
                        </div>

                        <?php
                        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_search'])) {
                            $sahakornID = $_POST['search_sahakornID'];
                            $year = $_POST['search_year'];

                            // 1. ดึงชื่อสหกรณ์
                            $stmt_sh_name = mysqli_prepare($link, "SELECT name FROM sahakorn WHERE sahakornID = ?");
                            mysqli_stmt_bind_param($stmt_sh_name, "i", $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);

                            // 2. ฟังก์ชันแปลงวันที่ปัจจุบันเป็นภาษาไทย
                            $thai_months = array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
                            $current_d = date('j');
                            $current_m = $thai_months[(int)date('n')];
                            $current_y = (int)date('Y') + 543;
                            $print_date = "$current_d $current_m $current_y";

                            // 3. หาชื่อวัตถุดิบ(แม่ปุ๋ย/สารเติมแต่ง) ทั้งหมดที่ใช้ใน "สหกรณ์นี้" และ "ปีนี้" เพื่อทำหัวตาราง
                            $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", $sahakornID, $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);

                            // 4. ดึงข้อมูลประวัติการผสม เพื่อนำมาจัดกลุ่มตามสูตร
                            $sql_data = "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_data = mysqli_prepare($link, $sql_data);
                            mysqli_stmt_bind_param($stmt_data, "ii", $sahakornID, $year);
                            mysqli_stmt_execute($stmt_data);
                            $res_data = mysqli_stmt_get_result($stmt_data);
                            
                            $report_data = array();
                            while($row_d = mysqli_fetch_assoc($res_data)){
                                $fname = $row_d['name_formula'];
                                if(!isset($report_data[$fname])) {
                                    $report_data[$fname] = array(
                                        'sack' => $row_d['sack'],
                                        'total_sum' => 0,
                                        'ingredients' => array()
                                    );
                                }
                                $report_data[$fname]['ingredients'][$row_d['S_mother_pui']] = $row_d['sum'];
                                $report_data[$fname]['total_sum'] += $row_d['sum'];
                            }
                            mysqli_stmt_close($stmt_data);

                        ?>
                            <div class="card">
                                <div class="card-content">
                                    
                                    <div style="position: relative; text-align: center; margin-bottom: 30px;">
                                        <h3 style="font-weight: bold; margin-bottom: 5px;">รายงานคำนวนแปรรูปปุ๋ยผสม</h3>
                                        <h4 style="margin-top: 5px; margin-bottom: 5px;">ชื่อสหกรณ์: <?php echo htmlspecialchars($sh_name); ?></h4>
                                        <h4 style="margin-top: 5px; margin-bottom: 5px;">ปีบัญชี: <?php echo $year; ?></h4>
                                        <p style="margin-top: 10px; color: #777;">วันที่ออกรายงาน: <?php echo $print_date; ?></p>

                                        <a href="print_pdf.php?sahakornID=<?php echo $sahakornID; ?>&year=<?php echo $year; ?>" target="_blank" class="btn btn-info" style="position: absolute; right: 0; top: 0;">
                                            <i class="material-icons">print</i> พิมพ์รายงาน (PDF)
                                        </a>
                                    </div>

                                    <div class="table-responsive">
                                        <table class="table table-bordered table-hover">
                                            <thead style="background-color: #f2f2f2; color: #333;">
                                                <tr>
                                                    <th class="text-center" width="5%">ลำดับ</th>
                                                    <th class="text-center" width="20%">สูตรปุ๋ยที่ผสม</th>
                                                    <th class="text-right" width="15%">จำนวนที่ผสม<br><small>(เป็นกระสอบ)</small></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;" width="15%">ยอดรวม<br><small>(กิโลกรัม)</small></th>
                                                </tr>
                                            </thead>
                                            
                                            <tbody>
                                                <?php 
                                                $i = 1;
                                                $grand_total_sack = 0;
                                                $grand_total_sum = 0;

                                                if (count($report_data) > 0) {
                                                    foreach($report_data as $fname => $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($fname); ?></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>
                                                </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>
                                                </tr>
                                                <?php
                                                } else {
                                                    // กรณีไม่มีข้อมูลในปีบัญชีที่ค้นหา
                                                    $colspan = 4 + count($all_ingredients);
                                                    echo "<tr><td colspan='".$colspan."' class='text-center text-danger'>--- ไม่พบข้อมูลการแปรรูปปุ๋ยในปีบัญชีนี้ ---</td></tr>";
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        <?php 
                        } 
                        ?>

                    </div>
                </div>

                <?php include 'footer1.php'; ?> 

            </div>
        </div>
        <?php include 'footer2.php'; ?>
        
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>

        <script>
        $(document).ready(function() {
            // 1. เรียกใช้งาน Select2
            $('#searchSahakorn, #searchYear').select2({
                allowClear: true // อนุญาตให้มีปุ่มกากบาทลบข้อมูลได้
            });

            // 2. จัดการ Animation ของ label-floating ให้ตัวหนังสือลอยขึ้น-ลงอย่างนุ่มนวล
            $('#searchSahakorn, #searchYear').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');
                }
            });

            // 3. ตรวจสอบค่าเริ่มต้นตอนโหลดหน้า (ในกรณีที่กดปุ่ม "ดูรายงาน" แล้วโหลดหน้าใหม่)
            $('#searchSahakorn, #searchYear').each(function() {
                if ($(this).val()) {
                    $(this).closest('.form-group').removeClass('is-empty');
                }
            });
        });
        </script>
    </body>
</html>

Zerion Mini Shell 1.0