pedido_ticket.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.SizedBox(height: 20),
  190. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  191. pw.Padding(
  192. padding: const pw.EdgeInsets.only(right: 15),
  193. child: pw.Text('Fecha: ${pedido.peticion}',
  194. style: pw.TextStyle(
  195. fontSize: 9, fontWeight: pw.FontWeight.bold))),
  196. pw.SizedBox(height: 5),
  197. pw.Text('Pedido: ${pedido.folio}',
  198. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  199. pw.SizedBox(height: 10),
  200. pw.Text('Cliente: ${pedido.nombreCliente}',
  201. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  202. pw.SizedBox(height: 10),
  203. ];
  204. content.addAll(pedido.productos
  205. .map((producto) {
  206. final productPrice = producto.producto?.precio ?? 0.0;
  207. final productTotal = productPrice * (producto.cantidad ?? 1);
  208. final toppingsList = producto.toppings.map((topping) {
  209. return pw.Row(
  210. children: [
  211. pw.Text(
  212. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  213. style: const pw.TextStyle(fontSize: 7)),
  214. ],
  215. );
  216. }).toList();
  217. return [
  218. pw.Row(
  219. mainAxisAlignment: pw.MainAxisAlignment.start,
  220. children: [
  221. pw.Expanded(
  222. flex: 3,
  223. child: pw.Text(
  224. producto.producto?.nombre ??
  225. "Producto no especificado",
  226. style: const pw.TextStyle(fontSize: 9)),
  227. ),
  228. pw.Expanded(
  229. flex: 1,
  230. child: pw.Text('x${producto.cantidad}',
  231. style: const pw.TextStyle(fontSize: 9)),
  232. ),
  233. ],
  234. ),
  235. ...toppingsList,
  236. ];
  237. })
  238. .expand((e) => e)
  239. .toList());
  240. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  241. content.add(pw.SizedBox(height: 10));
  242. content.add(pw.Text('Comentarios:',
  243. style:
  244. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)));
  245. content.add(pw.Padding(
  246. padding: const pw.EdgeInsets.only(right: 15),
  247. child: pw.Text(pedido.comentarios!,
  248. style: const pw.TextStyle(fontSize: 9)),
  249. ));
  250. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  251. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  252. }
  253. content.add(pw.SizedBox(height: 20));
  254. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  255. return pw.Column(
  256. crossAxisAlignment: pw.CrossAxisAlignment.center,
  257. children: content);
  258. });
  259. }
  260. Future<void> printPdf(Uint8List pdfBytes) async {
  261. await Printing.layoutPdf(
  262. onLayout: (PdfPageFormat format) => pdfBytes,
  263. );
  264. }