pedido_ticket.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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: 8)),
  57. pw.Spacer(),
  58. pw.Text('\$${numberFormat.format(toppingPrice)}',
  59. style: const pw.TextStyle(fontSize: 8)),
  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: 8)),
  72. ),
  73. pw.Expanded(
  74. flex: 1,
  75. child: pw.Text('x${producto.cantidad}',
  76. style: const pw.TextStyle(fontSize: 8)),
  77. ),
  78. pw.Padding(
  79. padding: pw.EdgeInsets.only(right: 2),
  80. child: pw.Text('\$${numberFormat.format(productPrice)}',
  81. style: const pw.TextStyle(fontSize: 8)),
  82. )
  83. ],
  84. ),
  85. ...toppingsList,
  86. ];
  87. },
  88. )
  89. .expand((e) => e)
  90. .toList();
  91. precioDescuento = totalSinDescuento * (descuento / 100);
  92. totalConDescuento = totalSinDescuento - precioDescuento;
  93. return pw.Page(
  94. pageFormat: PdfPageFormat.roll57,
  95. margin: pw.EdgeInsets.all(5),
  96. build: (pw.Context context) {
  97. return pw.Column(
  98. crossAxisAlignment: pw.CrossAxisAlignment.center,
  99. children: [
  100. pw.Center(child: pw.Image(image, width: 50, height: 50)),
  101. pw.SizedBox(height: 5),
  102. pw.Text('Fecha: ${pedido.peticion}',
  103. style: const pw.TextStyle(fontSize: 8)),
  104. pw.Text('Conalep', style: const pw.TextStyle(fontSize: 8)),
  105. pw.Text('Hermosillo', style: const pw.TextStyle(fontSize: 8)),
  106. pw.SizedBox(height: 5),
  107. pw.Text('Pedido: ${pedido.folio}',
  108. style: pw.TextStyle(
  109. fontWeight: pw.FontWeight.bold, fontSize: 9)),
  110. pw.SizedBox(height: 5),
  111. pw.Column(children: productList),
  112. pw.Divider(),
  113. pw.Row(
  114. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  115. children: [
  116. pw.Text('Subtotal:',
  117. style: pw.TextStyle(
  118. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  119. pw.Text('\$${numberFormat.format(totalSinDescuento)}',
  120. style: pw.TextStyle(
  121. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  122. ],
  123. ),
  124. if (descuento > 0) ...[
  125. pw.Row(
  126. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  127. children: [
  128. pw.Text('Descuento:',
  129. style: pw.TextStyle(
  130. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  131. pw.Text('-\$${numberFormat.format(precioDescuento)}',
  132. style: pw.TextStyle(
  133. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  134. ],
  135. ),
  136. ],
  137. pw.Row(
  138. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  139. children: [
  140. pw.Text('Total:',
  141. style: pw.TextStyle(
  142. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  143. pw.Text('\$${numberFormat.format(totalConDescuento)}',
  144. style: pw.TextStyle(
  145. fontWeight: pw.FontWeight.bold, fontSize: 8)),
  146. ],
  147. ),
  148. pw.SizedBox(height: 5),
  149. pw.Text('¡GRACIAS POR SU COMPRA!',
  150. style: pw.TextStyle(
  151. fontSize: 7, fontWeight: pw.FontWeight.bold)),
  152. pw.Divider(),
  153. ]);
  154. });
  155. }
  156. Future<pw.Page> generarPaginaSegundoTicket(
  157. BuildContext context, Pedido pedido) async {
  158. final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  159. double subtotal = 0;
  160. double totalConDescuento = 0;
  161. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  162. double precioDescuento = 0;
  163. final sucursalVariable =
  164. await Provider.of<VariableViewModel>(context, listen: false)
  165. .getVariableByClave('sucursal');
  166. final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
  167. List<pw.Widget> content = [
  168. pw.Row(
  169. mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  170. children: [
  171. pw.Text('${pedido.folio}/ ',
  172. style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
  173. if (sucursalDescripcion.isNotEmpty)
  174. pw.Text('$sucursalDescripcion/ ',
  175. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  176. pw.Text('${pedido.peticion}',
  177. style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
  178. ],
  179. ),
  180. pw.SizedBox(height: 2),
  181. if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  182. pw.Text('Cliente: ${pedido.nombreCliente}',
  183. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  184. pw.SizedBox(height: 2),
  185. ];
  186. // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
  187. content.addAll(pedido.productos
  188. .map((producto) {
  189. final productPrice = double.parse(producto.producto?.precio ?? '0');
  190. final productTotal = productPrice * (producto.cantidad ?? 1);
  191. subtotal += productTotal;
  192. final toppingsList = producto.toppings.map((topping) {
  193. final toppingPrice = double.parse(topping.topping?.precio ?? '0');
  194. final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
  195. subtotal += toppingTotal;
  196. return pw.Row(
  197. mainAxisAlignment: pw.MainAxisAlignment.start,
  198. children: [
  199. pw.Expanded(
  200. flex: 3,
  201. child: pw.Text(
  202. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  203. style: const pw.TextStyle(fontSize: 6)),
  204. ),
  205. if (toppingPrice > 0)
  206. pw.Expanded(
  207. flex: 1,
  208. child: pw.Text(
  209. '\$${numberFormat.format(toppingTotal)}',
  210. style: const pw.TextStyle(fontSize: 6),
  211. textAlign: pw.TextAlign.right,
  212. ),
  213. ),
  214. ],
  215. );
  216. }).toList();
  217. return [
  218. pw.Row(
  219. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  220. children: [
  221. pw.Expanded(
  222. flex: 1,
  223. child: pw.Text('${producto.cantidad}',
  224. style: const pw.TextStyle(fontSize: 7)),
  225. ),
  226. pw.Expanded(
  227. flex: 5,
  228. child: pw.Text(
  229. producto.producto?.nombre ?? "Producto no especificado",
  230. style: const pw.TextStyle(fontSize: 7)),
  231. ),
  232. pw.Expanded(
  233. flex: 2,
  234. child: pw.Align(
  235. alignment: pw.Alignment.centerRight,
  236. child: pw.Text(
  237. '\$${numberFormat.format(productTotal)}',
  238. style: const pw.TextStyle(fontSize: 7),
  239. ),
  240. )),
  241. ],
  242. ),
  243. ...toppingsList,
  244. ];
  245. })
  246. .expand((e) => e)
  247. .toList());
  248. // Calcular el descuento y el total final
  249. precioDescuento = subtotal * (descuento / 100);
  250. totalConDescuento = subtotal - precioDescuento;
  251. content.add(pw.Divider());
  252. if (descuento > 0) {
  253. content.addAll([
  254. pw.Row(
  255. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  256. children: [
  257. pw.Text('Subtotal:',
  258. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  259. pw.Text('\$${numberFormat.format(subtotal)}',
  260. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  261. ],
  262. ),
  263. pw.Row(
  264. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  265. children: [
  266. pw.Text('Descuento:',
  267. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  268. pw.Text('-\$${numberFormat.format(precioDescuento)}',
  269. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  270. ],
  271. ),
  272. ]);
  273. }
  274. content.add(
  275. pw.Row(
  276. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  277. children: [
  278. pw.Text('Total:',
  279. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  280. pw.Text(
  281. '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
  282. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  283. ],
  284. ),
  285. );
  286. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  287. content.add(pw.SizedBox(height: 1));
  288. content.add(pw.Text('Comentarios:',
  289. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)));
  290. content.add(pw.Padding(
  291. padding: const pw.EdgeInsets.only(right: 15),
  292. child:
  293. pw.Text(pedido.comentarios!, style: const pw.TextStyle(fontSize: 7)),
  294. ));
  295. }
  296. content.add(pw.SizedBox(height: 1));
  297. return pw.Page(
  298. pageFormat: PdfPageFormat.roll57,
  299. margin: pw.EdgeInsets.all(5),
  300. build: (pw.Context context) {
  301. return pw.Column(
  302. crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
  303. },
  304. );
  305. }
  306. Future<void> printPdf(Uint8List pdfBytes) async {
  307. await Printing.layoutPdf(
  308. onLayout: (PdfPageFormat format) => pdfBytes,
  309. );
  310. }