Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
collection_voucher_pdf.dart
Go to the documentation of this file.
1import 'dart:developer';
2import 'dart:io';
3import 'dart:convert';
4import 'package:flutter/services.dart';
5import 'package:flutter_screenutil/flutter_screenutil.dart';
6import 'package:open_file/open_file.dart';
7import 'package:pdf/pdf.dart';
8import 'package:pdf/widgets.dart' as pw;
9import 'package:path_provider/path_provider.dart';
10
11import '../../constants/assets.dart';
12import '../../services/user_info_service.dart';
13import '../../common/models/collection_voucher_document_model.dart';
14
16 final pdf = pw.Document();
17
18 final font = await rootBundle.load('assets/fonts/cairo_regular_font.ttf');
19 final ttf = pw.Font.ttf(font);
20
21 final theme = pw.ThemeData.withFont(
22 base: ttf,
23 );
24
25 pw.MemoryImage? signatureImage;
27 try {
28 final signatureBytes = base64Decode(UserInfoService.instance.userInfo!.signature ?? '');
29 signatureImage = pw.MemoryImage(signatureBytes);
30 } catch (e) {
31 log('Error decoding signature: $e');
32 }
33 }
34
35 final qrCode = pw.BarcodeWidget(
36 barcode: pw.Barcode.qrCode(),
37 data: model.collection.name ?? '',
38 width: 50,
39 height: 50,
40 );
41
42 final directorSignature = pw.Column(
43 children: [
44 pw.Text(
45 'Driver / Collector : ${UserInfoService.instance.userInfo?.name ?? '--'}',
46 style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold),
47 ),
48 if (signatureImage != null)
49 pw.Container(
50 width: 90,
51 height: 90,
52 child: pw.Center(
53 child: pw.Image(
55 fit: pw.BoxFit.contain,
56 ),
57 ),
58 ),
59 ],
60 );
61
62 final collectorSignature = pw.Column(
63 children: [
64 pw.Text(
65 'Collection Point : ${model.collection.partnerId?.name ?? 'Shah waqar'}',
66 style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold),
67 ),
68 pw.Container(
69 width: 90,
70 height: 90,
71 child: pw.Center(
72 child: pw.Image(
73 pw.MemoryImage(
74 (await rootBundle.load(Assets.directorSign)).buffer.asUint8List(),
75 ),
76 fit: pw.BoxFit.contain,
77 ),
78 ),
79 ),
80 ],
81 );
82
83 pdf.addPage(
84 pw.Page(
85 textDirection: pw.TextDirection.ltr,
86 theme: theme,
87 build: (context) {
88 return pw.Column(
89 crossAxisAlignment: pw.CrossAxisAlignment.center,
90 children: [
91 pw.Text(
92 'Collection Voucher',
93 style: pw.TextStyle(fontSize: 20, fontWeight: pw.FontWeight.bold),
94 ),
95 pw.SizedBox(height: 8),
96 pw.Text(
97 'Document Number : ${model.collection.name ?? ''}',
98 style: pw.TextStyle(
99 fontSize: 16,
100 fontWeight: pw.FontWeight.normal,
101 color: PdfColor.fromHex('#1402a3'),
102 ),
103 ),
104 pw.SizedBox(height: 25),
105 pw.Align(
106 alignment: pw.Alignment.centerLeft,
107 child: pw.Column(
108 crossAxisAlignment: pw.CrossAxisAlignment.start,
109
110 children: [
111 pw.Text(
112 'Name:',
113 style: pw.TextStyle(fontSize: 15, fontWeight: pw.FontWeight.bold),
114 ),
115 pw.Text(
116 model.collection.partnerId?.name ?? '',
117 style: pw.TextStyle(
118 fontSize: 14, fontWeight: pw.FontWeight.normal),
119 ),
120 pw.SizedBox(height: 8),
121 pw.Text(
122 'Address:',
123 style: pw.TextStyle(fontSize: 15, fontWeight: pw.FontWeight.bold),
124 ),
125 pw.Text(
126 model.collection.partnerId?.street ?? '',
127 textDirection: pw.TextDirection.rtl,
128 style: pw.TextStyle(
129 fontSize: 14, fontWeight: pw.FontWeight.normal,
130 ),
131 ),
132 ],
133 ),
134 ),
135
136 pw.SizedBox(height: 30),
137 pw.Table(
138 border: pw.TableBorder.all(),
139 children: [
140 pw.TableRow(
141 children: [
142 pw.Container(
143 padding: const pw.EdgeInsets.all(8),
144 child: pw.Text(
145 'Collection Date',
146 style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
147 ),
148 ),
149 pw.Container(
150 padding: const pw.EdgeInsets.all(8),
151 child: pw.Text(model.collection.confirmationDate?.split('T')[0] ?? '--'),
152 ),
153 pw.Container(
154 padding: const pw.EdgeInsets.all(8),
155 child: pw.Text(
156 'Payment Bill Ref',
157 style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
158 ),
159 ),
160 pw.Container(
161 padding: const pw.EdgeInsets.all(8),
162 child: pw.Text(model.collection.paymentBillRef ?? '--'),
163 ),
164 ],
165 ),
166 pw.TableRow(
167 children: [
168 pw.Container(
169 padding: const pw.EdgeInsets.all(8),
170 child: pw.Text(
171 'Collection Order Ref',
172 style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
173 ),
174 ),
175 pw.Container(
176 padding: const pw.EdgeInsets.all(8),
177 child: pw.Text(model.collection.collectionOrderRef ?? ''),
178 ),
179 pw.Container(
180 padding: const pw.EdgeInsets.all(8),
181 child: pw.Text(
182 'Payment Method',
183 style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
184 ),
185 ),
186 pw.Container(
187 padding: const pw.EdgeInsets.all(8),
188 child: pw.Text('Deferred Payment'),
189 ),
190 ],
191 ),
192 // Removed the last row (Collection Confirmation Ref and Bill Status)
193 ],
194 ),
195 pw.SizedBox(height: 25),
196 // ignore: deprecated_member_use
197
198 pw.TableHelper.fromTextArray(
199 context: context,
200 headers: [
201 'Product',
202 'Quantity Received',
203 'Quantity Billed',
204 // Removed 'Unit Price' from headers
205 ],
206 data: List.generate(1, (index) {
207 return [
208 model.product ?? '--',
209 '${model.quantityReceived ?? ''} Kg',
210 '${model.quantityBilled ?? ''} Kg',
211 // Removed '2.2' (Unit Price) from data
212 ];
213 }),
214 headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
215 cellAlignment: pw.Alignment.centerLeft,
216 ),
217
218 // Removed the Payment Amount container
219
220 pw.SizedBox(height: 30),
221 pw.Row(
222 mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
223 children: [directorSignature, collectorSignature],
224 ),
225 pw.SizedBox(height: 30),
226 pw.Row(
227 children: [
228 pw.Spacer(),
229 pw.Column(
230 children: [
231 pw.Row(
232 mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
233 children: [
234 qrCode,
235 pw.SizedBox(width: 10),
236 ],
237 ),
238 pw.SizedBox(height: 10.sp),
239 pw.Text(
240 'STRICTLY CONFIDENTIAL',
241 style: pw.TextStyle(
242 fontSize: 8, fontWeight: pw.FontWeight.normal,
243 color: const PdfColor(0.5, 0.5, 0.5),
244 ),
245 ),
246 ]
247 )
248 ],
249 ),
250 ],
251 );
252 },
253 ),
254 );
255
256 final output = await getTemporaryDirectory();
257 final file = File('${output.path}/collection_voucher_document.pdf');
258 await file.writeAsBytes(await pdf.save());
259
260 OpenFile.open(file.path);
261 }
class App extends StatefulWidget build(BuildContext context)
Definition app.dart:31
static const String directorSign
Definition assets.dart:5
static final UserInfoService instance
final String signature
Definition user_info.dart:5
Future< void > generateCollectionVoucher(CollectionVoucherDocumentModel model) async
final String signatureImage
final Widget child
final Color color
Definition failures.dart:1