From f62b7d924b86695113ca2ad1ac73a02fd61b1640 Mon Sep 17 00:00:00 2001 From: Vaibhav Surve Date: Tue, 11 Nov 2025 18:50:13 +0530 Subject: [PATCH] fixed add and edit expense issue --- .../expense/add_expense_controller.dart | 119 ++++++++++++------ 1 file changed, 78 insertions(+), 41 deletions(-) diff --git a/lib/controller/expense/add_expense_controller.dart b/lib/controller/expense/add_expense_controller.dart index 1264cac..11ce63d 100644 --- a/lib/controller/expense/add_expense_controller.dart +++ b/lib/controller/expense/add_expense_controller.dart @@ -394,47 +394,86 @@ class AddExpenseController extends GetxController { } } - Future _submitToApi(Map payload) async { - if (isEditMode.value && editingExpenseId != null) { - return ApiService.editExpenseApi( - expenseId: editingExpenseId!, - payload: payload, - ); + Future _submitToApi(Map? payload) async { + if (payload == null) { + _errorSnackbar("Payload is empty. Cannot submit."); + return false; + } + + try { + if (isEditMode.value && editingExpenseId != null) { + // Edit existing expense + return await ApiService.editExpenseApi( + expenseId: editingExpenseId!, + payload: payload, + ); + } else { + // Create new expense + return await ApiService.createExpenseApi( + projectId: payload['projectId'], + expensesTypeId: payload['expenseCategoryId'], + paymentModeId: payload['paymentModeId'], + paidById: payload['paidById'], + transactionDate: DateTime.parse(payload['transactionDate']), + transactionId: payload['transactionId'], + description: payload['description'], + location: payload['location'], + supplerName: payload['supplerName'], + amount: payload['amount'], + noOfPersons: payload['noOfPersons'], + billAttachments: payload['billAttachments'], + ); + } + } catch (e) { + _errorSnackbar("Failed to submit expense: $e"); + return false; } - return ApiService.createExpenseApi( - projectId: payload['projectId'], - expensesTypeId: payload['expensesTypeId'], - paymentModeId: payload['paymentModeId'], - paidById: payload['paidById'], - transactionDate: DateTime.parse(payload['transactionDate']), - transactionId: payload['transactionId'], - description: payload['description'], - location: payload['location'], - supplerName: payload['supplerName'], - amount: payload['amount'], - noOfPersons: payload['noOfPersons'], - billAttachments: payload['billAttachments'], - ); } - Future> _buildExpensePayload() async { + Future?> _buildExpensePayload() async { final now = DateTime.now(); + // --- Get IDs safely --- + final projectId = projectsMap[selectedProject.value]; + final expenseType = selectedExpenseType.value; + final paymentMode = selectedPaymentMode.value; + final paidBy = selectedPaidBy.value; + + // --- Validate essential fields --- + if (projectId == null) { + _errorSnackbar("Project not selected or invalid"); + return null; + } + if (expenseType == null) { + _errorSnackbar("Expense type not selected"); + return null; + } + if (paymentMode == null) { + _errorSnackbar("Payment mode not selected"); + return null; + } + if (paidBy == null) { + _errorSnackbar("Paid By not selected"); + return null; + } + + // --- Process existing attachments (for edit mode) --- final existingPayload = isEditMode.value ? existingAttachments .map((e) => { "documentId": e['documentId'], - "fileName": e['fileName'], - "contentType": e['contentType'], + "fileName": e['fileName'] ?? "", + "contentType": e['contentType'] ?? "", "fileSize": 0, "description": "", - "url": e['url'], + "url": e['url'] ?? "", "isActive": e['isActive'] ?? true, "base64Data": "", }) .toList() : >[]; + // --- Process new attachments --- final newPayload = await Future.wait( attachments.map((file) async { final bytes = await file.readAsBytes(); @@ -449,31 +488,29 @@ class AddExpenseController extends GetxController { }), ); - final type = selectedExpenseType.value!; - - return { + // --- Build final payload --- + final payload = { if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId, - "projectId": projectsMap[selectedProject.value]!, - "expenseCategoryId": type.id, - "paymentModeId": selectedPaymentMode.value!.id, - "paidById": selectedPaidBy.value!.id, + "projectId": projectId, + "expenseCategoryId": expenseType.id, + "paymentModeId": paymentMode.id, + "paidById": paidBy.id, "transactionDate": (selectedTransactionDate.value ?? now).toUtc().toIso8601String(), - "transactionId": transactionIdController.text, - "description": descriptionController.text, - "location": locationController.text, - "supplerName": supplierController.text, - "amount": double.parse(amountController.text.trim()), - "noOfPersons": type.noOfPersonsRequired == true + "transactionId": transactionIdController.text.trim(), + "description": descriptionController.text.trim(), + "location": locationController.text.trim(), + "supplerName": supplierController.text.trim(), + "amount": double.tryParse(amountController.text.trim()) ?? 0, + "noOfPersons": expenseType.noOfPersonsRequired == true ? int.tryParse(noOfPersonsController.text.trim()) ?? 0 : 0, - "billAttachments": [ - ...existingPayload, - ...newPayload, - ].isEmpty + "billAttachments": [...existingPayload, ...newPayload].isEmpty ? null : [...existingPayload, ...newPayload], }; + + return payload; } String validateForm() {