feat: Enhance expense payload construction with attachment change detection
This commit is contained in:
parent
bd6f175ca7
commit
5c923bb48b
@ -358,56 +358,66 @@ class AddExpenseController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _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<Map<String, dynamic>> _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 = <String>[];
|
||||
|
Loading…
x
Reference in New Issue
Block a user