diff --git a/accounting.php b/accounting.php index 43cb47c..9ba5d6e 100644 --- a/accounting.php +++ b/accounting.php @@ -4,7 +4,12 @@ require_once 'includes/header.php'; require_once 'includes/accounting_functions.php'; // Check permission -$user_id = $_SESSION['user_id']; +$user_id = $_SESSION['user_id'] ?? 0; +if (!$user_id) { + header('Location: login.php'); + exit; +} + $stmt = db()->prepare("SELECT * FROM user_permissions WHERE user_id = ? AND page = 'accounting' AND can_view = 1"); $stmt->execute([$user_id]); if (!$stmt->fetch()) { @@ -14,19 +19,28 @@ if (!$stmt->fetch()) { } // Handle form submission -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_entry'])) { - $date = $_POST["date"] ?? ""; - $description = $_POST["description"] ?? ""; - $reference = $_POST["reference"] ?? ""; - $entries = [ - ["account" => $_POST["debit_account"] ?? "", "debit" => (float)($_POST["amount"] ?? 0), "credit" => 0], - ["account" => $_POST["credit_account"] ?? "", "debit" => 0, "credit" => (float)($_POST["amount"] ?? 0)] - ]; - - if (add_journal_entry($date, $description, $reference, $entries)) { - $message = "تم إضافة القيد بنجاح."; - } else { - $error = "حدث خطأ أثناء إضافة القيد."; +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['add_entry'])) { + $date = $_POST["date"] ?? ""; + $description = $_POST["description"] ?? ""; + $reference = $_POST["reference"] ?? ""; + $entries = [ + ["account" => $_POST["debit_account"] ?? "", "debit" => (float)($_POST["amount"] ?? 0), "credit" => 0], + ["account" => $_POST["credit_account"] ?? "", "debit" => 0, "credit" => (float)($_POST["amount"] ?? 0)] + ]; + + if (add_journal_entry($date, $description, $reference, $entries)) { + $message = "تم إضافة القيد بنجاح."; + } else { + $error = "حدث خطأ أثناء إضافة القيد."; + } + } elseif (isset($_POST['delete_entry'])) { + $id_to_delete = (int)$_POST['delete_id']; + if (delete_journal_entry($id_to_delete)) { + $message = "تم حذف القيد بنجاح."; + } else { + $error = "حدث خطأ أثناء حذف القيد."; + } } } @@ -50,8 +64,7 @@ $ledger = array_slice($ledger_all, $offset, $limit);
@@ -159,8 +172,15 @@ $ledger = array_slice($ledger_all, $offset, $limit); - ')"> - ')"> + + +
+ + + +
diff --git a/accounting_temp.php b/accounting_temp.php new file mode 100644 index 0000000..002defd --- /dev/null +++ b/accounting_temp.php @@ -0,0 +1,2 @@ + + diff --git a/includes/accounting_functions.php b/includes/accounting_functions.php index c882467..a61150c 100644 --- a/includes/accounting_functions.php +++ b/includes/accounting_functions.php @@ -12,7 +12,7 @@ function get_journal_entries() { function get_full_ledger() { $db = db(); - $stmt = $db->query("SELECT j.date, j.description, j.reference, e.account_name, e.debit, e.credit + $stmt = $db->query("SELECT j.id, j.date, j.description, j.reference, e.account_name, e.debit, e.credit FROM accounting_journal j JOIN accounting_entries e ON j.id = e.journal_id ORDER BY j.date DESC, j.id DESC"); @@ -21,7 +21,7 @@ function get_full_ledger() { function get_full_ledger_filtered($search = '', $date_from = '', $date_to = '') { $db = db(); - $sql = "SELECT j.date, j.description, j.reference, e.account_name, e.debit, e.credit + $sql = "SELECT j.id, j.date, j.description, j.reference, e.account_name, e.debit, e.credit FROM accounting_journal j JOIN accounting_entries e ON j.id = e.journal_id WHERE 1=1"; @@ -96,4 +96,22 @@ function add_journal_entry($date, $description, $reference, $entries) { return false; } } + +function delete_journal_entry($id) { + $db = db(); + $db->beginTransaction(); + try { + $stmt = $db->prepare("DELETE FROM accounting_entries WHERE journal_id = ?"); + $stmt->execute([$id]); + + $stmt = $db->prepare("DELETE FROM accounting_journal WHERE id = ?"); + $stmt->execute([$id]); + + $db->commit(); + return true; + } catch (Exception $e) { + $db->rollBack(); + return false; + } +} ?> \ No newline at end of file