Update: 2026-05-08 14:44:54
This commit is contained in:
160
app/Services/InvoiceProcessor.php
Normal file
160
app/Services/InvoiceProcessor.php
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Core\Database;
|
||||||
|
use App\Core\AI;
|
||||||
|
use App\Core\Encryption;
|
||||||
|
use App\Middleware\QuotaMiddleware;
|
||||||
|
use App\Services\NotificationService;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class InvoiceProcessor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Processes a single invoice queue item by its ID.
|
||||||
|
*/
|
||||||
|
public static function processQueueItem(int $queueId): bool
|
||||||
|
{
|
||||||
|
$db = Database::getInstance();
|
||||||
|
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
SELECT q.*, b.tenant_id, b.company_id, b.uploaded_by, b.total_images
|
||||||
|
FROM invoice_processing_queue q
|
||||||
|
JOIN invoice_batches b ON q.batch_id = b.id COLLATE utf8mb4_unicode_ci
|
||||||
|
WHERE q.id = ? AND q.status = 'pending'
|
||||||
|
");
|
||||||
|
$stmt->execute([$queueId]);
|
||||||
|
$item = $stmt->fetch();
|
||||||
|
|
||||||
|
if (!$item) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$batchId = $item['batch_id'];
|
||||||
|
$tenantId = $item['tenant_id'];
|
||||||
|
$companyId = $item['company_id'];
|
||||||
|
$userId = $item['uploaded_by'];
|
||||||
|
$imagePath = $item['image_path'];
|
||||||
|
|
||||||
|
// Mark as processing
|
||||||
|
$db->prepare("UPDATE invoice_processing_queue SET status = 'processing' WHERE id = ?")->execute([$queueId]);
|
||||||
|
|
||||||
|
if (!file_exists($imagePath)) {
|
||||||
|
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = 'File not found' WHERE id = ?")->execute([$queueId]);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mimeType = mime_content_type($imagePath) ?: 'image/jpeg';
|
||||||
|
$fileContent = file_get_contents($imagePath);
|
||||||
|
$base64Data = base64_encode($fileContent);
|
||||||
|
|
||||||
|
$extracted = AI::extractInvoiceData($base64Data, $mimeType);
|
||||||
|
|
||||||
|
if (!$extracted) {
|
||||||
|
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = 'AI failed to extract' WHERE id = ?")->execute([$queueId]);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db->beginTransaction();
|
||||||
|
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
|
||||||
|
|
||||||
|
$supplierTin = $extracted['supplier']['tin'] ?? '';
|
||||||
|
$invoiceNum = $extracted['invoice_number'] ?? '';
|
||||||
|
$invoiceDate = $extracted['invoice_date'] ?? '';
|
||||||
|
$validDate = (!empty($invoiceDate) && strtotime($invoiceDate)) ? $invoiceDate : null;
|
||||||
|
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
INSERT INTO invoices (
|
||||||
|
id, tenant_id, company_id, uploaded_by, original_file_path, status,
|
||||||
|
invoice_number, invoice_date, invoice_type, invoice_category,
|
||||||
|
supplier_tin, supplier_name, supplier_address,
|
||||||
|
buyer_tin, buyer_name, buyer_national_id,
|
||||||
|
subtotal, tax_amount, discount_total, grand_total, currency_code,
|
||||||
|
created_at
|
||||||
|
) VALUES (
|
||||||
|
?, ?, ?, ?, ?, 'extracted',
|
||||||
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
||||||
|
?, ?, ?, ?, ?,
|
||||||
|
NOW()
|
||||||
|
)
|
||||||
|
");
|
||||||
|
|
||||||
|
$stmt->execute([
|
||||||
|
$invoiceId, $tenantId, $companyId, $userId, $imagePath,
|
||||||
|
$invoiceNum, $validDate, $extracted['invoice_type'] ?? 'cash', $extracted['invoice_category'] ?? 'simplified',
|
||||||
|
Encryption::encrypt($supplierTin), Encryption::encrypt($extracted['supplier']['name'] ?? ''), Encryption::encrypt($extracted['supplier']['address'] ?? ''),
|
||||||
|
Encryption::encrypt($extracted['buyer']['tin'] ?? ''), Encryption::encrypt($extracted['buyer']['name'] ?? ''), Encryption::encrypt($extracted['buyer']['national_id'] ?? ''),
|
||||||
|
$extracted['subtotal'] ?? 0, $extracted['tax_amount'] ?? 0, $extracted['discount_total'] ?? 0, $extracted['grand_total'] ?? 0, $extracted['currency_code'] ?? 'JOD'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Save Lines
|
||||||
|
if (!empty($extracted['lines'])) {
|
||||||
|
$lineStmt = $db->prepare("INSERT INTO invoice_lines (id, invoice_id, line_number, description, quantity, unit_price, tax_rate, line_total) VALUES (?,?,?,?,?,?,?,?)");
|
||||||
|
foreach ($extracted['lines'] as $idx => $line) {
|
||||||
|
$lineStmt->execute([
|
||||||
|
vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)),
|
||||||
|
$invoiceId, $line['line_number'] ?? ($idx + 1), $line['description'] ?? '', $line['quantity'] ?? 1, $line['unit_price'] ?? 0, $line['tax_rate'] ?? 0, $line['line_total'] ?? $line['total_amount'] ?? 0
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$db->prepare("UPDATE invoice_processing_queue SET status = 'done', invoice_id = ?, processed_at = NOW() WHERE id = ?")->execute([$invoiceId, $queueId]);
|
||||||
|
$db->prepare("UPDATE invoice_batches SET processed_images = processed_images + 1 WHERE id = ?")->execute([$batchId]);
|
||||||
|
|
||||||
|
QuotaMiddleware::incrementInvoiceUsage($tenantId);
|
||||||
|
|
||||||
|
$db->commit();
|
||||||
|
|
||||||
|
// Progress Push
|
||||||
|
try {
|
||||||
|
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
||||||
|
$stmt->execute([$batchId]);
|
||||||
|
$currentBatch = $stmt->fetch();
|
||||||
|
if ($currentBatch) {
|
||||||
|
$notifier = new NotificationService();
|
||||||
|
$notifier->sendDataNotification($currentBatch['uploaded_by'], [
|
||||||
|
'type' => 'batch_progress',
|
||||||
|
'batch_id' => $batchId,
|
||||||
|
'processed' => $currentBatch['processed_images'],
|
||||||
|
'total' => $currentBatch['total_images']
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
} catch (\Exception $pushErr) {}
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if ($db->inTransaction()) {
|
||||||
|
$db->rollBack();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = ? WHERE id = ?")->execute([$e->getMessage(), $queueId]);
|
||||||
|
} catch (\Exception $e2) {}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if batch complete
|
||||||
|
self::checkBatchCompletion($batchId);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function checkBatchCompletion(string $batchId): void
|
||||||
|
{
|
||||||
|
$db = Database::getInstance();
|
||||||
|
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
||||||
|
$stmt->execute([$batchId]);
|
||||||
|
$batch = $stmt->fetch();
|
||||||
|
if ($batch && $batch['processed_images'] >= $batch['total_images']) {
|
||||||
|
$db->prepare("UPDATE invoice_batches SET status = 'done', completed_at = NOW() WHERE id = ?")->execute([$batchId]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$notifier = new NotificationService();
|
||||||
|
$title = "اكتملت معالجة الدفعة";
|
||||||
|
$body = "تمت معالجة جميع الفواتير بنجاح. يمكنك الآن مراجعتها وتدقيقها في لوحة التحكم قبل اعتمادها.";
|
||||||
|
$notifier->sendNotification($batch['uploaded_by'], $title, $body, ['batch_id' => $batchId]);
|
||||||
|
} catch (\Exception $e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,11 +8,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
require_once __DIR__ . '/../bootstrap/init.php';
|
require_once __DIR__ . '/../bootstrap/init.php';
|
||||||
|
|
||||||
use App\Core\Database;
|
use App\Services\InvoiceProcessor;
|
||||||
use App\Core\AI;
|
|
||||||
use App\Core\Encryption;
|
|
||||||
use App\Middleware\QuotaMiddleware;
|
|
||||||
use App\Services\NotificationService;
|
|
||||||
|
|
||||||
// Prevent multiple instances (Lock file)
|
// Prevent multiple instances (Lock file)
|
||||||
$lockFile = STORAGE_PATH . '/logs/process_batches.lock';
|
$lockFile = STORAGE_PATH . '/logs/process_batches.lock';
|
||||||
@@ -27,151 +23,28 @@ try {
|
|||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// 1. Get next pending item from queue
|
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
SELECT q.*, b.tenant_id, b.company_id, b.uploaded_by
|
SELECT id FROM invoice_processing_queue
|
||||||
FROM invoice_processing_queue q
|
WHERE status = 'pending'
|
||||||
JOIN invoice_batches b ON q.batch_id = b.id COLLATE utf8mb4_unicode_ci
|
ORDER BY created_at ASC
|
||||||
WHERE q.status = 'pending' AND b.status = 'processing'
|
|
||||||
ORDER BY q.created_at ASC
|
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
");
|
");
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$item = $stmt->fetch();
|
$queueId = $stmt->fetchColumn();
|
||||||
|
|
||||||
if (!$item) {
|
if (!$queueId) {
|
||||||
echo "Queue empty. Waiting...\n";
|
echo "Queue empty. Waiting...\n";
|
||||||
sleep(5);
|
sleep(5);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$queueId = $item['id'];
|
echo "Processing Queue ID: $queueId\n";
|
||||||
$batchId = $item['batch_id'];
|
$success = InvoiceProcessor::processQueueItem((int)$queueId);
|
||||||
$tenantId = $item['tenant_id'];
|
|
||||||
$companyId = $item['company_id'];
|
|
||||||
$userId = $item['uploaded_by'];
|
|
||||||
$imagePath = $item['image_path'];
|
|
||||||
|
|
||||||
echo "Processing Image: $imagePath (Queue ID: $queueId)\n";
|
if ($success) {
|
||||||
|
echo "Success for Queue ID $queueId\n";
|
||||||
// Mark as processing
|
} else {
|
||||||
$db->prepare("UPDATE invoice_processing_queue SET status = 'processing' WHERE id = ?")->execute([$queueId]);
|
echo "Failed for Queue ID $queueId\n";
|
||||||
|
|
||||||
// 2. Perform AI Extraction
|
|
||||||
if (!file_exists($imagePath)) {
|
|
||||||
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = 'File not found' WHERE id = ?")->execute([$queueId]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$mimeType = mime_content_type($imagePath) ?: 'image/jpeg';
|
|
||||||
$fileContent = file_get_contents($imagePath);
|
|
||||||
$base64Data = base64_encode($fileContent);
|
|
||||||
|
|
||||||
$extracted = AI::extractInvoiceData($base64Data, $mimeType);
|
|
||||||
|
|
||||||
if (!$extracted) {
|
|
||||||
echo "AI Extraction Failed.\n";
|
|
||||||
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = 'AI failed to extract' WHERE id = ?")->execute([$queueId]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Save to Invoices Table
|
|
||||||
$db->beginTransaction();
|
|
||||||
try {
|
|
||||||
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
|
|
||||||
|
|
||||||
$supplierTin = $extracted['supplier']['tin'] ?? '';
|
|
||||||
$invoiceNum = $extracted['invoice_number'] ?? '';
|
|
||||||
$invoiceDate = $extracted['invoice_date'] ?? '';
|
|
||||||
$validDate = (!empty($invoiceDate) && strtotime($invoiceDate)) ? $invoiceDate : null;
|
|
||||||
|
|
||||||
$stmt = $db->prepare("
|
|
||||||
INSERT INTO invoices (
|
|
||||||
id, tenant_id, company_id, uploaded_by, original_file_path, status,
|
|
||||||
invoice_number, invoice_date, invoice_type, invoice_category,
|
|
||||||
supplier_tin, supplier_name, supplier_address,
|
|
||||||
buyer_tin, buyer_name, buyer_national_id,
|
|
||||||
subtotal, tax_amount, discount_total, grand_total, currency_code,
|
|
||||||
created_at
|
|
||||||
) VALUES (
|
|
||||||
?, ?, ?, ?, ?, 'extracted',
|
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
||||||
?, ?, ?, ?, ?,
|
|
||||||
NOW()
|
|
||||||
)
|
|
||||||
");
|
|
||||||
|
|
||||||
$stmt->execute([
|
|
||||||
$invoiceId, $tenantId, $companyId, $userId, $imagePath,
|
|
||||||
$invoiceNum, $validDate, $extracted['invoice_type'] ?? 'cash', $extracted['invoice_category'] ?? 'simplified',
|
|
||||||
Encryption::encrypt($supplierTin), Encryption::encrypt($extracted['supplier']['name'] ?? ''), Encryption::encrypt($extracted['supplier']['address'] ?? ''),
|
|
||||||
Encryption::encrypt($extracted['buyer']['tin'] ?? ''), Encryption::encrypt($extracted['buyer']['name'] ?? ''), Encryption::encrypt($extracted['buyer']['national_id'] ?? ''),
|
|
||||||
$extracted['subtotal'] ?? 0, $extracted['tax_amount'] ?? 0, $extracted['discount_total'] ?? 0, $extracted['grand_total'] ?? 0, $extracted['currency_code'] ?? 'JOD'
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Save Lines
|
|
||||||
if (!empty($extracted['lines'])) {
|
|
||||||
$lineStmt = $db->prepare("INSERT INTO invoice_lines (id, invoice_id, line_number, description, quantity, unit_price, tax_rate, line_total) VALUES (?,?,?,?,?,?,?,?)");
|
|
||||||
foreach ($extracted['lines'] as $idx => $line) {
|
|
||||||
$lineStmt->execute([
|
|
||||||
vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)),
|
|
||||||
$invoiceId, $line['line_number'] ?? ($idx + 1), $line['description'] ?? '', $line['quantity'] ?? 1, $line['unit_price'] ?? 0, $line['tax_rate'] ?? 0, $line['line_total'] ?? $line['total_amount'] ?? 0
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark queue item done
|
|
||||||
$db->prepare("UPDATE invoice_processing_queue SET status = 'done', invoice_id = ?, processed_at = NOW() WHERE id = ?")->execute([$invoiceId, $queueId]);
|
|
||||||
|
|
||||||
// Update batch progress
|
|
||||||
$db->prepare("UPDATE invoice_batches SET processed_images = processed_images + 1 WHERE id = ?")->execute([$batchId]);
|
|
||||||
|
|
||||||
// Increment Quota
|
|
||||||
QuotaMiddleware::incrementInvoiceUsage($tenantId);
|
|
||||||
|
|
||||||
$db->commit();
|
|
||||||
echo "Success: Created Invoice $invoiceId\n";
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Send silent push update for progress
|
|
||||||
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
|
||||||
$stmt->execute([$batchId]);
|
|
||||||
$currentBatch = $stmt->fetch();
|
|
||||||
if ($currentBatch) {
|
|
||||||
$notifier = new NotificationService();
|
|
||||||
$notifier->sendDataNotification($currentBatch['uploaded_by'], [
|
|
||||||
'type' => 'batch_progress',
|
|
||||||
'batch_id' => $batchId,
|
|
||||||
'processed' => $currentBatch['processed_images'],
|
|
||||||
'total' => $currentBatch['total_images']
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
} catch (\Exception $pushErr) {
|
|
||||||
echo "Push error: " . $pushErr->getMessage() . "\n";
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
if ($db->inTransaction()) {
|
|
||||||
$db->rollBack();
|
|
||||||
}
|
|
||||||
echo "DB Error: " . $e->getMessage() . "\n";
|
|
||||||
try {
|
|
||||||
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = ? WHERE id = ?")->execute([$e->getMessage(), $queueId]);
|
|
||||||
} catch (\Exception $e2) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if batch is complete
|
|
||||||
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
|
||||||
$stmt->execute([$batchId]);
|
|
||||||
$batch = $stmt->fetch();
|
|
||||||
if ($batch && $batch['processed_images'] >= $batch['total_images']) {
|
|
||||||
$db->prepare("UPDATE invoice_batches SET status = 'done', completed_at = NOW() WHERE id = ?")->execute([$batchId]);
|
|
||||||
echo "Batch $batchId Complete!\n";
|
|
||||||
|
|
||||||
// Send Notification to user
|
|
||||||
$notifier = new NotificationService();
|
|
||||||
$title = "اكتملت معالجة الدفعة";
|
|
||||||
$body = "تمت معالجة جميع الفواتير بنجاح. يمكنك الآن مراجعتها وتدقيقها في لوحة التحكم قبل اعتمادها.";
|
|
||||||
$notifier->sendNotification($batch['uploaded_by'], $title, $body, ['batch_id' => $batchId]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
|||||||
use App\Core\Database;
|
use App\Core\Database;
|
||||||
use App\Middleware\AuthMiddleware;
|
use App\Middleware\AuthMiddleware;
|
||||||
use App\Core\Security;
|
use App\Core\Security;
|
||||||
|
use App\Services\InvoiceProcessor;
|
||||||
|
|
||||||
$decoded = AuthMiddleware::check();
|
$decoded = AuthMiddleware::check();
|
||||||
$tenantId = $decoded['tenant_id'];
|
$tenantId = $decoded['tenant_id'];
|
||||||
@@ -57,11 +58,23 @@ $stmt = $db->prepare("
|
|||||||
");
|
");
|
||||||
$stmt->execute([$batchId]);
|
$stmt->execute([$batchId]);
|
||||||
|
|
||||||
// 3. If it's a single invoice, try triggering the worker in the background immediately
|
// 3. If it's a single invoice, try processing it SYNCHRONOUSLY right now!
|
||||||
// This helps if the Cron Job is delayed or failing.
|
if ($batch['total_images'] == 1) {
|
||||||
$workerPath = ROOT_PATH . '/app/cron/process_batches.php';
|
// We need the queue ID for this batch
|
||||||
$logPath = STORAGE_PATH . '/logs/cron.log';
|
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending' LIMIT 1");
|
||||||
exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &");
|
$queueStmt->execute([$batchId]);
|
||||||
|
$queueId = $queueStmt->fetchColumn();
|
||||||
|
|
||||||
|
if ($queueId) {
|
||||||
|
InvoiceProcessor::processQueueItem((int)$queueId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// For multiple invoices, try triggering the worker in the background
|
||||||
|
$workerPath = ROOT_PATH . '/app/cron/process_batches.php';
|
||||||
|
$logPath = STORAGE_PATH . '/logs/cron.log';
|
||||||
|
// Mute exec since it might fail depending on server config
|
||||||
|
@exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &");
|
||||||
|
}
|
||||||
|
|
||||||
json_success([
|
json_success([
|
||||||
'batch_id' => $batchId,
|
'batch_id' => $batchId,
|
||||||
|
|||||||
Reference in New Issue
Block a user