0% found this document useful (0 votes)
17 views6 pages

Add Expense - PHP

The document is a PHP script for managing expense entries in an application, including functionalities for retrieving active employees and suppliers from a database. It handles form submissions for different types of expenses, validates input data, and logs actions in the database. The script also includes a dynamic form that adjusts fields based on the selected expense type, with error handling and session management for user feedback.

Uploaded by

adenmarina2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Add Expense - PHP

The document is a PHP script for managing expense entries in an application, including functionalities for retrieving active employees and suppliers from a database. It handles form submissions for different types of expenses, validates input data, and logs actions in the database. The script also includes a dynamic form that adjusts fields based on the selected expense type, with error handling and session management for user feedback.

Uploaded by

adenmarina2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

<?

php
session_start();
require_once '../../includes/db.php';

// ‫جلب البيانات الالزمة للقوائم المنسدلة‬


$employees = [];
$suppliers = [];

$emp_query = "SELECT id, name FROM employees WHERE status = 'active'";


$emp_result = $conn->query($emp_query);
while ($row = $emp_result->fetch_assoc()) {
$employees[$row['id']] = $row['name'];
}

$sup_query = "SELECT id, name FROM suppliers";


$sup_result = $conn->query($sup_query);
while ($row = $sup_result->fetch_assoc()) {
$suppliers[$row['id']] = $row['name'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$expense_type = $_POST['expense_type'];
$description = trim(htmlspecialchars($_POST['description']));
$amount = floatval($_POST['amount']);
$date = $_POST['date'];
$related_id = isset($_POST['related_id']) ? (int)$_POST['related_id'] : null;
$withdrawal_type = isset($_POST['withdrawal_type']) ? $_POST['withdrawal_type']
: null;

// ‫التحقق من الصحة حسب النوع‬


$errors = [];

// ‫تحقق أساسي‬
if (empty($expense_type)) {
$errors[] = '‫;'يجب تحديد نوع المصروف‬
}

if ($amount <= 0) {
$errors[] = '‫;'يجب أن يكون المبلغ أكبر من الصفر‬
}

// ‫تحقق حسب النوع‬


switch ($expense_type) {
case 'salaries':
if (empty($related_id)) {
$errors[] = '‫;'يجب تحديد الموظف‬
}
break;

case 'utilities':
if (empty($description)) {
$errors[] = '‫;'يجب إدخال وصف الفاتورة‬
}
break;

case 'purchases':
if (empty($related_id)) {
$errors[] = '‫;'يجب تحديد المورد‬
}
break;

default:
if (empty($description)) {
$errors[] = '‫;'يجب إدخال وصف المصروف‬
}
}

if (empty($errors)) {
try {
$conn->begin_transaction();

if ($expense_type === 'salaries') {


// ‫ إدخال سحب راتب فقط في جدول‬salary_withdrawals
$withdrawal_query = "INSERT INTO salary_withdrawals
(employee_id, amount, date, notes,
withdrawal_type, created_by)
VALUES (?, ?, ?, ?, ?, ?)";
$withdrawal_stmt = $conn->prepare($withdrawal_query);
$withdrawal_stmt->bind_param(
"idsssi",
$related_id,
$amount,
$date,
$description,
$withdrawal_type,
$_SESSION['user_id']
);
$withdrawal_stmt->execute();
$withdrawal_id = $conn->insert_id;

$log = "‫ تم سحب راتب للموظف‬ID: $related_id - ‫المبلغ‬: $amount";

// ‫ تسجيل في السجل باستخدام‬withdrawal_id


$log_query = "INSERT INTO expense_logs
(expense_id, action, details, user_id)
VALUES (?, 'salary_withdrawal', ?, ?)";
$log_stmt = $conn->prepare($log_query);
$log_stmt->bind_param("isi", $withdrawal_id, $log,
$_SESSION['user_id']);
$log_stmt->execute();
} else {
// ‫ إدخال مصروف عادي في جدول‬expenses
$query = "INSERT INTO expenses
(expense_type, related_id, description, amount, date,
created_by)
VALUES (?, ?, ?, ?, ?, ?)";

$stmt = $conn->prepare($query);
$stmt->bind_param(
"sisdsi",
$expense_type,
$related_id,
$description,
$amount,
$date,
$_SESSION['user_id']
);
$stmt->execute();
$expense_id = $conn->insert_id;

// ‫معالجة خاصة لكل نوع‬


switch ($expense_type) {
case 'utilities':
$log = "‫ تم دفع فاتورة‬$description";
break;

case 'purchases':
$log = "‫ تم الشراء من المورد‬ID: $related_id";
break;

default:
$log = "‫تم إضافة مصروف‬: $description";
}

// ‫ تسجيل في السجل باستخدام‬expense_id


$log_query = "INSERT INTO expense_logs
(expense_id, action, details, user_id)
VALUES (?, 'create', ?, ?)";
$log_stmt = $conn->prepare($log_query);
$log_stmt->bind_param("isi", $expense_id, $log,
$_SESSION['user_id']);
$log_stmt->execute();
}

$conn->commit();
$_SESSION['success'] = "‫;"تم حفظ المصروف بنجاح‬
} catch (Exception $e) {
$conn->rollback();
$_SESSION['error'] = "‫خطأ‬: " . $e->getMessage();
}
} else {
$_SESSION['error'] = implode('<br>', $errors);
}

header("Location: expenses.php");
exit();
}
?>

<!DOCTYPE html>
<html dir="rtl">
<head>
<title>‫<إضافة مصروف‬/title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container py-4">
<h2 class="text-center mb-4">‫<إضافة مصروف جديد‬/h2>

<form method="post" class="border p-4 rounded">


<div class="row g-3">
<!-- ‫ نوع المصروف‬-->
<div class="col-md-6">
<label class="form-label">‫<نوع المصروف‬/label>
<select name="expense_type" id="expense_type" class="form-
select" required>
<option value="">-- ‫ اختر نوع المصروف‬--</option>
<option value="salaries">‫<رواتب الموظفين‬/option>
<option value="utilities">)‫هاتف‬/‫ماء‬/‫<فواتير (كهرباء‬/option>
<option value="purchases">‫<مشتريات‬/option>
<option value="other">‫<أخرى‬/option>
</select>
</div>

<!-- ‫ الحقول الديناميكية‬-->


<div id="dynamic_fields" class="col-12">
<!-- ‫ سيتم عرض الحقول المناسبة هنا حسب النوع‬-->
</div>

<!-- ‫ المبلغ والتاريخ‬-->


<div class="col-md-6">
<label class="form-label">‫<المبلغ‬/label>
<input type="number" name="amount" class="form-control"
step="0.01" min="0.01" required>
</div>

<div class="col-md-6">
<label class="form-label">‫<التاريخ‬/label>
<input type="date" name="date" class="form-control" max="<?=
date('Y-m-d') ?>" required>
</div>

<div class="col-12">
<button type="submit" class="btn btn-primary"> ‫حفظ‬
‫<المصروف‬/button>
</div>
</div>
</form>
</div>

<script>
$(document).ready(function() {
// ‫عند تغيير نوع المصروف‬
$('#expense_type').change(function() {
const type = $(this).val();
let html = '';

switch(type) {
case 'salaries':
html = `
<div class="col-md-6">
<label class="form-label">‫<الموظف‬/label>
<select name="related_id" class="form-select" required>
<option value="">-- ‫ اختر الموظف‬--</option>
<?php foreach($employees as $id => $name): ?>
<option value="<?= $id ?>"><?=
htmlspecialchars($name) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">‫<وصف إضافي‬/label>
<input type="text" name="description" class="form-
control" placeholder=")‫>"وصف إضافي (اختياري‬
</div>
<div class="col-md-6">
<label class="form-label">‫<نوع السحب‬/label>
<select name="withdrawal_type" class="form-select">
<option value="cash">‫<كاش‬/option>
<option value="bank_transfer">‫<تحويل بنكي‬/option>
</select>
</div>
`;
break;

case 'utilities':
html = `
<div class="col-md-6">
<label class="form-label">‫<نوع الفاتورة‬/label>
<select name="description" class="form-select"
required>
<option value="">-- ‫ اختر نوع الفاتورة‬--</option>
<option value=" ‫فاتورة كهرباء">فاتورة‬
‫<كهرباء‬/option>
<option value="‫<فاتورة ماء">فاتورة ماء‬/option>
<option value="‫<فاتورة هاتف">فاتورة هاتف‬/option>
<option value=" ‫فاتورة إنترنت">فاتورة‬
‫<إنترنت‬/option>
</select>
</div>
`;
break;

case 'purchases':
html = `
<div class="col-md-6">
<label class="form-label">‫<المورد‬/label>
<select name="related_id" class="form-select" required>
<option value="">-- ‫ اختر المورد‬--</option>
<?php foreach($suppliers as $id => $name): ?>
<option value="<?= $id ?>"><?=
htmlspecialchars($name) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">‫<وصف المشتريات‬/label>
<input type="text" name="description" class="form-
control" placeholder="‫ "وصف المشتريات‬required>
</div>
`;
break;

default:
html = `
<div class="col-12">
<label class="form-label">‫<وصف المصروف‬/label>
<input type="text" name="description" class="form-
control" placeholder="‫ "أدخل وصف المصروف‬required>
</div>
`;
}

$('#dynamic_fields').html(html);
});
});
</script>
</body>
</html>
<style>
body {
background-color: #f8f9fa;
font-family: 'Tajawal', sans-serif;
font-weight: 600; /* ‫* زيادة سماكة الخط العام‬/
}
.card {
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.form-label {
font-weight: 600; /* ‫* زيادة سماكة تسميات الحقول‬/
}
.form-label.required:after {
content: " *";
color: red;
}
.btn-submit {
background-color: #28a745;
border-color: #28a745;
font-weight: 600; /* ‫* زيادة سماكة نص الأزرار‬/
}
.page-title {
font-size: 1.5rem;
font-weight: 700; /* ‫* زيادة سماكة العنوان الرئيسي‬/
}
.card-header h4 {
font-weight: 700; /* ‫* زيادة سماكة عنوان البطاقة‬/
}
.form-control, .form-select {
font-weight: 500; /* ‫* سماكة متوسطة لنص الحقول‬/
}
.alert {
font-weight: 600; /* ‫* زيادة سماكة نص التنبيهات‬/
}
@media (max-width: 768px) {
.container {
padding: 15px;
}
}
</style>

You might also like