pedido_ticket.dart 13 KB

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