pedido_ticket.dart 11 KB

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