Files
musadaq-saas/musadaq-app/lib/features/invoices/controllers/invoice_detail_controller.dart
2026-05-07 03:50:16 +03:00

93 lines
3.0 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('عذراً', 'لا توجد صورة مرتبطة بهذه الفاتورة');
}
}
}