pedido_ticket.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import 'dart:typed_data';
  2. import 'package:pdf/pdf.dart';
  3. import 'package:pdf/widgets.dart' as pw;
  4. import '../../models/models.dart';
  5. import 'package:printing/printing.dart';
  6. import 'package:flutter/services.dart' show rootBundle;
  7. // Future<Uint8List> generateTicket(Pedido pedido) async {
  8. // final pdf = pw.Document();
  9. // final image = pw.MemoryImage(
  10. // (await rootBundle.load('assets/JoshiLogo-BN.png')).buffer.asUint8List(),
  11. // );
  12. // pdf.addPage(
  13. // pw.Page(
  14. // pageFormat: PdfPageFormat.roll57,
  15. // build: (pw.Context context) {
  16. // return pw.Column(
  17. // crossAxisAlignment: pw.CrossAxisAlignment.center,
  18. // children: [
  19. // pw.Center(child: pw.Image(image, width: 50, height: 50)),
  20. // pw.SizedBox(height: 10),
  21. // pw.Text('Joshi Papas Tu Sabor tu Estilo',
  22. // style:
  23. // pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
  24. // pw.SizedBox(height: 10),
  25. // pw.Text('RFC: XXXX0000', style: pw.TextStyle(fontSize: 9)),
  26. // pw.Text('Dirección: Calle Falsa 123',
  27. // style: pw.TextStyle(fontSize: 9)),
  28. // pw.Text('Ciudad: Ciudad Ejemplo', style: pw.TextStyle(fontSize: 9)),
  29. // pw.Text('Régimen: General de Ley',
  30. // style: pw.TextStyle(fontSize: 9)),
  31. // pw.SizedBox(height: 10),
  32. // pw.Text('Folio: ${pedido.folio}',
  33. // style:
  34. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10)),
  35. // pw.SizedBox(height: 10),
  36. // ...pedido.productos.map(
  37. // (producto) => pw.Row(
  38. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  39. // children: [
  40. // pw.Expanded(
  41. // flex: 2,
  42. // child: pw.Text(
  43. // producto.producto?.nombre ?? "Producto no especificado",
  44. // style: pw.TextStyle(fontSize: 7)),
  45. // ),
  46. // pw.Expanded(
  47. // flex: 1,
  48. // child: pw.Text('x${producto.cantidad}',
  49. // style: pw.TextStyle(fontSize: 9)),
  50. // ),
  51. // pw.Expanded(
  52. // flex: 1,
  53. // child: pw.Text('\$${producto.producto?.precio}',
  54. // style: pw.TextStyle(fontSize: 9)),
  55. // ),
  56. // ],
  57. // ),
  58. // ),
  59. // pw.Divider(),
  60. // pw.Row(
  61. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  62. // children: [
  63. // pw.Text('Total:',
  64. // style: pw.TextStyle(
  65. // fontWeight: pw.FontWeight.bold, fontSize: 9)),
  66. // pw.Padding(
  67. // padding: pw.EdgeInsets.only(right: 20),
  68. // child: pw.Text(
  69. // '\$${pedido.productos.fold<double>(0.0, (sum, p) => sum + (double.parse(p.producto?.precio ?? '0') * (p.cantidad ?? 1)))}',
  70. // style: pw.TextStyle(
  71. // fontWeight: pw.FontWeight.bold, fontSize: 9)),
  72. // ),
  73. // ],
  74. // ),
  75. // pw.SizedBox(height: 5),
  76. // pw.Text('¡GRACIAS POR SU COMPRA!',
  77. // style:
  78. // pw.TextStyle(fontSize: 8, fontWeight: pw.FontWeight.bold)),
  79. // pw.SizedBox(height: 20),
  80. // pw.Divider(),
  81. // pw.SizedBox(height: 10),
  82. // // Existing Ticket Content
  83. // pw.Text('Folio: ${pedido.folio}',
  84. // style:
  85. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  86. // pw.SizedBox(height: 10),
  87. // pw.Text('Cliente: ${pedido.nombreCliente}',
  88. // style:
  89. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  90. // pw.SizedBox(height: 10),
  91. // ...pedido.productos.map(
  92. // (producto) => pw.Row(
  93. // mainAxisAlignment: pw.MainAxisAlignment.start,
  94. // children: [
  95. // pw.Expanded(
  96. // flex: 3,
  97. // child: pw.Text(
  98. // producto.producto?.nombre ?? "Producto no especificado",
  99. // style: pw.TextStyle(fontSize: 9)),
  100. // ),
  101. // pw.Expanded(
  102. // flex: 1,
  103. // child: pw.Text('x${producto.cantidad}',
  104. // style: pw.TextStyle(fontSize: 9)),
  105. // ),
  106. // ],
  107. // ),
  108. // ),
  109. // pw.SizedBox(height: 80),
  110. // ],
  111. // );
  112. // },
  113. // ),
  114. // );
  115. // return Uint8List.fromList(await pdf.save());
  116. // }
  117. Future<Uint8List> primerTicket(Pedido pedido) async {
  118. final pdf = pw.Document();
  119. final image = pw.MemoryImage(
  120. (await rootBundle.load('assets/JoshiLogo-BN.png')).buffer.asUint8List(),
  121. );
  122. pdf.addPage(pw.Page(
  123. pageFormat: PdfPageFormat.roll57,
  124. build: (pw.Context context) {
  125. return pw.Column(
  126. crossAxisAlignment: pw.CrossAxisAlignment.center,
  127. children: [
  128. pw.Padding(
  129. padding: const pw.EdgeInsets.only(right: 20),
  130. child:
  131. pw.Center(child: pw.Image(image, width: 50, height: 50))),
  132. pw.SizedBox(height: 10),
  133. pw.Padding(
  134. padding: const pw.EdgeInsets.only(right: 15),
  135. child: pw.Column(children: [
  136. pw.Text('Joshi Papas Tu Sabor tu Estilo',
  137. style: pw.TextStyle(
  138. fontSize: 12, fontWeight: pw.FontWeight.bold)),
  139. pw.SizedBox(height: 10),
  140. pw.Text('Fecha: ${pedido.peticion}',
  141. style: const pw.TextStyle(fontSize: 9)),
  142. pw.Text('RFC: XXXX0000',
  143. style: const pw.TextStyle(fontSize: 9)),
  144. pw.Text('Dirección: Calle Falsa 123',
  145. style: const pw.TextStyle(fontSize: 9)),
  146. pw.Text('Ciudad: Ciudad Ejemplo',
  147. style: const pw.TextStyle(fontSize: 9)),
  148. pw.Text('Régimen: General de Ley',
  149. style: const pw.TextStyle(fontSize: 9)),
  150. ])),
  151. pw.SizedBox(height: 10),
  152. pw.Text('Folio: ${pedido.folio}',
  153. style: pw.TextStyle(
  154. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  155. pw.SizedBox(height: 10),
  156. ...pedido.productos.map(
  157. (producto) => pw.Row(
  158. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  159. children: [
  160. pw.Expanded(
  161. flex: 2,
  162. child: pw.Text(
  163. producto.producto?.nombre ??
  164. "Producto no especificado",
  165. style: const pw.TextStyle(fontSize: 7)),
  166. ),
  167. pw.Expanded(
  168. flex: 1,
  169. child: pw.Text('x${producto.cantidad}',
  170. style: const pw.TextStyle(fontSize: 9)),
  171. ),
  172. pw.Expanded(
  173. flex: 1,
  174. child: pw.Text('\$${producto.producto?.precio}',
  175. style: const pw.TextStyle(fontSize: 9)),
  176. ),
  177. ],
  178. ),
  179. ),
  180. pw.Divider(),
  181. pw.Row(
  182. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  183. children: [
  184. pw.Text('Total:',
  185. style: pw.TextStyle(
  186. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  187. pw.Padding(
  188. padding: const pw.EdgeInsets.only(right: 20),
  189. child: pw.Text(
  190. '\$${pedido.productos.fold<double>(0.0, (sum, p) => sum + (double.parse(p.producto?.precio ?? '0') * (p.cantidad ?? 1)))}',
  191. style: pw.TextStyle(
  192. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  193. ),
  194. ],
  195. ),
  196. pw.SizedBox(height: 5),
  197. pw.Padding(
  198. padding: const pw.EdgeInsets.only(right: 15),
  199. child: pw.Text('¡GRACIAS POR SU COMPRA!',
  200. style: pw.TextStyle(
  201. fontSize: 8, fontWeight: pw.FontWeight.bold))),
  202. pw.SizedBox(height: 20),
  203. pw.Divider(),
  204. ]);
  205. }));
  206. return Uint8List.fromList(await pdf.save());
  207. }
  208. Future<Uint8List> segundoTicket(Pedido pedido) async {
  209. final pdf = pw.Document();
  210. pdf.addPage(pw.Page(
  211. pageFormat: PdfPageFormat.roll57,
  212. build: (pw.Context context) {
  213. List<pw.Widget> content = [
  214. pw.SizedBox(height: 10),
  215. pw.Padding(
  216. padding: const pw.EdgeInsets.only(right: 15),
  217. child: pw.Text('Fecha: ${pedido.peticion}',
  218. style: pw.TextStyle(
  219. fontSize: 9, fontWeight: pw.FontWeight.bold))),
  220. pw.SizedBox(height: 5),
  221. pw.Text('Folio: ${pedido.folio}',
  222. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  223. pw.SizedBox(height: 10),
  224. pw.Text('Cliente: ${pedido.nombreCliente}',
  225. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  226. pw.SizedBox(height: 10),
  227. ];
  228. content.addAll(pedido.productos.map((producto) => pw.Row(
  229. mainAxisAlignment: pw.MainAxisAlignment.start,
  230. children: [
  231. pw.Expanded(
  232. flex: 3,
  233. child: pw.Text(
  234. producto.producto?.nombre ?? "Producto no especificado",
  235. style: const pw.TextStyle(fontSize: 9)),
  236. ),
  237. pw.Expanded(
  238. flex: 1,
  239. child: pw.Text('x${producto.cantidad}',
  240. style: const pw.TextStyle(fontSize: 9)),
  241. ),
  242. ],
  243. )));
  244. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  245. content.add(pw.SizedBox(height: 10));
  246. content.add(pw.Text('Comentarios:',
  247. style:
  248. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)));
  249. content.add(pw.Padding(
  250. padding: const pw.EdgeInsets.only(right: 15),
  251. child: pw.Text(pedido.comentarios!,
  252. style: const pw.TextStyle(fontSize: 9)),
  253. ));
  254. }
  255. content.add(
  256. pw.SizedBox(height: 20)); // Some spacing before finalizing the page
  257. return pw.Column(
  258. crossAxisAlignment: pw.CrossAxisAlignment.center,
  259. children: content);
  260. }));
  261. return Uint8List.fromList(await pdf.save());
  262. }
  263. Future<void> printPdf(Uint8List pdfBytes) async {
  264. await Printing.layoutPdf(
  265. onLayout: (PdfPageFormat format) => pdfBytes,
  266. );
  267. }
  268. void printTickets(Pedido pedido) async {
  269. Uint8List firstTicket = await primerTicket(pedido);
  270. await printPdf(firstTicket);
  271. Uint8List secondTicket = await segundoTicket(pedido);
  272. await printPdf(secondTicket);
  273. }