Update: 2026-05-05 00:01:17
This commit is contained in:
95
app/modules_app/subscriptions/assign.php
Normal file
95
app/modules_app/subscriptions/assign.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Assign/Upgrade Subscription Plan (Super Admin only)
|
||||
* POST /api/v1/subscriptions/assign
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
|
||||
// Only Super Admin can change plans manually via this API
|
||||
if ($decoded['role'] !== 'super_admin') {
|
||||
json_error('غير مصرح لك بتغيير الباقات. يرجى التواصل مع الدعم الفني.', 403);
|
||||
}
|
||||
|
||||
$data = input();
|
||||
$targetTenantId = $data['tenant_id'] ?? null;
|
||||
$planId = $data['plan_id'] ?? null;
|
||||
$durationDays = (int)($data['duration_days'] ?? 30);
|
||||
|
||||
if (!$targetTenantId || !$planId) {
|
||||
json_error('معرف المكتب ومعرف الباقة مطلوبان.', 422);
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
// 1. Validate Plan
|
||||
$stmt = $db->prepare("SELECT * FROM subscription_plans WHERE id = ? AND is_active = 1");
|
||||
$stmt->execute([$planId]);
|
||||
$plan = $stmt->fetch();
|
||||
|
||||
if (!$plan) {
|
||||
json_error('الباقة المختارة غير صالحة أو غير نشطة.', 422);
|
||||
}
|
||||
|
||||
// 2. Update or Create Subscription
|
||||
$db->beginTransaction();
|
||||
|
||||
$startDate = date('Y-m-d H:i:s');
|
||||
$endDate = date('Y-m-d H:i:s', strtotime("+{$durationDays} days"));
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO subscriptions (
|
||||
tenant_id, plan_id, max_companies, max_invoices_per_month, max_users,
|
||||
price_jod, status, current_period_start, current_period_end, updated_at
|
||||
) VALUES (
|
||||
:t_id, :p_id, :max_c, :max_i, :max_u, :price, 'active', :start, :end, NOW()
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
plan_id = VALUES(plan_id),
|
||||
max_companies = VALUES(max_companies),
|
||||
max_invoices_per_month = VALUES(max_invoices_per_month),
|
||||
max_users = VALUES(max_users),
|
||||
price_jod = VALUES(price_jod),
|
||||
status = 'active',
|
||||
current_period_start = VALUES(current_period_start),
|
||||
current_period_end = VALUES(current_period_end),
|
||||
updated_at = NOW()
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
't_id' => $targetTenantId,
|
||||
'p_id' => $planId,
|
||||
'max_c' => $plan['max_companies'],
|
||||
'max_i' => $plan['max_invoices_month'],
|
||||
'max_u' => $plan['max_users'],
|
||||
'price' => $plan['price_jod'],
|
||||
'start' => $startDate,
|
||||
'end' => $endDate
|
||||
]);
|
||||
|
||||
// 3. Log the change
|
||||
$logStmt = $db->prepare("INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, details) VALUES (?, ?, 'plan_assigned', 'tenant', ?, ?)");
|
||||
$logStmt->execute([
|
||||
$targetTenantId,
|
||||
$decoded['user_id'],
|
||||
$targetTenantId,
|
||||
json_encode(['plan_id' => $planId, 'assigned_by' => $decoded['user_id']])
|
||||
]);
|
||||
|
||||
$db->commit();
|
||||
|
||||
json_success([
|
||||
'tenant_id' => $targetTenantId,
|
||||
'plan_id' => $planId,
|
||||
'period_end' => $endDate
|
||||
], 'تم تحديث باقة الاشتراك بنجاح');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
if ($db->inTransaction()) $db->rollBack();
|
||||
error_log("Subscription Assign Error: " . $e->getMessage());
|
||||
json_error('حدث خطأ أثناء تعيين الباقة: ' . $e->getMessage(), 500);
|
||||
}
|
||||
25
app/modules_app/subscriptions/current.php
Normal file
25
app/modules_app/subscriptions/current.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* Get Current Tenant Subscription & Usage
|
||||
* GET /api/v1/subscriptions/current
|
||||
*/
|
||||
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\Middleware\QuotaMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
|
||||
try {
|
||||
$usage = QuotaMiddleware::getUsageSummary($tenantId);
|
||||
|
||||
if (!$usage['has_subscription']) {
|
||||
json_error('لم يتم العثور على اشتراك نشط لهذا المكتب.', 404);
|
||||
}
|
||||
|
||||
json_success($usage, 'تفاصيل الاشتراك الحالي');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("Subscription Current Error: " . $e->getMessage());
|
||||
json_error('حدث خطأ أثناء جلب بيانات الاشتراك', 500);
|
||||
}
|
||||
52
app/modules_app/subscriptions/plans.php
Normal file
52
app/modules_app/subscriptions/plans.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* List Available Subscription Plans
|
||||
* GET /api/v1/subscriptions/plans
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
|
||||
// No auth required — public endpoint for pricing page
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
$stmt = $db->query("
|
||||
SELECT id, name_ar, name_en, max_companies, max_invoices_month, max_users,
|
||||
price_jod, ai_features, jofotara_enabled, sort_order
|
||||
FROM subscription_plans
|
||||
WHERE is_active = 1
|
||||
ORDER BY sort_order ASC
|
||||
");
|
||||
$plans = $stmt->fetchAll();
|
||||
|
||||
// Merge with config features (for richer display)
|
||||
$configPlans = require APP_PATH . '/config/plans.php';
|
||||
|
||||
foreach ($plans as &$plan) {
|
||||
$configPlan = $configPlans[$plan['id']] ?? null;
|
||||
if ($configPlan) {
|
||||
$plan['description_ar'] = $configPlan['description_ar'] ?? '';
|
||||
$plan['features'] = $configPlan['features'] ?? [];
|
||||
$plan['badge_color'] = $configPlan['badge_color'] ?? 'gray';
|
||||
$plan['is_popular'] = $configPlan['is_popular'] ?? false;
|
||||
}
|
||||
// Cast numeric fields
|
||||
$plan['max_companies'] = (int)$plan['max_companies'];
|
||||
$plan['max_invoices_month'] = (int)$plan['max_invoices_month'];
|
||||
$plan['max_users'] = (int)$plan['max_users'];
|
||||
$plan['price_jod'] = (float)$plan['price_jod'];
|
||||
$plan['ai_features'] = (bool)$plan['ai_features'];
|
||||
$plan['jofotara_enabled'] = (bool)$plan['jofotara_enabled'];
|
||||
}
|
||||
|
||||
json_success($plans, 'الباقات المتاحة');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("Subscription Plans Error: " . $e->getMessage());
|
||||
|
||||
// Fallback to config file
|
||||
$configPlans = require APP_PATH . '/config/plans.php';
|
||||
$fallback = array_values($configPlans);
|
||||
json_success($fallback, 'الباقات المتاحة (من الإعدادات)');
|
||||
}
|
||||
58
app/modules_app/subscriptions/usage.php
Normal file
58
app/modules_app/subscriptions/usage.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Detailed Usage Statistics (for Charts/Stats)
|
||||
* GET /api/v1/subscriptions/usage
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
try {
|
||||
// 1. Monthly growth (Invoices over last 6 months)
|
||||
$stmt = $db->prepare("
|
||||
SELECT DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count
|
||||
FROM invoices
|
||||
WHERE tenant_id = ? AND created_at >= DATE_SUB(NOW(), INTERVAL 6 MONTH)
|
||||
GROUP BY month
|
||||
ORDER BY month ASC
|
||||
");
|
||||
$stmt->execute([$tenantId]);
|
||||
$monthlyInvoices = $stmt->fetchAll();
|
||||
|
||||
// 2. Usage by company
|
||||
$stmt = $db->prepare("
|
||||
SELECT c.name, COUNT(i.id) as count
|
||||
FROM companies c
|
||||
LEFT JOIN invoices i ON i.company_id = c.id
|
||||
WHERE c.tenant_id = ? AND c.deleted_at IS NULL
|
||||
GROUP BY c.id
|
||||
ORDER BY count DESC
|
||||
");
|
||||
$stmt->execute([$tenantId]);
|
||||
$usageByCompany = $stmt->fetchAll();
|
||||
|
||||
// 3. Status distribution
|
||||
$stmt = $db->prepare("
|
||||
SELECT status, COUNT(*) as count
|
||||
FROM invoices
|
||||
WHERE tenant_id = ?
|
||||
GROUP BY status
|
||||
");
|
||||
$stmt->execute([$tenantId]);
|
||||
$statusDistribution = $stmt->fetchAll();
|
||||
|
||||
json_success([
|
||||
'monthly_growth' => $monthlyInvoices,
|
||||
'usage_by_company' => $usageByCompany,
|
||||
'status_distribution' => $statusDistribution
|
||||
], 'إحصائيات الاستهلاك');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("Usage Stats Error: " . $e->getMessage());
|
||||
json_error('حدث خطأ أثناء جلب إحصائيات الاستهلاك', 500);
|
||||
}
|
||||
Reference in New Issue
Block a user