pedido_ticket.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/JoshiLogo-BN.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('Joshi Papas Tu Sabor tu Estilo',
  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('JoshiPapas',
  114. style: const pw.TextStyle(fontSize: 9)),
  115. pw.Text('Chihuahua',
  116. style: const pw.TextStyle(fontSize: 9)),
  117. ])),
  118. pw.SizedBox(height: 10),
  119. pw.Text('Pedido: ${pedido.folio}',
  120. style: pw.TextStyle(
  121. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  122. pw.SizedBox(height: 10),
  123. pw.Padding(
  124. padding: const pw.EdgeInsets.only(right: 20),
  125. child: pw.Column(children: productList)),
  126. pw.Divider(),
  127. pw.Row(
  128. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  129. children: [
  130. pw.Text('Subtotal:',
  131. style: pw.TextStyle(
  132. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  133. pw.Padding(
  134. padding: const pw.EdgeInsets.only(right: 30),
  135. child: pw.Text(
  136. '\$${numberFormat.format(totalSinDescuento)}',
  137. style: pw.TextStyle(
  138. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  139. ),
  140. ],
  141. ),
  142. if (descuento > 0) ...[
  143. pw.Row(
  144. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  145. children: [
  146. pw.Text('Descuento:',
  147. style: pw.TextStyle(
  148. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  149. pw.Padding(
  150. padding: const pw.EdgeInsets.only(right: 30),
  151. child: pw.Text(
  152. '-\$${numberFormat.format(precioDescuento)}',
  153. style: pw.TextStyle(
  154. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  155. ),
  156. ],
  157. ),
  158. ],
  159. pw.Row(
  160. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  161. children: [
  162. pw.Text('Total:',
  163. style: pw.TextStyle(
  164. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  165. pw.Padding(
  166. padding: const pw.EdgeInsets.only(right: 30),
  167. child: pw.Text(
  168. '\$${numberFormat.format(totalConDescuento)}',
  169. style: pw.TextStyle(
  170. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  171. ),
  172. ],
  173. ),
  174. pw.SizedBox(height: 5),
  175. pw.Padding(
  176. padding: const pw.EdgeInsets.only(right: 15),
  177. child: pw.Text('¡GRACIAS POR SU COMPRA!',
  178. style: pw.TextStyle(
  179. fontSize: 8, fontWeight: pw.FontWeight.bold))),
  180. pw.Divider(),
  181. pw.SizedBox(height: 20),
  182. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  183. ]);
  184. });
  185. }
  186. pw.Page generarPaginaSegundoTicket(Pedido pedido) {
  187. return pw.Page(
  188. pageFormat: PdfPageFormat.roll57,
  189. build: (pw.Context context) {
  190. List<pw.Widget> content = [
  191. pw.SizedBox(height: 20),
  192. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  193. pw.Padding(
  194. padding: const pw.EdgeInsets.only(right: 15),
  195. child: pw.Text('Fecha: ${pedido.peticion}',
  196. style: pw.TextStyle(
  197. fontSize: 9, fontWeight: pw.FontWeight.bold))),
  198. pw.SizedBox(height: 5),
  199. pw.Text('Pedido: ${pedido.folio}',
  200. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  201. pw.SizedBox(height: 10),
  202. pw.Text('Cliente: ${pedido.nombreCliente}',
  203. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)),
  204. pw.SizedBox(height: 10),
  205. ];
  206. content.addAll(pedido.productos
  207. .map((producto) {
  208. final productPrice = producto.producto?.precio ?? 0.0;
  209. final productTotal = productPrice * (producto.cantidad ?? 1);
  210. final toppingsList = producto.toppings.map((topping) {
  211. return pw.Row(
  212. children: [
  213. pw.Text(
  214. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  215. style: const pw.TextStyle(fontSize: 7)),
  216. ],
  217. );
  218. }).toList();
  219. return [
  220. pw.Row(
  221. mainAxisAlignment: pw.MainAxisAlignment.start,
  222. children: [
  223. pw.Expanded(
  224. flex: 3,
  225. child: pw.Text(
  226. producto.producto?.nombre ??
  227. "Producto no especificado",
  228. style: const pw.TextStyle(fontSize: 9)),
  229. ),
  230. pw.Expanded(
  231. flex: 1,
  232. child: pw.Text('x${producto.cantidad}',
  233. style: const pw.TextStyle(fontSize: 9)),
  234. ),
  235. ],
  236. ),
  237. ...toppingsList,
  238. ];
  239. })
  240. .expand((e) => e)
  241. .toList());
  242. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  243. content.add(pw.SizedBox(height: 10));
  244. content.add(pw.Text('Comentarios:',
  245. style:
  246. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9)));
  247. content.add(pw.Padding(
  248. padding: const pw.EdgeInsets.only(right: 15),
  249. child: pw.Text(pedido.comentarios!,
  250. style: const pw.TextStyle(fontSize: 9)),
  251. ));
  252. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  253. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  254. }
  255. content.add(pw.SizedBox(height: 20));
  256. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  257. return pw.Column(
  258. crossAxisAlignment: pw.CrossAxisAlignment.center,
  259. children: content);
  260. });
  261. }
  262. Future<void> printPdf(Uint8List pdfBytes) async {
  263. await Printing.layoutPdf(
  264. onLayout: (PdfPageFormat format) => pdfBytes,
  265. );
  266. }