Skip to content

Commit

Permalink
Merge pull request #292 from avinyafoundation/main
Browse files Browse the repository at this point in the history
fixed issues in late attendance report
  • Loading branch information
YujithIsura authored Oct 3, 2023
2 parents 3766201 + 8469bbb commit f7fe208
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 126 deletions.
4 changes: 2 additions & 2 deletions campus/bffs/attendance/api/attendance_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public isolated client class GraphqlClient {
return <GetClassAttendanceReportResponse> check performDataBinding(graphqlResponse, GetClassAttendanceReportResponse);
}
remote isolated function getLateAttendanceReportByDate(int organization_id, int activity_id, string from_date, string to_date) returns GetLateAttendanceReportResponse|graphql:ClientError {
string query = string `query getLateAttendanceReport($organization_id:Int!,$activity_id:Int!,$from_date:String!,$to_date:String!) {late_attendance_report(organization_id:$organization_id,activity_id:$activity_id,from_date:$from_date,to_date:$to_date) {id person {id} activity_instance_id sign_in_time sign_out_time in_marked_by out_marked_by person_id}}`;
string query = string `query getLateAttendanceReport($organization_id:Int!,$activity_id:Int!,$from_date:String!,$to_date:String!) {late_attendance_report(organization_id:$organization_id,activity_id:$activity_id,from_date:$from_date,to_date:$to_date) {id person {id} activity_instance_id sign_in_time sign_out_time in_marked_by out_marked_by preferred_name digital_id person_id}}`;
map<anydata> variables = {"organization_id": organization_id, "activity_id": activity_id,"from_date": from_date, "to_date": to_date};
json graphqlResponse = check self.graphqlClient->executeWithType(query, variables);
return <GetLateAttendanceReportResponse> check performDataBinding(graphqlResponse, GetLateAttendanceReportResponse);
}
remote isolated function getLateAttendanceReportByParentOrg(int parent_organization_id, int activity_id, string from_date, string to_date) returns GetLateAttendanceReportResponseForParentOrg|graphql:ClientError {
string query = string `query getClassAttendanceReport($parent_organization_id:Int!,$activity_id:Int!,$from_date:String!,$to_date:String!) {late_attendance_report(parent_organization_id:$parent_organization_id,activity_id:$activity_id,from_date:$from_date,to_date:$to_date) {id person {id} activity_instance_id sign_in_time sign_out_time in_marked_by out_marked_by description person_id}}`;
string query = string `query getClassAttendanceReport($parent_organization_id:Int!,$activity_id:Int!,$from_date:String!,$to_date:String!) {late_attendance_report(parent_organization_id:$parent_organization_id,activity_id:$activity_id,from_date:$from_date,to_date:$to_date) {id person {id} activity_instance_id sign_in_time sign_out_time in_marked_by out_marked_by description preferred_name digital_id person_id}}`;
map<anydata> variables = {"parent_organization_id": parent_organization_id, "activity_id": activity_id,"from_date": from_date, "to_date": to_date};
json graphqlResponse = check self.graphqlClient->executeWithType(query, variables);
return <GetLateAttendanceReportResponseForParentOrg> check performDataBinding(graphqlResponse, GetLateAttendanceReportResponseForParentOrg);
Expand Down
6 changes: 6 additions & 0 deletions campus/bffs/attendance/api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public type ActivityParticipantAttendance record {
string? in_marked_by?;
string? out_marked_by?;
string? description?;
string? preferred_name?;
string? digital_id?;
};

public type ActivitySequencePlan record {
Expand Down Expand Up @@ -405,6 +407,8 @@ public type GetLateAttendanceReportResponse record {|
string? sign_out_time;
string? in_marked_by;
string? out_marked_by;
string? preferred_name;
string? digital_id;
|}[] late_attendance_report;
|};

Expand All @@ -422,6 +426,8 @@ public type GetLateAttendanceReportResponseForParentOrg record {|
string? in_marked_by;
string? out_marked_by;
string? description;
string? preferred_name;
string? digital_id;
|}[] late_attendance_report;
|};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ActivityAttendance {
bool? selected = false;
int? person;
String? description;
String? preferred_name;
String? digital_id;


ActivityAttendance({
this.id,
Expand All @@ -30,6 +33,9 @@ class ActivityAttendance {
this.out_marked_by,
this.person,
this.description,
this.preferred_name,
this.digital_id,

});

factory ActivityAttendance.fromJson(Map<String, dynamic> json) {
Expand All @@ -43,6 +49,8 @@ class ActivityAttendance {
sign_out_time: json['sign_out_time'],
in_marked_by: json['in_marked_by'],
out_marked_by: json['out_marked_by'],
preferred_name: json['preferred_name'],
digital_id: json['digital_id'],
description: json['description'],
person: json['person'] != null ? json['person']['id'] : null,
);
Expand All @@ -59,6 +67,8 @@ class ActivityAttendance {
if (sign_out_time != null) 'sign_out_time': sign_out_time,
if (in_marked_by != null) 'in_marked_by': in_marked_by,
if (out_marked_by != null) 'out_marked_by': out_marked_by,
if (preferred_name != null) 'preferred_name': preferred_name,
if (digital_id != null) 'digital_id': digital_id,
if (description != null) 'description': description,
if (person != null) 'person': person,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
DateTime? _selectedDay;

late DataTableSource _data;
List<String?> columnNames = [];
List<Map<String, bool>> attendanceList = [];
var _selectedValue;
var activityId = 0;
Expand Down Expand Up @@ -66,8 +65,7 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
@override
void didChangeDependencies() async {
super.didChangeDependencies();
_data = MyData(_fetchedAttendance, columnNames, _fetchedOrganization,
_selectedValue, updateSelected);
_data = MyData(_fetchedAttendance, _selectedValue, updateSelected);
DateRangePicker(updateDateRange, formattedStartDate);
}

Expand Down Expand Up @@ -112,10 +110,7 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
});
int? parentOrgId =
campusAppsPortalInstance.getUserPerson().organization!.id;
var cols =
columnNames.map((label) => DataColumn(label: Text(label!))).toList();
_selectedValue = newValue ?? null;
// print(newValue.id);

if (_selectedValue == null) {
_fetchedStudentList = await fetchOrganizationForAll(parentOrgId!);
Expand Down Expand Up @@ -146,13 +141,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
DateFormat('yyyy-MM-dd')
.format(DateFormat('MMM d, yyyy').parse(this.formattedEndDate)));
}
columnNames.clear();
List<String?> names = _fetchedAttendance
.map((attendance) => attendance.sign_in_time?.split(" ")[0])
.where((name) => name != null) // Filter out null values
.toList();
columnNames.addAll(names);
columnNames.sort((a, b) => b!.compareTo(a!));

String? newSelectedVal;
if (_selectedValue != null) {
Expand All @@ -162,8 +150,7 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
setState(() {
_fetchedOrganization;
this._isFetching = false;
_data = MyData(_fetchedAttendance, columnNames, _fetchedOrganization,
newSelectedVal, updateSelected);
_data = MyData(_fetchedAttendance, newSelectedVal, updateSelected);
});
}

Expand Down Expand Up @@ -211,8 +198,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {

@override
Widget build(BuildContext context) {
var cols =
columnNames.map((label) => DataColumn(label: Text(label!))).toList();

return SingleChildScrollView(
child: campusAppsPortalPersonMetaDataInstance
Expand Down Expand Up @@ -297,28 +282,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
.formattedEndDate)));
}

