48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Core\Database;
|
|
use Exception;
|
|
|
|
final class SubscriptionService
|
|
{
|
|
public function checkLimit(string $tenantId, string $type): void
|
|
{
|
|
$db = Database::getInstance();
|
|
|
|
$stmt = $db->prepare("SELECT * FROM subscriptions WHERE tenant_id = ? LIMIT 1");
|
|
$stmt->execute([$tenantId]);
|
|
$sub = $stmt->fetch();
|
|
|
|
if (!$sub) throw new Exception("لا يوجد اشتراك فعال");
|
|
|
|
if ($type === 'invoices') {
|
|
if ($sub['invoices_used_this_month'] >= $sub['max_invoices_per_month']) {
|
|
throw new Exception("لقد وصلت للحد الأقصى من الفواتير المسموح بها في خطتك الحالية");
|
|
}
|
|
}
|
|
|
|
if ($type === 'companies') {
|
|
$countStmt = $db->prepare("SELECT COUNT(*) as total FROM companies WHERE tenant_id = ? AND deleted_at IS NULL");
|
|
$countStmt->execute([$tenantId]);
|
|
$count = $countStmt->fetch()['total'];
|
|
|
|
if ($count >= $sub['max_companies']) {
|
|
throw new Exception("لقد وصلت للحد الأقصى من الشركات المسموح بها في خطتك الحالية");
|
|
}
|
|
}
|
|
}
|
|
|
|
public function incrementUsage(string $tenantId, string $type): void
|
|
{
|
|
if ($type === 'invoices') {
|
|
$db = Database::getInstance();
|
|
$stmt = $db->prepare("UPDATE subscriptions SET invoices_used_this_month = invoices_used_this_month + 1 WHERE tenant_id = ?");
|
|
$stmt->execute([$tenantId]);
|
|
}
|
|
}
|
|
}
|