corrected the attachment issue
This commit is contained in:
parent
fd861b3adb
commit
fd57686c8a
@ -156,6 +156,28 @@ class AddPaymentRequestController extends GetxController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> pickFromCamera() async {
|
||||||
|
try {
|
||||||
|
final pickedFile = await _picker.pickImage(source: ImageSource.camera);
|
||||||
|
if (pickedFile != null) {
|
||||||
|
isProcessingAttachment.value = true;
|
||||||
|
File imageFile = File(pickedFile.path);
|
||||||
|
|
||||||
|
// Add timestamp to the captured image
|
||||||
|
File timestampedFile = await TimestampImageHelper.addTimestamp(
|
||||||
|
imageFile: imageFile,
|
||||||
|
);
|
||||||
|
|
||||||
|
attachments.add(timestampedFile);
|
||||||
|
attachments.refresh(); // refresh UI
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
_errorSnackbar("Camera error: $e");
|
||||||
|
} finally {
|
||||||
|
isProcessingAttachment.value = false; // stop loading
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Selection handlers
|
/// Selection handlers
|
||||||
void selectProject(Map<String, dynamic> project) =>
|
void selectProject(Map<String, dynamic> project) =>
|
||||||
selectedProject.value = project;
|
selectedProject.value = project;
|
||||||
|
|||||||
@ -479,7 +479,6 @@ class _AddExpenseBottomSheetState extends State<_AddExpenseBottomSheet>
|
|||||||
message: 'Attachment has been removed.',
|
message: 'Attachment has been removed.',
|
||||||
type: SnackbarType.success,
|
type: SnackbarType.success,
|
||||||
);
|
);
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
// payment_request_bottom_sheet.dart
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:marco/controller/finance/add_payment_request_controller.dart';
|
import 'package:marco/controller/finance/add_payment_request_controller.dart';
|
||||||
@ -10,16 +9,31 @@ import 'package:marco/helpers/widgets/my_snackbar.dart';
|
|||||||
import 'package:marco/helpers/widgets/expense/expense_form_widgets.dart';
|
import 'package:marco/helpers/widgets/expense/expense_form_widgets.dart';
|
||||||
import 'package:marco/helpers/widgets/my_confirmation_dialog.dart';
|
import 'package:marco/helpers/widgets/my_confirmation_dialog.dart';
|
||||||
|
|
||||||
Future<T?> showPaymentRequestBottomSheet<T>({bool isEdit = false}) {
|
Future<T?> showPaymentRequestBottomSheet<T>({
|
||||||
|
bool isEdit = false,
|
||||||
|
Map<String, dynamic>? existingData,
|
||||||
|
VoidCallback? onUpdated,
|
||||||
|
}) {
|
||||||
return Get.bottomSheet<T>(
|
return Get.bottomSheet<T>(
|
||||||
_PaymentRequestBottomSheet(isEdit: isEdit),
|
_PaymentRequestBottomSheet(
|
||||||
|
isEdit: isEdit,
|
||||||
|
existingData: existingData,
|
||||||
|
onUpdated: onUpdated,
|
||||||
|
),
|
||||||
isScrollControlled: true,
|
isScrollControlled: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PaymentRequestBottomSheet extends StatefulWidget {
|
class _PaymentRequestBottomSheet extends StatefulWidget {
|
||||||
final bool isEdit;
|
final bool isEdit;
|
||||||
const _PaymentRequestBottomSheet({this.isEdit = false});
|
final Map<String, dynamic>? existingData;
|
||||||
|
final VoidCallback? onUpdated;
|
||||||
|
|
||||||
|
const _PaymentRequestBottomSheet({
|
||||||
|
this.isEdit = false,
|
||||||
|
this.existingData,
|
||||||
|
this.onUpdated,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<_PaymentRequestBottomSheet> createState() =>
|
State<_PaymentRequestBottomSheet> createState() =>
|
||||||
@ -35,6 +49,64 @@ class _PaymentRequestBottomSheetState extends State<_PaymentRequestBottomSheet>
|
|||||||
final _categoryDropdownKey = GlobalKey();
|
final _categoryDropdownKey = GlobalKey();
|
||||||
final _currencyDropdownKey = GlobalKey();
|
final _currencyDropdownKey = GlobalKey();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
|
if (widget.isEdit && widget.existingData != null) {
|
||||||
|
final data = widget.existingData!;
|
||||||
|
|
||||||
|
// 🧩 Prefill basic text fields
|
||||||
|
controller.titleController.text = data["title"] ?? "";
|
||||||
|
controller.amountController.text = data["amount"]?.toString() ?? "";
|
||||||
|
controller.descriptionController.text = data["description"] ?? "";
|
||||||
|
controller.dueDateController.text =
|
||||||
|
data["dueDate"]?.toString().split(" ")[0] ?? "";
|
||||||
|
|
||||||
|
// 🧩 Prefill dropdowns & toggles
|
||||||
|
controller.selectedProject.value = {
|
||||||
|
'id': data["projectId"],
|
||||||
|
'name': data["projectName"],
|
||||||
|
};
|
||||||
|
controller.selectedPayee.value = data["payee"] ?? "";
|
||||||
|
controller.isAdvancePayment.value = data["isAdvancePayment"] ?? false;
|
||||||
|
|
||||||
|
// 🕒 Wait until categories & currencies are loaded before setting them
|
||||||
|
everAll([
|
||||||
|
controller.categories,
|
||||||
|
controller.currencies,
|
||||||
|
], (_) {
|
||||||
|
controller.selectedCategory.value = controller.categories
|
||||||
|
.firstWhereOrNull((c) => c.id == data["expenseCategoryId"]);
|
||||||
|
controller.selectedCurrency.value = controller.currencies
|
||||||
|
.firstWhereOrNull((c) => c.id == data["currencyId"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 🖇 Attachments - Safe parsing (avoids null or wrong type)
|
||||||
|
final attachmentsData = data["attachments"];
|
||||||
|
if (attachmentsData != null &&
|
||||||
|
attachmentsData is List &&
|
||||||
|
attachmentsData.isNotEmpty) {
|
||||||
|
final attachments = attachmentsData
|
||||||
|
.whereType<Map<String, dynamic>>()
|
||||||
|
.map((a) => {
|
||||||
|
"id": a["id"],
|
||||||
|
"fileName": a["fileName"],
|
||||||
|
"url": a["url"],
|
||||||
|
"thumbUrl": a["thumbUrl"],
|
||||||
|
"fileSize": a["fileSize"] ?? 0,
|
||||||
|
"contentType": a["contentType"] ?? "",
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
controller.existingAttachments.assignAll(attachments);
|
||||||
|
} else {
|
||||||
|
controller.existingAttachments.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Obx(() => Form(
|
return Obx(() => Form(
|
||||||
@ -49,12 +121,14 @@ class _PaymentRequestBottomSheetState extends State<_PaymentRequestBottomSheet>
|
|||||||
if (_formKey.currentState!.validate() && _validateSelections()) {
|
if (_formKey.currentState!.validate() && _validateSelections()) {
|
||||||
final success = await controller.submitPaymentRequest();
|
final success = await controller.submitPaymentRequest();
|
||||||
if (success) {
|
if (success) {
|
||||||
// First close the BottomSheet
|
|
||||||
Get.back();
|
Get.back();
|
||||||
// Then show Snackbar
|
if (widget.onUpdated != null) widget.onUpdated!();
|
||||||
|
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: "Success",
|
title: "Success",
|
||||||
message: "Payment request created successfully!",
|
message: widget.isEdit
|
||||||
|
? "Payment request updated successfully!"
|
||||||
|
: "Payment request created successfully!",
|
||||||
type: SnackbarType.success,
|
type: SnackbarType.success,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -360,7 +434,6 @@ class _PaymentRequestBottomSheetState extends State<_PaymentRequestBottomSheet>
|
|||||||
title: 'Removed',
|
title: 'Removed',
|
||||||
message: 'Attachment has been removed.',
|
message: 'Attachment has been removed.',
|
||||||
type: SnackbarType.success);
|
type: SnackbarType.success);
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -425,12 +498,15 @@ class _PaymentRequestBottomSheetState extends State<_PaymentRequestBottomSheet>
|
|||||||
controller.selectedProject.value!['id'].toString().isEmpty) {
|
controller.selectedProject.value!['id'].toString().isEmpty) {
|
||||||
return _showError("Please select a project");
|
return _showError("Please select a project");
|
||||||
}
|
}
|
||||||
if (controller.selectedCategory.value == null)
|
if (controller.selectedCategory.value == null) {
|
||||||
return _showError("Please select a category");
|
return _showError("Please select a category");
|
||||||
if (controller.selectedPayee.value.isEmpty)
|
}
|
||||||
|
if (controller.selectedPayee.value.isEmpty) {
|
||||||
return _showError("Please select a payee");
|
return _showError("Please select a payee");
|
||||||
if (controller.selectedCurrency.value == null)
|
}
|
||||||
|
if (controller.selectedCurrency.value == null) {
|
||||||
return _showError("Please select currency");
|
return _showError("Please select currency");
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import 'package:marco/model/employees/employee_info.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
import 'package:marco/model/finance/payment_request_rembursement_bottom_sheet.dart';
|
import 'package:marco/model/finance/payment_request_rembursement_bottom_sheet.dart';
|
||||||
import 'package:marco/model/finance/make_expense_bottom_sheet.dart';
|
import 'package:marco/model/finance/make_expense_bottom_sheet.dart';
|
||||||
|
import 'package:marco/model/finance/add_payment_request_bottom_sheet.dart';
|
||||||
|
|
||||||
class PaymentRequestDetailScreen extends StatefulWidget {
|
class PaymentRequestDetailScreen extends StatefulWidget {
|
||||||
final String paymentRequestId;
|
final String paymentRequestId;
|
||||||
@ -53,17 +54,7 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
final isCreatedByCurrentUser = employeeInfo?.id == request.createdBy.id;
|
final isCreatedByCurrentUser = employeeInfo?.id == request.createdBy.id;
|
||||||
final hasDraftNextStatus =
|
final hasDraftNextStatus =
|
||||||
request.nextStatus.any((s) => s.id == draftStatusId);
|
request.nextStatus.any((s) => s.id == draftStatusId);
|
||||||
|
canSubmit.value = isCreatedByCurrentUser && hasDraftNextStatus;
|
||||||
final result = isCreatedByCurrentUser && hasDraftNextStatus;
|
|
||||||
|
|
||||||
// Debug log
|
|
||||||
print('🔐 Submit Permission Check:\n'
|
|
||||||
'Logged-in employee: ${employeeInfo?.id}\n'
|
|
||||||
'Created by: ${request.createdBy.id}\n'
|
|
||||||
'Has Draft Next Status: $hasDraftNextStatus\n'
|
|
||||||
'Can Submit: $result');
|
|
||||||
|
|
||||||
canSubmit.value = result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadEmployeeInfo() async {
|
Future<void> _loadEmployeeInfo() async {
|
||||||
@ -77,6 +68,38 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
return Color(int.parse(hex, radix: 16));
|
return Color(int.parse(hex, radix: 16));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _openEditPaymentRequestBottomSheet(request) {
|
||||||
|
showPaymentRequestBottomSheet(
|
||||||
|
isEdit: true,
|
||||||
|
existingData: {
|
||||||
|
"paymentRequestId": request.paymentRequestUID,
|
||||||
|
"title": request.title,
|
||||||
|
"projectId": request.project.id,
|
||||||
|
"projectName": request.project.name,
|
||||||
|
"expenseCategoryId": request.expenseCategory.id,
|
||||||
|
"expenseCategoryName": request.expenseCategory.name,
|
||||||
|
"amount": request.amount.toString(),
|
||||||
|
"currencyId": request.currency.id,
|
||||||
|
"currencySymbol": request.currency.symbol,
|
||||||
|
"payee": request.payee,
|
||||||
|
"description": request.description,
|
||||||
|
"isAdvancePayment": request.isAdvancePayment,
|
||||||
|
"dueDate": request.dueDate,
|
||||||
|
"attachments": request.attachments
|
||||||
|
.map((a) => {
|
||||||
|
"url": a.url,
|
||||||
|
"fileName": a.fileName,
|
||||||
|
"documentId": a.id,
|
||||||
|
"contentType": a.contentType,
|
||||||
|
})
|
||||||
|
.toList(),
|
||||||
|
},
|
||||||
|
onUpdated: () async {
|
||||||
|
await controller.fetchPaymentRequestDetail();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -125,6 +148,7 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
_DetailsTable(request: request),
|
_DetailsTable(request: request),
|
||||||
const Divider(height: 30, thickness: 1.2),
|
const Divider(height: 30, thickness: 1.2),
|
||||||
_Documents(documents: request.attachments),
|
_Documents(documents: request.attachments),
|
||||||
|
MySpacing.height(24),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -135,15 +159,54 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
bottomNavigationBar: Obx(() {
|
bottomNavigationBar: _buildBottomActionBar(),
|
||||||
|
|
||||||
|
// ✅ Added Floating Action Button for Edit
|
||||||
|
floatingActionButton: Obx(() {
|
||||||
|
if (controller.isLoading.value) return const SizedBox.shrink();
|
||||||
|
|
||||||
final request = controller.paymentRequest.value;
|
final request = controller.paymentRequest.value;
|
||||||
if (request == null ||
|
if (controller.errorMessage.isNotEmpty || request == null) {
|
||||||
controller.isLoading.value ||
|
return const SizedBox.shrink();
|
||||||
employeeInfo == null) {
|
}
|
||||||
|
|
||||||
|
if (!_checkedPermission) {
|
||||||
|
_checkedPermission = true;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
_checkPermissionToSubmit(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
final canEdit = PaymentRequestPermissionHelper.canEditPaymentRequest(
|
||||||
|
employeeInfo,
|
||||||
|
request,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!canEdit) return const SizedBox.shrink();
|
||||||
|
|
||||||
|
return FloatingActionButton.extended(
|
||||||
|
onPressed: () => _openEditPaymentRequestBottomSheet(request),
|
||||||
|
backgroundColor: contentTheme.primary,
|
||||||
|
icon: const Icon(Icons.edit),
|
||||||
|
label: MyText.bodyMedium(
|
||||||
|
"Edit Payment Request",
|
||||||
|
fontWeight: 600,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildBottomActionBar() {
|
||||||
|
return Obx(() {
|
||||||
|
final request = controller.paymentRequest.value;
|
||||||
|
if (request == null ||
|
||||||
|
controller.isLoading.value ||
|
||||||
|
employeeInfo == null) {
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check permissions once
|
|
||||||
if (!_checkedPermission) {
|
if (!_checkedPermission) {
|
||||||
_checkedPermission = true;
|
_checkedPermission = true;
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
@ -151,7 +214,6 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter statuses
|
|
||||||
const reimbursementStatusId = '61578360-3a49-4c34-8604-7b35a3787b95';
|
const reimbursementStatusId = '61578360-3a49-4c34-8604-7b35a3787b95';
|
||||||
const draftStatusId = '6537018f-f4e9-4cb3-a210-6c3b2da999d7';
|
const draftStatusId = '6537018f-f4e9-4cb3-a210-6c3b2da999d7';
|
||||||
|
|
||||||
@ -180,8 +242,8 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
|
|
||||||
return ElevatedButton(
|
return ElevatedButton(
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding:
|
||||||
horizontal: 16, vertical: 10),
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||||
backgroundColor: color,
|
backgroundColor: color,
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
@ -217,8 +279,7 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
context, status.displayName);
|
context, status.displayName);
|
||||||
if (comment == null || comment.trim().isEmpty) return;
|
if (comment == null || comment.trim().isEmpty) return;
|
||||||
|
|
||||||
final success =
|
final success = await controller.updatePaymentRequestStatus(
|
||||||
await controller.updatePaymentRequestStatus(
|
|
||||||
statusId: status.id,
|
statusId: status.id,
|
||||||
comment: comment.trim(),
|
comment: comment.trim(),
|
||||||
);
|
);
|
||||||
@ -228,8 +289,7 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
message: success
|
message: success
|
||||||
? 'Status updated successfully'
|
? 'Status updated successfully'
|
||||||
: 'Failed to update status',
|
: 'Failed to update status',
|
||||||
type:
|
type: success ? SnackbarType.success : SnackbarType.error,
|
||||||
success ? SnackbarType.success : SnackbarType.error,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (success) await controller.fetchPaymentRequestDetail();
|
if (success) await controller.fetchPaymentRequestDetail();
|
||||||
@ -242,8 +302,7 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PreferredSizeWidget _buildAppBar() {
|
PreferredSizeWidget _buildAppBar() {
|
||||||
@ -306,6 +365,29 @@ class _PaymentRequestDetailScreenState extends State<PaymentRequestDetailScreen>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PaymentRequestPermissionHelper {
|
||||||
|
static bool canEditPaymentRequest(
|
||||||
|
EmployeeInfo? employee, PaymentRequestData request) {
|
||||||
|
return employee?.id == request.createdBy.id &&
|
||||||
|
_isInAllowedEditStatus(request.expenseStatus.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool canSubmitPaymentRequest(
|
||||||
|
EmployeeInfo? employee, PaymentRequestData request) {
|
||||||
|
return employee?.id == request.createdBy.id &&
|
||||||
|
request.nextStatus.isNotEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _isInAllowedEditStatus(String statusId) {
|
||||||
|
const editableStatusIds = [
|
||||||
|
"d1ee5eec-24b6-4364-8673-a8f859c60729",
|
||||||
|
"965eda62-7907-4963-b4a1-657fb0b2724b",
|
||||||
|
"297e0d8f-f668-41b5-bfea-e03b354251c8",
|
||||||
|
];
|
||||||
|
return editableStatusIds.contains(statusId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class _Header extends StatelessWidget {
|
class _Header extends StatelessWidget {
|
||||||
final PaymentRequestData request;
|
final PaymentRequestData request;
|
||||||
final Color Function(String) colorParser;
|
final Color Function(String) colorParser;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user