[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: create.php
<?php require_once '../config/config.php'; require_once '../lib/whatsapp.php'; requirePermission(); $pageTitle = 'تحصيل الأموال'; $db = Database::getInstance(); $whatsapp = new WhatsAppService(); $error = ''; $activeShiftSql = "SELECT id FROM shifts WHERE representative_id = ? AND status = 'active' LIMIT 1"; $activeShift = $db->query($activeShiftSql, [$_SESSION['user_id']])->fetch(); if (!$activeShift && $_SESSION['user_role'] === 'representative') { redirect('index.php?error=no_active_shift'); } $customersSql = "SELECT id, name, phone, current_debt FROM customers WHERE current_debt > 0 ORDER BY name ASC"; $customers = $db->query($customersSql)->fetchAll(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $customerId = intval($_POST['customer_id'] ?? 0); $amount = floatval($_POST['amount'] ?? 0); $paymentMethod = cleanInput($_POST['payment_method'] ?? 'cash'); $notes = cleanInput($_POST['notes'] ?? ''); if (empty($customerId) || $amount <= 0) { $error = 'يرجى اختيار العميل وإدخال مبلغ صحيح'; } else { try { $db->beginTransaction(); $customerSql = "SELECT name, phone, current_debt FROM customers WHERE id = ?"; $customer = $db->query($customerSql, [$customerId])->fetch(); if (!$customer) { throw new Exception('العميل غير موجود'); } if ($amount > $customer['current_debt']) { throw new Exception('المبلغ المدخل أكبر من المديونية الحالية'); } $previousDebt = $customer['current_debt']; $newDebt = $previousDebt - $amount; $shiftId = $activeShift['id'] ?? null; if ($_SESSION['user_role'] !== 'representative') { $anyShiftSql = "SELECT id FROM shifts WHERE status = 'active' LIMIT 1"; $anyShift = $db->query($anyShiftSql)->fetch(); $shiftId = $anyShift['id'] ?? null; } $paymentSql = "INSERT INTO payments (customer_id, representative_id, shift_id, amount, previous_debt, new_debt, payment_method, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $db->query($paymentSql, [ $customerId, $_SESSION['user_id'], $shiftId, $amount, $previousDebt, $newDebt, $paymentMethod, $notes ]); $paymentId = $db->lastInsertId(); $unpaidInvoicesSql = "SELECT id, remaining_amount FROM invoices WHERE customer_id = ? AND remaining_amount > 0 ORDER BY created_at ASC"; $unpaidInvoices = $db->query($unpaidInvoicesSql, [$customerId])->fetchAll(); $remainingPayment = $amount; foreach ($unpaidInvoices as $invoice) { if ($remainingPayment <= 0) break; $amountToApply = min($remainingPayment, $invoice['remaining_amount']); $applicationSql = "INSERT INTO payment_applications (payment_id, invoice_id, amount_applied) VALUES (?, ?, ?)"; $db->query($applicationSql, [$paymentId, $invoice['id'], $amountToApply]); $newInvoiceRemaining = $invoice['remaining_amount'] - $amountToApply; $newInvoicePaid = $invoice['remaining_amount'] - $newInvoiceRemaining; $invoiceStatus = 'unpaid'; if ($newInvoiceRemaining == 0) { $invoiceStatus = 'paid'; } elseif ($newInvoicePaid > 0) { $invoiceStatus = 'partial'; } $updateInvoiceSql = "UPDATE invoices SET remaining_amount = ?, paid_amount = paid_amount + ?, status = ? WHERE id = ?"; $db->query($updateInvoiceSql, [$newInvoiceRemaining, $amountToApply, $invoiceStatus, $invoice['id']]); $remainingPayment -= $amountToApply; } $updateCustomerSql = "UPDATE customers SET current_debt = ? WHERE id = ?"; $db->query($updateCustomerSql, [$newDebt, $customerId]); $whatsappSent = $whatsapp->sendPaymentMessage( $customer['name'], $customer['phone'], $amount, $previousDebt, $newDebt ); if ($whatsappSent) { $db->query("UPDATE payments SET whatsapp_sent = 1 WHERE id = ?", [$paymentId]); } logActivity($_SESSION['user_id'], 'payment_collected', "تم تحصيل مبلغ: $amount جنيه من العميل: {$customer['name']}", $paymentId, 'payment'); $db->commit(); redirect('index.php?success=payment_collected'); } catch (Exception $e) { $db->rollback(); $error = 'حدث خطأ أثناء تحصيل الدفعة: ' . $e->getMessage(); error_log($e->getMessage()); } } } include '../includes/header.php'; ?> <div class="row"> <div class="col-md-8 mx-auto"> <div class="card shadow-sm"> <div class="card-header bg-success text-white"> <h4 class="mb-0"><i class="bi bi-cash-coin"></i> تحصيل الأموال</h4> </div> <div class="card-body"> <?php if ($error): ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php endif; ?> <?php if (empty($customers)): ?> <div class="alert alert-info"> لا يوجد عملاء لديهم مديونيات حالياً </div> <a href="../customers/index.php" class="btn btn-primary"> <i class="bi bi-arrow-right"></i> العودة للعملاء </a> <?php else: ?> <form method="POST" action="" id="paymentForm"> <div class="mb-3"> <label for="customer_id" class="form-label">اختر العميل <span class="text-danger">*</span></label> <select class="form-select" id="customer_id" name="customer_id" required> <option value="">-- اختر العميل --</option> <?php foreach ($customers as $customer): ?> <option value="<?php echo $customer['id']; ?>" data-debt="<?php echo $customer['current_debt']; ?>"> <?php echo htmlspecialchars($customer['name']); ?> - المديونية: <?php echo formatMoney($customer['current_debt']); ?> </option> <?php endforeach; ?> </select> </div> <div class="alert alert-warning" id="debtInfo" style="display:none;"> <h5>معلومات المديونية</h5> <p class="mb-1"><strong>المديونية الحالية:</strong> <span id="currentDebt">0.00 جنيه</span></p> </div> <div class="mb-3"> <label for="amount" class="form-label">المبلغ المحصل <span class="text-danger">*</span></label> <input type="number" class="form-control form-control-lg" id="amount" name="amount" step="0.01" min="0.01" required placeholder="0.00"> </div> <div class="mb-3"> <label for="payment_method" class="form-label">طريقة الدفع</label> <select class="form-select" id="payment_method" name="payment_method"> <option value="cash">نقداً</option> <option value="bank_transfer">تحويل بنكي</option> <option value="other">أخرى</option> </select> </div> <div class="mb-3"> <label for="notes" class="form-label">ملاحظات</label> <textarea class="form-control" id="notes" name="notes" rows="3"></textarea> </div> <div class="card bg-light mb-3" id="paymentSummary" style="display:none;"> <div class="card-body"> <h5>ملخص التحصيل</h5> <table class="table table-sm mb-0"> <tr> <td><strong>المديونية السابقة:</strong></td> <td class="text-end" id="summaryPreviousDebt">0.00 جنيه</td> </tr> <tr> <td><strong>المبلغ المحصل:</strong></td> <td class="text-end" id="summaryAmount">0.00 جنيه</td> </tr> <tr class="table-success"> <td><strong>المديونية الجديدة:</strong></td> <td class="text-end"><strong id="summaryNewDebt">0.00 جنيه</strong></td> </tr> </table> </div> </div> <div class="d-flex justify-content-between"> <button type="submit" class="btn btn-success btn-lg"> <i class="bi bi-check-lg"></i> تأكيد التحصيل </button> <a href="index.php" class="btn btn-secondary"> <i class="bi bi-x-lg"></i> إلغاء </a> </div> </form> <?php endif; ?> </div> </div> </div> </div> <script> $('#customer_id').change(function() { const debt = parseFloat($(this).find(':selected').data('debt')) || 0; if (debt > 0) { $('#currentDebt').text(debt.toFixed(2) + ' جنيه'); $('#debtInfo').show(); $('#amount').attr('max', debt); } else { $('#debtInfo').hide(); } updateSummary(); }); $('#amount').on('input', function() { const maxAmount = parseFloat($('#customer_id').find(':selected').data('debt')) || 0; const amount = parseFloat($(this).val()) || 0; if (amount > maxAmount) { $(this).val(maxAmount.toFixed(2)); } updateSummary(); }); function updateSummary() { const previousDebt = parseFloat($('#customer_id').find(':selected').data('debt')) || 0; const amount = parseFloat($('#amount').val()) || 0; const newDebt = previousDebt - amount; if (amount > 0 && previousDebt > 0) { $('#summaryPreviousDebt').text(previousDebt.toFixed(2) + ' جنيه'); $('#summaryAmount').text(amount.toFixed(2) + ' جنيه'); $('#summaryNewDebt').text(newDebt.toFixed(2) + ' جنيه'); $('#paymentSummary').show(); } else { $('#paymentSummary').hide(); } } </script> <?php include '../includes/footer.php'; ?>
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: premium320.web-hosting.com
Server IP: 66.29.153.54
PHP Version: 8.2.29
Server Software: LiteSpeed
System: Linux premium320.web-hosting.com 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
HDD Total: 97.87 GB
HDD Free: 76.85 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
No
git:
Yes
User Info
Username: aoneqssk
User ID (UID): 1285
Group ID (GID): 1290
Script Owner UID: 1285
Current Dir Owner: 1285