if (_fetchedAttendance.length > 0) {
// Add null check here
// Process attendance data here
columnNames.clear();
List<String?> names =
_fetchedAttendance
.map((attendance) =>
attendance.sign_in_time
?.split(" ")[0])
.where((name) =>
name !=
null) // Filter out null values
.toList();
columnNames.addAll(names);
} else {
columnNames.clear();
}

cols = columnNames
.map((label) => DataColumn(
label: Text(label!)))
.toList();
if (_selectedValue == null) {
setState(() {
if (_fetchedOrganization !=
Expand All @@ -342,8 +305,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
_fetchedStudentList;
_data = MyData(
_fetchedAttendance,
columnNames,
_fetchedOrganization,
_selectedValue,
updateSelected);
});
Expand All @@ -353,8 +314,6 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
_fetchedStudentList;
_data = MyData(
_fetchedAttendance,
columnNames,
_fetchedOrganization,
_selectedValue.description,
updateSelected);
});
Expand Down Expand Up @@ -450,7 +409,7 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
size: 50, // Customize the size of the indicator
),
)
else if (cols.length > 0)
else if (_fetchedAttendance.length > 0)
PaginatedDataTable(
showCheckboxColumn: false,
source: _data,
Expand All @@ -474,35 +433,16 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
}

class MyData extends DataTableSource {
MyData(this._fetchedAttendance, this.columnNames, this._fetchedOrganization,
this._selectedValue, this.updateSelected);
MyData(this._fetchedAttendance, this._selectedValue, this.updateSelected);

final List<ActivityAttendance> _fetchedAttendance;
final List<String?> columnNames;
final Organization? _fetchedOrganization;
final String? _selectedValue;
final Function(int, bool, List<bool>) updateSelected;

List<String> getDatesFromMondayToToday() {
DateTime now = DateTime.now();
DateTime previousMonday = now.subtract(Duration(days: now.weekday - 1));
DateTime currentDate = DateTime(now.year, now.month, now.day);

List<String> dates = [];
for (DateTime date = previousMonday;
date.isBefore(currentDate);
date = date.add(Duration(days: 1))) {
if (date.weekday != DateTime.saturday &&
date.weekday != DateTime.sunday) {
dates.add(DateFormat('yyyy-MM-dd').format(date));
}
}

return dates;
}

List<int?> selectedPersonIds = [];
String? lastProcessedDate;
int? lastProcessedPersonId;
List<String?> lastProcessedDates = [];
@override
DataRow? getRow(int index) {
if (index == 0 && _selectedValue == null) {
Expand All @@ -517,68 +457,45 @@ class MyData extends DataTableSource {
);
}

final dateRegex = RegExp(r'^\d{4}-\d{2}-\d{2}$');
final dateFormatter = DateFormat('yyyy-MM-dd');
List<String> validDates = [];

for (var element in columnNames) {
if (dateRegex.hasMatch(element!)) {
try {
dateFormatter.parseStrict(element);
validDates.add(element);
} catch (e) {
// Handle the exception or continue to the next element
}
}
}

if (_fetchedOrganization != null &&
_fetchedOrganization!.people.isNotEmpty &&
validDates.length > 0 &&
index <= validDates.length) {
var date = validDates[index - 1];
if (_fetchedAttendance.length > 0 && index <= _fetchedAttendance.length) {
var date = _fetchedAttendance[index - 1].sign_in_time!.split(" ")[0];
List<DataCell> cells = [];
if (_selectedValue == null) {
cells = List<DataCell>.filled(6, DataCell.empty);
} else {
cells = List<DataCell>.filled(5, DataCell.empty);
}

cells[0] = DataCell(Text(date));
for (final person in _fetchedOrganization!.people) {
// Check if the person has already been selected

for (final attendance in _fetchedAttendance) {
if (attendance.person_id == person.id &&
attendance.sign_in_time!.startsWith(date)) {
var lateSignInTime = DateTime.parse(attendance.sign_in_time!);
var officeStartTime = DateTime.parse("$date 07:30:00");
var lateBy = lateSignInTime.difference(officeStartTime).inMinutes;

if (lateBy > 0) if (selectedPersonIds.contains(person.id)) {
break; // Skip to the next iteration if already selected
}
if (_selectedValue == null) {
cells[1] = DataCell(Text(person.preferred_name!));
cells[2] = DataCell(Text(person.digital_id.toString()));
cells[3] = DataCell(Text(attendance.description.toString()));
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);
cells[4] = DataCell(Text(formattedTime));
cells[5] = DataCell(Text(lateBy.toString() + " minutes"));
} else {
cells[1] = DataCell(Text(person.preferred_name!));
cells[2] = DataCell(Text(person.digital_id.toString()));
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);
cells[3] = DataCell(Text(formattedTime));
cells[4] = DataCell(Text(lateBy.toString() + " minutes"));
}

// Add the person ID to the set of selected IDs
selectedPersonIds.add(person.id);

return DataRow(cells: cells);
}
var lateSignInTime =
DateTime.parse(_fetchedAttendance[index - 1].sign_in_time!);
var officeStartTime = DateTime.parse("$date 07:30:00");
var lateBy = lateSignInTime.difference(officeStartTime).inMinutes;

if (lateBy > 0) {
if (_selectedValue == null) {
cells[0] = DataCell(
Text(_fetchedAttendance[index - 1].sign_in_time!.split(" ")[0]));
cells[1] =
DataCell(Text(_fetchedAttendance[index - 1].preferred_name!));
cells[2] = DataCell(
Text(_fetchedAttendance[index - 1].digital_id.toString()));
cells[3] = DataCell(
Text(_fetchedAttendance[index - 1].description.toString()));
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);
cells[4] = DataCell(Text(formattedTime));
cells[5] = DataCell(Text(lateBy.toString() + " minutes"));
} else {
cells[0] = DataCell(
Text(_fetchedAttendance[index - 1].sign_in_time!.split(" ")[0]));
cells[1] =
DataCell(Text(_fetchedAttendance[index - 1].preferred_name!));
cells[2] = DataCell(
Text(_fetchedAttendance[index - 1].digital_id.toString()));
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);
cells[3] = DataCell(Text(formattedTime));
cells[4] = DataCell(Text(lateBy.toString() + " minutes"));
}

return DataRow(cells: cells);
}

print(cells.length);
Expand All @@ -593,9 +510,7 @@ class MyData extends DataTableSource {
@override
int get rowCount {
int count = 0;
if (_fetchedOrganization != null) {
count = _fetchedAttendance.length + 1;
}
count = _fetchedAttendance.length + 1;
return count;
}

Expand Down

0 comments on commit f7fe208

Please sign in to comment.