88 lines
2.9 KiB
Dart
88 lines
2.9 KiB
Dart
import 'dart:io';
|
|
import 'package:pdf/pdf.dart';
|
|
import 'package:pdf/widgets.dart' as pw;
|
|
import 'package:excel/excel.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:intl/intl.dart';
|
|
import '../models/task_log_model.dart';
|
|
|
|
class ReportService {
|
|
Future<File> generatePDF(List<TaskLogModel> logs, DateTime from, DateTime to) async {
|
|
final pdf = pw.Document();
|
|
final DateFormat formatter = DateFormat('dd MMM yyyy');
|
|
|
|
pdf.addPage(
|
|
pw.MultiPage(
|
|
pageFormat: PdfPageFormat.a4,
|
|
build: (pw.Context context) => [
|
|
pw.Header(
|
|
level: 0,
|
|
child: pw.Text("Team Work Log Report", style: pw.TextStyle(fontSize: 24, fontWeight: pw.FontWeight.bold)),
|
|
),
|
|
pw.Text("Date Range: ${formatter.format(from)} - ${formatter.format(to)}"),
|
|
pw.SizedBox(height: 20),
|
|
pw.TableHelper.fromTextArray(
|
|
headers: ["Date", "Member", "Project", "Branch", "Module", "Status", "Remarks"],
|
|
data: logs.map((log) => [
|
|
formatter.format(log.date),
|
|
log.userName,
|
|
log.projectName,
|
|
log.branchName,
|
|
log.moduleName,
|
|
log.status.replaceAll('_', ' ').toUpperCase(),
|
|
log.remarks,
|
|
]).toList(),
|
|
headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
|
|
cellAlignment: pw.Alignment.centerLeft,
|
|
),
|
|
pw.SizedBox(height: 20),
|
|
pw.Text("Total Entries: ${logs.length}"),
|
|
],
|
|
),
|
|
);
|
|
|
|
Directory tempDir = await getApplicationDocumentsDirectory();
|
|
final file = File("${tempDir.path}/Report_${DateTime.now().millisecondsSinceEpoch}.pdf");
|
|
await file.writeAsBytes(await pdf.save());
|
|
return file;
|
|
}
|
|
|
|
Future<File> generateExcel(List<TaskLogModel> logs) async {
|
|
var excel = Excel.createExcel();
|
|
Sheet sheet = excel['Work Logs'];
|
|
excel.delete('Sheet1');
|
|
|
|
DateFormat formatter = DateFormat('dd MMM yyyy');
|
|
|
|
sheet.appendRow([
|
|
TextCellValue("Date"),
|
|
TextCellValue("Member"),
|
|
TextCellValue("Project"),
|
|
TextCellValue("Branch"),
|
|
TextCellValue("Module"),
|
|
TextCellValue("Status"),
|
|
TextCellValue("Remarks")
|
|
]);
|
|
|
|
for (var log in logs) {
|
|
sheet.appendRow([
|
|
TextCellValue(formatter.format(log.date)),
|
|
TextCellValue(log.userName),
|
|
TextCellValue(log.projectName),
|
|
TextCellValue(log.branchName),
|
|
TextCellValue(log.moduleName),
|
|
TextCellValue(log.status.replaceAll('_', ' ').toUpperCase()),
|
|
TextCellValue(log.remarks),
|
|
]);
|
|
}
|
|
|
|
Directory tempDir = await getApplicationDocumentsDirectory();
|
|
final file = File("${tempDir.path}/Report_${DateTime.now().millisecondsSinceEpoch}.xlsx");
|
|
var bytes = excel.encode();
|
|
if (bytes != null) {
|
|
await file.writeAsBytes(bytes);
|
|
}
|
|
return file;
|
|
}
|
|
}
|