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/text.php

<!doctype html>
<html lang="en">
    <?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">คำนวณและบันทึกการผสมปุ๋ย</h4>
                            </div>
                            <div class="card-content">
                                <form method="POST" action="">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกสหกรณ์</label>
                                                <select name="sahakornID" 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)) {
                                                        echo "<option value='".$row_sahakorn['sahakornID']."'>".$row_sahakorn['name']."</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกปีบัญชี(ปีพ.ศ.)</label>
                                                <select name="year" 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--) {
                                                        echo "<option value='".$y."'>".$y."</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="form-group label-floating">
                                                <label class="control-label">กรุณาเลือกสูตรปุ๋ยผสม</label>
                                                <select name="formulaID" class="form-control" required>
                                                    <option value=""></option>
                                                    <?php
                                                    $sql_formula = "SELECT formulaID, name_formula FROM formula";
                                                    $result_formula = mysqli_query($link, $sql_formula);
                                                    while($row_formula = mysqli_fetch_assoc($result_formula)) {
                                                        echo "<option value='".$row_formula['formulaID']."'>".$row_formula['name_formula']."</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <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>
                                    <button type="submit" name="btn_calculate" class="btn btn-primary pull-right">คำนวณและบันทึกข้อมูล</button>
                                    <div class="clearfix"></div>
                                </form>
                            </div>
                        </div>

                        <?php
                        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_calculate'])) {
                            $sahakornID = $_POST['sahakornID'];
                            $year = $_POST['year'];
                            $formulaID = $_POST['formulaID'];
                            $sack = $_POST['sack'];

                            $display_data = array(); 
                            
                            // ----- ส่วนที่ 1: ตรวจสอบและอัปเดตข้อมูลเก่า (เฉพาะสูตรที่เลือกในปีนั้น) -----
                            $check_sql = "SELECT COUNT(*) as cnt FROM sahakorn_process WHERE sahakornID = ? AND year = ? AND formulaID = ?";
                            $stmt_check = mysqli_prepare($link, $check_sql);
                            mysqli_stmt_bind_param($stmt_check, "iii", $sahakornID, $year, $formulaID);
                            mysqli_stmt_execute($stmt_check);
                            $res_check = mysqli_stmt_get_result($stmt_check);
                            $row_check = mysqli_fetch_assoc($res_check);
                            $is_update = ($row_check['cnt'] > 0) ? true : false;
                            mysqli_stmt_close($stmt_check);

                            if ($is_update) {
                                $del_sql = "DELETE FROM sahakorn_process WHERE sahakornID = ? AND year = ? AND formulaID = ?";
                                $stmt_del = mysqli_prepare($link, $del_sql);
                                mysqli_stmt_bind_param($stmt_del, "iii", $sahakornID, $year, $formulaID);
                                mysqli_stmt_execute($stmt_del);
                                mysqli_stmt_close($stmt_del);
                            }

                            // ----- ส่วนที่ 2: ดึงข้อมูลสูตรและคำนวณ -----
                            $stmt_formula = mysqli_prepare($link, "SELECT name_formula FROM formula WHERE formulaID = ?");
                            mysqli_stmt_bind_param($stmt_formula, "i", $formulaID);
                            mysqli_stmt_execute($stmt_formula);
                            $res_f = mysqli_stmt_get_result($stmt_formula);
                            $row_f = mysqli_fetch_assoc($res_f); 
                            $formula_name = $row_f['name_formula'];
                            mysqli_stmt_close($stmt_formula);

                            $stmt_insert = mysqli_prepare($link, "INSERT INTO sahakorn_process (sahakornID, year, formulaID, sack, S_mother_pui, sum) VALUES (?, ?, ?, ?, ?, ?)");

                            // ดึงข้อมูลแม่ปุ๋ย
                            $sql_mother = "SELECT mn.mother_name as ingredient_name, mf.mother_quantity as quantity 
                                           FROM mother_fertilizer mf
                                           JOIN mother_name mn ON mf.mother_fertilizer_name_id = mn.mother_name_id
                                           WHERE mf.formulaID = ?";
                            $stmt_mother = mysqli_prepare($link, $sql_mother);
                            mysqli_stmt_bind_param($stmt_mother, "i", $formulaID);
                            mysqli_stmt_execute($stmt_mother);
                            $result_mother = mysqli_stmt_get_result($stmt_mother);

                            while ($row = mysqli_fetch_assoc($result_mother)) {
                                $kg_sum = ($row['quantity'] / 100) * $sack * 50;
                                // เติมคำว่า "แม่ปุ๋ย" ไว้ที่ชื่อคอลัมน์เพื่อแสดงผล
                                $display_data[] = array('col_name' => 'แม่ปุ๋ย' . $row['ingredient_name'], 'kg_sum' => $kg_sum);
                                mysqli_stmt_bind_param($stmt_insert, "iiidsd", $sahakornID, $year, $formulaID, $sack, $row['ingredient_name'], $kg_sum);
                                mysqli_stmt_execute($stmt_insert);
                            }
                            mysqli_stmt_close($stmt_mother);

                            // ดึงข้อมูลสารเติมแต่ง
                            $sql_additives = "SELECT a.additives_name as ingredient_name, af.additives_quantity as quantity 
                                              FROM additives_fertilizer af
                                              JOIN additives a ON af.additives_fertilizer_name_id = a.additives_id
                                              WHERE af.formulaID = ?";
                            $stmt_add = mysqli_prepare($link, $sql_additives);
                            mysqli_stmt_bind_param($stmt_add, "i", $formulaID);
                            mysqli_stmt_execute($stmt_add);
                            $result_add = mysqli_stmt_get_result($stmt_add);

                            while ($row = mysqli_fetch_assoc($result_add)) {
                                $kg_sum = ($row['quantity'] / 100) * $sack * 50;
                                // สารเติมแต่งมีคำว่า "สารเติ่ม" ในฐานข้อมูลอยู่แล้ว
                                $display_data[] = array('col_name' => $row['ingredient_name'], 'kg_sum' => $kg_sum);
                                mysqli_stmt_bind_param($stmt_insert, "iiidsd", $sahakornID, $year, $formulaID, $sack, $row['ingredient_name'], $kg_sum);
                                mysqli_stmt_execute($stmt_insert);
                            }
                            mysqli_stmt_close($stmt_add);
                            mysqli_stmt_close($stmt_insert);
                            
                            // ----- ส่วนที่ 3: แสดงผลลัพธ์รอบปัจจุบันแบบแนวนอน (Pivot) -----
                            if (!empty($display_data)) {
                        ?>
                            <div class="card">
                                <div class="card-header" data-background-color="<?php echo $is_update ? 'orange' : 'green'; ?>">
                                    <h4 class="title">
                                        ผลลัพธ์การคำนวณ: <?php echo htmlspecialchars($formula_name); ?> 
                                        <?php echo $is_update ? '(อัปเดตข้อมูลสูตรเดิม)' : '(เพิ่มข้อมูลสูตรใหม่)'; ?>
                                    </h4>
                                    <p class="category">บันทึกข้อมูลสำหรับปีบัญชี <?php echo $year; ?> เรียบร้อยแล้ว</p>
                                </div>
                                <div class="card-content table-responsive">
                                    <table class="table table-hover">
                                        <thead class="text-success">
                                            <tr>
                                                <th class="text-center">สูตรปุ๋ยที่ผสม</th>
                                                <th class="text-right">จำนวนที่ผสม(เป็นกระสอบ)</th>
                                                <?php 
                                                $total_kg = 0;
                                                foreach ($display_data as $data) { 
                                                    $total_kg += $data['kg_sum'];
                                                    echo '<th class="text-right">' . htmlspecialchars($data['col_name']) . '</th>';
                                                } 
                                                ?>
                                                <th class="text-right">รวมทั้งสิ้น(กิโลกรัม)</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td class="text-center"><?php echo htmlspecialchars($formula_name); ?></td>
                                                <td class="text-right"><?php echo number_format($sack, 2); ?></td>
                                                <?php foreach ($display_data as $data) { ?>
                                                    <td class="text-right"><?php echo number_format($data['kg_sum'], 2); ?></td>
                                                <?php } ?>
                                                <td class="text-right"><?php echo number_format($total_kg, 2); ?></td>
                                            </tr>
                                        </tbody>
                                        </table>
                                </div>
                            </div>
                        <?php 
                            } 

                            // ----- ส่วนที่ 4: ตารางประวัติของสหกรณ์นั้น (แสดงเป็นแถวยาว) -----
                            $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);

                            $sql_history = "SELECT sp.year, f.name_formula, sp.sack, 
                                            GROUP_CONCAT(CONCAT(sp.S_mother_pui, ' (', FORMAT(sp.sum, 2), ' ก.ก.)') SEPARATOR ', ') as ingredients_list
                                            FROM sahakorn_process sp
                                            LEFT JOIN formula f ON sp.formulaID = f.formulaID
                                            WHERE sp.sahakornID = ?
                                            GROUP BY sp.year, sp.formulaID, sp.sack, f.name_formula
                                            ORDER BY sp.year DESC";
                            
                            $stmt_hist = mysqli_prepare($link, $sql_history);
                            mysqli_stmt_bind_param($stmt_hist, "i", $sahakornID);
                            mysqli_stmt_execute($stmt_hist);
                            $res_hist = mysqli_stmt_get_result($stmt_hist);
                        ?>
                            <div class="card">
                                <div class="card-header" data-background-color="blue">
                                    <h4 class="title">ประวัติการบันทึกข้อมูลของ: <?php echo htmlspecialchars($sh_name); ?></h4>
                                    <p class="category">แสดงรายละเอียดการผสมปุ๋ยแยกตามปีบัญชี</p>
                                </div>
                                <div class="card-content table-responsive">
                                    <table class="table table-bordered">
                                        <thead class="text-info">
                                            <tr>
                                                <th width="10%">ปีบัญชี</th>
                                                <th width="20%">ชื่อสูตรปุ๋ย</th>
                                                <th width="15%" class="text-right">ที่ผสม (กระสอบ)</th>
                                                <th width="55%">ส่วนผสมทั้งหมด (แม่ปุ๋ยและสารเติมแต่ง)</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            if (mysqli_num_rows($res_hist) > 0) {
                                                while($row_h = mysqli_fetch_assoc($res_hist)) { 
                                            ?>
                                            <tr>
                                                <td><?php echo $row_h['year']; ?></td>
                                                <td><?php echo htmlspecialchars($row_h['name_formula']); ?></td>
                                                <td class="text-right"><?php echo number_format($row_h['sack'], 2); ?></td>
                                                <td><?php echo $row_h['ingredients_list']; ?></td>
                                            </tr>
                                            <?php 
                                                }
                                            } else {
                                                echo "<tr><td colspan='4' class='text-center'>ไม่มีประวัติข้อมูล</td></tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        <?php 
                            mysqli_stmt_close($stmt_hist);
                        } 
                        ?>
                    </div>
                </div>

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

            </div>
        </div>
        <?php include 'footer2.php'; ?>
    </body>
</html>

Zerion Mini Shell 1.0