Update: 2026-05-15 15:02:14

This commit is contained in:
Hamza-Ayed
2026-05-15 15:02:14 +03:00
parent 54a4acdcab
commit 7ee897ff3d
6 changed files with 277 additions and 45 deletions

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace App\Middleware;
use App\Core\Database;
use App\Core\Cache;
final class QuotaMiddleware
{
@@ -22,17 +23,26 @@ final class QuotaMiddleware
*/
public static function checkInvoiceQuota(string $tenantId): array
{
$db = Database::getInstance();
$cacheKey = "quota_sub_{$tenantId}";
$sub = Cache::get($cacheKey);
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub === false || $sub === null) {
$db = Database::getInstance();
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub) {
Cache::set($cacheKey, $sub, 300); // Cache for 5 minutes
}
}
if (!$sub) {
json_error('لا يوجد اشتراك فعّال لهذا المكتب. يرجى التواصل مع الإدارة.', 403);
@@ -100,6 +110,9 @@ final class QuotaMiddleware
WHERE tenant_id = ?
");
$stmt->execute([$tenantId]);
// Invalidate cache
Cache::forget("quota_sub_{$tenantId}");
}
/**

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace App\Middleware;
use App\Core\Database;
use App\Core\Cache;
final class QuotaMiddleware
{
@@ -22,17 +23,26 @@ final class QuotaMiddleware
*/
public static function checkInvoiceQuota(string $tenantId): array
{
$db = Database::getInstance();
$cacheKey = "quota_sub_{$tenantId}";
$sub = Cache::get($cacheKey);
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub === false || $sub === null) {
$db = Database::getInstance();
// Fetch subscription with plan info
$stmt = $db->prepare("
SELECT s.*, sp.name_ar as plan_name, sp.ai_features, sp.jofotara_enabled
FROM subscriptions s
LEFT JOIN subscription_plans sp ON s.plan_id = sp.id
WHERE s.tenant_id = ?
");
$stmt->execute([$tenantId]);
$sub = $stmt->fetch();
if ($sub) {
Cache::set($cacheKey, $sub, 300); // Cache for 5 minutes
}
}
if (!$sub) {
json_error('لا يوجد اشتراك فعّال لهذا المكتب. يرجى التواصل مع الإدارة.', 403);
@@ -100,6 +110,9 @@ final class QuotaMiddleware
WHERE tenant_id = ?
");
$stmt->execute([$tenantId]);
// Invalidate cache
Cache::forget("quota_sub_{$tenantId}");
}
/**

View File

@@ -20,6 +20,7 @@ use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
// Enable error reporting for debugging
ini_set('display_errors', '1');
@@ -148,7 +149,25 @@ $summarySheet->getStyle("A1")->applyFromArray([
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $headerBg]],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
]);
$summarySheet->getRowDimension(1)->setRowHeight(40);
$summarySheet->getRowDimension(1)->setRowHeight(45);
// --- Add Logo ---
$logoSummary = new Drawing();
$logoSummary->setName('Musadaq Logo');
$logoSummary->setPath(ROOT_PATH . '/public/assets/img/logo.jpg');
$logoSummary->setHeight(38);
$logoSummary->setCoordinates('A1');
$logoSummary->setOffsetX(15);
$logoSummary->setOffsetY(5);
$logoSummary->setWorksheet($summarySheet);
// --- Add Clickable Website Link ---
$summarySheet->setCellValue("J1", 'musadaq.intaleqapp.com');
$summarySheet->getCell("J1")->getHyperlink()->setUrl('https://musadaq.intaleqapp.com/');
$summarySheet->getStyle("J1")->applyFromArray([
'font' => ['color' => ['argb' => 'FFFFFFFF'], 'underline' => true, 'size' => 9],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_LEFT, 'vertical' => Alignment::VERTICAL_CENTER],
]);
// Summary Meta Info
$companyNameFilter = 'جميع الشركات';
@@ -269,7 +288,41 @@ foreach ($invoices as $invIdx => $inv) {
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF' . $headerBg]],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
]);
$sheet->getRowDimension($invRow)->setRowHeight(40);
$sheet->getRowDimension($invRow)->setRowHeight(45);
// --- Add Logo ---
$logoInv = new Drawing();
$logoInv->setName('Musadaq Logo');
$logoInv->setPath(ROOT_PATH . '/public/assets/img/logo.jpg');
$logoInv->setHeight(38);
$logoInv->setCoordinates('A' . $invRow);
$logoInv->setOffsetX(15);
$logoInv->setOffsetY(5);
$logoInv->setWorksheet($sheet);
// --- Add Clickable Website Link ---
$sheet->setCellValue("I" . $invRow, 'musadaq.intaleqapp.com');
$sheet->getCell("I" . $invRow)->getHyperlink()->setUrl('https://musadaq.intaleqapp.com/');
$sheet->getStyle("I" . $invRow)->applyFromArray([
'font' => ['color' => ['argb' => 'FFFFFFFF'], 'underline' => true, 'size' => 9],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_LEFT, 'vertical' => Alignment::VERTICAL_CENTER],
]);
// --- Add Verification QR Code ---
$verifyUrl = "https://musadaq.intaleqapp.com/index.php?route=v1/verify&id=" . $inv['id'];
$qrApiUrl = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=" . urlencode($verifyUrl);
// Download QR to temp file
$tmpQr = tempnam(sys_get_temp_dir(), 'qr_');
file_put_contents($tmpQr, file_get_contents($qrApiUrl));
$drawingQr = new Drawing();
$drawingQr->setName('Verification QR');
$drawingQr->setPath($tmpQr);
$drawingQr->setHeight(70);
$drawingQr->setCoordinates('H' . ($invRow + 2)); // Place below the headers area
$drawingQr->setWorksheet($sheet);
$invRow++;
// Invoice meta data

View File

@@ -0,0 +1,172 @@
<?php
/**
* Public Invoice Verification Page
* GET /v1/verify?id=INVOICE_ID
*/
use App\Core\Database;
use App\Core\Encryption;
try {
$invoiceId = $_GET['id'] ?? null;
if (!$invoiceId) {
die("<h1>رابط التحقق غير صالح</h1>");
}
$db = Database::getInstance();
// Fetch invoice with company and supplier details
$stmt = $db->prepare("
SELECT i.*, c.name as company_name_raw
FROM invoices i
JOIN companies c ON i.company_id = c.id
WHERE i.id = ? AND i.deleted_at IS NULL
");
$stmt->execute([$invoiceId]);
$invoice = $stmt->fetch();
if (!$invoice) {
die("<h1>الفاتورة غير موجودة أو تم حذفها</h1>");
}
// Decrypt helper
$dec = function($val) {
if (empty($val)) return '-';
$result = Encryption::decrypt((string)$val);
return ($result !== false && $result !== null) ? $result : (string)$val;
};
$supplierName = $dec($invoice['supplier_name']);
$companyName = $dec($invoice['company_name_raw']);
$total = number_format((float)$invoice['grand_total'], 3);
$date = $invoice['invoice_date'] ?: 'غير محدد';
$status = match($invoice['status']) {
'extracted' => 'مستخرجة',
'approved' => 'معتمدة ✅',
'submitted' => 'مقدمة للضريبة 🏛️',
'rejected' => 'مرفوضة ❌',
default => 'قيد المعالجة'
};
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>التحقق من الفاتورة - مُصادَق</title>
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--primary: #1C1550;
--accent: #00D1B2;
--bg: #F8F9FA;
}
body {
font-family: 'Tajawal', sans-serif;
background-color: var(--bg);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: #333;
}
.verify-card {
background: white;
padding: 30px;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
max-width: 450px;
width: 90%;
text-align: center;
}
.logo {
font-size: 28px;
font-weight: bold;
color: var(--primary);
margin-bottom: 20px;
}
.status-badge {
display: inline-block;
padding: 8px 20px;
border-radius: 50px;
background: #E9ECEF;
font-weight: bold;
margin-bottom: 25px;
color: var(--primary);
}
.info-grid {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
text-align: right;
border-top: 1px solid #EEE;
padding-top: 20px;
}
.info-item label {
font-size: 13px;
color: #888;
display: block;
margin-bottom: 4px;
}
.info-item span {
font-size: 16px;
font-weight: 700;
color: var(--primary);
}
.footer-note {
margin-top: 30px;
font-size: 12px;
color: #AAA;
}
.btn-home {
margin-top: 20px;
display: inline-block;
text-decoration: none;
color: var(--accent);
font-weight: bold;
}
</style>
</head>
<body>
<div class="verify-card">
<div class="logo">مُـصَـادَق</div>
<div class="status-badge"><?php echo $status; ?></div>
<div class="info-grid">
<div class="info-item">
<label>اسم المكتب (الشركة)</label>
<span><?php echo htmlspecialchars($companyName); ?></span>
</div>
<div class="info-item">
<label>اسم المورّد</label>
<span><?php echo htmlspecialchars($supplierName); ?></span>
</div>
<div class="info-item">
<label>رقم الفاتورة</label>
<span><?php echo htmlspecialchars($invoice['invoice_number'] ?: '-'); ?></span>
</div>
<div class="info-item">
<label>تاريخ الفاتورة</label>
<span><?php echo htmlspecialchars($date); ?></span>
</div>
<div class="info-item">
<label>المبلغ الإجمالي</label>
<span style="font-size: 24px; color: var(--accent);"><?php echo $total; ?> JOD</span>
</div>
</div>
<div class="footer-note">
تم التحقق من هذه الفاتورة رسمياً عبر منصة مُصادَق.<br>
<?php echo date('Y-m-d H:i:s'); ?>
</div>
<a href="https://musadaq.intaleqapp.com/" class="btn-home">زيارة منصة مُصادَق</a>
</div>
</body>
</html>
<?php
} catch (\Exception $e) {
die("خطأ في النظام");
}

View File

@@ -29,6 +29,7 @@ $routes = [
'v1/companies/delete' => ['POST', 'companies/delete.php'],
'v1/invoices' => ['GET', 'invoices/index.php'],
'v1/invoices/view' => ['GET', 'invoices/view.php'],
'v1/verify' => ['GET', 'invoices/verify_public.php'],
'v1/invoices/file' => ['GET', 'invoices/file.php'],
'v1/invoices/approve' => ['POST', 'invoices/approve.php'],
'v1/invoices/upload' => ['POST', 'invoices/upload.php'],

View File

@@ -3039,29 +3039,9 @@
getQrSrc(inv) {
if (!inv) return '';
if (inv.jofotara?.qr_image_uri) return inv.jofotara.qr_image_uri;
let qrData = inv.qr_code;
// If no QR data in DB but approved, generate a fallback data string
if (!qrData && inv.status === 'approved') {
qrData = `Invoice: ${inv.invoice_number || 'N/A'}\nSupplier: ${inv.supplier_name || 'N/A'}\nTotal: ${inv.grand_total || '0'} JOD\nDate: ${inv.invoice_date || ''}`;
}
if (qrData) {
if (qrData.startsWith('data:')) return qrData;
try {
const qr = new QRious({
value: qrData,
size: 300,
level: 'M'
});
return qr.toDataURL();
} catch (e) {
console.error('QR Gen Error:', e);
return '';
}
}
return '';
const verifyUrl = `https://musadaq.intaleqapp.com/index.php?route=v1/verify&id=${inv.id}`;
const qr = new QRious({ value: verifyUrl, size: 300, level: 'H' });
return qr.toDataURL();
},
async showCompanyStats(companyId) {