diff --git a/lib/controller/expense/add_expense_controller.dart b/lib/controller/expense/add_expense_controller.dart index 34ae284..e2b09e0 100644 --- a/lib/controller/expense/add_expense_controller.dart +++ b/lib/controller/expense/add_expense_controller.dart @@ -358,56 +358,66 @@ class AddExpenseController extends GetxController { } } - Future> _buildExpensePayload() async { - final now = DateTime.now(); - final existingAttachmentPayloads = existingAttachments - .map((e) => { - "documentId": e['documentId'], - "fileName": e['fileName'], - "contentType": e['contentType'], - "fileSize": 0, - "description": "", - "url": e['url'], - "isActive": e['isActive'] ?? true, - "base64Data": e['isActive'] == false ? null : e['base64Data'], - }) - .toList(); +Future> _buildExpensePayload() async { + final now = DateTime.now(); - final newAttachmentPayloads = - await Future.wait(attachments.map((file) async { - final bytes = await file.readAsBytes(); - return { - "fileName": file.path.split('/').last, - "base64Data": base64Encode(bytes), - "contentType": lookupMimeType(file.path) ?? 'application/octet-stream', - "fileSize": await file.length(), - "description": "", - }; - })); + // Determine if attachments were changed + bool attachmentsChanged = + attachments.isNotEmpty || existingAttachments.any((e) => e['isActive'] == false); - final type = selectedExpenseType.value!; - return { - if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId, - "projectId": projectsMap[selectedProject.value]!, - "expensesTypeId": type.id, - "paymentModeId": selectedPaymentMode.value!.id, - "paidById": selectedPaidBy.value!.id, - "transactionDate": (selectedTransactionDate.value?.toUtc() ?? 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 - ? int.tryParse(noOfPersonsController.text.trim()) ?? 0 - : 0, - "billAttachments": [ - ...existingAttachmentPayloads, - ...newAttachmentPayloads - ], - }; - } + // Existing attachments payload + final existingAttachmentPayloads = attachmentsChanged + ? existingAttachments.map((e) => { + "documentId": e['documentId'], + "fileName": e['fileName'], + "contentType": e['contentType'], + "fileSize": 0, + "description": "", + "url": e['url'], + "isActive": e['isActive'] ?? true, + // If attachment removed, base64Data should be empty array + "base64Data": e['isActive'] == false ? "" : e['base64Data'], + }).toList() + : []; + + // New attachments payload + final newAttachmentPayloads = attachmentsChanged + ? await Future.wait(attachments.map((file) async { + final bytes = await file.readAsBytes(); + return { + "fileName": file.path.split('/').last, + "base64Data": base64Encode(bytes), + "contentType": lookupMimeType(file.path) ?? 'application/octet-stream', + "fileSize": await file.length(), + "description": "", + }; + })) + : []; + + final type = selectedExpenseType.value!; + + return { + if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId, + "projectId": projectsMap[selectedProject.value]!, + "expensesTypeId": type.id, + "paymentModeId": selectedPaymentMode.value!.id, + "paidById": selectedPaidBy.value!.id, + "transactionDate": (selectedTransactionDate.value?.toUtc() ?? 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 + ? int.tryParse(noOfPersonsController.text.trim()) ?? 0 + : 0, + // Attachments logic + "billAttachments": isEditMode.value && !attachmentsChanged + ? null + : [...existingAttachmentPayloads, ...newAttachmentPayloads], + }; +} String validateForm() { final missing = [];