fixed add and edit expense issue

This commit is contained in:
Vaibhav Surve 2025-11-11 18:50:13 +05:30
parent ee9c94dc86
commit f62b7d924b

View File

@ -394,16 +394,24 @@ class AddExpenseController extends GetxController {
} }
} }
Future<bool> _submitToApi(Map<String, dynamic> payload) async { Future<bool> _submitToApi(Map<String, dynamic>? payload) async {
if (payload == null) {
_errorSnackbar("Payload is empty. Cannot submit.");
return false;
}
try {
if (isEditMode.value && editingExpenseId != null) { if (isEditMode.value && editingExpenseId != null) {
return ApiService.editExpenseApi( // Edit existing expense
return await ApiService.editExpenseApi(
expenseId: editingExpenseId!, expenseId: editingExpenseId!,
payload: payload, payload: payload,
); );
} } else {
return ApiService.createExpenseApi( // Create new expense
return await ApiService.createExpenseApi(
projectId: payload['projectId'], projectId: payload['projectId'],
expensesTypeId: payload['expensesTypeId'], expensesTypeId: payload['expenseCategoryId'],
paymentModeId: payload['paymentModeId'], paymentModeId: payload['paymentModeId'],
paidById: payload['paidById'], paidById: payload['paidById'],
transactionDate: DateTime.parse(payload['transactionDate']), transactionDate: DateTime.parse(payload['transactionDate']),
@ -416,25 +424,56 @@ class AddExpenseController extends GetxController {
billAttachments: payload['billAttachments'], billAttachments: payload['billAttachments'],
); );
} }
} catch (e) {
_errorSnackbar("Failed to submit expense: $e");
return false;
}
}
Future<Map<String, dynamic>> _buildExpensePayload() async { Future<Map<String, dynamic>?> _buildExpensePayload() async {
final now = DateTime.now(); 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 final existingPayload = isEditMode.value
? existingAttachments ? existingAttachments
.map((e) => { .map((e) => {
"documentId": e['documentId'], "documentId": e['documentId'],
"fileName": e['fileName'], "fileName": e['fileName'] ?? "",
"contentType": e['contentType'], "contentType": e['contentType'] ?? "",
"fileSize": 0, "fileSize": 0,
"description": "", "description": "",
"url": e['url'], "url": e['url'] ?? "",
"isActive": e['isActive'] ?? true, "isActive": e['isActive'] ?? true,
"base64Data": "", "base64Data": "",
}) })
.toList() .toList()
: <Map<String, dynamic>>[]; : <Map<String, dynamic>>[];
// --- Process new attachments ---
final newPayload = await Future.wait( final newPayload = await Future.wait(
attachments.map((file) async { attachments.map((file) async {
final bytes = await file.readAsBytes(); final bytes = await file.readAsBytes();
@ -449,31 +488,29 @@ class AddExpenseController extends GetxController {
}), }),
); );
final type = selectedExpenseType.value!; // --- Build final payload ---
final payload = {
return {
if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId, if (isEditMode.value && editingExpenseId != null) "id": editingExpenseId,
"projectId": projectsMap[selectedProject.value]!, "projectId": projectId,
"expenseCategoryId": type.id, "expenseCategoryId": expenseType.id,
"paymentModeId": selectedPaymentMode.value!.id, "paymentModeId": paymentMode.id,
"paidById": selectedPaidBy.value!.id, "paidById": paidBy.id,
"transactionDate": "transactionDate":
(selectedTransactionDate.value ?? now).toUtc().toIso8601String(), (selectedTransactionDate.value ?? now).toUtc().toIso8601String(),
"transactionId": transactionIdController.text, "transactionId": transactionIdController.text.trim(),
"description": descriptionController.text, "description": descriptionController.text.trim(),
"location": locationController.text, "location": locationController.text.trim(),
"supplerName": supplierController.text, "supplerName": supplierController.text.trim(),
"amount": double.parse(amountController.text.trim()), "amount": double.tryParse(amountController.text.trim()) ?? 0,
"noOfPersons": type.noOfPersonsRequired == true "noOfPersons": expenseType.noOfPersonsRequired == true
? int.tryParse(noOfPersonsController.text.trim()) ?? 0 ? int.tryParse(noOfPersonsController.text.trim()) ?? 0
: 0, : 0,
"billAttachments": [ "billAttachments": [...existingPayload, ...newPayload].isEmpty
...existingPayload,
...newPayload,
].isEmpty
? null ? null
: [...existingPayload, ...newPayload], : [...existingPayload, ...newPayload],
}; };
return payload;
} }
String validateForm() { String validateForm() {