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 {
|
||||
if (isEditMode.value && editingExpenseId != null) {
|
||||
return ApiService.editExpenseApi(
|
||||
expenseId: editingExpenseId!,
|
||||
payload: payload,
|
||||
);
|
||||
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) {
|
||||
// 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();
|
||||
|
||||
// --- 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()
|
||||
: <Map<String, dynamic>>[];
|
||||
|
||||
// --- 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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user