Update: 2026-05-08 15:18:01
This commit is contained in:
@@ -155,16 +155,18 @@ class InvoiceProcessor
|
|||||||
// Check if entire batch is complete
|
// Check if entire batch is complete
|
||||||
self::checkBatchCompletion($batchId);
|
self::checkBatchCompletion($batchId);
|
||||||
|
|
||||||
// Send push notification (non-critical, don't fail on error)
|
// Progress/Completion Push
|
||||||
try {
|
try {
|
||||||
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
$stmt = $db->prepare("SELECT total_images, processed_images, uploaded_by FROM invoice_batches WHERE id = ?");
|
||||||
$stmt->execute([$batchId]);
|
$stmt->execute([$batchId]);
|
||||||
$currentBatch = $stmt->fetch();
|
$currentBatch = $stmt->fetch();
|
||||||
if ($currentBatch) {
|
if ($currentBatch) {
|
||||||
$notifier = new NotificationService();
|
$notifier = new NotificationService();
|
||||||
|
// Send data notification with invoice_id for auto-navigation
|
||||||
$notifier->sendDataNotification($currentBatch['uploaded_by'], [
|
$notifier->sendDataNotification($currentBatch['uploaded_by'], [
|
||||||
'type' => 'batch_progress',
|
'type' => 'invoice_processed',
|
||||||
'batch_id' => $batchId,
|
'batch_id' => $batchId,
|
||||||
|
'invoice_id' => $invoiceId,
|
||||||
'processed' => $currentBatch['processed_images'],
|
'processed' => $currentBatch['processed_images'],
|
||||||
'total' => $currentBatch['total_images']
|
'total' => $currentBatch['total_images']
|
||||||
]);
|
]);
|
||||||
@@ -194,12 +196,21 @@ class InvoiceProcessor
|
|||||||
self::log("Batch $batchId: COMPLETE ({$batch['processed_images']}/{$batch['total_images']})");
|
self::log("Batch $batchId: COMPLETE ({$batch['processed_images']}/{$batch['total_images']})");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Try to get the last invoice_id for this batch for completion navigation
|
||||||
|
$invStmt = $db->prepare("SELECT id FROM invoices WHERE original_file_path IN (SELECT image_path FROM invoice_processing_queue WHERE batch_id = ?) ORDER BY created_at DESC LIMIT 1");
|
||||||
|
$invStmt->execute([$batchId]);
|
||||||
|
$lastInvoiceId = $invStmt->fetchColumn();
|
||||||
|
|
||||||
$notifier = new NotificationService();
|
$notifier = new NotificationService();
|
||||||
$notifier->sendNotification(
|
$notifier->sendNotification(
|
||||||
$batch['uploaded_by'],
|
$batch['uploaded_by'],
|
||||||
"اكتملت معالجة الدفعة",
|
"اكتملت معالجة الدفعة",
|
||||||
"تمت معالجة جميع الفواتير بنجاح. يمكنك الآن مراجعتها وتدقيقها في لوحة التحكم قبل اعتمادها.",
|
"تمت معالجة جميع الفواتير بنجاح. يمكنك الآن مراجعتها وتدقيقها.",
|
||||||
['batch_id' => $batchId]
|
[
|
||||||
|
'type' => 'batch_complete',
|
||||||
|
'batch_id' => $batchId,
|
||||||
|
'invoice_id' => $lastInvoiceId ?: ''
|
||||||
|
]
|
||||||
);
|
);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
self::log("Batch $batchId: Completion notification failed: " . $e->getMessage());
|
self::log("Batch $batchId: Completion notification failed: " . $e->getMessage());
|
||||||
|
|||||||
@@ -17,8 +17,15 @@ class NotificationService
|
|||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->projectId = env('FIREBASE_PROJECT_ID', '');
|
|
||||||
$this->serviceAccountPath = env('FIREBASE_SERVICE_ACCOUNT_PATH', APP_PATH . '/config/firebase-service-account.json');
|
$this->serviceAccountPath = env('FIREBASE_SERVICE_ACCOUNT_PATH', APP_PATH . '/config/firebase-service-account.json');
|
||||||
|
|
||||||
|
// Auto-detect Project ID from Service Account JSON to prevent RESOURCE_PROJECT_INVALID
|
||||||
|
if (file_exists($this->serviceAccountPath)) {
|
||||||
|
$sa = json_decode(file_get_contents($this->serviceAccountPath), true);
|
||||||
|
$this->projectId = $sa['project_id'] ?? env('FIREBASE_PROJECT_ID', '');
|
||||||
|
} else {
|
||||||
|
$this->projectId = env('FIREBASE_PROJECT_ID', '');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -38,8 +38,12 @@ class ScannerController extends GetxController {
|
|||||||
void _initFcmListener() {
|
void _initFcmListener() {
|
||||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||||
final data = message.data;
|
final data = message.data;
|
||||||
if (data['type'] == 'batch_progress' &&
|
final type = data['type'];
|
||||||
data['batch_id'] == currentBatchId.value) {
|
final batchId = data['batch_id'];
|
||||||
|
|
||||||
|
if (batchId != currentBatchId.value) return;
|
||||||
|
|
||||||
|
if (type == 'invoice_processed' || type == 'batch_progress') {
|
||||||
processedImagesCount.value =
|
processedImagesCount.value =
|
||||||
int.tryParse(data['processed'].toString()) ?? 0;
|
int.tryParse(data['processed'].toString()) ?? 0;
|
||||||
totalImagesCount.value = int.tryParse(data['total'].toString()) ?? 0;
|
totalImagesCount.value = int.tryParse(data['total'].toString()) ?? 0;
|
||||||
@@ -48,9 +52,21 @@ class ScannerController extends GetxController {
|
|||||||
_progressService.updateProcessingProgress(
|
_progressService.updateProcessingProgress(
|
||||||
processedImagesCount.value, totalImagesCount.value);
|
processedImagesCount.value, totalImagesCount.value);
|
||||||
|
|
||||||
if (processedImagesCount.value >= totalImagesCount.value) {
|
// If it's a single invoice, we can navigate directly
|
||||||
|
if (totalImagesCount.value == 1 && data['invoice_id'] != null) {
|
||||||
isBatchDone.value = true;
|
isBatchDone.value = true;
|
||||||
_progressService.complete();
|
_progressService.complete();
|
||||||
|
|
||||||
|
// Open invoice details
|
||||||
|
Get.toNamed('/invoice-detail', arguments: data['invoice_id']);
|
||||||
|
}
|
||||||
|
} else if (type == 'batch_complete') {
|
||||||
|
isBatchDone.value = true;
|
||||||
|
_progressService.complete();
|
||||||
|
|
||||||
|
// Optionally navigate to invoices list or specific invoice
|
||||||
|
if (data['invoice_id'] != null && data['invoice_id'].toString().isNotEmpty) {
|
||||||
|
Get.toNamed('/invoice-detail', arguments: data['invoice_id']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -137,6 +153,9 @@ class ScannerController extends GetxController {
|
|||||||
Get.back(); // Go back to dashboard, progress will show in overlay
|
Get.back(); // Go back to dashboard, progress will show in overlay
|
||||||
AppSnackbar.showSuccess(
|
AppSnackbar.showSuccess(
|
||||||
'تم البدء', 'تم رفع الصور بنجاح، جاري استخراج البيانات في الخلفية');
|
'تم البدء', 'تم رفع الصور بنجاح، جاري استخراج البيانات في الخلفية');
|
||||||
|
|
||||||
|
// Start polling for status (Reliable fallback for FCM)
|
||||||
|
_startPolling(batchId);
|
||||||
} else {
|
} else {
|
||||||
_progressService.fail();
|
_progressService.fail();
|
||||||
AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
|
AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
|
||||||
@@ -150,6 +169,46 @@ class ScannerController extends GetxController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _startPolling(String batchId) {
|
||||||
|
// Check status every 2 seconds
|
||||||
|
Future.doWhile(() async {
|
||||||
|
// If we are no longer interested in this batch or it's done, stop polling
|
||||||
|
if (currentBatchId.value != batchId || isBatchDone.value) return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final res = await DioClient().client.get('batches/status', queryParameters: {'batch_id': batchId});
|
||||||
|
if (res.data['success'] == true) {
|
||||||
|
final batch = res.data['data']['batch'];
|
||||||
|
final items = res.data['data']['items'] as List;
|
||||||
|
|
||||||
|
processedImagesCount.value = int.tryParse(batch['processed_images'].toString()) ?? 0;
|
||||||
|
totalImagesCount.value = int.tryParse(batch['total_images'].toString()) ?? 1;
|
||||||
|
|
||||||
|
_progressService.updateProcessingProgress(processedImagesCount.value, totalImagesCount.value);
|
||||||
|
|
||||||
|
if (batch['status'] == 'done') {
|
||||||
|
isBatchDone.value = true;
|
||||||
|
_progressService.complete();
|
||||||
|
|
||||||
|
// If it's a single invoice, find the invoice_id and navigate
|
||||||
|
if (totalImagesCount.value == 1 && items.isNotEmpty) {
|
||||||
|
final invoiceId = items.first['invoice_id'];
|
||||||
|
if (invoiceId != null) {
|
||||||
|
Get.toNamed('/invoice-detail', arguments: invoiceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // Stop polling
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
AppLogger.error('Polling error', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
|
return true; // Continue polling
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void selectCompany(String id, String name) {
|
void selectCompany(String id, String name) {
|
||||||
selectedCompanyId.value = id;
|
selectedCompanyId.value = id;
|
||||||
selectedCompanyName.value = name;
|
selectedCompanyName.value = name;
|
||||||
|
|||||||
Reference in New Issue
Block a user