pedido_ticket.dart 11 KB

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