[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: functions.php
<?php // حماية الملف define('SECURE_ACCESS', true); session_start(); // Check if user is logged in function isLoggedIn() { return isset($_SESSION['user_id']) && isset($_SESSION['user_role']); } // Check user role function hasRole($required_role) { if (!isLoggedIn()) return false; $allowed_roles = []; switch($required_role) { case 'manager': $allowed_roles = ['manager']; break; case 'accountant': $allowed_roles = ['manager', 'accountant']; break; case 'representative': $allowed_roles = ['manager', 'accountant', 'representative']; break; } return in_array($_SESSION['user_role'], $allowed_roles); } // Redirect if not authorized function requireAuth($role = null) { if (!isLoggedIn()) { header("Location: /login.php"); exit; } if ($role && !hasRole($role)) { header("Location: /unauthorized.php"); exit; } } // Generate invoice number function generateInvoiceNumber() { return 'INV-' . date('Ymd') . '-' . rand(1000, 9999); } // Generate receipt number function generateReceiptNumber() { return 'RCP-' . date('Ymd') . '-' . rand(1000, 9999); } // Generate return number function generateReturnNumber() { return 'RTN-' . date('Ymd') . '-' . rand(1000, 9999); } // Format currency function formatCurrency($amount) { return number_format($amount, 2) . ' جنيه'; } // Format phone number function formatPhoneNumber($phone) { // Remove all non-digits $phone = preg_replace('/[^0-9]/', '', $phone); // Must be exactly 11 digits starting with 01 if (strlen($phone) == 11 && substr($phone, 0, 2) == '01') { return '+20' . $phone; } // Return original if doesn't match format return $phone; } // Validate Egyptian phone number function isValidEgyptianPhone($phone) { // Remove all non-digits first $digits = preg_replace('/[^0-9]/', '', $phone); // Must be exactly 11 digits starting with 01 return strlen($digits) == 11 && substr($digits, 0, 2) == '01'; } // Add audit log entry function addAuditLog($pdo, $user_id, $action_type, $entity_type, $entity_id, $old_value, $new_value, $description) { $stmt = $pdo->prepare(" INSERT INTO audit_log (user_id, action_type, entity_type, entity_id, old_value, new_value, description, ip_address) VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $user_id, $action_type, $entity_type, $entity_id, json_encode($old_value), json_encode($new_value), $description, $_SERVER['REMOTE_ADDR'] ?? 'unknown' ]); } // Get user's active shift function getActiveShift($pdo, $user_id) { $stmt = $pdo->prepare("SELECT * FROM shifts WHERE representative_id = ? AND status = 'active' ORDER BY id DESC LIMIT 1"); $stmt->execute([$user_id]); return $stmt->fetch(); } // Calculate client balance function calculateClientBalance($pdo, $client_id) { // Get total unpaid invoices $stmt = $pdo->prepare("SELECT COALESCE(SUM(remaining_amount), 0) as total_debt FROM invoices WHERE client_id = ?"); $stmt->execute([$client_id]); $debt = $stmt->fetch()['total_debt']; // Update client balance $stmt = $pdo->prepare("UPDATE clients SET balance = ? WHERE id = ?"); $stmt->execute([$debt, $client_id]); return $debt; } // Apply payment to oldest invoices (FIFO) function applyPaymentToInvoices($pdo, $client_id, $payment_amount) { // Get unpaid/partially paid invoices ordered by date $stmt = $pdo->prepare(" SELECT * FROM invoices WHERE client_id = ? AND remaining_amount > 0 ORDER BY created_at ASC "); $stmt->execute([$client_id]); $invoices = $stmt->fetchAll(); $remaining_payment = $payment_amount; $applied_invoices = []; foreach ($invoices as $invoice) { if ($remaining_payment <= 0) break; $amount_to_apply = min($remaining_payment, $invoice['remaining_amount']); $new_remaining = $invoice['remaining_amount'] - $amount_to_apply; // Update invoice $stmt = $pdo->prepare("UPDATE invoices SET remaining_amount = ?, status = ? WHERE id = ?"); $new_status = $new_remaining == 0 ? 'paid' : ($new_remaining < $invoice['invoice_total'] ? 'partially_paid' : 'unpaid'); $stmt->execute([$new_remaining, $new_status, $invoice['id']]); $applied_invoices[] = [ 'invoice_id' => $invoice['id'], 'invoice_number' => $invoice['invoice_number'], 'amount_applied' => $amount_to_apply ]; $remaining_payment -= $amount_to_apply; } return $applied_invoices; } // Generate WhatsApp URL for invoice function generateInvoiceWhatsAppURL($invoice, $client_phone) { $message = "فاتورة مبيعات\n\n" . "رقم الفاتورة: " . $invoice['invoice_number'] . "\n" . "التاريخ: " . date('Y-m-d', strtotime($invoice['created_at'])) . "\n\n" . "تفاصيل الفاتورة:\n" . "اجمالي الفاتورة: " . number_format($invoice['invoice_total'], 2) . " جنيه\n" . "المبلغ المدفوع: " . number_format($invoice['amount_paid_at_creation'], 2) . " جنيه\n" . "المبلغ المتبقي: " . number_format($invoice['remaining_amount'], 2) . " جنيه\n\n" . "حالة الحساب:\n" . "الرصيد السابق: " . number_format($invoice['previous_balance'], 2) . " جنيه\n" . "اجمالي المديونية: " . number_format($invoice['new_total_debt'], 2) . " جنيه\n\n" . "حسابات عربية بن فريش\n" . "شكرا لتعاملكم معنا"; return "https://wa.me/" . str_replace('+', '', $client_phone) . "?text=" . urlencode($message); } // Generate WhatsApp URL for payment receipt function generatePaymentWhatsAppURL($payment, $client_phone) { // Handle missing fields with default values $receipt_number = $payment['receipt_number'] ?? $payment['number'] ?? 'غير محدد'; $created_at = $payment['created_at'] ?? date('Y-m-d H:i:s'); $amount = $payment['amount'] ?? 0; $previous_balance = $payment['previous_balance'] ?? 0; $new_balance = $payment['new_balance'] ?? 0; $message = "ايصال استلام\n\n" . "رقم الايصال: " . $receipt_number . "\n" . "التاريخ: " . date('Y-m-d', strtotime($created_at)) . "\n\n" . "تفاصيل الدفعة:\n" . "المبلغ المستلم: " . number_format($amount, 2) . " جنيه\n\n" . "حالة الحساب:\n" . "الرصيد السابق: " . number_format($previous_balance, 2) . " جنيه\n" . "الرصيد الحالي: " . number_format($new_balance, 2) . " جنيه\n\n" . "تم استلام المبلغ بنجاح\n\n" . "حسابات عربية بن فريش\n" . "شكرا لتعاملكم معنا"; return "https://wa.me/" . str_replace('+', '', $client_phone) . "?text=" . urlencode($message); } // Generate WhatsApp URL for return function generateReturnWhatsAppURL($return_data, $client_phone) { // Handle missing fields with default values $processed_at = $return_data['processed_at'] ?? $return_data['created_at'] ?? date('Y-m-d H:i:s'); $return_value = $return_data['return_value'] ?? $return_data['amount'] ?? 0; $client_balance_before = $return_data['client_balance_before'] ?? $return_data['previous_balance'] ?? 0; $client_balance_after = $return_data['client_balance_after'] ?? $return_data['new_balance'] ?? 0; $product_name = $return_data['product_name'] ?? 'منتج'; $quantity = $return_data['quantity'] ?? 0; $return_number = $return_data['return_number'] ?? $return_data['number'] ?? 'غير محدد'; $message = "اشعار مرتجع\n\n" . "رقم المرتجع: " . $return_number . "\n" . "التاريخ: " . date('Y-m-d', strtotime($processed_at)) . "\n\n" . "تفاصيل المرتجع:\n" . "المنتج: " . $product_name . "\n" . "الكمية: " . $quantity . "\n" . "قيمة المرتجع: " . number_format($return_value, 2) . " جنيه\n\n" . "حالة الحساب:\n" . "الرصيد السابق: " . number_format($client_balance_before, 2) . " جنيه\n" . "الرصيد الحالي: " . number_format($client_balance_after, 2) . " جنيه\n\n" . "تم قبول المرتجع بنجاح\n\n" . "حسابات عربية بن فريش\n" . "شكرا لتعاملكم معنا"; return "https://wa.me/" . str_replace('+', '', $client_phone) . "?text=" . urlencode($message); } // Get current timestamp in Arabic format function getArabicDateTime($timestamp = null) { if (!$timestamp) $timestamp = time(); return date('Y-m-d H:i', $timestamp); } ?>
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.87 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