Skip to content

Commit

Permalink
Merge pull request #352 from lahirulakruwan/main
Browse files Browse the repository at this point in the history
Daily  late attendance report excel export functionality changes added
  • Loading branch information
YujithIsura authored Apr 9, 2024
2 parents 06c50de + 6db17ee commit c72caf7
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class _AttendanceDashboardScreenState extends State<AttendanceDashboardScreen> {
formattedEndDate = DateFormat('MMM d, yyyy').format(today);
batchStartDate = DateFormat('MMM d, yyyy').format(today);
batchEndDate = DateFormat('MMM d, yyyy').format(today);
;
String formattedToday = DateFormat('yyyy-MM-dd').format(today);
refreshState(null, formattedToday, formattedToday);
_fetchBatchData = _loadBatchData();
Expand Down Expand Up @@ -391,6 +390,7 @@ class _AttendanceDashboardScreenState extends State<AttendanceDashboardScreen> {
if (newValue.organization_metadata.isEmpty) {
return;
}


setState(() {
_selectedOrganizationValue = newValue;
Expand All @@ -399,6 +399,7 @@ class _AttendanceDashboardScreenState extends State<AttendanceDashboardScreen> {
.organization_metadata[0].value
.toString()));


batchEndDate = DateFormat('MMM d, yyyy').format(
DateTime.parse(_selectedOrganizationValue!
.organization_metadata[1].value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import 'package:excel/excel.dart';
import 'package:flutter/material.dart';
import 'package:attendance/data/activity_attendance.dart';
import 'package:gallery/data/person.dart';
import 'package:intl/intl.dart';

class DailyLateAttendanceExcelReportExport extends StatefulWidget {
final List<ActivityAttendance> fetchedDailyLateAttendanceData;
final Function() updateExcelState;
final bool isFetching;
final String formattedStartDate;
final String formattedEndDate;
var selectedValue;

DailyLateAttendanceExcelReportExport(
{Key? key,
required this.fetchedDailyLateAttendanceData,
required this.updateExcelState,
required this.isFetching,
required this.selectedValue,
required this.formattedStartDate,
required this.formattedEndDate
})
: super(key: key);

@override
_DailyLateAttendanceExcelReportExportState createState() => _DailyLateAttendanceExcelReportExportState();
}

class _DailyLateAttendanceExcelReportExportState extends State<DailyLateAttendanceExcelReportExport> {
List<ActivityAttendance> _fetchedDailyLateAttendanceData = [];
List<String?> columnNamesWithoutDates = [];
var _selectedValue;


void exportToExcel() async {
await widget.updateExcelState();


setState(() {
this._fetchedDailyLateAttendanceData = widget.fetchedDailyLateAttendanceData;
this._selectedValue = widget.selectedValue;
this.columnNamesWithoutDates.clear();
});

if (_fetchedDailyLateAttendanceData.length > 0) {


if (columnNamesWithoutDates.isEmpty) {

if(_selectedValue == null){

columnNamesWithoutDates.addAll([
"Date",
"Name",
"Digital ID",
"Class",
"In Time",
"Late By"
]);

}else{
columnNamesWithoutDates.addAll([
"Date",
"Name",
"Digital ID",
"In Time",
"Late By"
]);
}
}

final excel = Excel.createExcel();
final Sheet sheet = excel[excel.getDefaultSheet()!];

// Styling for organization header
final organizationHeaderStyle = CellStyle(
bold: true,
backgroundColorHex: '#807f7d',
horizontalAlign: HorizontalAlign.Center,
);

// Styling for row cells
final rowCellsStyle = CellStyle(
horizontalAlign: HorizontalAlign.Center,
);

// Adding organization header
sheet.merge(
CellIndex.indexByColumnRow(columnIndex: 0, rowIndex: 0),
CellIndex.indexByColumnRow(
columnIndex: columnNamesWithoutDates.length - 1, rowIndex: 0),
);

// Adding subheaders
final subHeaderStyle = CellStyle(
bold: true,
horizontalAlign: HorizontalAlign.Center,
backgroundColorHex: '#a3a3a2',
textWrapping: TextWrapping.WrapText,
);

// Adding column headers
for (var colIndex = 0;
colIndex < columnNamesWithoutDates.length;
colIndex++) {
sheet
.cell(CellIndex.indexByColumnRow(columnIndex: colIndex, rowIndex: 1))
.value = columnNamesWithoutDates[colIndex];
sheet
.cell(CellIndex.indexByColumnRow(columnIndex: colIndex, rowIndex: 1))
.cellStyle = subHeaderStyle;
}
sheet.setColWidth(0, 15);
sheet.setColWidth(1, 30);
sheet.setColWidth(2, 50);

if(_selectedValue == null){
sheet.setColWidth(3, 20);
sheet.setColWidth(4, 25);
sheet.setColWidth(5, 26);
}else{
sheet.setColWidth(3, 20);
sheet.setColWidth(4, 25);
}



sheet.cell(CellIndex.indexByColumnRow(columnIndex: 0, rowIndex: 0)).value =
"Avinya Foundation Daily Late Attendance Summary Report From ${widget.formattedStartDate} to ${widget.formattedEndDate}";
sheet
.cell(CellIndex.indexByColumnRow(columnIndex: 0, rowIndex: 0))
.cellStyle = organizationHeaderStyle;

if (_fetchedDailyLateAttendanceData.isNotEmpty) {

for (var index = 0; index < _fetchedDailyLateAttendanceData.length; index++) {

var dailyLateAttendanceData = _fetchedDailyLateAttendanceData[index];
var date = dailyLateAttendanceData.sign_in_time!.split(" ")[0];

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 0, rowIndex: index + 2))
.value = date.toString() ?? '';

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 1, rowIndex: index + 2))
.value = dailyLateAttendanceData.preferred_name?.toString() ?? '';

sheet
.cell(CellIndex.indexByColumnRow(
columnIndex: 2, rowIndex: index + 2))
.value =
(dailyLateAttendanceData.digital_id?.toString() ?? '');

if(_selectedValue == null){

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 3, rowIndex: index + 2))
.value = dailyLateAttendanceData.description?.toString() ?? '';


var lateSignInTime =
DateTime.parse(dailyLateAttendanceData.sign_in_time!);
var officeStartTime = DateTime.parse("$date 08:30:00");
var lateBy = lateSignInTime.difference(officeStartTime).inMinutes;
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 4, rowIndex: index + 2))
.value = (formattedTime.toString()??'');

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 5, rowIndex: index + 2))
.value = (lateBy.toString()??'') + " minutes";

}else{

var lateSignInTime =
DateTime.parse(dailyLateAttendanceData.sign_in_time!);
var officeStartTime = DateTime.parse("$date 08:30:00");
var lateBy = lateSignInTime.difference(officeStartTime).inMinutes;
var formattedTime = DateFormat('hh:mm a').format(lateSignInTime);

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 3, rowIndex: index + 2))
.value = (formattedTime.toString()??'');

