marco.pms.mobileapp/lib/controller/inventory/material_requisition_controller.dart

209 lines
5.9 KiB
Dart

// import 'package:get/get.dart';
// import 'package:marco/helpers/services/api_service.dart';
// import 'package:marco/model/inventory/material_requisition_model.dart';
// import 'package:marco/model/inventory/requisition_item_model.dart';
// import 'package:marco/helpers/services/app_logger.dart';
// class MaterialRequisitionController extends GetxController {
// final isLoading = false.obs;
// final requisitions = <MaterialRequisition>[].obs;
// final selectedMR = MaterialRequisition().obs;
// // Dropdown master lists
// final projects = <Project>[].obs;
// final materials = <Map<String, dynamic>>[].obs;
// // Form state
// // 🔧 FIX: use integer type since project.id is int
// final selectedProjectId = RxnInt();
// final status = 'Draft'.obs;
// @override
// void onInit() {
// super.onInit();
// fetchProjects();
// fetchAllRequisitions();
// }
// /// === Fetch all Projects ===
// Future<void> fetchProjects() async {
// try {
// isLoading(true);
// final res = await ApiService.getProjects();
// if (res != null) {
// projects.assignAll(
// (res as List).map((e) => Project.fromJson(e)).toList(),
// );
// }
// } catch (e) {
// logSafe("fetchProjects() error: $e", level: LogLevel.error);
// } finally {
// isLoading(false);
// }
// }
// /// === Fetch materials for a selected project ===
// /// 🔧 FIX: change argument from String → int
// Future<void> fetchMaterialsByProject(int projectId) async {
// try {
// isLoading(true);
// final res = await ApiService.getMaterialsByProject(projectId.toString());
// if (res != null) {
// materials.assignAll(List<Map<String, dynamic>>.from(res));
// }
// } catch (e) {
// logSafe("fetchMaterialsByProject() error: $e", level: LogLevel.error);
// } finally {
// isLoading(false);
// }
// }
// /// === Fetch all Material Requisitions ===
// Future<void> fetchAllRequisitions() async {
// try {
// isLoading(true);
// final res = await ApiService.getMaterialRequisitions();
// if (res != null) {
// requisitions.assignAll(
// (res as List)
// .map((e) => MaterialRequisition.fromJson(e))
// .toList(),
// );
// }
// } catch (e) {
// logSafe("fetchAllRequisitions() error: $e", level: LogLevel.error);
// } finally {
// isLoading(false);
// }
// }
// /// === Save Draft ===
// Future<void> saveDraft(MaterialRequisition mr) async {
// try {
// isLoading(true);
// final res = await ApiService.createMaterialRequisition(mr.toJson());
// if (res != null) {
// Get.snackbar('Success', 'Saved as Draft');
// await fetchAllRequisitions(); // refresh list
// }
// } catch (e) {
// logSafe("saveDraft() error: $e", level: LogLevel.error);
// } finally {
// isLoading(false);
// }
// }
// /// === Submit Material Requisition ===
// /// 🔧 FIX: id type → int for consistency
// Future<void> submitMR(int mrId) async {
// try {
// isLoading(true);
// final res =
// await ApiService.updateMaterialRequisitionStatus(mrId.toString(), 'Submitted');
// if (res != null) {
// Get.snackbar('Submitted', 'MR submitted for review');
// await fetchAllRequisitions();
// }
// } catch (e) {
// logSafe("submitMR() error: $e", level: LogLevel.error);
// } finally {
// isLoading(false);
// }
// }
// /// === Approve / Reject / Comment actions ===
// /// 🔧 FIX: id type → int for consistency
// Future<void> performAction(int mrId, String action, String? comment) async {
// try {
// final body = {
// "mrId": mrId,
// "action": action,
// "comments": comment,
// };
// final res = await ApiService.postMaterialRequisitionAction(body);
// if (res != null) {
// Get.snackbar('Success', 'Action: $action');
// await fetchAllRequisitions();
// }
// } catch (e) {
// logSafe("performAction() error: $e", level: LogLevel.error);
// }
// }
// }
import 'package:get/get.dart';
class MaterialRequisitionController extends GetxController {
// --- Mock UI State ---
var isLoading = false.obs;
// Mock project dropdown
var projects = [
{'id': 1, 'name': 'Project Alpha'},
{'id': 2, 'name': 'Project Beta'},
].obs;
var selectedProjectId = RxnInt();
// Mock MR list
var materialRequisitions = <Map<String, dynamic>>[].obs;
// Mock selected MR object
var selectedMR = Rxn<Map<String, dynamic>>();
// --- Methods ---
void fetchMaterialRequisitions() {
isLoading.value = true;
Future.delayed(const Duration(seconds: 1), () {
materialRequisitions.value = [
{
'id': 1,
'projectName': 'Project Alpha',
'status': 'Draft',
'items': [
{'material': 'Cement', 'qty': 50},
{'material': 'Steel', 'qty': 100},
]
},
{
'id': 2,
'projectName': 'Project Beta',
'status': 'Approved',
'items': [
{'material': 'Bricks', 'qty': 500},
{'material': 'Sand', 'qty': 200},
]
},
];
isLoading.value = false;
});
}
void fetchMaterialsByProject(int? projectId) {
if (projectId == null) return;
selectedMR.value = {
'id': projectId,
'projectName': projects.firstWhere(
(p) => p['id'] == projectId,
orElse: () => {'name': 'Unknown'})['name'],
'items': [
{'material': 'Cement', 'qty': 25},
{'material': 'Steel', 'qty': 80},
]
};
}
void saveDraft(Map<String, dynamic> mr) {
print("📝 Saved draft for ${mr['projectName']}");
}
void submitMR(int? mrId) {
print("🚀 Submitted MR ID: $mrId");
}
}