diff --git a/app/Middleware/QuotaMiddleware.php b/app/Middleware/QuotaMiddleware.php index fa09e02..56271b6 100644 --- a/app/Middleware/QuotaMiddleware.php +++ b/app/Middleware/QuotaMiddleware.php @@ -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}"); } /** diff --git a/app/middleware/QuotaMiddleware.php b/app/middleware/QuotaMiddleware.php index fa09e02..56271b6 100644 --- a/app/middleware/QuotaMiddleware.php +++ b/app/middleware/QuotaMiddleware.php @@ -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}"); } /** diff --git a/app/modules_app/invoices/export_excel.php b/app/modules_app/invoices/export_excel.php index f251352..21d333c 100644 --- a/app/modules_app/invoices/export_excel.php +++ b/app/modules_app/invoices/export_excel.php @@ -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 diff --git a/app/modules_app/invoices/verify_public.php b/app/modules_app/invoices/verify_public.php new file mode 100644 index 0000000..b326f01 --- /dev/null +++ b/app/modules_app/invoices/verify_public.php @@ -0,0 +1,172 @@ +رابط التحقق غير صالح"); + } + + $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("