Update: 2026-05-07 01:14:37

This commit is contained in:
Hamza-Ayed
2026-05-07 01:14:37 +03:00
parent e36a078de2
commit 8a935dc362
5 changed files with 321 additions and 30 deletions

View File

@@ -0,0 +1,155 @@
<?php
/**
* Background Worker for AI Invoice Extraction
* Processes images in the invoice_processing_queue.
*/
declare(strict_types=1);
require_once __DIR__ . '/../bootstrap/init.php';
use App\Core\Database;
use App\Core\AI;
use App\Core\Encryption;
use App\Middleware\QuotaMiddleware;
// Prevent multiple instances (Lock file)
$lockFile = STORAGE_PATH . '/logs/process_batches.lock';
$fp = fopen($lockFile, 'c+');
if (!flock($fp, LOCK_EX | LOCK_NB)) {
exit("Worker already running.\n");
}
echo "Starting Musadaq AI Worker [" . date('Y-m-d H:i:s') . "]\n";
try {
$db = Database::getInstance();
while (true) {
// 1. Get next pending item from queue
$stmt = $db->prepare("
SELECT q.*, b.tenant_id, b.company_id, b.uploaded_by
FROM invoice_processing_queue q
JOIN invoice_batches b ON q.batch_id = b.id
WHERE q.status = 'pending' AND b.status = 'processing'
ORDER BY q.created_at ASC
LIMIT 1
");
$stmt->execute();
$item = $stmt->fetch();
if (!$item) {
echo "Queue empty. Waiting...\n";
sleep(5);
continue;
}
$queueId = $item['id'];
$batchId = $item['batch_id'];
$tenantId = $item['tenant_id'];
$companyId = $item['company_id'];
$userId = $item['uploaded_by'];
$imagePath = $item['image_path'];
echo "Processing Image: $imagePath (Queue ID: $queueId)\n";
// Mark as processing
$db->prepare("UPDATE invoice_processing_queue SET status = 'processing' WHERE id = ?")->execute([$queueId]);
// 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'] ?? 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]);
$db->commit();
echo "Success: Created Invoice $invoiceId\n";
// Increment Quota
QuotaMiddleware::incrementInvoiceUsage($tenantId);
} catch (Exception $e) {
$db->rollBack();
echo "DB Error: " . $e->getMessage() . "\n";
$db->prepare("UPDATE invoice_processing_queue SET status = 'failed', error_message = ? WHERE id = ?")->execute([$e->getMessage(), $queueId]);
}
// Check if batch is complete
$stmt = $db->prepare("SELECT total_images, processed_images 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";
}
}
} catch (Exception $e) {
echo "Fatal Worker Error: " . $e->getMessage() . "\n";
} finally {
flock($fp, LOCK_UN);
fclose($fp);
}

View File

@@ -47,19 +47,16 @@ if ($batch['total_images'] == 0) {
json_error('لا يمكن إنهاء دفعة فارغة', 400);
}
// 2. Mark as processing
// 2. Mark as done since AI processing is now synchronous
$stmt = $db->prepare("
UPDATE invoice_batches
SET status = 'processing', updated_at = NOW()
SET status = 'done', completed_at = NOW(), updated_at = NOW()
WHERE id = ?
");
$stmt->execute([$batchId]);
// In a real production environment, you would dispatch a job to a queue worker here.
// For now, the queue worker is a cron job that checks the `invoice_processing_queue` table.
json_success([
'batch_id' => $batchId,
'status' => 'processing',
'status' => 'done',
'total_images' => $batch['total_images']
], 'تم إنهاء الدفعة بنجاح وإرسالها للمعالجة');
], 'تم رفع ومعالجة الدفعة بنجاح');

View File

