pedido_screen.dart 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. import 'dart:typed_data';
  2. import 'package:flutter/material.dart';
  3. import 'package:intl/intl.dart';
  4. import 'package:omni_datetime_picker/omni_datetime_picker.dart';
  5. import 'package:provider/provider.dart';
  6. import '../pedido/pedido_csv.dart';
  7. import '../pedido/pedido_detalle_screen.dart';
  8. import '../../widgets/widgets.dart';
  9. import '../../themes/themes.dart';
  10. import '../../models/models.dart';
  11. import '../../viewmodels/viewmodels.dart';
  12. import '../../widgets/widgets_components.dart' as clase;
  13. import 'pedido_form.dart';
  14. import 'package:otp/otp.dart';
  15. import 'package:pdf/widgets.dart' as pw;
  16. import 'package:timezone/data/latest.dart' as timezone;
  17. import 'package:timezone/timezone.dart' as timezone;
  18. import 'pedido_ticket.dart';
  19. class PedidoScreen extends StatefulWidget {
  20. const PedidoScreen({Key? key}) : super(key: key);
  21. @override
  22. State<PedidoScreen> createState() => _PedidoScreenState();
  23. }
  24. class _PedidoScreenState extends State<PedidoScreen> {
  25. final _busqueda = TextEditingController(text: '');
  26. DateTime? fechaInicio;
  27. DateTime? fechaFin;
  28. ScrollController horizontalScrollController = ScrollController();
  29. TextEditingController codeController = new TextEditingController();
  30. bool _isMesasActive = false;
  31. @override
  32. void initState() {
  33. super.initState();
  34. WidgetsBinding.instance.addPostFrameCallback((_) {
  35. Provider.of<PedidoViewModel>(context, listen: false)
  36. .fetchLocalPedidosForScreen();
  37. Provider.of<MesaViewModel>(context, listen: false)
  38. .fetchLocalAll(sinLimite: true);
  39. });
  40. Future.microtask(() async {
  41. bool isMesasActive =
  42. await Provider.of<VariableViewModel>(context, listen: false)
  43. .isVariableActive('MESAS');
  44. setState(() {
  45. _isMesasActive = isMesasActive;
  46. });
  47. });
  48. }
  49. void exportCSV() async {
  50. final pedidosViewModel =
  51. Provider.of<PedidoViewModel>(context, listen: false);
  52. List<Pedido> pedidosConProductos = [];
  53. for (Pedido pedido in pedidosViewModel.pedidos) {
  54. Pedido? pedidoConProductos =
  55. await pedidosViewModel.fetchPedidoConProductos(pedido.id);
  56. if (pedidoConProductos != null) {
  57. pedidosConProductos.add(pedidoConProductos);
  58. }
  59. }
  60. if (pedidosConProductos.isNotEmpty) {
  61. String fileName = 'Pedidos_Turquessa_POS';
  62. if (fechaInicio != null && fechaFin != null) {
  63. String startDateStr = DateFormat('dd-MM-yyyy').format(fechaInicio!);
  64. String endDateStr = DateFormat('dd-MM-yyyy').format(fechaFin!);
  65. fileName += '_${startDateStr}_al_${endDateStr}';
  66. }
  67. fileName += '.csv';
  68. await exportarPedidosACSV(pedidosConProductos, fileName);
  69. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  70. content: Text('Archivo CSV descargado! Archivo: $fileName')));
  71. } else {
  72. ScaffoldMessenger.of(context).showSnackBar(
  73. SnackBar(content: Text('No hay pedidos para exportar.')));
  74. }
  75. }
  76. void clearSearchAndReset() {
  77. setState(() {
  78. _busqueda.clear();
  79. fechaInicio = null;
  80. fechaFin = null;
  81. Provider.of<PedidoViewModel>(context, listen: false)
  82. .fetchLocalPedidosForScreen();
  83. });
  84. }
  85. void go(Pedido item, {bool? detalle = false}) async {
  86. Pedido? pedidoCompleto =
  87. await Provider.of<PedidoViewModel>(context, listen: false)
  88. .fetchPedidoConProductos(item.id);
  89. if (pedidoCompleto != null) {
  90. if (pedidoCompleto.estatus == 'TERMINADO' ||
  91. pedidoCompleto.estatus == 'CANCELADO' ||
  92. !_isMesasActive ||
  93. detalle!) {
  94. Navigator.push(
  95. context,
  96. MaterialPageRoute(
  97. builder: (context) => PedidoDetalleScreen(pedido: pedidoCompleto),
  98. ),
  99. );
  100. } else {
  101. Navigator.push(
  102. context,
  103. MaterialPageRoute(
  104. builder: (context) => PedidoForm(
  105. pedidoExistente: pedidoCompleto,
  106. ),
  107. ),
  108. ).then((value) async {
  109. await Provider.of<PedidoViewModel>(context, listen: false)
  110. .fetchLocalPedidosForScreen();
  111. });
  112. }
  113. } else {
  114. print("Error al cargar el pedido con productos");
  115. }
  116. }
  117. @override
  118. Widget build(BuildContext context) {
  119. final pvm = Provider.of<PedidoViewModel>(context);
  120. double screenWidth = MediaQuery.of(context).size.width;
  121. final isMobile = screenWidth < 1250;
  122. final double? columnSpacing = isMobile ? null : 0;
  123. TextStyle estilo = const TextStyle(fontWeight: FontWeight.bold);
  124. List<DataRow> registros = [];
  125. final permisoViewModel = Provider.of<PermisoViewModel>(context);
  126. List<String> userPermisos = permisoViewModel.userPermisos;
  127. for (Pedido item in pvm.pedidos) {
  128. final sincronizadoStatus =
  129. item.sincronizado == null || item.sincronizado!.isEmpty
  130. ? "No Sincronizado"
  131. : _formatDateTime(item.sincronizado);
  132. final mesaViewModel = Provider.of<MesaViewModel>(context, listen: false);
  133. final mesa = mesaViewModel.fetchLocalById(idMesa: item.idMesa);
  134. registros.add(DataRow(cells: [
  135. DataCell(
  136. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  137. PopupMenuButton(
  138. itemBuilder: (context) => [
  139. PopupMenuItem(
  140. child: const Text('Detalle'),
  141. onTap: () => go(item, detalle: true),
  142. ),
  143. PopupMenuItem(
  144. child: const Text('Estado Ticket'),
  145. onTap: () async {
  146. await imprimirEstadoTicket(context, item);
  147. },
  148. ),
  149. if (userPermisos.contains(Usuario.CANCELAR_PEDIDO))
  150. PopupMenuItem(
  151. child: const Text('Cancelar Pedido'),
  152. onTap: () async {
  153. bool confirmado = await showDialog<bool>(
  154. context: context,
  155. builder: (context) {
  156. return AlertDialog(
  157. title: const Text("Cancelar Pedido",
  158. style: TextStyle(
  159. fontWeight: FontWeight.w500,
  160. fontSize: 22)),
  161. content: const Text(
  162. '¿Estás seguro de que deseas cancelar este pedido?',
  163. style: TextStyle(fontSize: 18)),
  164. actions: [
  165. Column(
  166. children: [
  167. Container(
  168. padding: EdgeInsets.all(8.0),
  169. child: TextField(
  170. controller: codeController,
  171. decoration: InputDecoration(
  172. label: Text(
  173. "Para cancelar debe capturar el código")),
  174. ),
  175. ),
  176. Row(
  177. mainAxisAlignment:
  178. MainAxisAlignment.spaceBetween,
  179. children: [
  180. TextButton(
  181. onPressed: () =>
  182. Navigator.of(context).pop(false),
  183. child: const Text('No',
  184. style: TextStyle(fontSize: 18)),
  185. style: ButtonStyle(
  186. padding: MaterialStatePropertyAll(
  187. EdgeInsets.fromLTRB(
  188. 20, 10, 20, 10)),
  189. backgroundColor:
  190. MaterialStatePropertyAll(
  191. Colors.red),
  192. foregroundColor:
  193. MaterialStatePropertyAll(
  194. AppTheme.secondary)),
  195. ),
  196. TextButton(
  197. onPressed: () {
  198. final now = DateTime.now().toUtc();
  199. timezone.initializeTimeZones();
  200. final pacificTimeZone =
  201. timezone.getLocation(
  202. 'America/Los_Angeles');
  203. final date =
  204. timezone.TZDateTime.from(
  205. now, pacificTimeZone);
  206. final codigoTotp =
  207. OTP.generateTOTPCodeString(
  208. 'TYSNE4CMT5LVLGWS',
  209. date.millisecondsSinceEpoch,
  210. algorithm: Algorithm.SHA1,
  211. isGoogle: true);
  212. String codigo = codeController.text;
  213. print(
  214. "totp: $codigoTotp codigo:$codigo");
  215. List<String> codigosEstaticos = [
  216. '172449',
  217. '827329',
  218. // Agregar más token fijos
  219. ];
  220. bool esCodigoEstatico =
  221. codigosEstaticos
  222. .contains(codigo);
  223. print(
  224. "¿Es código estático? ${esCodigoEstatico} ${!esCodigoEstatico && codigo != codigoTotp}");
  225. if (!esCodigoEstatico &&
  226. codigo != codigoTotp) {
  227. ScaffoldMessenger.of(context)
  228. .showSnackBar(SnackBar(
  229. content: Text(
  230. 'El código no es correcto'),
  231. duration: Duration(seconds: 2),
  232. ));
  233. return;
  234. } else {
  235. codeController.text = '';
  236. Navigator.of(context).pop(true);
  237. }
  238. },
  239. child: const Text('Sí',
  240. style: TextStyle(fontSize: 18)),
  241. style: ButtonStyle(
  242. padding: MaterialStatePropertyAll(
  243. EdgeInsets.fromLTRB(
  244. 20, 10, 20, 10)),
  245. backgroundColor:
  246. MaterialStatePropertyAll(
  247. AppTheme.tertiary),
  248. foregroundColor:
  249. MaterialStatePropertyAll(
  250. AppTheme.quaternary)),
  251. ),
  252. ],
  253. )
  254. ],
  255. ),
  256. ],
  257. );
  258. },
  259. ) ??
  260. false;
  261. if (confirmado) {
  262. await Provider.of<PedidoViewModel>(context, listen: false)
  263. .cancelarPedido(item.id);
  264. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  265. content: Text("Pedido cancelado correctamente")));
  266. }
  267. },
  268. )
  269. ],
  270. icon: const Icon(Icons.more_vert),
  271. )
  272. ])),
  273. if (_isMesasActive)
  274. DataCell(
  275. Text(mesa.nombre ?? "Mesa desconocida"),
  276. onTap: () => go(item),
  277. ),
  278. DataCell(
  279. Text(item.folio.toString()),
  280. onTap: () => go(item),
  281. ),
  282. DataCell(
  283. Text(item.nombreCliente ?? "Sin nombre"),
  284. onTap: () => go(item),
  285. ),
  286. DataCell(
  287. Text(item.comentarios ?? "Sin comentarios"),
  288. onTap: () => go(item),
  289. ),
  290. DataCell(
  291. Text(item.estatus ?? "Sin Estatus"),
  292. onTap: () => go(item),
  293. ),
  294. DataCell(
  295. Text(_formatDateTime(item.peticion)),
  296. onTap: () => go(item),
  297. ),
  298. DataCell(
  299. Text(sincronizadoStatus),
  300. onTap: () => go(item),
  301. ),
  302. ]));
  303. }
  304. return Scaffold(
  305. appBar: AppBar(
  306. title: Text(
  307. 'Pedidos',
  308. style: TextStyle(
  309. color: AppTheme.secondary, fontWeight: FontWeight.w500),
  310. ),
  311. actions: <Widget>[
  312. if (userPermisos.contains(Usuario.VER_REPORTE))
  313. IconButton(
  314. icon: const Icon(Icons.save_alt),
  315. onPressed: exportCSV,
  316. tooltip: 'Exportar a CSV',
  317. ),
  318. ],
  319. iconTheme: IconThemeData(color: AppTheme.secondary)),
  320. body: Stack(
  321. children: [
  322. Column(
  323. children: [
  324. Expanded(
  325. child: ListView(
  326. padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
  327. children: [
  328. const SizedBox(height: 8),
  329. clase.tarjeta(
  330. Padding(
  331. padding: const EdgeInsets.all(8.0),
  332. child: LayoutBuilder(
  333. builder: (context, constraints) {
  334. if (screenWidth > 1000) {
  335. return Row(
  336. crossAxisAlignment: CrossAxisAlignment.end,
  337. children: [
  338. Expanded(
  339. flex: 7,
  340. child: _buildDateRangePicker(),
  341. ),
  342. const SizedBox(width: 5),
  343. botonBuscar()
  344. ],
  345. );
  346. } else {
  347. return Column(
  348. children: [
  349. Row(
  350. children: [_buildDateRangePicker()],
  351. ),
  352. Row(
  353. children: [botonBuscar()],
  354. ),
  355. ],
  356. );
  357. }
  358. },
  359. ),
  360. ),
  361. ),
  362. const SizedBox(height: 8),
  363. pvm.isLoading
  364. ? const Center(child: CircularProgressIndicator())
  365. : Container(),
  366. clase.tarjeta(
  367. Column(
  368. children: [
  369. LayoutBuilder(builder: (context, constraints) {
  370. return SingleChildScrollView(
  371. scrollDirection: Axis.vertical,
  372. child: Scrollbar(
  373. controller: horizontalScrollController,
  374. interactive: true,
  375. thumbVisibility: true,
  376. thickness: 10.0,
  377. child: SingleChildScrollView(
  378. controller: horizontalScrollController,
  379. scrollDirection: Axis.horizontal,
  380. child: ConstrainedBox(
  381. constraints: BoxConstraints(
  382. minWidth: isMobile
  383. ? constraints.maxWidth
  384. : screenWidth),
  385. child: DataTable(
  386. columnSpacing: columnSpacing,
  387. sortAscending: true,
  388. sortColumnIndex: 1,
  389. columns: [
  390. DataColumn(
  391. label: Text(" ", style: estilo)),
  392. if (_isMesasActive)
  393. DataColumn(
  394. label:
  395. Text("MESA", style: estilo)),
  396. DataColumn(
  397. label:
  398. Text("FOLIO", style: estilo)),
  399. DataColumn(
  400. label:
  401. Text("NOMBRE", style: estilo)),
  402. DataColumn(
  403. label: Text("COMENTARIOS",
  404. style: estilo)),
  405. DataColumn(
  406. label:
  407. Text("ESTATUS", style: estilo)),
  408. DataColumn(
  409. label:
  410. Text("FECHA", style: estilo)),
  411. DataColumn(
  412. label: Text("SINCRONIZADO",
  413. style: estilo)),
  414. ],
  415. rows: registros,
  416. ),
  417. ),
  418. ),
  419. ),
  420. );
  421. }),
  422. ],
  423. ),
  424. ),
  425. const SizedBox(height: 15),
  426. if (!pvm.isLoading)
  427. Row(
  428. mainAxisAlignment: MainAxisAlignment.center,
  429. children: [
  430. TextButton(
  431. onPressed:
  432. pvm.currentPage > 1 ? pvm.previousPage : null,
  433. child: Text('Anterior'),
  434. style: ButtonStyle(
  435. backgroundColor:
  436. MaterialStateProperty.resolveWith<Color?>(
  437. (Set<MaterialState> states) {
  438. if (states.contains(MaterialState.disabled)) {
  439. return Colors.grey;
  440. }
  441. return AppTheme.tertiary;
  442. },
  443. ),
  444. foregroundColor:
  445. MaterialStateProperty.resolveWith<Color?>(
  446. (Set<MaterialState> states) {
  447. if (states.contains(MaterialState.disabled)) {
  448. return Colors.black;
  449. }
  450. return Colors.white;
  451. },
  452. ),
  453. ),
  454. ),
  455. SizedBox(width: 15),
  456. Text(
  457. 'Página ${pvm.currentPage} de ${pvm.totalPages}'),
  458. SizedBox(width: 15),
  459. TextButton(
  460. onPressed: pvm.currentPage < pvm.totalPages
  461. ? pvm.nextPage
  462. : null,
  463. child: Text('Siguiente'),
  464. style: ButtonStyle(
  465. backgroundColor:
  466. MaterialStateProperty.resolveWith<Color?>(
  467. (Set<MaterialState> states) {
  468. if (states.contains(MaterialState.disabled)) {
  469. return Colors.grey;
  470. }
  471. return AppTheme.tertiary;
  472. },
  473. ),
  474. foregroundColor:
  475. MaterialStateProperty.resolveWith<Color?>(
  476. (Set<MaterialState> states) {
  477. if (states.contains(MaterialState.disabled)) {
  478. return Colors.black;
  479. }
  480. return Colors.white;
  481. },
  482. ),
  483. ),
  484. ),
  485. ],
  486. ),
  487. const SizedBox(height: 15),
  488. ],
  489. ),
  490. ),
  491. ],
  492. ),
  493. Positioned(
  494. bottom: 16,
  495. right: 16,
  496. child: FloatingActionButton.extended(
  497. heroTag: 'addPedido',
  498. onPressed: () async {
  499. final corteCajaViewModel =
  500. Provider.of<CorteCajaViewModel>(context, listen: false);
  501. if (corteCajaViewModel.hasOpenCorteCaja()) {
  502. await Navigator.push(
  503. context,
  504. MaterialPageRoute(
  505. builder: (context) => PedidoForm(),
  506. ),
  507. ).then((_) =>
  508. Provider.of<PedidoViewModel>(context, listen: false)
  509. .fetchLocalPedidosForScreen());
  510. } else {
  511. alerta(context,
  512. etiqueta:
  513. 'Solo se pueden realizar pedidos cuando se encuentre la caja abierta.');
  514. }
  515. },
  516. icon: Icon(Icons.add, size: 30, color: AppTheme.quaternary),
  517. label: Text(
  518. "Agregar Pedido",
  519. style: TextStyle(fontSize: 20, color: AppTheme.quaternary),
  520. ),
  521. shape: RoundedRectangleBorder(
  522. borderRadius: BorderRadius.circular(8),
  523. ),
  524. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  525. backgroundColor: AppTheme.tertiary,
  526. ),
  527. ),
  528. ],
  529. ),
  530. );
  531. }
  532. Widget _buildDateRangePicker() {
  533. return Row(
  534. children: [
  535. Expanded(
  536. flex: 3,
  537. child: AppTextField(
  538. prefixIcon: const Icon(Icons.search),
  539. etiqueta: 'Búsqueda por folio...',
  540. controller: _busqueda,
  541. hintText: 'Búsqueda por folio...',
  542. ),
  543. ),
  544. const SizedBox(width: 5),
  545. Expanded(
  546. flex: 3,
  547. child: clase.FechaSelectWidget(
  548. fecha: fechaInicio,
  549. onFechaChanged: (d) {
  550. setState(() {
  551. fechaInicio = d;
  552. });
  553. },
  554. etiqueta: "Fecha Inicial",
  555. context: context,
  556. ),
  557. ),
  558. const SizedBox(width: 5),
  559. Expanded(
  560. flex: 3,
  561. child: clase.FechaSelectWidget(
  562. fecha: fechaFin,
  563. onFechaChanged: (d) {
  564. setState(() {
  565. fechaFin = d;
  566. });
  567. },
  568. etiqueta: "Fecha Final",
  569. context: context,
  570. ),
  571. ),
  572. ],
  573. );
  574. }
  575. Widget botonBuscar() {
  576. return Expanded(
  577. flex: 2,
  578. child: Row(
  579. children: [
  580. Expanded(
  581. flex: 2,
  582. child: Padding(
  583. padding: const EdgeInsets.only(bottom: 5),
  584. child: ElevatedButton(
  585. onPressed: clearSearchAndReset,
  586. style: ElevatedButton.styleFrom(
  587. shape: RoundedRectangleBorder(
  588. borderRadius: BorderRadius.circular(20.0),
  589. ),
  590. primary: AppTheme.tertiary,
  591. padding: const EdgeInsets.symmetric(vertical: 25),
  592. ),
  593. child: Text('Limpiar',
  594. style: TextStyle(color: AppTheme.quaternary)),
  595. ),
  596. ),
  597. ),
  598. const SizedBox(width: 8),
  599. Expanded(
  600. flex: 2,
  601. child: Padding(
  602. padding: const EdgeInsets.only(bottom: 5),
  603. child: ElevatedButton(
  604. onPressed: () async {
  605. if (_busqueda.text.isNotEmpty) {
  606. await Provider.of<PedidoViewModel>(context, listen: false)
  607. .buscarPedidosPorFolio(_busqueda.text.trim());
  608. } else if (fechaInicio != null && fechaFin != null) {
  609. DateTime fechaInicioUTC = DateTime(fechaInicio!.year,
  610. fechaInicio!.month, fechaInicio!.day)
  611. .toUtc();
  612. DateTime fechaFinUTC = DateTime(fechaFin!.year,
  613. fechaFin!.month, fechaFin!.day, 23, 59, 59)
  614. .toUtc();
  615. await Provider.of<PedidoViewModel>(context, listen: false)
  616. .buscarPedidosPorFecha(fechaInicioUTC, fechaFinUTC);
  617. } else {
  618. ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
  619. content: Text(
  620. 'Introduce un folio o selecciona un rango de fechas para buscar.')));
  621. }
  622. },
  623. style: ElevatedButton.styleFrom(
  624. shape: RoundedRectangleBorder(
  625. borderRadius: BorderRadius.circular(20.0),
  626. ),
  627. primary: AppTheme.tertiary,
  628. padding: const EdgeInsets.symmetric(vertical: 25),
  629. ),
  630. child: Text('Buscar',
  631. style: TextStyle(color: AppTheme.quaternary)),
  632. ),
  633. ),
  634. ),
  635. ],
  636. ),
  637. );
  638. }
  639. void alertaSync(BuildContext context) {
  640. showDialog(
  641. context: context,
  642. builder: (BuildContext context) {
  643. return AlertDialog(
  644. title: const Text('Confirmación',
  645. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22)),
  646. content: const Text('¿Deseas sincronizar todos los pedidos de nuevo?',
  647. style: TextStyle(fontSize: 18)),
  648. actions: [
  649. Row(
  650. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  651. children: [
  652. TextButton(
  653. child: const Text('No', style: TextStyle(fontSize: 18)),
  654. style: ButtonStyle(
  655. padding: MaterialStatePropertyAll(
  656. EdgeInsets.fromLTRB(20, 10, 20, 10)),
  657. backgroundColor: MaterialStatePropertyAll(Colors.red),
  658. foregroundColor:
  659. MaterialStatePropertyAll(AppTheme.secondary)),
  660. onPressed: () {
  661. Navigator.of(context).pop();
  662. },
  663. ),
  664. TextButton(
  665. child: const Text('Sí', style: TextStyle(fontSize: 18)),
  666. style: ButtonStyle(
  667. padding: MaterialStatePropertyAll(
  668. EdgeInsets.fromLTRB(20, 10, 20, 10)),
  669. backgroundColor:
  670. MaterialStatePropertyAll(AppTheme.tertiary),
  671. foregroundColor:
  672. MaterialStatePropertyAll(AppTheme.quaternary)),
  673. onPressed: () {
  674. // No hace nada por el momento
  675. Navigator.of(context).pop();
  676. },
  677. ),
  678. ],
  679. )
  680. ],
  681. );
  682. },
  683. );
  684. }
  685. String _formatDateTime(String? dateTimeString) {
  686. if (dateTimeString == null) return "Sin fecha";
  687. DateTime parsedDate = DateTime.parse(dateTimeString);
  688. var formatter = DateFormat('dd-MM-yyyy HH:mm:ss');
  689. return formatter.format(parsedDate.toLocal());
  690. }
  691. Future<void> imprimirEstadoTicket(BuildContext context, Pedido pedido) async {
  692. if (pedido.productos == null || pedido.productos.isEmpty) {
  693. pedido = (await Provider.of<PedidoViewModel>(context, listen: false)
  694. .fetchPedidoConProductos(pedido.id))!;
  695. if (pedido == null) {
  696. ScaffoldMessenger.of(context).showSnackBar(
  697. SnackBar(content: Text('No se pudo cargar el pedido')),
  698. );
  699. return;
  700. }
  701. }
  702. final pdf = pw.Document();
  703. pdf.addPage(generarPaginaSegundoTicket(pedido));
  704. final pdfBytes = Uint8List.fromList(await pdf.save());
  705. await printPdf(pdfBytes);
  706. }
  707. }