handelled the condition

This commit is contained in:
Vaibhav Surve 2025-05-07 10:11:44 +05:30
parent be96ac0de7
commit fb21885c32

View File

@ -624,12 +624,11 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
), ),
DataCell( DataCell(
Obx(() { Obx(() {
// Check if any record for this employee is uploading
final uniqueLogKey = '${log.employeeId}_${log.id}'; final uniqueLogKey = '${log.employeeId}_${log.id}';
final isUploading = final isUploading =
attendanceController.uploadingStates[uniqueLogKey]?.value ?? attendanceController.uploadingStates[uniqueLogKey]?.value ??
false; false;
// Check if both checkIn and checkOut exist and the date is yesterday
final isYesterday = log.checkIn != null && final isYesterday = log.checkIn != null &&
log.checkOut != null && log.checkOut != null &&
DateUtils.isSameDay(log.checkIn!, DateUtils.isSameDay(log.checkIn!,
@ -637,21 +636,26 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
DateUtils.isSameDay(log.checkOut!, DateUtils.isSameDay(log.checkOut!,
DateTime.now().subtract(Duration(days: 1))); DateTime.now().subtract(Duration(days: 1)));
final isTodayApproved = log.activity == 4 &&
DateUtils.isSameDay(
log.checkIn ?? DateTime(2000), DateTime.now());
final isApprovedButNotToday =
log.activity == 4 && !isTodayApproved;
final isButtonDisabled = isUploading ||
isYesterday ||
log.activity == 2 ||
log.activity == 5 ||
isApprovedButNotToday;
return SizedBox( return SizedBox(
width: 90, width: 90,
height: 25, height: 25,
child: ElevatedButton( child: ElevatedButton(
onPressed: isUploading || onPressed: isButtonDisabled
isYesterday ||
log.activity == 2 ||
log.activity == 5 ||
(log.activity == 4 &&
!(DateUtils.isSameDay(
log.checkIn ?? DateTime(2000),
DateTime.now())))
? null ? null
: () async { : () async {
// Set the uploading state for the employee when the action starts
attendanceController.uploadingStates[uniqueLogKey] = attendanceController.uploadingStates[uniqueLogKey] =
RxBool(true); RxBool(true);
@ -666,17 +670,16 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
return; return;
} }
// Existing logic for updating action
int updatedAction; int updatedAction;
String actionText; String actionText;
bool imageCapture = true; bool imageCapture = true;
if (log.activity == 0) { if (log.activity == 0) {
updatedAction = 0; updatedAction = 0;
actionText = "Check In"; actionText = "Check In";
} else if (log.activity == 1) { } else if (log.activity == 1) {
DateTime currentDate = DateTime.now(); final twoDaysAgo =
DateTime twoDaysAgo = DateTime.now().subtract(Duration(days: 2));
currentDate.subtract(Duration(days: 2));
if (log.checkOut == null && if (log.checkOut == null &&
log.checkIn != null && log.checkIn != null &&
@ -695,11 +698,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
} else if (log.activity == 2) { } else if (log.activity == 2) {
updatedAction = 2; updatedAction = 2;
actionText = "Request Regularize"; actionText = "Request Regularize";
} else if (log.activity == 4 && } else if (isTodayApproved) {
log.checkOut != null &&
log.checkIn != null &&
DateTime.now().difference(log.checkIn!).inDays <=
2) {
updatedAction = 0; updatedAction = 0;
actionText = "Check In"; actionText = "Check In";
} else { } else {
@ -707,7 +706,6 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
actionText = "Unknown Action"; actionText = "Unknown Action";
} }
// Proceed with capturing and uploading attendance
final success = await attendanceController final success = await attendanceController
.captureAndUploadAttendance( .captureAndUploadAttendance(
log.id, log.id,
@ -718,7 +716,6 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
imageCapture: imageCapture, imageCapture: imageCapture,
); );
// Show result in SnackBar
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(success content: Text(success
@ -727,12 +724,10 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
), ),
); );
// Reset the uploading state after the action is complete
attendanceController.uploadingStates[uniqueLogKey] = attendanceController.uploadingStates[uniqueLogKey] =
RxBool(false); RxBool(false);
if (success) { if (success) {
// Update the UI with the new data
attendanceController.fetchEmployeesByProject( attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!); attendanceController.selectedProjectId!);
attendanceController.fetchAttendanceLogs( attendanceController.fetchAttendanceLogs(
@ -746,15 +741,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: isYesterday backgroundColor: isYesterday
? Colors ? Colors.grey
.grey // Button color for the disabled state (Yesterday's date) : isTodayApproved
: (log.activity == 4 &&
log.checkOut != null &&
log.checkIn != null &&
DateTime.now()
.difference(log.checkIn!)
.inDays <=
2)
? Colors.green ? Colors.green
: AttendanceActionColors.colors[(log.activity == 0) : AttendanceActionColors.colors[(log.activity == 0)
? ButtonActions.checkIn ? ButtonActions.checkIn
@ -774,20 +762,14 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
), ),
) )
: Text( : Text(
(log.activity == 5) log.activity == 5
? ButtonActions.rejected ? ButtonActions.rejected
: (log.activity == 4 && : isTodayApproved
log.checkOut != null &&
log.checkIn != null &&
DateTime.now()
.difference(log.checkIn!)
.inDays <=
2)
? ButtonActions.checkIn ? ButtonActions.checkIn
: (log.activity == 2) : log.activity == 4
? "Requested" ? ButtonActions.approved
: (log.activity == 4) : log.activity == 2
? ButtonActions.approved ? ButtonActions.requested
: (log.activity == 0 && : (log.activity == 0 &&
!(log.checkIn != null && !(log.checkIn != null &&
log.checkOut != null && log.checkOut != null &&
@ -803,17 +785,16 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
.inDays <= .inDays <=
2) 2)
? ButtonActions.checkOut ? ButtonActions.checkOut
: (log.activity == 2 || : (log.activity == 1 &&
(log.activity == 1 && log.checkOut ==
log.checkOut == null &&
null && log.checkIn != null &&
log.checkIn != log.checkIn!.isBefore(
null && DateTime.now()
log.checkIn!.isBefore( .subtract(
DateTime.now() Duration(
.subtract(Duration(
days: days:
2))))) 2))))
? ButtonActions ? ButtonActions
.requestRegularize .requestRegularize
: ButtonActions.checkOut, : ButtonActions.checkOut,