fixed add and edit expense issue
This commit is contained in:
parent
ee9c94dc86
commit
f62b7d924b
@ -394,47 +394,86 @@ class AddExpenseController extends GetxController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> _submitToApi(Map<String, dynamic> payload) async {
|
Future<bool> _submitToApi(Map<String, dynamic>? payload) async {
|
||||||
if (isEditMode.value && editingExpenseId != null) {
|
if (payload == null) {
|
||||||
return ApiService.editExpenseApi(
|
_errorSnackbar("Payload is empty. Cannot submit.");
|
||||||
expenseId: editingExpenseId!,
|
return false;
|
||||||
payload: payload,
|
}
|
||||||
);
|
|
||||||
|
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<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() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user