[
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 ORDER BY name ASC"; $customers = $db->query($customersSql)->fetchAll(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $customerId = intval($_POST['customer_id'] ?? 0); $notes = cleanInput($_POST['notes'] ?? ''); $items = $_POST['items'] ?? []; if (empty($customerId) || empty($items)) { $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('العميل غير موجود'); } $invoiceTotal = 0; foreach ($items as $item) { if (!empty($item['product_name']) && $item['quantity'] > 0 && $item['unit_price'] > 0) { $invoiceTotal += $item['quantity'] * $item['unit_price']; } } $previousDebt = $customer['current_debt']; $newTotalDebt = $previousDebt + $invoiceTotal; $invoiceNumber = 'INV-' . date('Ymd') . '-' . str_pad(rand(1, 9999), 4, '0', STR_PAD_LEFT); $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; } $invoiceSql = "INSERT INTO invoices (invoice_number, customer_id, representative_id, shift_id, previous_debt, invoice_total, new_total_debt, remaining_amount, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; $db->query($invoiceSql, [ $invoiceNumber, $customerId, $_SESSION['user_id'], $shiftId, $previousDebt, $invoiceTotal, $newTotalDebt, $invoiceTotal, $notes ]); $invoiceId = $db->lastInsertId(); foreach ($items as $item) { if (!empty($item['product_name']) && $item['quantity'] > 0 && $item['unit_price'] > 0) { $itemTotal = $item['quantity'] * $item['unit_price']; $itemSql = "INSERT INTO invoice_items (invoice_id, product_name, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)"; $db->query($itemSql, [ $invoiceId, $item['product_name'], $item['quantity'], $item['unit_price'], $itemTotal ]); } } $updateCustomerSql = "UPDATE customers SET current_debt = ? WHERE id = ?"; $db->query($updateCustomerSql, [$newTotalDebt, $customerId]); $whatsappSent = $whatsapp->sendInvoiceMessage( $customer['name'], $customer['phone'], $invoiceTotal, $newTotalDebt ); if ($whatsappSent) { $db->query("UPDATE invoices SET whatsapp_sent = 1 WHERE id = ?", [$invoiceId]); } logActivity($_SESSION['user_id'], 'invoice_created', "تم إنشاء فاتورة رقم: $invoiceNumber", $invoiceId, 'invoice'); $db->commit(); redirect('view.php?id=' . $invoiceId); } catch (Exception $e) { $db->rollback(); $error = 'حدث خطأ أثناء إنشاء الفاتورة: ' . $e->getMessage(); error_log($e->getMessage()); } } } include '../includes/header.php'; ?> <div class="row"> <div class="col-12"> <div class="card shadow-sm"> <div class="card-header bg-primary text-white"> <h4 class="mb-0"><i class="bi bi-file-earmark-plus"></i> إنشاء فاتورة جديدة</h4> </div> <div class="card-body"> <?php if ($error): ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php endif; ?> <form method="POST" action="" id="invoiceForm"> <div class="row mb-4"> <div class="col-md-6"> <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 $customer['phone']; ?> (المديونية: <?php echo formatMoney($customer['current_debt']); ?>) </option> <?php endforeach; ?> </select> </div> <div class="col-md-6"> <label class="form-label">المديونية السابقة</label> <div class="alert alert-info mb-0" id="previousDebt"> <strong>0.00 جنيه</strong> </div> </div> </div> <hr> <h5 class="mb-3">بنود الفاتورة</h5> <div id="itemsContainer"> <div class="row mb-3 item-row"> <div class="col-md-4"> <input type="text" class="form-control" name="items[0][product_name]" placeholder="اسم المنتج" required> </div> <div class="col-md-2"> <input type="number" class="form-control item-quantity" name="items[0][quantity]" placeholder="الكمية" min="1" required> </div> <div class="col-md-2"> <input type="number" class="form-control item-price" name="items[0][unit_price]" placeholder="السعر" step="0.01" min="0.01" required> </div> <div class="col-md-3"> <input type="text" class="form-control item-total" placeholder="الإجمالي" readonly> </div> <div class="col-md-1"> <button type="button" class="btn btn-danger btn-sm" onclick="removeItem(this)" disabled> <i class="bi bi-trash"></i> </button> </div> </div> </div> <button type="button" class="btn btn-success mb-3" onclick="addItem()"> <i class="bi bi-plus-lg"></i> إضافة منتج </button> <div class="row"> <div class="col-md-6"> <label for="notes" class="form-label">ملاحظات</label> <textarea class="form-control" id="notes" name="notes" rows="3"></textarea> </div> <div class="col-md-6"> <div class="card bg-light"> <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="summaryInvoiceTotal">0.00 جنيه</td> </tr> <tr class="table-danger"> <td><strong>إجمالي المديونية الجديدة:</strong></td> <td class="text-end"><strong id="summaryNewDebt">0.00 جنيه</strong></td> </tr> </table> </div> </div> </div> </div> <hr> <div class="d-flex justify-content-between"> <button type="submit" class="btn btn-primary 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> </div> </div> </div> </div> <script> let itemCount = 1; $('#customer_id').change(function() { const debt = $(this).find(':selected').data('debt') || 0; $('#previousDebt strong').text(debt.toFixed(2) + ' جنيه'); updateSummary(); }); function addItem() { const newItem = ` <div class="row mb-3 item-row"> <div class="col-md-4"> <input type="text" class="form-control" name="items[${itemCount}][product_name]" placeholder="اسم المنتج" required> </div> <div class="col-md-2"> <input type="number" class="form-control item-quantity" name="items[${itemCount}][quantity]" placeholder="الكمية" min="1" required> </div> <div class="col-md-2"> <input type="number" class="form-control item-price" name="items[${itemCount}][unit_price]" placeholder="السعر" step="0.01" min="0.01" required> </div> <div class="col-md-3"> <input type="text" class="form-control item-total" placeholder="الإجمالي" readonly> </div> <div class="col-md-1"> <button type="button" class="btn btn-danger btn-sm" onclick="removeItem(this)"> <i class="bi bi-trash"></i> </button> </div> </div> `; $('#itemsContainer').append(newItem); itemCount++; } function removeItem(button) { $(button).closest('.item-row').remove(); updateSummary(); } $(document).on('input', '.item-quantity, .item-price', function() { const row = $(this).closest('.item-row'); const quantity = parseFloat(row.find('.item-quantity').val()) || 0; const price = parseFloat(row.find('.item-price').val()) || 0; const total = quantity * price; row.find('.item-total').val(total.toFixed(2) + ' جنيه'); updateSummary(); }); function updateSummary() { let invoiceTotal = 0; $('.item-row').each(function() { const quantity = parseFloat($(this).find('.item-quantity').val()) || 0; const price = parseFloat($(this).find('.item-price').val()) || 0; invoiceTotal += quantity * price; }); const previousDebt = parseFloat($('#customer_id').find(':selected').data('debt')) || 0; const newDebt = previousDebt + invoiceTotal; $('#summaryPreviousDebt').text(previousDebt.toFixed(2) + ' جنيه'); $('#summaryInvoiceTotal').text(invoiceTotal.toFixed(2) + ' جنيه'); $('#summaryNewDebt').text(newDebt.toFixed(2) + ' جنيه'); } </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.86 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