165 lines
6.1 KiB
Dart
165 lines
6.1 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:pdf/pdf.dart';
|
|
import 'package:pdf/widgets.dart' as pw;
|
|
import 'package:printing/printing.dart';
|
|
|
|
import '../../data/models/tank_model.dart';
|
|
import '../tanklist/tanklist_view.dart';
|
|
|
|
class PrintController extends GetxController {
|
|
final argunments = Get.arguments;
|
|
final List<dynamic> tankList = Get.arguments['tankList'] ?? [];
|
|
final String year = Get.arguments['year'] ?? 'NoYear';
|
|
final String summeLiter = Get.arguments['summeLiter'] ?? '0.0';
|
|
final String summePreis = Get.arguments['summePreis'] ?? '0.0';
|
|
|
|
Future<void> printPdfReport() async {
|
|
final doc = pw.Document();
|
|
final font = await PdfGoogleFonts.robotoRegular();
|
|
final List<AppWriteTankModel> tankungen =
|
|
tankList.map((e) => AppWriteTankModel.fromMap(e)).toList();
|
|
|
|
// Daten nach Monat gruppieren
|
|
final dFormat = DateFormat('MMMM yyyy', 'de');
|
|
final Map<String, List<AppWriteTankModel>> tankungenByMonth = {};
|
|
for (var tankung in tankungen) {
|
|
final month = dFormat.format(DateTime.parse(tankung.date));
|
|
if (!tankungenByMonth.containsKey(month)) {
|
|
tankungenByMonth[month] = [];
|
|
}
|
|
tankungenByMonth[month]!.add(tankung);
|
|
}
|
|
|
|
doc.addPage(
|
|
pw.Page(
|
|
pageFormat: PdfPageFormat.a4,
|
|
build: (pw.Context context) {
|
|
return pw.Column(
|
|
crossAxisAlignment: pw.CrossAxisAlignment.start,
|
|
children: [
|
|
// PDF-Design hier
|
|
pw.Text('Tankbericht $year',
|
|
style: pw.TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: pw.FontWeight.bold,
|
|
color: PdfColors.blue900,
|
|
font: font)),
|
|
pw.SizedBox(height: 10),
|
|
pw.Text('Alle Tankungen im Überblick',
|
|
style: pw.TextStyle(
|
|
fontSize: 16, color: PdfColors.grey700, font: font)),
|
|
pw.Divider(color: PdfColors.grey400),
|
|
pw.SizedBox(height: 20),
|
|
|
|
// Dynamische Erstellung der monatlichen Abschnitte
|
|
...tankungenByMonth.entries.map((entry) {
|
|
final monthName = entry.key;
|
|
final monthlyData = entry.value;
|
|
return pw.Column(
|
|
crossAxisAlignment: pw.CrossAxisAlignment.start,
|
|
children: [
|
|
// Monatsüberschrift
|
|
pw.Text(
|
|
monthName,
|
|
style: pw.TextStyle(
|
|
fontWeight: pw.FontWeight.bold,
|
|
fontSize: 18,
|
|
font: font,
|
|
),
|
|
),
|
|
pw.SizedBox(height: 10),
|
|
// Tabelle für den jeweiligen Monat
|
|
pw.TableHelper.fromTextArray(
|
|
headers: [
|
|
'Datum',
|
|
'Ort',
|
|
'Menge (L)',
|
|
'Preis/L (€)',
|
|
'Summe (€)'
|
|
],
|
|
cellAlignment: pw.Alignment.centerLeft,
|
|
border: pw.TableBorder.all(color: PdfColors.grey200),
|
|
headerStyle: pw.TextStyle(
|
|
fontWeight: pw.FontWeight.bold, font: font),
|
|
data: monthlyData.map((item) {
|
|
var modDate = item.date.substring(5);
|
|
var modPreisPerLiter =
|
|
item.pricePerLiter.padRight(5, '0');
|
|
return [
|
|
modDate,
|
|
item.location,
|
|
item.liters,
|
|
modPreisPerLiter,
|
|
item.szSummePreis ?? '0.0',
|
|
];
|
|
}).toList(),
|
|
),
|
|
pw.SizedBox(height: 20),
|
|
],
|
|
);
|
|
}),
|
|
|
|
// pw.TableHelper.fromTextArray(
|
|
// headers: [
|
|
// 'Datum',
|
|
// 'Ort',
|
|
// 'Menge (L)',
|
|
// 'Preis/L (€)',
|
|
// 'Summe (€)'
|
|
// ],
|
|
// cellAlignment: pw.Alignment.centerLeft,
|
|
// border: pw.TableBorder.all(color: PdfColors.grey200),
|
|
// headerStyle:
|
|
// pw.TextStyle(fontWeight: pw.FontWeight.bold, font: font),
|
|
// data: tankungen.map((item) {
|
|
// var modDate = item.date.substring(5);
|
|
// var modPreisPerLiter = item.pricePerLiter.padRight(5, '0');
|
|
// return [
|
|
// modDate,
|
|
// item.location,
|
|
// item.liters,
|
|
// modPreisPerLiter,
|
|
// item.szSummePreis ?? '0.0',
|
|
// ];
|
|
// }).toList(),
|
|
// ),
|
|
|
|
pw.Spacer(),
|
|
pw.Divider(color: PdfColors.grey400),
|
|
pw.Container(
|
|
alignment: pw.Alignment.centerRight,
|
|
child: pw.Column(
|
|
crossAxisAlignment: pw.CrossAxisAlignment.end,
|
|
children: [
|
|
pw.Text('Jahres-Gesamtmenge: $summeLiter L',
|
|
style: pw.TextStyle(
|
|
fontWeight: pw.FontWeight.bold,
|
|
fontSize: 16,
|
|
font: font)),
|
|
pw.Text('Jahres-Gesamtsumme: $summePreis €',
|
|
style: pw.TextStyle(
|
|
fontWeight: pw.FontWeight.bold,
|
|
fontSize: 16,
|
|
color: PdfColors.blue700,
|
|
font: font)),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
|
|
// Druckansicht öffnen
|
|
await Printing.layoutPdf(
|
|
onLayout: (PdfPageFormat format) async => doc.save(),
|
|
);
|
|
}
|
|
|
|
void goToTankListPage() async {
|
|
await Get.offAndToNamed(TanklistPage.namedRoute);
|
|
}
|
|
}
|