Update: 2026-05-07 01:14:37
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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']);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user