403Webshell
Server IP : 104.21.80.248  /  Your IP : 162.159.115.42
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 :  E:/Inetpub/www/supply_system/supplies/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : E:/Inetpub/www/supply_system/supplies/action_batch_update.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 = 0;
$supply_id = 0; // For redirect

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['batch_id']) && isset($_POST['supply_id'])) {
    $batch_id = intval($_POST['batch_id']);
    $supply_id = intval($_POST['supply_id']); // Get supply_id for redirect

    // --- Get and Sanitize Data ---
    // Note: quantity_received and quantity_remaining are NOT updated from form
    $unit_price = isset($_POST['unit_price']) ? floatval($_POST['unit_price']) : -1.0; // Use -1 to detect invalid input easily
    $supplier = isset($_POST['supplier']) ? sanitize_input($conn, $_POST['supplier']) : '';
    $invoice_no = isset($_POST['invoice_no']) ? sanitize_input($conn, $_POST['invoice_no']) : '';
    $expiry_date = isset($_POST['expiry_date']) && !empty($_POST['expiry_date']) ? sanitize_input($conn, $_POST['expiry_date']) : NULL; // Allow NULL

    // --- Validate Data ---
    if ($batch_id <= 0 || $supply_id <= 0 || $unit_price < 0) {
        $message = "ข้อมูลไม่ถูกต้อง (ID หรือ ราคาต่อหน่วย ไม่ถูกต้อง)";
    } else {
        // --- Prepare SQL UPDATE ---
        // Recalculate total_value based on the NEW unit_price and EXISTING quantity_remaining
        // We need quantity_remaining to recalculate the batch's total_value correctly
        $sql_get_qty = "SELECT quantity_remaining FROM supply_batches WHERE id = $batch_id";
        $res_get_qty = mysqli_query($conn, $sql_get_qty);
        if ($res_get_qty && $row_qty = mysqli_fetch_assoc($res_get_qty)) {
            $quantity_remaining = intval($row_qty['quantity_remaining']);
            $new_total_value = $quantity_remaining * $unit_price; // Calculate based on remaining quantity

            $expiry_date_sql = ($expiry_date === NULL) ? "NULL" : "'". mysqli_real_escape_string($conn, $expiry_date) ."'"; // Escape expiry date

            $sql_update = "UPDATE supply_batches SET
                                unit_price = $unit_price,
                                total_value = $new_total_value, -- Update based on remaining qty
                                supplier = '$supplier',
                                invoice_no = '$invoice_no',
                                expiry_date = $expiry_date_sql
                           WHERE id = $batch_id";

             if (mysqli_query($conn, $sql_update)) {
                // The trigger 'update_supply_price_after_batch_change' should automatically
                // recalculate the average price and total value in the 'supplies' table.

                 if (mysqli_affected_rows($conn) > 0) {
                      $message = "แก้ไขข้อมูล Batch ID: $batch_id สำเร็จแล้ว";
                      $message_type = 'success';
                 } else {
                     $message = "ไม่มีข้อมูลที่เปลี่ยนแปลงสำหรับ Batch ID: $batch_id";
                     $message_type = 'warning';
                 }
                $_SESSION['message'] = $message;
                $_SESSION['message_type'] = $message_type;
                mysqli_close($conn);
                redirect('supply_view.php?id=' . $supply_id); // Redirect back to supply details

             } else {
                 $message = "เกิดข้อผิดพลาดในการบันทึกข้อมูล Batch: " . mysqli_error($conn);
                 $message_type = 'danger';
             }
        } else {
             $message = "เกิดข้อผิดพลาด: ไม่พบข้อมูล Batch ID: $batch_id เพื่อคำนวณมูลค่าใหม่";
             $message_type = 'danger';
        }
         if($res_get_qty) mysqli_free_result($res_get_qty);

    } // End validation check
} else {
    // If accessed directly or missing IDs
    $message = 'ข้อมูลไม่ถูกต้อง';
    $_SESSION['message'] = $message;
    $_SESSION['message_type'] = $message_type;
    // Try redirecting back to supply list if supply_id is unknown
    redirect($supply_id > 0 ? 'supply_view.php?id=' . $supply_id : 'index.php');
}

// --- Display Error Message if Redirect Failed ---
if (!empty($message)) {
    $_SESSION['message'] = $message;
    $_SESSION['message_type'] = $message_type;
    // Redirect back to edit form if possible
    redirect($batch_id > 0 && $supply_id > 0 ? 'batch_edit.php?id=' . $batch_id . '&supply_id=' . $supply_id : 'index.php');
}
?>

Youez - 2016 - github.com/yon3zu
LinuXploit