pedido_mesa_screen.dart 20 KB

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