السلام عليكم بش مهندسين لو تكرمتم معي زر التعديل عند التعديل على فاتوره المبيعات عند التعديل يضاف الرصيد قيمه الفاتوره المعدله الى الرصيد السابق ولا يتعدل الفارق في الرصيد للفاتوره
<?php
// الاتصال بقاعدة البيانات
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "00";
$conn = new mysqli($servername, $username, $password, $dbname);
// التحقق من الاتصال
if ($conn->connect_error) {
die("فشل الاتصال بقاعدة البيانات: " . $conn->connect_error);
}
// الحصول على بيانات الفاتورة من طلب POST
$sales_number = $_POST["sales_number"];
$customer = $_POST["customer"];
$account_name = $_POST["account_name"];
$sales_date = $_POST["sales_date"];
$item_names = $_POST["item_name"];
$item_quantities = $_POST["item_quantity"];
$item_unit_prices = $_POST["item_unit_price"];
$totals = $_POST["total"];
$total = $_POST["total"];
// تحديث الفاتورة في قاعدة البيانات
$sql = "UPDATE sales SET customer='$customer', account_name='$account_name', sales_date='$sales_date', total='$total' WHERE sales_number='$sales_number'";
$conn->query($sql);
// تحديث العناصر في الفاتورة
for ($i = 0; $i < count($item_names); $i++) {
$item_name = $item_names[$i];
$item_quantity = $item_quantities[$i];
$item_unit_price = $item_unit_prices[$i];
$total = $totals[$i];
$sql = "UPDATE sales SET item_name='$item_name', item_quantity='$item_quantity', item_unit_price='$item_unit_price', total='$total' WHERE sales_number='$sales_number'";
$conn->query($sql);
}
$name = $account_name;
$sql = "UPDATE account SET debit = debit + $total WHERE name = '$name'";
$conn->query($sql);
// التحقق من وجود العناصر المحدثة
if (isset($_POST['item_name']) && isset($_POST['item_quantity'])) {
$item_names = $_POST['item_name'];
$item_quantities = $_POST['item_quantity'];
// حلقة لتحديث الكمية لكل صنف
foreach ($item_names as $index => $item_name) {
$item_quantity = $item_quantities[$index];
// استعلام لتحديث الكمية في المخزن
$sql = "UPDATE inventory SET quantity = quantity - $item_quantity WHERE name = '$item_name'";
$conn->query($sql);
}
}
// تحديث القيد المحاسبي المرتبط بالفاتورة
// تحديث القيد المحاسبي المرتبط بالفاتورة
$sql = "UPDATE journal_entries SET credit = 0, debit = $total WHERE name = '$customer' AND debit > 0";
$conn->query($sql);
$sql2 = "UPDATE journal_entries SET credit = $total, debit = 0 WHERE name = 'حساب المبيعات' AND credit > 0";
$conn->query($sql2);
echo "تم تحديث القيد المحاسبي بنجاح.";
// إغلاق الاتصال بقاعدة البيانات
$conn->close();
// إعادة توجيه المستخدم إلى الصفحة الرئيسية
header("Location: index.php");
?>