137 lines
4.7 KiB
Dart
137 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import '../../../core/network/dio_client.dart';
|
|
import '../../../core/utils/app_snackbar.dart';
|
|
import '../../../core/utils/logger.dart';
|
|
|
|
class InvoiceDetailController extends GetxController {
|
|
var invoice = {}.obs;
|
|
var isLoading = true.obs;
|
|
String? invoiceId;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
if (Get.arguments != null) {
|
|
invoiceId = Get.arguments['id'];
|
|
if (invoiceId != null) {
|
|
fetchInvoiceDetails();
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> fetchInvoiceDetails() async {
|
|
try {
|
|
isLoading.value = true;
|
|
final res = await DioClient()
|
|
.client
|
|
.get('invoices/view', queryParameters: {'id': invoiceId});
|
|
|
|
if (res.data['success'] == true && res.data['data'] != null) {
|
|
invoice.value = res.data['data'];
|
|
} else {
|
|
AppSnackbar.showError('خطأ', 'لم يتم العثور على الفاتورة');
|
|
Get.back();
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('Failed to fetch invoice details', e);
|
|
AppSnackbar.showError('خطأ', 'فشل تحميل بيانات الفاتورة');
|
|
Get.back();
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> approveInvoice() async {
|
|
try {
|
|
final res = await DioClient()
|
|
.client
|
|
.post('invoices/approve', data: {'invoice_id': invoiceId});
|
|
if (res.data['success'] == true) {
|
|
AppSnackbar.showSuccess('تم الاعتماد', 'تم اعتماد الفاتورة بنجاح');
|
|
// Refresh the detail view
|
|
fetchInvoiceDetails();
|
|
} else {
|
|
AppSnackbar.showError('خطأ', 'فشل اعتماد الفاتورة');
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('Failed to approve invoice', e);
|
|
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع');
|
|
}
|
|
}
|
|
|
|
void viewOriginalImage() {
|
|
final fileUrl = invoice['file_url'];
|
|
if (fileUrl != null && fileUrl.isNotEmpty) {
|
|
// Navigate to a dedicated image viewer or show in a dialog
|
|
final fullUrl = 'https://musadaq.intaleqapp.com/api$fileUrl';
|
|
Get.to(() => Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('صورة الفاتورة'),
|
|
backgroundColor: const Color(0xFF0F4C81)),
|
|
body: Center(
|
|
child: InteractiveViewer(
|
|
child: Image.network(
|
|
fullUrl,
|
|
loadingBuilder: (context, child, loadingProgress) {
|
|
if (loadingProgress == null) return child;
|
|
return const CircularProgressIndicator();
|
|
},
|
|
errorBuilder: (context, error, stackTrace) {
|
|
return const Text(
|
|
'فشل تحميل الصورة. قد يكون الملف مفقوداً على الخادم.');
|
|
},
|
|
),
|
|
),
|
|
),
|
|
));
|
|
} else {
|
|
AppSnackbar.showWarning('عذراً', 'لا توجد صورة مرتبطة بهذه الفاتورة');
|
|
}
|
|
}
|
|
|
|
Future<void> submitToJoFotara() async {
|
|
// Confirmation dialog
|
|
final confirmed = await Get.dialog<bool>(
|
|
AlertDialog(
|
|
title: const Text('تأكيد الإرسال'),
|
|
content: const Text(
|
|
'هل أنت متأكد من إرسال هذه الفاتورة لمنظومة جوفتورة؟\nلا يمكن التراجع عن هذا الإجراء.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Get.back(result: false),
|
|
child: const Text('إلغاء'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () => Get.back(result: true),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF6366F1),
|
|
),
|
|
child: const Text('إرسال', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmed != true) return;
|
|
|
|
try {
|
|
AppSnackbar.showInfo('جاري الإرسال', 'يتم إرسال الفاتورة لمنظومة جوفتورة...');
|
|
final res = await DioClient().client.post(
|
|
'invoices/submit-jofotara',
|
|
data: {'invoice_id': invoiceId},
|
|
);
|
|
|
|
if (res.data['success'] == true) {
|
|
AppSnackbar.showSuccess('تم الإرسال', 'تم تقديم الفاتورة لجوفتورة بنجاح');
|
|
fetchInvoiceDetails(); // Refresh to show JoFotara status
|
|
} else {
|
|
AppSnackbar.showError('خطأ', res.data['message'] ?? 'فشل الإرسال');
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('Failed to submit to JoFotara', e);
|
|
AppSnackbar.showError('خطأ', 'فشل إرسال الفاتورة لجوفتورة');
|
|
}
|
|
}
|
|
}
|