pedido_ticket.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 = producto.producto?.precio ?? 0.0;
  44. final productTotal = productPrice * (producto.cantidad ?? 1);
  45. totalSinDescuento += productTotal;
  46. final toppingsList = producto.toppings.where((topping) {
  47. final toppingPrice = topping.topping?.precio ?? 0.0;
  48. return toppingPrice > 0;
  49. }).map((topping) {
  50. final toppingPrice = topping.topping?.precio ?? 0.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. //Ticket PC
  157. // Future<pw.Page> generarPaginaSegundoTicket(
  158. // BuildContext context, Pedido pedido) async {
  159. // final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  160. // double subtotal = 0;
  161. // double totalConDescuento = 0;
  162. // double descuento = pedido.descuento?.toDouble() ?? 0.0;
  163. // double precioDescuento = 0;
  164. // final sucursalVariable =
  165. // await Provider.of<VariableViewModel>(context, listen: false)
  166. // .getVariableByClave('Sucursal');
  167. // final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
  168. // List<pw.Widget> content = [
  169. // pw.Padding(
  170. // padding: pw.EdgeInsets.only(right: 10),
  171. // child: pw.Row(
  172. // mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  173. // children: [
  174. // pw.Text('${pedido.folio}/ ',
  175. // style:
  176. // pw.TextStyle(fontSize: 10.5, fontWeight: pw.FontWeight.bold)),
  177. // if (sucursalDescripcion.isNotEmpty)
  178. // pw.Text('$sucursalDescripcion/ ',
  179. // style: pw.TextStyle(
  180. // fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
  181. // ],
  182. // ),
  183. // ),
  184. // pw.SizedBox(height: 2),
  185. // pw.Row(
  186. // mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  187. // children: [
  188. // pw.Text('${_formatDateTime(pedido.peticion)}',
  189. // style:
  190. // pw.TextStyle(fontSize: 10.5, fontWeight: pw.FontWeight.bold)),
  191. // ],
  192. // ),
  193. // pw.SizedBox(height: 2),
  194. // if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  195. // pw.Text('Cliente: ${pedido.nombreCliente}',
  196. // style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
  197. // pw.SizedBox(height: 2),
  198. // ];
  199. // // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
  200. // content.addAll(pedido.productos
  201. // .map((producto) {
  202. // final productPrice = producto.producto?.precio ?? 0.0;
  203. // final productTotal = productPrice * (producto.cantidad ?? 1);
  204. // subtotal += productTotal;
  205. // final toppingsList = producto.toppings.map((topping) {
  206. // final toppingPrice = topping.topping?.precio ?? 0.0;
  207. // final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
  208. // subtotal += toppingTotal;
  209. // return pw.Row(
  210. // mainAxisAlignment: pw.MainAxisAlignment.start,
  211. // children: [
  212. // pw.Expanded(
  213. // flex: 3,
  214. // child: pw.Text(
  215. // '-${topping.topping?.nombre ?? "Topping no especificado"}',
  216. // style: const pw.TextStyle(fontSize: 8.5)),
  217. // ),
  218. // if (toppingPrice > 0)
  219. // pw.Expanded(
  220. // flex: 1,
  221. // child: pw.Text(
  222. // '\$${numberFormat.format(toppingTotal)}',
  223. // style: const pw.TextStyle(fontSize: 8.5),
  224. // textAlign: pw.TextAlign.right,
  225. // ),
  226. // ),
  227. // ],
  228. // );
  229. // }).toList();
  230. // return [
  231. // pw.Row(
  232. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  233. // children: [
  234. // pw.Expanded(
  235. // flex: 1,
  236. // child: pw.Text('${producto.cantidad}',
  237. // style: const pw.TextStyle(fontSize: 10.5)),
  238. // ),
  239. // pw.Expanded(
  240. // flex: 6,
  241. // child: pw.Text(
  242. // producto.producto?.nombre ?? "Producto no especificado",
  243. // style: const pw.TextStyle(fontSize: 10.5)),
  244. // ),
  245. // pw.Expanded(
  246. // flex: 4,
  247. // child: pw.Align(
  248. // alignment: pw.Alignment.centerLeft,
  249. // child: pw.Text(
  250. // '\$${numberFormat.format(productTotal)}',
  251. // style: const pw.TextStyle(fontSize: 10.5),
  252. // ),
  253. // )),
  254. // ],
  255. // ),
  256. // ...toppingsList,
  257. // ];
  258. // })
  259. // .expand((e) => e)
  260. // .toList());
  261. // // Calcular el descuento y el total final
  262. // precioDescuento = subtotal * (descuento / 100);
  263. // totalConDescuento = subtotal - precioDescuento;
  264. // content.add(pw.Divider());
  265. // if (descuento > 0) {
  266. // content.addAll([
  267. // pw.Row(
  268. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  269. // children: [
  270. // pw.Text('Subtotal:',
  271. // style:
  272. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
  273. // pw.Padding(
  274. // padding: pw.EdgeInsets.only(right: 15),
  275. // child: pw.Text('\$${numberFormat.format(subtotal)}',
  276. // style: pw.TextStyle(
  277. // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
  278. // ],
  279. // ),
  280. // pw.Row(
  281. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  282. // children: [
  283. // pw.Text('Descuento:',
  284. // style:
  285. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
  286. // pw.Padding(
  287. // padding: pw.EdgeInsets.only(right: 15),
  288. // child: pw.Text('-\$${numberFormat.format(precioDescuento)}',
  289. // style: pw.TextStyle(
  290. // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
  291. // ],
  292. // ),
  293. // ]);
  294. // }
  295. // content.add(
  296. // pw.Row(
  297. // mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  298. // children: [
  299. // pw.Text('Total:',
  300. // style:
  301. // pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)),
  302. // pw.Padding(
  303. // padding: pw.EdgeInsets.only(right: 20),
  304. // child: pw.Text(
  305. // '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
  306. // style: pw.TextStyle(
  307. // fontWeight: pw.FontWeight.bold, fontSize: 10.5))),
  308. // ],
  309. // ),
  310. // );
  311. // if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  312. // content.add(pw.SizedBox(height: 1));
  313. // content.add(pw.Text('Comentarios:',
  314. // style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 10.5)));
  315. // content.add(pw.Padding(
  316. // padding: const pw.EdgeInsets.only(right: 15),
  317. // child: pw.Text(pedido.comentarios!,
  318. // style: const pw.TextStyle(fontSize: 10.5)),
  319. // ));
  320. // }
  321. // content.add(pw.SizedBox(height: 15));
  322. // content.add(pw.Text('.', style: pw.TextStyle(fontSize: 1)));
  323. // return pw.Page(
  324. // pageFormat: PdfPageFormat.roll57,
  325. // margin: pw.EdgeInsets.all(5),
  326. // build: (pw.Context context) {
  327. // return pw.Column(
  328. // crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
  329. // },
  330. // );
  331. // }
  332. //Ticket Tablet
  333. Future<pw.Page> generarPaginaSegundoTicket(
  334. BuildContext context, Pedido pedido) async {
  335. final numberFormat = NumberFormat('#,##0.00', 'es_MX');
  336. double subtotal = 0;
  337. double totalConDescuento = 0;
  338. double descuento = pedido.descuento?.toDouble() ?? 0.0;
  339. double precioDescuento = 0;
  340. final sucursalVariable =
  341. await Provider.of<VariableViewModel>(context, listen: false)
  342. .getVariableByClave('Sucursal');
  343. final sucursalDescripcion = sucursalVariable?.descripcion ?? '';
  344. List<pw.Widget> content = [
  345. pw.Row(
  346. mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  347. children: [
  348. pw.Text('${pedido.folio}/ ',
  349. style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
  350. if (sucursalDescripcion.isNotEmpty)
  351. pw.Text('$sucursalDescripcion/ ',
  352. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  353. ],
  354. ),
  355. pw.SizedBox(height: 2),
  356. pw.Row(
  357. mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
  358. children: [
  359. pw.Text('${_formatDateTime(pedido.peticion)}',
  360. style: pw.TextStyle(fontSize: 7, fontWeight: pw.FontWeight.bold)),
  361. ],
  362. ),
  363. pw.SizedBox(height: 2),
  364. if (pedido.nombreCliente != null && pedido.nombreCliente!.isNotEmpty)
  365. pw.Text('Cliente: ${pedido.nombreCliente}',
  366. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  367. pw.SizedBox(height: 2),
  368. ];
  369. // Mostrar los productos con la cantidad, el precio total y el precio de los toppings
  370. content.addAll(pedido.productos
  371. .map((producto) {
  372. final productPrice = producto.producto?.precio ?? 0.0;
  373. final productTotal = productPrice * (producto.cantidad ?? 1);
  374. subtotal += productTotal;
  375. final toppingsList = producto.toppings.map((topping) {
  376. final toppingPrice = topping.topping?.precio ?? 0.0;
  377. final toppingTotal = toppingPrice * (producto.cantidad ?? 1);
  378. subtotal += toppingTotal;
  379. return pw.Row(
  380. mainAxisAlignment: pw.MainAxisAlignment.start,
  381. children: [
  382. pw.Expanded(
  383. flex: 3,
  384. child: pw.Text(
  385. '-${topping.topping?.nombre ?? "Topping no especificado"}',
  386. style: const pw.TextStyle(fontSize: 6)),
  387. ),
  388. if (toppingPrice > 0)
  389. pw.Expanded(
  390. flex: 1,
  391. child: pw.Text(
  392. '\$${numberFormat.format(toppingTotal)}',
  393. style: const pw.TextStyle(fontSize: 6),
  394. textAlign: pw.TextAlign.right,
  395. ),
  396. ),
  397. ],
  398. );
  399. }).toList();
  400. return [
  401. pw.Row(
  402. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  403. children: [
  404. pw.Expanded(
  405. flex: 1,
  406. child: pw.Text('${producto.cantidad}',
  407. style: const pw.TextStyle(fontSize: 7)),
  408. ),
  409. pw.Expanded(
  410. flex: 5,
  411. child: pw.Text(
  412. producto.producto?.nombre ?? "Producto no especificado",
  413. style: const pw.TextStyle(fontSize: 7)),
  414. ),
  415. pw.Expanded(
  416. flex: 2,
  417. child: pw.Align(
  418. alignment: pw.Alignment.centerRight,
  419. child: pw.Text(
  420. '\$${numberFormat.format(productTotal)}',
  421. style: const pw.TextStyle(fontSize: 7),
  422. ),
  423. )),
  424. ],
  425. ),
  426. ...toppingsList,
  427. ];
  428. })
  429. .expand((e) => e)
  430. .toList());
  431. // Calcular el descuento y el total final
  432. precioDescuento = subtotal * (descuento / 100);
  433. totalConDescuento = subtotal - precioDescuento;
  434. content.add(pw.Divider());
  435. if (descuento > 0) {
  436. content.addAll([
  437. pw.Row(
  438. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  439. children: [
  440. pw.Text('Subtotal:',
  441. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  442. pw.Text('\$${numberFormat.format(subtotal)}',
  443. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  444. ],
  445. ),
  446. pw.Row(
  447. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  448. children: [
  449. pw.Text('Descuento:',
  450. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  451. pw.Text('-\$${numberFormat.format(precioDescuento)}',
  452. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  453. ],
  454. ),
  455. ]);
  456. }
  457. content.add(
  458. pw.Row(
  459. mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
  460. children: [
  461. pw.Text('Total:',
  462. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  463. pw.Text(
  464. '\$${numberFormat.format(descuento > 0 ? totalConDescuento : subtotal)}',
  465. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)),
  466. ],
  467. ),
  468. );
  469. if (pedido.comentarios != null && pedido.comentarios!.isNotEmpty) {
  470. content.add(pw.SizedBox(height: 1));
  471. content.add(pw.Text('Comentarios:',
  472. style: pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 7)));
  473. content.add(pw.Padding(
  474. padding: const pw.EdgeInsets.only(right: 15),
  475. child:
  476. pw.Text(pedido.comentarios!, style: const pw.TextStyle(fontSize: 7)),
  477. ));
  478. }
  479. content.add(pw.SizedBox(height: 1));
  480. return pw.Page(
  481. pageFormat: PdfPageFormat.roll57,
  482. margin: pw.EdgeInsets.all(5),
  483. build: (pw.Context context) {
  484. return pw.Column(
  485. crossAxisAlignment: pw.CrossAxisAlignment.center, children: content);
  486. },
  487. );
  488. }
  489. Future<void> printPdf(Uint8List pdfBytes) async {
  490. await Printing.layoutPdf(
  491. onLayout: (PdfPageFormat format) => pdfBytes,
  492. );
  493. }
  494. String _formatDateTime(String? dateTimeString) {
  495. if (dateTimeString == null) return "Sin fecha";
  496. DateTime parsedDate = DateTime.parse(dateTimeString);
  497. var formatter = DateFormat('dd-MM-yyyy HH:mm:ss');
  498. return formatter.format(parsedDate.toLocal());
  499. }