first commit
This commit is contained in:
114
lib/pdf_report/income_report/income_report_excel.dart
Normal file
114
lib/pdf_report/income_report/income_report_excel.dart
Normal file
@@ -0,0 +1,114 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:excel/excel.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../../Screens/Income/Model/income_modle.dart';
|
||||
import '../../model/business_info_model.dart';
|
||||
import '../ledger_report/generate_pdf_date_range.dart';
|
||||
|
||||
Future<void> generateIncomeReportExcel(
|
||||
BuildContext context,
|
||||
List<Income>? data,
|
||||
BusinessInformationModel? business,
|
||||
String selectedTime,
|
||||
DateTime? fromDate,
|
||||
DateTime? toDate,
|
||||
) async {
|
||||
EasyLoading.show(status: 'Generating Excel');
|
||||
|
||||
try {
|
||||
final excel = Excel.createExcel();
|
||||
final sheet = excel['Income Report'];
|
||||
|
||||
final pdfRange = getPdfDateRangeForSelectedTime(
|
||||
selectedTime,
|
||||
fromDate: fromDate,
|
||||
toDate: toDate,
|
||||
);
|
||||
|
||||
final fromStr = pdfRange['from']!;
|
||||
final toStr = pdfRange['to']!;
|
||||
|
||||
double totalAmount = data?.fold(0, (sum, item) => sum! + (item.amount ?? 0)) ?? 0;
|
||||
|
||||
// // Styles
|
||||
// final businessStyle = CellStyle(bold: true, fontSize: 12);
|
||||
// final titleStyle = CellStyle(bold: true, fontSize: 12);
|
||||
// final headerStyle = CellStyle(bold: true, fontSize: 12);
|
||||
// final footerStyle = CellStyle(bold: true, fontSize: 12);
|
||||
|
||||
// Row 1: Company Name
|
||||
sheet.appendRow([TextCellValue(business?.data?.companyName ?? '')]);
|
||||
sheet.cell(CellIndex.indexByString("A1")).cellStyle;
|
||||
|
||||
// Row 2: Report Title
|
||||
sheet.appendRow([TextCellValue('Income Report')]);
|
||||
sheet.cell(CellIndex.indexByString("A2")).cellStyle;
|
||||
|
||||
// Row 3: Duration
|
||||
sheet.appendRow([
|
||||
TextCellValue('Duration: $fromStr to $toStr'),
|
||||
]);
|
||||
|
||||
// Row 4: Empty for spacing
|
||||
sheet.appendRow([]);
|
||||
|
||||
// Row 5: Header
|
||||
final headerRowIndex = sheet.maxRows;
|
||||
sheet.appendRow([
|
||||
TextCellValue('SL'),
|
||||
TextCellValue('Date'),
|
||||
TextCellValue('Income For'),
|
||||
TextCellValue('Category'),
|
||||
TextCellValue('Amount'),
|
||||
]);
|
||||
|
||||
sheet.appendRow([]);
|
||||
|
||||
// Apply bold style to each header cell only
|
||||
for (var i = 0; i < 5; i++) {
|
||||
sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: headerRowIndex)).cellStyle;
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
sheet.appendRow([
|
||||
TextCellValue('${i + 1}'),
|
||||
TextCellValue(DateFormat('dd-MM-yyyy').format(DateTime.parse(data[i].incomeDate.toString()))),
|
||||
TextCellValue(data[i].incomeFor ?? 'n/a'),
|
||||
TextCellValue(data[i].category?.categoryName ?? 'n/a'),
|
||||
TextCellValue(data[i].amount?.toStringAsFixed(2) ?? '0.00'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
final totalRowIndex = sheet.maxRows;
|
||||
sheet.appendRow([
|
||||
TextCellValue('Total'),
|
||||
TextCellValue(''),
|
||||
TextCellValue(''),
|
||||
TextCellValue(''),
|
||||
TextCellValue(totalAmount.toStringAsFixed(2)),
|
||||
]);
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: totalRowIndex)).cellStyle;
|
||||
}
|
||||
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final filePath = '${dir.path}/${business?.data?.companyName ?? "Company"}_Income_Report.xlsx';
|
||||
final file = File(filePath);
|
||||
await file.writeAsBytes(excel.encode()!);
|
||||
|
||||
EasyLoading.showSuccess('Report Generated');
|
||||
await OpenFile.open(filePath);
|
||||
} catch (e) {
|
||||
EasyLoading.showError('Error: $e');
|
||||
debugPrint('Error during Excel generation: $e');
|
||||
}
|
||||
}
|
||||
217
lib/pdf_report/income_report/income_report_pdf.dart
Normal file
217
lib/pdf_report/income_report/income_report_pdf.dart
Normal file
@@ -0,0 +1,217 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:mobile_pos/constant.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
import 'package:printing/printing.dart';
|
||||
|
||||
import '../../Screens/Income/Model/income_modle.dart';
|
||||
import '../../Screens/PDF/pdf.dart';
|
||||
import '../../model/business_info_model.dart';
|
||||
import '../ledger_report/generate_pdf_date_range.dart';
|
||||
|
||||
Future<void> generateIncomeReportPdf(
|
||||
BuildContext context,
|
||||
List<Income>? data,
|
||||
BusinessInformationModel? business,
|
||||
DateTime? fromDate,
|
||||
DateTime? toDate,
|
||||
String selectedTime,
|
||||
) async {
|
||||
final pw.Document pdf = pw.Document();
|
||||
final interFont = await PdfGoogleFonts.notoSansRegular();
|
||||
|
||||
// Show loading indicator
|
||||
EasyLoading.show(status: 'Generating PDF');
|
||||
double totalAmount = 0;
|
||||
|
||||
// Calculate totals from data
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
totalAmount += item.amount ?? 0;
|
||||
}
|
||||
}
|
||||
// print('--font family---$ttf---------------');
|
||||
|
||||
// Calculate correct PDF date range
|
||||
final pdfDateRange = getPdfDateRangeForSelectedTime(
|
||||
selectedTime,
|
||||
fromDate: fromDate,
|
||||
toDate: toDate,
|
||||
);
|
||||
|
||||
final fromDateStr = pdfDateRange['from']!;
|
||||
final toDateStr = pdfDateRange['to']!;
|
||||
|
||||
try {
|
||||
pdf.addPage(pw.MultiPage(
|
||||
pageFormat: PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
|
||||
margin: pw.EdgeInsets.symmetric(horizontal: 16),
|
||||
//----------------pdf header--------------
|
||||
header: (pw.Context context) {
|
||||
return pw.Center(
|
||||
child: pw.Column(
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.center,
|
||||
children: [
|
||||
pw.Text(
|
||||
business?.data?.companyName.toString() ?? '',
|
||||
style: pw.TextStyle(
|
||||
// font: interFont,
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
pw.Text(
|
||||
// 'বিক্রয় প্রতিবেদন',
|
||||
'Income Report',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
// font: ttf,
|
||||
),
|
||||
),
|
||||
pw.SizedBox(height: 4),
|
||||
pw.Text(
|
||||
'$fromDateStr TO $toDateStr',
|
||||
style: pw.TextStyle(
|
||||
font: interFont,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
//-----------------pdf footer-------------
|
||||
footer: (pw.Context context) {
|
||||
return pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
pw.Text('${business?.data?.developByLevel ?? ''} ${business?.data?.developBy ?? ''}'),
|
||||
pw.Text('Page-${context.pageNumber}'),
|
||||
],
|
||||
);
|
||||
},
|
||||
build: (pw.Context context) {
|
||||
final List<List<String>> tableData = [];
|
||||
|
||||
for (int i = 0; i < data!.length; i++) {
|
||||
tableData.add([
|
||||
'${i + 1}',
|
||||
DateFormat('dd-MM-yyyy').format(DateTime.parse(data[i].incomeDate.toString())),
|
||||
data[i].incomeFor ?? 'n/a',
|
||||
data[i].category?.categoryName.toString() ?? 'n/a',
|
||||
data[i].amount.toString(),
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
pw.SizedBox(height: 16),
|
||||
|
||||
// Main Table
|
||||
pw.Table.fromTextArray(
|
||||
headers: [
|
||||
'SL',
|
||||
'Date',
|
||||
'Income For',
|
||||
'Category',
|
||||
'Amount',
|
||||
],
|
||||
data: tableData,
|
||||
headerDecoration: const pw.BoxDecoration(
|
||||
color: PdfColor.fromInt(0xffC52127),
|
||||
),
|
||||
border: pw.TableBorder.all(color: PdfColor.fromInt(0xffD9D9D9)),
|
||||
headerStyle: pw.TextStyle(
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
color: PdfColors.white,
|
||||
),
|
||||
rowDecoration: const pw.BoxDecoration(
|
||||
color: PdfColors.white,
|
||||
),
|
||||
oddRowDecoration: pw.BoxDecoration(
|
||||
color: PdfColor.fromInt(0xffF7F7F7),
|
||||
),
|
||||
cellPadding: const pw.EdgeInsets.all(8),
|
||||
columnWidths: <int, pw.TableColumnWidth>{
|
||||
0: const pw.FlexColumnWidth(1),
|
||||
1: const pw.FlexColumnWidth(3),
|
||||
2: const pw.FlexColumnWidth(4),
|
||||
3: const pw.FlexColumnWidth(4),
|
||||
4: const pw.FlexColumnWidth(2),
|
||||
},
|
||||
cellAlignments: {
|
||||
0: pw.Alignment.center,
|
||||
1: pw.Alignment.center,
|
||||
2: pw.Alignment.center,
|
||||
3: pw.Alignment.center,
|
||||
4: pw.Alignment.center,
|
||||
},
|
||||
),
|
||||
// Totals row (styled to match)
|
||||
pw.Table.fromTextArray(
|
||||
border: const pw.TableBorder(
|
||||
left: pw.BorderSide(color: PdfColor.fromInt(0xffD9D9D9)),
|
||||
right: pw.BorderSide(color: PdfColor.fromInt(0xffD9D9D9)),
|
||||
bottom: pw.BorderSide(color: PdfColor.fromInt(0xffD9D9D9)),
|
||||
),
|
||||
columnWidths: <int, pw.TableColumnWidth>{
|
||||
0: const pw.FlexColumnWidth(2),
|
||||
1: const pw.FlexColumnWidth(2),
|
||||
2: const pw.FlexColumnWidth(4),
|
||||
3: const pw.FlexColumnWidth(4),
|
||||
4: const pw.FlexColumnWidth(2),
|
||||
},
|
||||
data: [
|
||||
[
|
||||
'Total',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
totalAmount.toStringAsFixed(2),
|
||||
]
|
||||
],
|
||||
headerDecoration: const pw.BoxDecoration(
|
||||
color: PdfColor.fromInt(0xffC52127),
|
||||
),
|
||||
headerStyle: pw.TextStyle(
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
color: PdfColors.white,
|
||||
),
|
||||
cellAlignment: pw.Alignment.center,
|
||||
cellPadding: const pw.EdgeInsets.all(8),
|
||||
),
|
||||
];
|
||||
}));
|
||||
|
||||
final byteData = await pdf.save();
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final file = File('${dir.path}/${appsName}-income.pdf');
|
||||
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
|
||||
EasyLoading.showSuccess('Generate Complete');
|
||||
//print pdf
|
||||
if (context.mounted) {
|
||||
await Printing.layoutPdf(
|
||||
name: 'Income Report',
|
||||
usePrinterSettings: true,
|
||||
dynamicLayout: true,
|
||||
forceCustomPrintPaper: true,
|
||||
onLayout: (PdfPageFormat format) async => pdf.save(),
|
||||
);
|
||||
}
|
||||
// Navigator.push(
|
||||
// context,
|
||||
// MaterialPageRoute(
|
||||
// builder: (context) => PDFViewerPage(path: file.path),
|
||||
// ),
|
||||
// );
|
||||
} catch (e) {
|
||||
EasyLoading.showError('Error: $e');
|
||||
print('Error during PDF generation: $e');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user