pedido_ticket.dart 20 KB

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