pedido_ticket.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. bool ticketVentaActivo =
  16. await Provider.of<VariableViewModel>(context, listen: false)
  17. .isVariableActive('ticket_venta');
  18. final pdf = pw.Document();
  19. final image = pw.MemoryImage(
  20. (await rootBundle.load('assets/icono-BN.png')).buffer.asUint8List(),
  21. );
  22. if (ticketVentaActivo) {
  23. pdf.addPage(
  24. generarPaginaPrimerTicket(pedido, image),
  25. );
  26. }
  27. if (ticketCocinaActivo) {
  28. pdf.addPage(
  29. generarPaginaSegundoTicket(pedido),
  30. );
  31. }
  32. await printPdf(Uint8List.fromList(await pdf.save()));
  33. }
  34. pw.Page generarPaginaPrimerTicket(Pedido pedido, pw.MemoryImage image) {
  35. final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  36. double totalSinDescuento = 0;
  37. double totalConDescuento = 0;
  38. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  39. double precioDescuento = 0;
  40. final productList = pedido.productos
  41. .map(
  42. (producto) {
  43. final productPrice = double.parse(producto.producto?.precio ?? '0');
  44. final productTotal = productPrice * (producto.cantidad ?? 1);
  45. totalSinDescuento += productTotal;
  46. final toppingsList = producto.toppings.where((topping) {
  47. final toppingPrice = double.parse(topping.topping?.precio ?? '0');
  48. return toppingPrice > 0;
  49. }).map((topping) {
  50. final toppingPrice = double.parse(topping.topping?.precio ?? '0');
  51. totalSinDescuento += toppingPrice * (producto.cantidad ?? 1);
  52. return pw.Row(
  53. children: [
  54. pw.Text(
  55. '- ${topping.topping?.nombre ?? "Topping no especificado"}',
  56. style: const pw.TextStyle(fontSize: 7)),
  57. pw.Spacer(),
  58. pw.Text('\$${numberFormat.format(toppingPrice)}',
  59. style: const pw.TextStyle(fontSize: 7)),
  60. ],
  61. );
  62. }).toList();
  63. return [
  64. pw.Row(
  65. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  66. children: [
  67. pw.Expanded(
  68. flex: 2,
  69. child: pw.Text(
  70. producto.producto?.nombre ?? "Producto no especificado",
  71. style: const pw.TextStyle(fontSize: 7)),
  72. ),
  73. pw.Expanded(
  74. flex: 1,
  75. child: pw.Text('x${producto.cantidad}',
  76. style: const pw.TextStyle(fontSize: 7)),
  77. ),
  78. pw.Padding(
  79. padding: pw.EdgeInsets.only(right: 2),
  80. child: pw.Expanded(
  81. flex: 1,
  82. child: pw.Text('\$${numberFormat.format(productPrice)}',
  83. style: const pw.TextStyle(fontSize: 8)),
  84. ),
  85. )
  86. ],
  87. ),
  88. ...toppingsList,
  89. ];
  90. },
  91. )
  92. .expand((e) => e)
  93. .toList();
  94. precioDescuento = totalSinDescuento * (descuento / 100);
  95. totalConDescuento = totalSinDescuento - precioDescuento;
  96. return pw.Page(
  97. pageFormat: PdfPageFormat.roll57,
  98. build: (pw.Context context) {
  99. return pw.Column(
  100. crossAxisAlignment: pw.CrossAxisAlignment.center,
  101. children: [
  102. pw.Padding(
  103. padding: const pw.EdgeInsets.only(right: 20),
  104. child:
  105. pw.Center(child: pw.Image(image, width: 50, height: 50))),
  106. pw.SizedBox(height: 10),
  107. pw.Padding(
  108. padding: const pw.EdgeInsets.only(right: 15),
  109. child: pw.Column(children: [
  110. pw.SizedBox(height: 5),
  111. pw.Text('Fecha: ${pedido.peticion}',
  112. style: const pw.TextStyle(fontSize: 9)),
  113. pw.Text('Conalep', style: const pw.TextStyle(fontSize: 9)),
  114. pw.Text('Hermosillo',
  115. style: const pw.TextStyle(fontSize: 9)),
  116. ])),
  117. pw.SizedBox(height: 10),
  118. pw.Text('Pedido: ${pedido.folio}',
  119. style: pw.TextStyle(
  120. fontWeight: pw.FontWeight.bold, fontSize: 10)),
  121. pw.SizedBox(height: 10),
  122. pw.Padding(
  123. padding: const pw.EdgeInsets.only(right: 20),
  124. child: pw.Column(children: productList)),
  125. pw.Divider(),
  126. pw.Row(
  127. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  128. children: [
  129. pw.Text('Subtotal:',
  130. style: pw.TextStyle(
  131. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  132. pw.Padding(
  133. padding: const pw.EdgeInsets.only(right: 30),
  134. child: pw.Text(
  135. '\$${numberFormat.format(totalSinDescuento)}',
  136. style: pw.TextStyle(
  137. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  138. ),
  139. ],
  140. ),
  141. if (descuento > 0) ...[
  142. pw.Row(
  143. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  144. children: [
  145. pw.Text('Descuento:',
  146. style: pw.TextStyle(
  147. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  148. pw.Padding(
  149. padding: const pw.EdgeInsets.only(right: 30),
  150. child: pw.Text(
  151. '-\$${numberFormat.format(precioDescuento)}',
  152. style: pw.TextStyle(
  153. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  154. ),
  155. ],
  156. ),
  157. ],
  158. pw.Row(
  159. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  160. children: [
  161. pw.Text('Total:',
  162. style: pw.TextStyle(
  163. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  164. pw.Padding(
  165. padding: const pw.EdgeInsets.only(right: 30),
  166. child: pw.Text(
  167. '\$${numberFormat.format(totalConDescuento)}',
  168. style: pw.TextStyle(
  169. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  170. ),
  171. ],
  172. ),
  173. pw.SizedBox(height: 5),
  174. pw.Padding(
  175. padding: const pw.EdgeInsets.only(right: 15),
  176. child: pw.Text('¡GRACIAS POR SU COMPRA!',
  177. style: pw.TextStyle(
  178. fontSize: 8, fontWeight: pw.FontWeight.bold))),
  179. pw.Divider(),
  180. pw.SizedBox(height: 20),
  181. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  182. ]);
  183. });
  184. }
  185. pw.Page generarPaginaSegundoTicket(Pedido pedido) {
  186. final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  187. double subtotal = 0;
  188. double totalConDescuento = 0;
  189. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  190. double precioDescuento = 0;
  191. List<pw.Widget> content = [
  192. pw.SizedBox(height: 20),
  193. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  194. pw.Row(
  195. mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  196. children: [
  197. pw.Text('${pedido.folio}',
  198. style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
  199. pw.Text('${pedido.peticion}',
  200. style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
  201. ],
  202. ),
  203. pw.SizedBox(height: 5),
  204. if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  205. pw.Text('Cliente: ${pedido.nombreCliente}',
  206. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  207. pw.SizedBox(height: 10),
  208. ];
  209. // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
  210. content.addAll(pedido.productos
  211. .map((producto) {
  212. final productPrice = double.parse(producto.producto?.precio ?? '0');
  213. final productTotal = productPrice * (producto.cantidad ?? 1);
  214. subtotal += productTotal;
  215. final toppingsList = producto.toppings.map((topping) {
  216. final toppingPrice = double.parse(topping.topping?.precio ?? '0');
  217. final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
  218. subtotal += toppingTotal;
  219. return pw.Row(
  220. mainAxisAlignment: pw.MainAxisAlignment.start,
  221. children: [
  222. pw.Expanded(
  223. flex: 3,
  224. child: pw.Text(
  225. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  226. style: const pw.TextStyle(fontSize: 9)),
  227. ),
  228. if (toppingPrice > 0)
  229. pw.Expanded(
  230. flex: 1,
  231. child: pw.Text(
  232. '\$${numberFormat.format(toppingTotal)}',
  233. style: const pw.TextStyle(fontSize: 9),
  234. textAlign: pw.TextAlign.right,
  235. ),
  236. ),
  237. ],
  238. );
  239. }).toList();
  240. return [
  241. pw.Row(
  242. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  243. children: [
  244. pw.Expanded(
  245. flex: 1,
  246. child: pw.Text('${producto.cantidad}',
  247. style: const pw.TextStyle(fontSize: 12)),
  248. ),
  249. pw.Expanded(
  250. flex: 3,
  251. child: pw.Text(
  252. producto.producto?.nombre ?? "Producto no especificado",
  253. style: const pw.TextStyle(fontSize: 11)),
  254. ),
  255. pw.Expanded(
  256. flex: 2,
  257. child: pw.Text(
  258. '\$${numberFormat.format(productTotal)}',
  259. style: const pw.TextStyle(fontSize: 12),
  260. textAlign: pw.TextAlign.right,
  261. ),
  262. ),
  263. ],
  264. ),
  265. ...toppingsList,
  266. ];
  267. })
  268. .expand((e) => e)
  269. .toList());
  270. // Calcular el descuento y el total final
  271. precioDescuento = subtotal * (descuento / 100);
  272. totalConDescuento = subtotal - precioDescuento;
  273. content.add(pw.Divider());
  274. if (descuento > 0) {
  275. content.addAll([
  276. pw.Row(
  277. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  278. children: [
  279. pw.Text('Subtotal:',
  280. style:
  281. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  282. pw.Text('\$${numberFormat.format(subtotal)}',
  283. style:
  284. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  285. ],
  286. ),
  287. pw.Row(
  288. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  289. children: [
  290. pw.Text('Descuento:',
  291. style:
  292. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  293. pw.Text('-\$${numberFormat.format(precioDescuento)}',
  294. style:
  295. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  296. ],
  297. ),
  298. ]);
  299. }
  300. content.add(
  301. pw.Row(
  302. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  303. children: [
  304. pw.Text('Total:',
  305. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  306. pw.Text(
  307. '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
  308. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  309. ],
  310. ),
  311. );
  312. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  313. content.add(pw.SizedBox(height: 10));
  314. content.add(pw.Text('Comentarios:',
  315. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)));
  316. content.add(pw.Padding(
  317. padding: const pw.EdgeInsets.only(right: 15),
  318. child:
  319. pw.Text(pedido.comentarios!, style: const pw.TextStyle(fontSize: 12)),
  320. ));
  321. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  322. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  323. }
  324. content.add(pw.SizedBox(height: 20));
  325. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  326. return pw.Page(
  327. pageFormat: PdfPageFormat.roll57,
  328. build: (pw.Context context) {
  329. return pw.Column(
  330. crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
  331. },
  332. );
  333. }
  334. Future<void> printPdf(Uint8List pdfBytes) async {
  335. await Printing.layoutPdf(
  336. onLayout: (PdfPageFormat format) => pdfBytes,
  337. );
  338. }