<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'db_connection.php';

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["update_sale"])) {
    $invoice_number = $_POST["invoice_number"];
    $buyer = $_POST["buyer"];
    $sale_date = $_POST["sale_date"];
    $total_weight = $_POST["total_weight"];
    $sale_amount = $_POST["sale_amount"];
    $sale_amount2 = $_POST["sale_amount2"];
    $profit_percentage = $_POST["profit_percentage"];
    $profit_percentage = ($sale_amount2 - $sale_amount) / $sale_amount * 100;
    $sql_update_sale = "UPDATE sales SET 
                        buyer = '$buyer',
                        sale_date = '$sale_date',
                        total_weight = '$total_weight',
                        sale_amount = '$sale_amount',
                        sale_amount2 = '$sale_amount2',
                        profit_percentage = '$profit_percentage'
                        WHERE invoice_number = '$invoice_number'";

    if ($conn->query($sql_update_sale) === TRUE) {
        header("Location: display_data.php");
        exit();
    } else {
        echo "Ошибка: " . $sql_update_sale . "<br>" . $conn->error;
    }

    // Перенаправляем пользователя после успешного выполнения операции
    header("Location: display_data.php");
    exit();
} else {
    echo "<p>Некорректные данные запроса</p>";
}

$conn->close();
?>
