Update: 2026-05-04 17:29:56
This commit is contained in:
65
app/modules_app/companies/connect_jofotara.php
Normal file
65
app/modules_app/companies/connect_jofotara.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Link Company to JoFotara API
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Core\Encryption;
|
||||
use App\Core\JoFotara;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
// 1. Auth Check
|
||||
$decoded = AuthMiddleware::check();
|
||||
if (!in_array($decoded['role'], [ 'super_admin'])) {
|
||||
json_error('Unauthorized to modify JoFotara settings', 403);
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$companyId = $data['id'] ?? null;
|
||||
$clientId = $data['client_id'] ?? null;
|
||||
$secretKey = $data['secret_key'] ?? null;
|
||||
$sequence = $data['income_source_sequence'] ?? null;
|
||||
|
||||
if (!$companyId || !$clientId || !$secretKey) {
|
||||
json_error('Company ID, Client ID, and Secret Key are required', 422);
|
||||
}
|
||||
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
|
||||
try {
|
||||
// 2. Validate Company Ownership
|
||||
$stmt = $db->prepare("SELECT id FROM companies WHERE id = ? AND tenant_id = ?");
|
||||
$stmt->execute([$companyId, $tenantId]);
|
||||
if (!$stmt->fetch()) json_error('Access denied', 403);
|
||||
|
||||
// 3. Test Connection (Optional but recommended)
|
||||
$jofotara = new JoFotara();
|
||||
// Here you would typically call a health check endpoint if JoFotara provides one,
|
||||
// or just assume the credentials are correct for now.
|
||||
|
||||
// 4. Update Company with Encrypted Credentials
|
||||
$stmtUpdate = $db->prepare("
|
||||
UPDATE companies
|
||||
SET
|
||||
jofotara_client_id_encrypted = ?,
|
||||
jofotara_secret_key_encrypted = ?,
|
||||
jofotara_income_source_sequence = ?,
|
||||
updated_at = NOW()
|
||||
WHERE id = ?
|
||||
");
|
||||
|
||||
$stmtUpdate->execute([
|
||||
Encryption::encrypt($clientId),
|
||||
Encryption::encrypt($secretKey),
|
||||
$sequence,
|
||||
$companyId
|
||||
]);
|
||||
|
||||
json_success(null, 'تم ربط الشركة بنظام جوفوترة بنجاح');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("JoFotara Connection Error: " . $e->getMessage());
|
||||
json_error('فشل في حفظ البيانات: ' . $e->getMessage(), 500);
|
||||
}
|
||||
67
app/modules_app/companies/stats.php
Normal file
67
app/modules_app/companies/stats.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Company Monthly Stats & JoFotara Status
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
|
||||
// 1. Auth Check
|
||||
$decoded = AuthMiddleware::check();
|
||||
$db = Database::getInstance();
|
||||
|
||||
$companyId = $_GET['id'] ?? null;
|
||||
if (!$companyId) json_error('Company ID is required', 422);
|
||||
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
|
||||
try {
|
||||
// 2. Permission Check
|
||||
$stmt = $db->prepare("SELECT id, name, tax_identification_number, is_active,
|
||||
(jofotara_client_id_encrypted IS NOT NULL) as is_jofotara_connected,
|
||||
jofotara_income_source_sequence
|
||||
FROM companies WHERE id = ? AND tenant_id = ?");
|
||||
$stmt->execute([$companyId, $tenantId]);
|
||||
$company = $stmt->fetch();
|
||||
|
||||
if (!$company) json_error('Company not found', 404);
|
||||
|
||||
// 3. Monthly Invoice Stats
|
||||
$stmtStats = $db->prepare("
|
||||
SELECT
|
||||
DATE_FORMAT(invoice_date, '%Y-%m') as month,
|
||||
COUNT(*) as total_invoices,
|
||||
SUM(CASE WHEN status='approved' THEN 1 ELSE 0 END) as approved_count,
|
||||
SUM(grand_total) as total_amount
|
||||
FROM invoices
|
||||
WHERE company_id = ? AND deleted_at IS NULL
|
||||
GROUP BY month
|
||||
ORDER BY month DESC
|
||||
LIMIT 12
|
||||
");
|
||||
$stmtStats->execute([$companyId]);
|
||||
$monthly = $stmtStats->fetchAll();
|
||||
|
||||
// 4. Lifetime Totals
|
||||
$stmtTotals = $db->prepare("
|
||||
SELECT
|
||||
COUNT(*) as total_invoices,
|
||||
SUM(grand_total) as total_amount,
|
||||
SUM(tax_amount) as total_tax,
|
||||
SUM(CASE WHEN status='approved' THEN 1 ELSE 0 END) as approved_count
|
||||
FROM invoices
|
||||
WHERE company_id = ? AND deleted_at IS NULL
|
||||
");
|
||||
$stmtTotals->execute([$companyId]);
|
||||
$totals = $stmtTotals->fetch();
|
||||
|
||||
json_success([
|
||||
'company' => $company,
|
||||
'monthly' => $monthly,
|
||||
'totals' => $totals
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
error_log("Company Stats Error: " . $e->getMessage());
|
||||
json_error('Server error', 500);
|
||||
}
|
||||
Reference in New Issue
Block a user