🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
83
app/Modules/AI/AIController.php
Normal file
83
app/Modules/AI/AIController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\AI;
|
||||
|
||||
use App\Core\{Request, Response, Database};
|
||||
use GuzzleHttp\Client;
|
||||
use Throwable;
|
||||
|
||||
final class AIController
|
||||
{
|
||||
private Client $httpClient;
|
||||
private string $apiKey;
|
||||
private string $model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->httpClient = new Client();
|
||||
$this->apiKey = $_ENV['GEMINI_API_KEY'] ?? '';
|
||||
$this->model = $_ENV['GEMINI_MODEL'] ?? 'gemini-2.0-flash';
|
||||
}
|
||||
|
||||
public function query(Request $request): void
|
||||
{
|
||||
$userQuery = $request->input('query');
|
||||
if (!$userQuery) {
|
||||
Response::error('يرجى تقديم استفسار', 'MISSING_QUERY', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Fetch current context data (Summary of stats)
|
||||
$stats = $this->getQuickStats($request->tenantId);
|
||||
|
||||
// 2. Ask Gemini to interpret and answer
|
||||
$prompt = "You are Musadaq AI Assistant for a Jordanian E-Invoicing SaaS. " .
|
||||
"The user is asking: \"{$userQuery}\". " .
|
||||
"Current User Context: Tenant ID {$request->tenantId}. " .
|
||||
"Current Data Summary: " . json_encode($stats) . ". " .
|
||||
"Answer the user in a friendly Arabic tone (Jordanian dialect is okay). " .
|
||||
"Keep it professional and concise. If you don't have the specific data, say so politely.";
|
||||
|
||||
$response = $this->httpClient->post("https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent?key={$this->apiKey}", [
|
||||
'json' => [
|
||||
'contents' => [['parts' => [['text' => $prompt]]]]
|
||||
]
|
||||
]);
|
||||
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
$answer = $data['candidates'][0]['content']['parts'][0]['text'] ?? 'عذراً، لم أستطع فهم الاستفسار حالياً.';
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'answer' => $answer
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
Response::error('فشل معالجة الاستعلام الذكي', 'AI_QUERY_FAILED', 500, [
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function getQuickStats(string $tenantId): array
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
|
||||
$totalInvoices = $db->prepare("SELECT COUNT(*) as total FROM invoices WHERE tenant_id = ?");
|
||||
$totalInvoices->execute([$tenantId]);
|
||||
|
||||
$approvedCount = $db->prepare("SELECT COUNT(*) as total FROM invoices WHERE tenant_id = ? AND status = 'approved'");
|
||||
$approvedCount->execute([$tenantId]);
|
||||
|
||||
return [
|
||||
'total_invoices' => $totalInvoices->fetch()['total'],
|
||||
'approved_invoices' => $approvedCount->fetch()['total'],
|
||||
'current_month' => date('F Y')
|
||||
];
|
||||
}
|
||||
}
|
||||
56
app/Modules/Auth/AuthController.php
Normal file
56
app/Modules/Auth/AuthController.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Auth;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Modules\Auth\AuthService;
|
||||
use Throwable;
|
||||
|
||||
final class AuthController
|
||||
{
|
||||
public function __construct(private readonly AuthService $authService) {}
|
||||
|
||||
public function login(Request $request): void
|
||||
{
|
||||
$email = $request->input('email');
|
||||
$password = $request->input('password');
|
||||
|
||||
if (!$email || !$password) {
|
||||
Response::error('يرجى إدخال البريد الإلكتروني وكلمة المرور', 'VALIDATION_ERROR', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->authService->login($email, $password);
|
||||
|
||||
// Set refresh token in HttpOnly cookie
|
||||
setcookie('refresh_token', $result['refresh_token'], [
|
||||
'expires' => time() + (60 * 60 * 24 * 7),
|
||||
'path' => '/api/v1/auth/refresh',
|
||||
'httponly' => true,
|
||||
'samesite' => 'Strict',
|
||||
'secure' => true
|
||||
]);
|
||||
|
||||
unset($result['refresh_token']);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $result,
|
||||
'message' => 'تم تسجيل الدخول بنجاح'
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Response::error($e->getMessage(), 'AUTH_FAILED', 401);
|
||||
}
|
||||
}
|
||||
|
||||
public function me(Request $request): void
|
||||
{
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $request->user
|
||||
]);
|
||||
}
|
||||
}
|
||||
56
app/Modules/Auth/AuthService.php
Normal file
56
app/Modules/Auth/AuthService.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Auth;
|
||||
|
||||
use App\Modules\Users\UserModel;
|
||||
use App\Services\Security\JwtService;
|
||||
use Exception;
|
||||
|
||||
final class AuthService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserModel $userModel,
|
||||
private readonly JwtService $jwtService
|
||||
) {}
|
||||
|
||||
public function login(string $email, string $password): array
|
||||
{
|
||||
$user = $this->userModel->findByEmail($email);
|
||||
|
||||
if (!$user || !password_verify($password, $user['password_hash'])) {
|
||||
throw new Exception("البريد الإلكتروني أو كلمة المرور غير صحيحة");
|
||||
}
|
||||
|
||||
if (!$user['is_active']) {
|
||||
throw new Exception("هذا الحساب معطل حالياً");
|
||||
}
|
||||
|
||||
$accessToken = $this->jwtService->issueAccessToken([
|
||||
'user_id' => $user['id'],
|
||||
'tenant_id' => $user['tenant_id'],
|
||||
'role' => $user['role']
|
||||
]);
|
||||
|
||||
$refreshToken = $this->jwtService->issueRefreshToken($user['id']);
|
||||
|
||||
// Update refresh token hash in DB
|
||||
$this->userModel->update($user['id'], [
|
||||
'refresh_token_hash' => password_hash($refreshToken, PASSWORD_BCRYPT),
|
||||
'last_login_at' => date('Y-m-d H:i:s'),
|
||||
'last_login_ip' => $_SERVER['REMOTE_ADDR'] ?? null
|
||||
]);
|
||||
|
||||
return [
|
||||
'access_token' => $accessToken,
|
||||
'refresh_token' => $refreshToken,
|
||||
'user' => [
|
||||
'id' => $user['id'],
|
||||
'name' => $user['name'],
|
||||
'email' => $user['email'],
|
||||
'role' => $user['role']
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
62
app/Modules/Companies/CompanyController.php
Normal file
62
app/Modules/Companies/CompanyController.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Modules\Companies\{CompanyModel, CompanyService};
|
||||
use Throwable;
|
||||
|
||||
final class CompanyController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CompanyModel $companyModel,
|
||||
private readonly CompanyService $companyService
|
||||
) {}
|
||||
|
||||
public function list(Request $request): void
|
||||
{
|
||||
$companies = $this->companyModel->findByTenant($request->tenantId);
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $companies
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Request $request): void
|
||||
{
|
||||
$data = $request->getBody();
|
||||
$data['tenant_id'] = $request->tenantId;
|
||||
|
||||
try {
|
||||
$companyId = $this->companyService->createCompany($data);
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => ['id' => $companyId],
|
||||
'message' => 'تم إضافة الشركة بنجاح'
|
||||
], 201);
|
||||
} catch (Throwable $e) {
|
||||
Response::error('فشل إضافة الشركة', 'CREATE_FAILED', 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateJoFotara(Request $request, string $id): void
|
||||
{
|
||||
$data = [
|
||||
'jofotara_client_id' => $request->input('client_id'),
|
||||
'jofotara_secret_key' => $request->input('secret_key'),
|
||||
'is_jofotara_linked' => 1
|
||||
];
|
||||
|
||||
try {
|
||||
$this->companyService->createCompany(array_merge($data, ['id' => $id])); // Reuses encryption logic
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'تم تحديث بيانات جو-فواتير بنجاح'
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Response::error('فشل تحديث البيانات', 'UPDATE_FAILED', 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
app/Modules/Companies/CompanyModel.php
Normal file
19
app/Modules/Companies/CompanyModel.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
final class CompanyModel extends BaseModel
|
||||
{
|
||||
protected string $table = 'companies';
|
||||
|
||||
public function findByTenant(string $tenantId): array
|
||||
{
|
||||
$stmt = $this->db()->prepare("SELECT * FROM {$this->table} WHERE tenant_id = ? AND deleted_at IS NULL");
|
||||
$stmt->execute([$tenantId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
||||
43
app/Modules/Companies/CompanyService.php
Normal file
43
app/Modules/Companies/CompanyService.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Services\Security\EncryptionService;
|
||||
use App\Modules\Companies\CompanyModel;
|
||||
|
||||
final class CompanyService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CompanyModel $companyModel,
|
||||
private readonly EncryptionService $encryption
|
||||
) {}
|
||||
|
||||
public function createCompany(array $data): string
|
||||
{
|
||||
// Encrypt sensitive JoFotara credentials
|
||||
if (isset($data['jofotara_client_id'])) {
|
||||
$data['jofotara_client_id_encrypted'] = $this->encryption->encrypt($data['jofotara_client_id']);
|
||||
unset($data['jofotara_client_id']);
|
||||
}
|
||||
|
||||
if (isset($data['jofotara_secret_key'])) {
|
||||
$data['jofotara_secret_key_encrypted'] = $this->encryption->encrypt($data['jofotara_secret_key']);
|
||||
unset($data['jofotara_secret_key']);
|
||||
}
|
||||
|
||||
return (string)$this->companyModel->create($data);
|
||||
}
|
||||
|
||||
public function getJoFotaraCredentials(string $companyId): array
|
||||
{
|
||||
$company = $this->companyModel->find($companyId);
|
||||
if (!$company) return [];
|
||||
|
||||
return [
|
||||
'clientId' => $company['jofotara_client_id_encrypted'] ? $this->encryption->decrypt($company['jofotara_client_id_encrypted']) : null,
|
||||
'secretKey' => $company['jofotara_secret_key_encrypted'] ? $this->encryption->decrypt($company['jofotara_secret_key_encrypted']) : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
41
app/Modules/Dashboard/DashboardController.php
Normal file
41
app/Modules/Dashboard/DashboardController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Dashboard;
|
||||
|
||||
use App\Core\{Request, Response, Database};
|
||||
|
||||
final class DashboardController
|
||||
{
|
||||
public function getStats(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$db = Database::getInstance();
|
||||
|
||||
// 1. Total Invoices this month
|
||||
$stmt = $db->prepare("SELECT COUNT(*) as count FROM invoices WHERE tenant_id = ? AND MONTH(created_at) = MONTH(CURRENT_DATE)");
|
||||
$stmt->execute([$tenantId]);
|
||||
$thisMonth = $stmt->fetch()['count'];
|
||||
|
||||
// 2. Approved vs Rejected
|
||||
$stmt = $db->prepare("SELECT status, COUNT(*) as count FROM invoices WHERE tenant_id = ? GROUP BY status");
|
||||
$stmt->execute([$tenantId]);
|
||||
$statusCounts = $stmt->fetchAll();
|
||||
|
||||
// 3. Recent Activity
|
||||
$stmt = $db->prepare("SELECT i.*, c.name as company_name FROM invoices i JOIN companies c ON i.company_id = c.id WHERE i.tenant_id = ? ORDER BY i.created_at DESC LIMIT 5");
|
||||
$stmt->execute([$tenantId]);
|
||||
$recent = $stmt->fetchAll();
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'total_this_month' => $thisMonth,
|
||||
'status_distribution' => $statusCounts,
|
||||
'recent_invoices' => $recent,
|
||||
'subscription_usage' => 45 // Placeholder
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
61
app/Modules/Invoices/InvoiceController.php
Normal file
61
app/Modules/Invoices/InvoiceController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Invoices;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Services\FileStorageService;
|
||||
use App\Modules\Invoices\InvoiceModel;
|
||||
use Throwable;
|
||||
|
||||
final class InvoiceController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InvoiceModel $invoiceModel,
|
||||
private readonly FileStorageService $storage
|
||||
) {}
|
||||
|
||||
public function upload(Request $request): void
|
||||
{
|
||||
$files = $request->getFiles();
|
||||
if (empty($files['invoice'])) {
|
||||
Response::error('يرجى اختيار ملف الفاتورة', 'MISSING_FILE', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
$companyId = $request->input('company_id');
|
||||
if (!$companyId) {
|
||||
Response::error('يرجى تحديد الشركة', 'MISSING_COMPANY', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$tenantId = $request->tenantId;
|
||||
$filePath = $this->storage->store($files['invoice'], $tenantId, $companyId);
|
||||
$fileHash = $this->storage->getHash($filePath);
|
||||
|
||||
// Create invoice record
|
||||
$invoiceId = $this->invoiceModel->create([
|
||||
'tenant_id' => $tenantId,
|
||||
'company_id' => $companyId,
|
||||
'uploaded_by' => $request->user->user_id,
|
||||
'status' => 'uploaded',
|
||||
'original_file_path' => $filePath,
|
||||
'original_file_hash' => $fileHash,
|
||||
'idempotency_key' => bin2hex(random_bytes(16))
|
||||
]);
|
||||
|
||||
// TODO: Push to queue for AI extraction
|
||||
// QueueService::push('extract_invoice', ['invoice_id' => $invoiceId]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => ['invoice_id' => $invoiceId],
|
||||
'message' => 'تم رفع الفاتورة بنجاح وبدء المعالجة'
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Response::error($e->getMessage(), 'UPLOAD_FAILED', 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
app/Modules/Invoices/InvoiceModel.php
Normal file
27
app/Modules/Invoices/InvoiceModel.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Invoices;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
final class InvoiceModel extends BaseModel
|
||||
{
|
||||
protected string $table = 'invoices';
|
||||
|
||||
public function findByStatus(string $status, ?string $tenantId = null): array
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->table} WHERE status = ? AND deleted_at IS NULL";
|
||||
$params = [$status];
|
||||
|
||||
if ($tenantId) {
|
||||
$sql .= " AND tenant_id = ?";
|
||||
$params[] = $tenantId;
|
||||
}
|
||||
|
||||
$stmt = $this->db()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
||||
27
app/Modules/Users/UserModel.php
Normal file
27
app/Modules/Users/UserModel.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Users;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
final class UserModel extends BaseModel
|
||||
{
|
||||
protected string $table = 'users';
|
||||
|
||||
public function findByEmail(string $email, ?string $tenantId = null): ?array
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->table} WHERE email = ? AND deleted_at IS NULL";
|
||||
$params = [$email];
|
||||
|
||||
if ($tenantId) {
|
||||
$sql .= " AND tenant_id = ?";
|
||||
$params[] = $tenantId;
|
||||
}
|
||||
|
||||
$stmt = $this->db()->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetch() ?: null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user