first commit
This commit is contained in:
133
lib/pdf_report/purchase_report/purchase_report_excel.dart
Normal file
133
lib/pdf_report/purchase_report/purchase_report_excel.dart
Normal file
@@ -0,0 +1,133 @@
|
||||
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:mobile_pos/Screens/Purchase/Model/purchase_transaction_model.dart';
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../../model/business_info_model.dart';
|
||||
import '../../model/sale_transaction_model.dart';
|
||||
|
||||
Future<void> generatePurchaseReportExcel(
|
||||
BuildContext context,
|
||||
List<PurchaseTransaction>? data,
|
||||
BusinessInformationModel? business,
|
||||
DateTime? fromDate,
|
||||
DateTime? toDate,
|
||||
) async {
|
||||
EasyLoading.show(status: 'Generating Excel');
|
||||
|
||||
try {
|
||||
final excel = Excel.createExcel();
|
||||
final sheet = excel['Purchase Report'];
|
||||
|
||||
// ---- DATE RANGE ----
|
||||
String fromStr = fromDate != null ? DateFormat('dd-MM-yyyy').format(fromDate) : '';
|
||||
String toStr = toDate != null ? DateFormat('dd-MM-yyyy').format(toDate) : '';
|
||||
|
||||
// ---- TOTAL CALCULATION ----
|
||||
double total = 0;
|
||||
double totalPaid = 0;
|
||||
double totalDue = 0;
|
||||
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
total += item.totalAmount ?? 0;
|
||||
totalPaid += item.paidAmount ?? 0;
|
||||
totalDue += item.dueAmount ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------- //
|
||||
// HEADER ROWS //
|
||||
// ----------------------------- //
|
||||
|
||||
// Row 1: Company Name
|
||||
sheet.appendRow([TextCellValue(business?.data?.companyName ?? '')]);
|
||||
|
||||
// Row 2: Report Title
|
||||
sheet.appendRow([TextCellValue('Purchase Report')]);
|
||||
|
||||
// Row 3: Duration
|
||||
sheet.appendRow([
|
||||
TextCellValue('Duration: $fromStr to $toStr'),
|
||||
]);
|
||||
|
||||
// Row 4: Empty Space
|
||||
sheet.appendRow([]);
|
||||
|
||||
// Row 5: Table Headers
|
||||
final headerStartRow = sheet.maxRows;
|
||||
sheet.appendRow([
|
||||
TextCellValue('Reference'),
|
||||
TextCellValue('Date'),
|
||||
TextCellValue('Customer'),
|
||||
TextCellValue('Status'),
|
||||
TextCellValue('Total'),
|
||||
TextCellValue('Paid'),
|
||||
TextCellValue('Due'),
|
||||
]);
|
||||
|
||||
// Apply bold header style
|
||||
for (int i = 0; i < 7; i++) {
|
||||
sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: headerStartRow)).cellStyle;
|
||||
}
|
||||
|
||||
// Row 6: Space before data
|
||||
sheet.appendRow([]);
|
||||
|
||||
// ----------------------------- //
|
||||
// TABLE DATA ROWS //
|
||||
// ----------------------------- //
|
||||
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
sheet.appendRow([
|
||||
TextCellValue(item.invoiceNumber ?? 'n/a'),
|
||||
TextCellValue(DateFormat('dd-MM-yyyy').format(DateTime.parse(item.purchaseDate.toString()))),
|
||||
TextCellValue(item.party?.name ?? 'n/a'),
|
||||
TextCellValue(item.isPaid == true ? 'Paid' : 'Unpaid'),
|
||||
TextCellValue((item.totalAmount ?? 0).toStringAsFixed(2)),
|
||||
TextCellValue((item.paidAmount ?? 0).toStringAsFixed(2)),
|
||||
TextCellValue((item.dueAmount ?? 0).toStringAsFixed(2)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------- //
|
||||
// TOTAL ROW //
|
||||
// ----------------------------- //
|
||||
final totalRowIndex = sheet.maxRows;
|
||||
|
||||
sheet.appendRow([
|
||||
TextCellValue('Total'),
|
||||
TextCellValue(''),
|
||||
TextCellValue(''),
|
||||
TextCellValue(''),
|
||||
TextCellValue(total.toStringAsFixed(2)),
|
||||
TextCellValue(totalPaid.toStringAsFixed(2)),
|
||||
TextCellValue(totalDue.toStringAsFixed(2)),
|
||||
]);
|
||||
|
||||
// Apply bold style
|
||||
for (int i = 0; i < 7; i++) {
|
||||
sheet.cell(CellIndex.indexByColumnRow(columnIndex: i, rowIndex: totalRowIndex)).cellStyle;
|
||||
}
|
||||
|
||||
// ----------------------------- //
|
||||
// SAVE FILE //
|
||||
// ----------------------------- //
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final filePath = '${dir.path}/${business?.data?.companyName ?? "Company"}_Purchase_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('Excel Generation Error: $e');
|
||||
}
|
||||
}
|
||||
239
lib/pdf_report/purchase_report/purchase_report_pdf.dart
Normal file
239
lib/pdf_report/purchase_report/purchase_report_pdf.dart
Normal file
@@ -0,0 +1,239 @@
|
||||
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/Screens/Purchase/Model/purchase_transaction_model.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 '../../model/business_info_model.dart';
|
||||
|
||||
Future<void> generatePurchaseReport(BuildContext context, List<PurchaseTransaction>? data, BusinessInformationModel? business, DateTime? fromDate, DateTime? toDate) async {
|
||||
final pw.Document pdf = pw.Document();
|
||||
final interFont = await PdfGoogleFonts.notoSansRegular();
|
||||
|
||||
// Show loading indicator
|
||||
EasyLoading.show(status: 'Generating PDF');
|
||||
double total = 0;
|
||||
double totalDue = 0;
|
||||
double totalPaid = 0;
|
||||
|
||||
// Calculate totals from data
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
final totalAmounts = item.totalAmount ?? 0;
|
||||
total += totalAmounts;
|
||||
}
|
||||
}
|
||||
|
||||
//total due
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
final due = item.paidAmount ?? 0;
|
||||
totalDue += due;
|
||||
}
|
||||
}
|
||||
|
||||
//total paid
|
||||
if (data != null) {
|
||||
for (var item in data) {
|
||||
final paid = item.dueAmount ?? 0;
|
||||
totalPaid += paid;
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
// 'বিক্রয় প্রতিবেদন',
|
||||
'Purchase Report',
|
||||
style: pw.TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: pw.FontWeight.bold,
|
||||
// font: ttf,
|
||||
),
|
||||
),
|
||||
pw.SizedBox(height: 4),
|
||||
pw.Text(
|
||||
fromDate != null ? 'Duration: ${DateFormat('dd-MM-yyyy').format(fromDate)} to ${DateFormat('dd-MM-yyyy').format(toDate!)}' : '',
|
||||
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}',
|
||||
data[i].invoiceNumber ?? 'n/a',
|
||||
DateFormat('dd-MM-yyyy').format(DateTime.parse(data[i].purchaseDate.toString())),
|
||||
data[i].party!.name ?? 'n/a',
|
||||
// data[i]. ?? 'n/a',
|
||||
data[i].isPaid == true ? 'Paid' : 'Unpaid',
|
||||
data[i].totalAmount.toString(),
|
||||
data[i].paidAmount.toString(),
|
||||
data[i].dueAmount.toString(),
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
pw.SizedBox(height: 16),
|
||||
|
||||
// Main Table
|
||||
pw.Table.fromTextArray(
|
||||
headers: [
|
||||
// 'SL',
|
||||
'Reference',
|
||||
'Date',
|
||||
'Supplier',
|
||||
'Status',
|
||||
'Total',
|
||||
'Paid',
|
||||
'Due',
|
||||
],
|
||||
data: tableData,
|
||||
headerDecoration: const pw.BoxDecoration(
|
||||
color: PdfColor.fromInt(0xffC52127),
|
||||
),
|
||||
cellAlignment: pw.Alignment.center,
|
||||
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(4),
|
||||
1: const pw.FlexColumnWidth(5),
|
||||
2: const pw.FlexColumnWidth(4),
|
||||
3: const pw.FlexColumnWidth(3),
|
||||
4: const pw.FlexColumnWidth(4),
|
||||
5: const pw.FlexColumnWidth(4),
|
||||
6: const pw.FlexColumnWidth(4),
|
||||
},
|
||||
cellAlignments: {
|
||||
0: pw.Alignment.center,
|
||||
1: pw.Alignment.center,
|
||||
2: pw.Alignment.center,
|
||||
3: pw.Alignment.center,
|
||||
4: pw.Alignment.center,
|
||||
5: pw.Alignment.center,
|
||||
6: 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(4),
|
||||
1: const pw.FlexColumnWidth(5),
|
||||
2: const pw.FlexColumnWidth(4),
|
||||
3: const pw.FlexColumnWidth(3),
|
||||
4: const pw.FlexColumnWidth(4),
|
||||
5: const pw.FlexColumnWidth(4),
|
||||
6: const pw.FlexColumnWidth(4),
|
||||
},
|
||||
cellAlignments: {
|
||||
0: pw.Alignment.center,
|
||||
1: pw.Alignment.center,
|
||||
2: pw.Alignment.center,
|
||||
3: pw.Alignment.center,
|
||||
4: pw.Alignment.center,
|
||||
5: pw.Alignment.center,
|
||||
6: pw.Alignment.center,
|
||||
},
|
||||
data: [
|
||||
[
|
||||
'Total',
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
formatPointNumber(total),
|
||||
formatPointNumber(totalPaid),
|
||||
formatPointNumber(totalDue),
|
||||
]
|
||||
],
|
||||
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}-purchase-report.pdf');
|
||||
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
|
||||
EasyLoading.showSuccess('Generate Complete');
|
||||
//print pdf
|
||||
if (context.mounted) {
|
||||
await Printing.layoutPdf(
|
||||
name: 'Purchase 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