pedido_mesa_screen.dart 20 KB

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