sheet
.cell(
CellIndex.indexByColumnRow(columnIndex: 4, rowIndex: index + 2))
.value = (lateBy.toString()??'')+ " minutes";

}

}
}

excel.save(fileName: "DailyLateAttendanceReport_${widget.formattedStartDate}_to_${widget.formattedEndDate}.xlsx");
}

}

@override
Widget build(BuildContext context) {
return IgnorePointer(
ignoring: widget.isFetching,
child: ElevatedButton.icon(
icon: Icon(Icons.download),
label: Text('Excel Export'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.deepPurpleAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
),
onPressed: (){
exportToExcel();
},

),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:gallery/data/person.dart';
import 'package:intl/intl.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'daily_late_attendance_excel_report_export.dart';

class LateAttendanceReport extends StatefulWidget {
const LateAttendanceReport({Key? key, required this.title}) : super(key: key);

Expand Down Expand Up @@ -40,6 +42,9 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
var _selectedValue;
var activityId = 0;

List<DataColumn> ColumnNames = [];


late String formattedStartDate;
late String formattedEndDate;
var today = DateTime.now();
Expand All @@ -62,6 +67,17 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
selectWeek(today, activityId);
}

void updateExcelState() {
DailyLateAttendanceExcelReportExport(
fetchedDailyLateAttendanceData: _fetchedAttendance,
updateExcelState: updateExcelState,
isFetching: _isFetching,
selectedValue: _selectedValue,
formattedStartDate: this.formattedStartDate,
formattedEndDate:this.formattedEndDate,
);
}

@override
void didChangeDependencies() async {
super.didChangeDependencies();
Expand Down Expand Up @@ -154,7 +170,7 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
});
}

List<DataColumn> _buildDataColumns() {
List<DataColumn> _buildDataColumns() {
List<DataColumn> ColumnNames = [];

if (_selectedValue == null) {
Expand Down Expand Up @@ -391,7 +407,27 @@ class _LateAttendanceReportState extends State<LateAttendanceReport> {
),
),
),
SizedBox(width: 20),
SizedBox(
width: 10,
),
Expanded(
child: Container(
alignment: Alignment.bottomRight,
margin: EdgeInsets.only(
right: 20.0
),
width: 25.0,
height: 30.0,
child: this._isFetching ? null: DailyLateAttendanceExcelReportExport(
fetchedDailyLateAttendanceData: _fetchedAttendance,
updateExcelState: updateExcelState,
isFetching: _isFetching,
selectedValue: _selectedValue,
formattedStartDate: this.formattedStartDate,
formattedEndDate:this.formattedEndDate,
),
),
)
],
),
SizedBox(height: 16.0),
Expand Down Expand Up @@ -466,7 +502,7 @@ class MyData extends DataTableSource {
}
var lateSignInTime =
DateTime.parse(_fetchedAttendance[index - 1].sign_in_time!);
var officeStartTime = DateTime.parse("$date 07:30:00");
var officeStartTime = DateTime.parse("$date 08:30:00");
var lateBy = lateSignInTime.difference(officeStartTime).inMinutes;

if (lateBy > 0) {
Expand Down

0 comments on commit c72caf7

Please sign in to comment.