Update: 2026-05-07 22:19:17
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import 'companies_management_controller.dart';
|
||||
|
||||
class AddCompanyController extends GetxController {
|
||||
final nameController = TextEditingController();
|
||||
final tinController = TextEditingController();
|
||||
final crnController = TextEditingController();
|
||||
|
||||
var isSubmitting = false.obs;
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
nameController.dispose();
|
||||
tinController.dispose();
|
||||
crnController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> submit() async {
|
||||
final name = nameController.text.trim();
|
||||
final tin = tinController.text.trim();
|
||||
|
||||
if (name.isEmpty || tin.isEmpty) {
|
||||
AppSnackbar.showError('خطأ', 'الرجاء إدخال اسم الشركة والرقم الضريبي');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
isSubmitting.value = true;
|
||||
final dio = DioClient().client;
|
||||
final response = await dio.post('companies', data: {
|
||||
'name': name,
|
||||
'tax_identification_number': tin,
|
||||
'commercial_registration_number': crnController.text.trim(),
|
||||
});
|
||||
|
||||
if (response.data['success'] == true) {
|
||||
AppSnackbar.showSuccess('نجاح', 'تمت إضافة الشركة بنجاح');
|
||||
|
||||
// Refresh list if controller exists
|
||||
if (Get.isRegistered<CompaniesManagementController>()) {
|
||||
Get.find<CompaniesManagementController>().fetchCompanies();
|
||||
}
|
||||
|
||||
Get.back();
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 403) {
|
||||
AppSnackbar.showError('خطأ', 'لقد وصلت للحد الأقصى المسموح به للشركات في باقتك');
|
||||
} else {
|
||||
AppSnackbar.showError('خطأ', 'تعذر إضافة الشركة');
|
||||
}
|
||||
} catch (e) {
|
||||
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع');
|
||||
} finally {
|
||||
isSubmitting.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
|
||||
class CompaniesManagementController extends GetxController {
|
||||
final Dio _dio = DioClient().client;
|
||||
|
||||
var isLoading = true.obs;
|
||||
var companies = <Map<String, dynamic>>[].obs;
|
||||
var employees = <Map<String, dynamic>>[].obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchCompanies();
|
||||
}
|
||||
|
||||
Future<void> fetchCompanies() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
final response = await _dio.get('companies');
|
||||
if (response.data['success'] == true) {
|
||||
companies.value = List<Map<String, dynamic>>.from(response.data['data']);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to fetch companies', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر تحميل قائمة الشركات');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteCompany(String id) async {
|
||||
try {
|
||||
final response = await _dio.delete('companies/$id');
|
||||
if (response.data['success'] == true) {
|
||||
companies.removeWhere((c) => c['id'] == id);
|
||||
AppSnackbar.showSuccess('نجاح', 'تم حذف الشركة بنجاح');
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to delete company', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر حذف الشركة');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user