pedido_ticket.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. final paginaSegundoTicket =
  29. await generarPaginaSegundoTicket(context, pedido);
  30. pdf.addPage(paginaSegundoTicket);
  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. Future<pw.Page> generarPaginaSegundoTicket(
  186. BuildContext context, Pedido pedido) async {
  187. final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  188. double subtotal = 0;
  189. double totalConDescuento = 0;
  190. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  191. double precioDescuento = 0;
  192. final sucursalVariable =
  193. await Provider.of<VariableViewModel>(context, listen: false)
  194. .getVariableByClave('sucursal');
  195. final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
  196. List<pw.Widget> content = [
  197. pw.SizedBox(height: 20),
  198. pw.Text('.', style: pw.TextStyle(fontSize: 1)),
  199. pw.Row(
  200. mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  201. children: [
  202. pw.Text('${pedido.folio}/ ',
  203. style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
  204. if (sucursalDescripcion.isNotEmpty)
  205. pw.Text('$sucursalDescripcion/ ',
  206. style:
  207. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  208. ],
  209. ),
  210. pw.SizedBox(height: 5),
  211. pw.Row(children: [
  212. pw.Text('${pedido.peticion}',
  213. style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold)),
  214. ]),
  215. if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  216. pw.SizedBox(height: 5),
  217. if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  218. pw.Text('Cliente: ${pedido.nombreCliente}',
  219. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  220. pw.SizedBox(height: 10),
  221. ];
  222. // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
  223. content.addAll(pedido.productos
  224. .map((producto) {
  225. final productPrice = double.parse(producto.producto?.precio ?? '0');
  226. final productTotal = productPrice * (producto.cantidad ?? 1);
  227. subtotal += productTotal;
  228. final toppingsList = producto.toppings.map((topping) {
  229. final toppingPrice = double.parse(topping.topping?.precio ?? '0');
  230. final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
  231. subtotal += toppingTotal;
  232. return pw.Row(
  233. mainAxisAlignment: pw.MainAxisAlignment.start,
  234. children: [
  235. pw.Expanded(
  236. flex: 3,
  237. child: pw.Text(
  238. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  239. style: const pw.TextStyle(fontSize: 9)),
  240. ),
  241. if (toppingPrice > 0)
  242. pw.Expanded(
  243. flex: 1,
  244. child: pw.Text(
  245. '\$${numberFormat.format(toppingTotal)}',
  246. style: const pw.TextStyle(fontSize: 9),
  247. textAlign: pw.TextAlign.right,
  248. ),
  249. ),
  250. ],
  251. );
  252. }).toList();
  253. return [
  254. pw.Row(
  255. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  256. children: [
  257. pw.Expanded(
  258. flex: 1,
  259. child: pw.Text('${producto.cantidad}',
  260. style: const pw.TextStyle(fontSize: 12)),
  261. ),
  262. pw.Expanded(
  263. flex: 3,
  264. child: pw.Text(
  265. producto.producto?.nombre ?? "Producto no especificado",
  266. style: const pw.TextStyle(fontSize: 11)),
  267. ),
  268. pw.Expanded(
  269. flex: 2,
  270. child: pw.Text(
  271. '\$${numberFormat.format(productTotal)}',
  272. style: const pw.TextStyle(fontSize: 12),
  273. textAlign: pw.TextAlign.right,
  274. ),
  275. ),
  276. ],
  277. ),
  278. ...toppingsList,
  279. ];
  280. })
  281. .expand((e) => e)
  282. .toList());
  283. // Calcular el descuento y el total final
  284. precioDescuento = subtotal * (descuento / 100);
  285. totalConDescuento = subtotal - precioDescuento;
  286. content.add(pw.Divider());
  287. if (descuento > 0) {
  288. content.addAll([
  289. pw.Row(
  290. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  291. children: [
  292. pw.Text('Subtotal:',
  293. style:
  294. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  295. pw.Text('\$${numberFormat.format(subtotal)}',
  296. style:
  297. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  298. ],
  299. ),
  300. pw.Row(
  301. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  302. children: [
  303. pw.Text('Descuento:',
  304. style:
  305. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  306. pw.Text('-\$${numberFormat.format(precioDescuento)}',
  307. style:
  308. pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  309. ],
  310. ),
  311. ]);
  312. }
  313. content.add(
  314. pw.Row(
  315. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  316. children: [
  317. pw.Text('Total:',
  318. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  319. pw.Text(
  320. '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
  321. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)),
  322. ],
  323. ),
  324. );
  325. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  326. content.add(pw.SizedBox(height: 10));
  327. content.add(pw.Text('Comentarios:',
  328. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 12)));
  329. content.add(pw.Padding(
  330. padding: const pw.EdgeInsets.only(right: 15),
  331. child:
  332. pw.Text(pedido.comentarios!, style: const pw.TextStyle(fontSize: 12)),
  333. ));
  334. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  335. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  336. }
  337. content.add(pw.SizedBox(height: 20));
  338. content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  339. return pw.Page(
  340. pageFormat: PdfPageFormat.roll57,
  341. build: (pw.Context context) {
  342. return pw.Column(
  343. crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
  344. },
  345. );
  346. }
  347. Future<void> printPdf(Uint8List pdfBytes) async {
  348. await Printing.layoutPdf(
  349. onLayout: (PdfPageFormat format) => pdfBytes,
  350. );
  351. }