pedido_ticket.dart 11 KB

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