🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 16:43

This commit is contained in:
Hamza-Ayed
2026-05-03 16:43:46 +03:00
parent 3aeb3220f1
commit 0488c17107
26 changed files with 1282 additions and 1153 deletions

View File

@@ -27,17 +27,43 @@ final class ExtractInvoiceJob
try {
$extractedData = $this->aiExtraction->extractInvoiceData($filePath, $mimeType);
// Map AI data to schema columns if needed, or just store in ai_raw_response
// Map AI data to schema columns
$this->invoiceModel->update($invoiceId, [
'status' => 'extracted',
'invoice_number' => $extractedData['invoice_number'] ?? null,
'invoice_date' => $extractedData['invoice_date'] ?? null,
'grand_total' => $extractedData['total_amount'] ?? 0,
'supplier_name' => $extractedData['supplier_name'] ?? null,
'supplier_tin' => $extractedData['supplier_tin'] ?? null,
'buyer_name' => $extractedData['buyer_name'] ?? null,
'buyer_tin' => $extractedData['buyer_tin'] ?? null,
'subtotal' => $extractedData['subtotal'] ?? 0,
'tax_amount' => $extractedData['tax_amount'] ?? 0,
'supplier_name' => $extractedData['vendor_name'] ?? null,
'supplier_tin' => $extractedData['vendor_tax_number'] ?? null,
'ai_raw_response' => json_encode($extractedData, JSON_UNESCAPED_UNICODE)
'discount_total' => $extractedData['discount_total'] ?? 0,
'grand_total' => $extractedData['grand_total'] ?? 0,
'ai_confidence_score' => $extractedData['confidence'] ?? null,
'ai_provider' => $extractedData['provider'] ?? 'gemini',
'ai_raw_response' => json_encode($extractedData, JSON_UNESCAPED_UNICODE),
]);
// Also insert invoice_lines:
if (!empty($extractedData['lines'])) {
$db = \App\Core\Database::getInstance();
$db->prepare("DELETE FROM invoice_lines WHERE invoice_id = ?")->execute([$invoiceId]);
foreach ($extractedData['lines'] as $i => $line) {
$db->prepare("INSERT INTO invoice_lines (id, invoice_id, line_number, description, quantity, unit_price, discount, tax_rate, tax_amount, line_total) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
->execute([
\Ramsey\Uuid\Uuid::uuid4()->toString(),
$invoiceId, $i + 1,
$line['description'] ?? '',
$line['quantity'] ?? 1,
$line['unit_price'] ?? 0,
$line['discount'] ?? 0,
$line['tax_rate'] ?? 0.16,
$line['tax_amount'] ?? 0,
$line['line_total'] ?? 0,
]);
}
}
} catch (Throwable $e) {
$this->invoiceModel->update($invoiceId, [
'status' => 'validation_failed'

View File

@@ -22,33 +22,18 @@ final class RiskAnalysisJob
try {
$analysis = $this->riskService->calculateCompanyRiskScore($companyId);
// Store or update risk score
// Store risk score
$db = Database::getInstance();
$stmt = $db->prepare("SELECT id FROM risk_scores WHERE company_id = ? LIMIT 1");
$stmt->execute([$companyId]);
$existing = $stmt->fetch();
if ($existing) {
$stmt = $db->prepare("UPDATE risk_scores SET risk_level = ?, score = ?, factors = ?, calculated_at = NOW() WHERE company_id = ?");
$stmt->execute([
$analysis['level'],
$analysis['score'],
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE),
$companyId
]);
} else {
$stmt = $db->prepare("INSERT INTO risk_scores (id, tenant_id, company_id, risk_type, risk_level, score, factors, calculated_at) VALUES (?, ?, ?, ?, ?, ?, ?, NOW())");
$stmt->execute([
\Ramsey\Uuid\Uuid::uuid4()->toString(),
$tenantId,
$companyId,
'overall_company_risk', // risk_type is required
$analysis['level'],
$analysis['score'],
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE)
]);
}
$stmt = $db->prepare("INSERT INTO risk_scores (id, tenant_id, company_id, risk_type, score, reason) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([
\Ramsey\Uuid\Uuid::uuid4()->toString(),
$tenantId,
$companyId,
$analysis['level'], // risk_type = high/medium/low
$analysis['score'],
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE), // reason
]);
} catch (Throwable $e) {
echo "[!] Risk Analysis failed for company {$companyId}: " . $e->getMessage() . "\n";
throw $e;