pedido_mesa_screen.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. import 'package:conalep_pos/models/models.dart';
  2. import 'package:conalep_pos/themes/themes.dart';
  3. import 'package:conalep_pos/viewmodels/mesa_view_model.dart';
  4. import 'package:conalep_pos/viewmodels/viewmodels.dart';
  5. import 'package:conalep_pos/views/pedido_mesa/pedido_mesa_detalle.dart';
  6. import 'package:conalep_pos/views/pedido_mesa/pedido_mesa_form.dart';
  7. import 'package:conalep_pos/widgets/widgets.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:provider/provider.dart';
  10. import '../../widgets/widgets_components.dart' as clase;
  11. class PedidoMesaScreen extends StatefulWidget {
  12. const PedidoMesaScreen({Key? key}) : super(key: key);
  13. @override
  14. State<PedidoMesaScreen> createState() => _PedidoMesaScreenState();
  15. }
  16. class _PedidoMesaScreenState extends State<PedidoMesaScreen> {
  17. final _busqueda = TextEditingController(text: '');
  18. DateTime? fechaInicio;
  19. DateTime? fechaFin;
  20. ScrollController horizontalScrollController = ScrollController();
  21. @override
  22. void initState() {
  23. super.initState();
  24. WidgetsBinding.instance.addPostFrameCallback((_) {
  25. Provider.of<PedidoViewModel>(context, listen: false)
  26. .fetchLocalMesaPedidosForScreen();
  27. Provider.of<MesaViewModel>(context, listen: false).fetchLocalAll();
  28. });
  29. }
  30. // void exportCSV() async {
  31. // final pedidosViewModel =
  32. // Provider.of<PedidoViewModel>(context, listen: false);
  33. // List<Pedido> pedidosConProductos = [];
  34. // for (Pedido pedido in pedidosViewModel.pedidos) {
  35. // Pedido? pedidoConProductos =
  36. // await pedidosViewModel.fetchPedidoConProductos(pedido.id);
  37. // if (pedidoConProductos != null) {
  38. // pedidosConProductos.add(pedidoConProductos);
  39. // }
  40. // }
  41. // if (pedidosConProductos.isNotEmpty) {
  42. // String fileName = 'Pedidos_OlivaMia_POS';
  43. // if (fechaInicio != null && fechaFin != null) {
  44. // String startDateStr = DateFormat('dd-MM-yyyy').format(fechaInicio!);
  45. // String endDateStr = DateFormat('dd-MM-yyyy').format(fechaFin!);
  46. // fileName += '_${startDateStr}_al_${endDateStr}';
  47. // }
  48. // fileName += '.csv';
  49. // await exportarPedidosACSV(pedidosConProductos, fileName);
  50. // ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  51. // content: Text('Archivo CSV descargado! Archivo: $fileName')));
  52. // } else {
  53. // ScaffoldMessenger.of(context).showSnackBar(
  54. // SnackBar(content: Text('No hay pedidos para exportar.')));
  55. // }
  56. // }
  57. void clearSearchAndReset() {
  58. setState(() {
  59. _busqueda.clear();
  60. fechaInicio = null;
  61. fechaFin = null;
  62. Provider.of<PedidoViewModel>(context, listen: false)
  63. .fetchLocalPedidosForScreen();
  64. });
  65. }
  66. void go(Pedido item) async {
  67. Pedido? pedidoCompleto =
  68. await Provider.of<PedidoViewModel>(context, listen: false)
  69. .fetchPedidoConProductos(item.id);
  70. if (pedidoCompleto != null) {
  71. if (pedidoCompleto.estatus == 'EN PROCESO') {
  72. Navigator.push(
  73. context,
  74. MaterialPageRoute(
  75. builder: (context) => PedidoMesaForm(pedido: pedidoCompleto),
  76. ),
  77. );
  78. } else {
  79. Navigator.push(
  80. context,
  81. MaterialPageRoute(
  82. builder: (context) =>
  83. PedidoMesaDetalleScreen(pedido: pedidoCompleto),
  84. ),
  85. );
  86. }
  87. } else {
  88. print("Error al cargar el pedido con productos");
  89. }
  90. }
  91. @override
  92. Widget build(BuildContext context) {
  93. final pvm = Provider.of<PedidoViewModel>(context);
  94. double screenWidth = MediaQuery.of(context).size.width;
  95. final isMobile = screenWidth < 1250;
  96. final double? columnSpacing = isMobile ? null : 0;
  97. TextStyle estilo = const TextStyle(fontWeight: FontWeight.bold);
  98. List<DataRow> registros = [];
  99. for (Pedido item in pvm.pedidos) {
  100. registros.add(DataRow(cells: [
  101. DataCell(
  102. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  103. PopupMenuButton(
  104. itemBuilder: (context) => [
  105. PopupMenuItem(
  106. child: const Text('Detalle'),
  107. onTap: () => go(item),
  108. ),
  109. PopupMenuItem(
  110. child: const Text('Cancelar Pedido'),
  111. onTap: () async {
  112. bool confirmado = await showDialog<bool>(
  113. context: context,
  114. builder: (context) {
  115. return AlertDialog(
  116. title: const Text("Cancelar Pedido",
  117. style: TextStyle(
  118. fontWeight: FontWeight.w500, fontSize: 22)),
  119. content: const Text(
  120. '¿Estás seguro de que deseas cancelar este pedido?',
  121. style: TextStyle(fontSize: 18)),
  122. actions: [
  123. Row(
  124. mainAxisAlignment:
  125. MainAxisAlignment.spaceBetween,
  126. children: [
  127. TextButton(
  128. onPressed: () =>
  129. Navigator.of(context).pop(false),
  130. child: const Text('No',
  131. style: TextStyle(fontSize: 18)),
  132. style: ButtonStyle(
  133. padding: MaterialStatePropertyAll(
  134. EdgeInsets.fromLTRB(
  135. 20, 10, 20, 10)),
  136. backgroundColor:
  137. MaterialStatePropertyAll(
  138. Colors.red),
  139. foregroundColor:
  140. MaterialStatePropertyAll(
  141. AppTheme.secondary)),
  142. ),
  143. TextButton(
  144. onPressed: () =>
  145. Navigator.of(context).pop(true),
  146. child: const Text('Sí',
  147. style: TextStyle(fontSize: 18)),
  148. style: ButtonStyle(
  149. padding: MaterialStatePropertyAll(
  150. EdgeInsets.fromLTRB(
  151. 20, 10, 20, 10)),
  152. backgroundColor:
  153. MaterialStatePropertyAll(
  154. AppTheme.tertiary),
  155. foregroundColor:
  156. MaterialStatePropertyAll(
  157. AppTheme.quaternary)),
  158. ),
  159. ],
  160. )
  161. ],
  162. );
  163. },
  164. ) ??
  165. false;
  166. if (confirmado) {
  167. await Provider.of<PedidoViewModel>(context, listen: false)
  168. .cancelarPedido(item.id);
  169. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  170. content: Text("Pedido cancelado correctamente")));
  171. }
  172. },
  173. )
  174. ],
  175. icon: const Icon(Icons.more_vert),
  176. )
  177. ])),
  178. DataCell(
  179. Text(item.peticion ?? "Sin fecha"),
  180. onTap: () => go(item),
  181. ),
  182. DataCell(
  183. Text(Provider.of<MesaViewModel>(context, listen: false).fetchLocalById(idMesa: item.idMesa).nombre.toString()),
  184. onTap: () => go(item),
  185. ),
  186. /* DataCell(
  187. Text(item.id.toString()),
  188. onTap: () => go(item),
  189. ), */
  190. DataCell(
  191. Text(item.folio.toString()),
  192. onTap: () => go(item),
  193. ),
  194. /* DataCell(
  195. Text(item.idLocal.toString()),
  196. onTap: () => go(item),
  197. ), */
  198. DataCell(
  199. Text(item.nombreCliente ?? "Sin nombre"),
  200. onTap: () => go(item),
  201. ),
  202. DataCell(
  203. Text(item.comentarios ?? "Sin comentarios"),
  204. onTap: () => go(item),
  205. ),
  206. DataCell(
  207. Text(item.estatus ?? "Sin Estatus"),
  208. onTap: () => go(item),
  209. ),
  210. ]));
  211. }
  212. return Scaffold(
  213. appBar: AppBar(
  214. title: Text(
  215. 'Pedidos de Mesa',
  216. style: TextStyle(
  217. color: AppTheme.secondary, fontWeight: FontWeight.w500),
  218. ),
  219. // actions: <Widget>[
  220. // IconButton(
  221. // icon: const Icon(Icons.save_alt),
  222. // onPressed: exportCSV,
  223. // tooltip: 'Exportar a CSV',
  224. // ),
  225. // ],
  226. iconTheme: IconThemeData(color: AppTheme.secondary)),
  227. floatingActionButton: FloatingActionButton.extended(
  228. onPressed: () async {
  229. await Navigator.push(
  230. context,
  231. MaterialPageRoute(
  232. builder: (context) => PedidoMesaForm(),
  233. ),
  234. ).then((_) => Provider.of<PedidoViewModel>(context, listen: false)
  235. .fetchLocalPedidosForScreen());
  236. },
  237. icon: Icon(Icons.add, size: 30, color: AppTheme.quaternary),
  238. label: Text(
  239. "Agregar Pedido",
  240. style: TextStyle(fontSize: 20, color: AppTheme.quaternary),
  241. ),
  242. shape: RoundedRectangleBorder(
  243. borderRadius: BorderRadius.circular(8),
  244. ),
  245. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  246. backgroundColor: AppTheme.tertiary,
  247. ),
  248. body: Column(
  249. children: [
  250. Expanded(
  251. child: ListView(
  252. padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
  253. children: [
  254. const SizedBox(height: 8),
  255. clase.tarjeta(
  256. Padding(
  257. padding: const EdgeInsets.all(8.0),
  258. child: LayoutBuilder(
  259. builder: (context, constraints) {
  260. if (screenWidth > 1000) {
  261. return Row(
  262. crossAxisAlignment: CrossAxisAlignment.end,
  263. children: [
  264. Expanded(
  265. flex: 7,
  266. child: _buildDateRangePicker(),
  267. ),
  268. const SizedBox(width: 5),
  269. botonBuscar()
  270. ],
  271. );
  272. } else {
  273. return Column(
  274. children: [
  275. Row(
  276. children: [_buildDateRangePicker()],
  277. ),
  278. Row(
  279. children: [botonBuscar()],
  280. ),
  281. ],
  282. );
  283. }
  284. },
  285. ),
  286. ),
  287. ),
  288. const SizedBox(height: 8),
  289. pvm.isLoading
  290. ? const Center(child: CircularProgressIndicator())
  291. : Container(),
  292. clase.tarjeta(
  293. Column(
  294. children: [
  295. LayoutBuilder(builder: (context, constraints) {
  296. return SingleChildScrollView(
  297. scrollDirection: Axis.vertical,
  298. child: Scrollbar(
  299. controller: horizontalScrollController,
  300. interactive: true,
  301. thumbVisibility: true,
  302. thickness: 10.0,
  303. child: SingleChildScrollView(
  304. controller: horizontalScrollController,
  305. scrollDirection: Axis.horizontal,
  306. child: ConstrainedBox(
  307. constraints: BoxConstraints(
  308. minWidth: isMobile
  309. ? constraints.maxWidth
  310. : screenWidth),
  311. child: DataTable(
  312. columnSpacing: columnSpacing,
  313. sortAscending: true,
  314. sortColumnIndex: 1,
  315. columns: [
  316. DataColumn(label: Text(" ", style: estilo)),
  317. DataColumn(
  318. label: Text("FECHA", style: estilo)),
  319. DataColumn(
  320. label: Text("MESA", style: estilo)),
  321. /* DataColumn(
  322. label: Text("ID", style: estilo)), */
  323. DataColumn(
  324. label: Text("FOLIO", style: estilo)),
  325. /* DataColumn(
  326. label: Text("IDLOCAL", style: estilo)), */
  327. DataColumn(
  328. label: Text("NOMBRE", style: estilo)),
  329. DataColumn(
  330. label:
  331. Text("COMENTARIOS", style: estilo)),
  332. DataColumn(
  333. label: Text("ESTATUS", style: estilo)),
  334. ],
  335. rows: registros,
  336. ),
  337. ),
  338. ),
  339. ),
  340. );
  341. }),
  342. ],
  343. ),
  344. ),
  345. const SizedBox(height: 15),
  346. if (!pvm.isLoading)
  347. Row(
  348. mainAxisAlignment: MainAxisAlignment.center,
  349. children: [
  350. TextButton(
  351. onPressed:
  352. pvm.currentPage > 1 ? pvm.previousPage : null,
  353. child: Text('Anterior'),
  354. style: ButtonStyle(
  355. backgroundColor:
  356. MaterialStateProperty.resolveWith<Color?>(
  357. (Set<MaterialState> states) {
  358. if (states.contains(MaterialState.disabled)) {
  359. return Colors.grey;
  360. }
  361. return AppTheme.tertiary;
  362. },
  363. ),
  364. foregroundColor:
  365. MaterialStateProperty.resolveWith<Color?>(
  366. (Set<MaterialState> states) {
  367. if (states.contains(MaterialState.disabled)) {
  368. return Colors.black;
  369. }
  370. return Colors.white;
  371. },
  372. ),
  373. ),
  374. ),
  375. SizedBox(width: 15),
  376. Text('Página ${pvm.currentPage} de ${pvm.totalPages}'),
  377. SizedBox(width: 15),
  378. TextButton(
  379. onPressed: pvm.currentPage < pvm.totalPages
  380. ? pvm.nextPage
  381. : null,
  382. child: Text('Siguiente'),
  383. style: ButtonStyle(
  384. backgroundColor:
  385. MaterialStateProperty.resolveWith<Color?>(
  386. (Set<MaterialState> states) {
  387. if (states.contains(MaterialState.disabled)) {
  388. return Colors.grey;
  389. }
  390. return AppTheme.tertiary;
  391. },
  392. ),
  393. foregroundColor:
  394. MaterialStateProperty.resolveWith<Color?>(
  395. (Set<MaterialState> states) {
  396. if (states.contains(MaterialState.disabled)) {
  397. return Colors.black;
  398. }
  399. return Colors.white;
  400. },
  401. ),
  402. ),
  403. ),
  404. ],
  405. ),
  406. const SizedBox(height: 15),
  407. ],
  408. ),
  409. ),
  410. ],
  411. ),
  412. );
  413. }
  414. Widget _buildDateRangePicker() {
  415. return Row(
  416. children: [
  417. Expanded(
  418. flex: 3,
  419. child: AppTextField(
  420. prefixIcon: const Icon(Icons.search),
  421. etiqueta: 'Búsqueda por folio...',
  422. controller: _busqueda,
  423. hintText: 'Búsqueda por folio...',
  424. ),
  425. ),
  426. const SizedBox(width: 5),
  427. Expanded(
  428. flex: 3,
  429. child: clase.FechaSelectWidget(
  430. fecha: fechaInicio,
  431. onFechaChanged: (d) {
  432. setState(() {
  433. fechaInicio = d;
  434. });
  435. },
  436. etiqueta: "Fecha Inicial",
  437. context: context,
  438. ),
  439. ),
  440. const SizedBox(width: 5),
  441. Expanded(
  442. flex: 3,
  443. child: clase.FechaSelectWidget(
  444. fecha: fechaFin,
  445. onFechaChanged: (d) {
  446. setState(() {
  447. fechaFin = d;
  448. });
  449. },
  450. etiqueta: "Fecha Final",
  451. context: context,
  452. ),
  453. ),
  454. ],
  455. );
  456. }
  457. Widget botonBuscar() {
  458. return Expanded(
  459. flex: 2,
  460. child: Row(
  461. children: [
  462. Expanded(
  463. flex: 2,
  464. child: Padding(
  465. padding: const EdgeInsets.only(bottom: 5),
  466. child: ElevatedButton(
  467. onPressed: clearSearchAndReset,
  468. style: ElevatedButton.styleFrom(
  469. shape: RoundedRectangleBorder(
  470. borderRadius: BorderRadius.circular(20.0),
  471. ),
  472. backgroundColor: AppTheme.tertiary,
  473. padding: const EdgeInsets.symmetric(vertical: 25),
  474. ),
  475. child: Text('Limpiar',
  476. style: TextStyle(color: AppTheme.quaternary)),
  477. ),
  478. ),
  479. ),
  480. const SizedBox(width: 8),
  481. Expanded(
  482. flex: 2,
  483. child: Padding(
  484. padding: const EdgeInsets.only(bottom: 5),
  485. child: ElevatedButton(
  486. onPressed: () async {
  487. if (_busqueda.text.isNotEmpty) {
  488. await Provider.of<PedidoViewModel>(context, listen: false)
  489. .buscarPedidosPorFolio(_busqueda.text.trim());
  490. } else if (fechaInicio != null && fechaFin != null) {
  491. await Provider.of<PedidoViewModel>(context, listen: false)
  492. .buscarPedidosPorFecha(fechaInicio!, fechaFin!);
  493. } else {
  494. ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
  495. content: Text(
  496. 'Introduce un folio o selecciona un rango de fechas para buscar.')));
  497. }
  498. },
  499. style: ElevatedButton.styleFrom(
  500. shape: RoundedRectangleBorder(
  501. borderRadius: BorderRadius.circular(20.0),
  502. ),
  503. backgroundColor: AppTheme.tertiary,
  504. padding: const EdgeInsets.symmetric(vertical: 25),
  505. ),
  506. child: Text('Buscar',
  507. style: TextStyle(color: AppTheme.quaternary)),
  508. ),
  509. ),
  510. ),
  511. ],
  512. ));
  513. }
  514. }