@@ -11,6 +11,9 @@ declare(strict_types=1);
use App\Core\Database;
use App\Middleware\AuthMiddleware;
use App\Core\AI;
use App\Core\Encryption;
use App\Middleware\QuotaMiddleware;
$decoded = AuthMiddleware::check();
$tenantId = $decoded['tenant_id'];
@@ -71,23 +74,77 @@ if (!move_uploaded_file($_FILES['image']['tmp_name'], $targetPath)) {
json_error('فشل في حفظ الصورة', 500);
}
// 6. Add to processing queue
$stmt = $db->prepare("
INSERT INTO invoice_processing_queue (batch_id, tenant_id, company_id, image_path, image_order, status)
VALUES (?, ?, ?, ?, ?, 'pending')
");
$stmt->execute([$batchId, $tenantId, $companyId, $targetPath, $imageOrder]);
// 6. Run AI Extraction
$fileContent = file_get_contents($targetPath);
$base64Data = base64_encode($fileContent);
$extracted = AI::extractInvoiceData($base64Data, $mimeType);
if (!$extracted) {
// Save as raw upload if AI fails
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
$stmt = $db->prepare("INSERT INTO invoices (id, tenant_id, company_id, uploaded_by, original_file_path, status, created_at) VALUES (?, ?, ?, ?, ?, 'uploaded', NOW())");
$stmt->execute([$invoiceId, $tenantId, $companyId, $userId, $targetPath]);
} else {
// Save Extracted Data
$db->beginTransaction();
try {
$supplierTin = $extracted['supplier']['tin'] ?? '';
$invoiceNum = $extracted['invoice_number'] ?? '';
$invoiceDate = $extracted['invoice_date'] ?? '';
$validDate = (!empty($invoiceDate) && strtotime($invoiceDate)) ? $invoiceDate : null;
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
$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, $targetPath,
$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'
]);
if (!empty($extracted['lines']) && is_array($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 $index => $item) {
$lineId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
$lineStmt->execute([
$lineId, $invoiceId, $item['line_number'] ?? ($index + 1), $item['description'] ?? 'بدون وصف',
$item['quantity'] ?? 1, $item['unit_price'] ?? 0, $item['tax_rate'] ?? 0.16, $item['line_total'] ?? 0
]);
}
}
$db->commit();
QuotaMiddleware::incrementInvoiceUsage($tenantId);
} catch (Exception $e) {
$db->rollBack();
error_log("Batch Upload Error: " . $e->getMessage());
}
}
// 7. Update batch image count
$stmt = $db->prepare("
UPDATE invoice_batches
SET total_images = total_images + 1, updated_at = NOW()
SET total_images = total_images + 1, processed_images = processed_images + 1, updated_at = NOW()
WHERE id = ?
");
$stmt->execute([$batchId]);
// Count uploaded so far
$stmt = $db->prepare("SELECT COUNT(*) FROM invoice_processing_queue WHERE batch_id = ?");
// Count processed so far in batch
$stmt = $db->prepare("SELECT processed_images FROM invoice_batches WHERE id = ?");
$stmt->execute([$batchId]);
$uploadedCount = (int)$stmt->fetchColumn();

View File

@@ -6,37 +6,61 @@ import '../../../core/utils/logger.dart';
import '../../../core/utils/app_snackbar.dart';
import '../../../core/services/image_processing_service.dart';
import '../../../core/services/invoice_upload_service.dart';
import '../../../core/network/dio_client.dart';
class ScannerController extends GetxController {
var capturedImages = <File>[].obs;
var isProcessing = false.obs;
var uploadProgress = 0.0.obs;
var companies = <Map<String, dynamic>>[].obs;
var isLoadingCompanies = false.obs;
final InvoiceUploadService _uploadService = InvoiceUploadService();
Future<void> addImage(String imagePath) async {
isProcessing.value = true;
@override
void onInit() {
super.onInit();
fetchCompanies();
}
Future<void> fetchCompanies() async {
isLoadingCompanies.value = true;
try {
File originalFile = File(imagePath);
// Process image (compress, grayscale, contrast)
File? processedFile = await ImageProcessingService.processInvoiceImage(originalFile);
if (processedFile != null) {
capturedImages.add(processedFile);
AppLogger.print('Added processed image to batch. Total: ${capturedImages.length}');
final res = await DioClient().client.get('companies');
if (res.data['success'] == true && res.data['data'] != null) {
companies.value = List<Map<String, dynamic>>.from(res.data['data']);
}
} catch (e) {
AppLogger.error('Failed to fetch companies', e);
} finally {
isProcessing.value = false;
isLoadingCompanies.value = false;
}
}
Future<void> addImage(String imagePath) async {
File originalFile = File(imagePath);
// Add to UI immediately so the user doesn't wait
capturedImages.add(originalFile);
int index = capturedImages.length - 1;
// Process in background without showing full-screen loader
ImageProcessingService.processInvoiceImage(originalFile).then((processedFile) {
if (processedFile != null && index < capturedImages.length) {
capturedImages[index] = processedFile;
AppLogger.print('Finished processing image in background. Replaced in batch.');
}
}).catchError((e) {
AppLogger.error('Failed to process image in background', e);
});
}
void removeImage(int index) {
if (index >= 0 && index < capturedImages.length) {
capturedImages.removeAt(index);
}
}
Future<void> uploadBatch(String companyId) async {
Future<void> uploadBatch(String fallbackCompanyId) async {
if (capturedImages.isEmpty) {
AppSnackbar.showWarning('تنبيه', 'الرجاء تصوير فاتورة واحدة على الأقل');
return;
@@ -45,7 +69,22 @@ class ScannerController extends GetxController {
try {
isProcessing.value = true;
uploadProgress.value = 0.0;
AppLogger.print('Uploading batch of ${capturedImages.length} images...');
// Fetch a valid company ID dynamically to prevent 403 Forbidden
String companyId = fallbackCompanyId;
if (companyId == 'mock_company_id_123' || companyId.isEmpty) {
final res = await DioClient().client.get('companies');
if (res.data['success'] == true && res.data['data'] != null && res.data['data'].isNotEmpty) {
companyId = res.data['data'][0]['id'];
AppLogger.print('Dynamically fetched company: $companyId');
} else {
AppSnackbar.showError('خطأ', 'لا توجد شركات مسجلة في حسابك');
isProcessing.value = false;
return;
}
}
AppLogger.print('Uploading batch of ${capturedImages.length} images to company $companyId...');
final batchId = await _uploadService.uploadBatch(
companyId: companyId,

View File

@@ -1,6 +1,7 @@
import 'package:camerawesome/camerawesome_plugin.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../core/utils/app_snackbar.dart';
import '../controllers/scanner_controller.dart';
class ScannerView extends GetView<ScannerController> {
@@ -138,8 +139,19 @@ class ScannerView extends GetView<ScannerController> {
: ElevatedButton.icon(
onPressed: controller.isProcessing.value
? null
// TODO: Get actual company_id from user selection
: () => controller.uploadBatch('mock_company_id_123'),
: () {
if (controller.companies.isEmpty) {
AppSnackbar.showError(
'خطأ', 'لا توجد شركات مسجلة في حسابك');
return;
}
if (controller.companies.length == 1) {
controller
.uploadBatch(controller.companies[0]['id']);
return;
}
_showCompanySelectionDialog(context);
},
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF0F4C81),
padding: const EdgeInsets.symmetric(vertical: 16),
@@ -170,4 +182,35 @@ class ScannerView extends GetView<ScannerController> {
),
);
}
void _showCompanySelectionDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('اختر الشركة',
textAlign: TextAlign.right,
style: TextStyle(fontFamily: 'El Messiri')),
content: SizedBox(
width: double.maxFinite,
child: ListView.builder(
shrinkWrap: true,
itemCount: controller.companies.length,
itemBuilder: (context, index) {
final company = controller.companies[index];
return ListTile(
title: Text(company['name'] ?? '', textAlign: TextAlign.right),
subtitle: Text(company['tax_identification_number'] ?? '',
textAlign: TextAlign.right),
leading: const Icon(Icons.business, color: Color(0xFF0F4C81)),
onTap: () {
Navigator.pop(context);
controller.uploadBatch(company['id']);
},
);
},
),
),
),
);
}
}