pedido_mesa_screen.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.folio.toString()),
  179. onTap: () => go(item),
  180. ),
  181. DataCell(
  182. Text(item.nombreCliente ?? "Sin nombre"),
  183. onTap: () => go(item),
  184. ),
  185. DataCell(
  186. Text(item.comentarios ?? "Sin comentarios"),
  187. onTap: () => go(item),
  188. ),
  189. DataCell(
  190. Text(item.estatus ?? "Sin Estatus"),
  191. onTap: () => go(item),
  192. ),
  193. DataCell(
  194. Text(item.peticion ?? "Sin fecha"),
  195. onTap: () => go(item),
  196. ),
  197. ]));
  198. }
  199. return Scaffold(
  200. appBar: AppBar(
  201. title: Text(
  202. 'Pedidos de Mesa',
  203. style: TextStyle(
  204. color: AppTheme.secondary, fontWeight: FontWeight.w500),
  205. ),
  206. // actions: <Widget>[
  207. // IconButton(
  208. // icon: const Icon(Icons.save_alt),
  209. // onPressed: exportCSV,
  210. // tooltip: 'Exportar a CSV',
  211. // ),
  212. // ],
  213. iconTheme: IconThemeData(color: AppTheme.secondary)),
  214. floatingActionButton: FloatingActionButton.extended(
  215. onPressed: () async {
  216. await Navigator.push(
  217. context,
  218. MaterialPageRoute(
  219. builder: (context) => PedidoMesaForm(),
  220. ),
  221. ).then((_) => Provider.of<PedidoViewModel>(context, listen: false)
  222. .fetchLocalPedidosForScreen());
  223. },
  224. icon: Icon(Icons.add, size: 30, color: AppTheme.quaternary),
  225. label: Text(
  226. "Agregar Pedido",
  227. style: TextStyle(fontSize: 20, color: AppTheme.quaternary),
  228. ),
  229. shape: RoundedRectangleBorder(
  230. borderRadius: BorderRadius.circular(8),
  231. ),
  232. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  233. backgroundColor: AppTheme.tertiary,
  234. ),
  235. body: Column(
  236. children: [
  237. Expanded(
  238. child: ListView(
  239. padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
  240. children: [
  241. const SizedBox(height: 8),
  242. clase.tarjeta(
  243. Padding(
  244. padding: const EdgeInsets.all(8.0),
  245. child: LayoutBuilder(
  246. builder: (context, constraints) {
  247. if (screenWidth > 1000) {
  248. return Row(
  249. crossAxisAlignment: CrossAxisAlignment.end,
  250. children: [
  251. Expanded(
  252. flex: 7,
  253. child: _buildDateRangePicker(),
  254. ),
  255. const SizedBox(width: 5),
  256. botonBuscar()
  257. ],
  258. );
  259. } else {
  260. return Column(
  261. children: [
  262. Row(
  263. children: [_buildDateRangePicker()],
  264. ),
  265. Row(
  266. children: [botonBuscar()],
  267. ),
  268. ],
  269. );
  270. }
  271. },
  272. ),
  273. ),
  274. ),
  275. const SizedBox(height: 8),
  276. pvm.isLoading
  277. ? const Center(child: CircularProgressIndicator())
  278. : Container(),
  279. clase.tarjeta(
  280. Column(
  281. children: [
  282. LayoutBuilder(builder: (context, constraints) {
  283. return SingleChildScrollView(
  284. scrollDirection: Axis.vertical,
  285. child: Scrollbar(
  286. controller: horizontalScrollController,
  287. interactive: true,
  288. thumbVisibility: true,
  289. thickness: 10.0,
  290. child: SingleChildScrollView(
  291. controller: horizontalScrollController,
  292. scrollDirection: Axis.horizontal,
  293. child: ConstrainedBox(
  294. constraints: BoxConstraints(
  295. minWidth: isMobile
  296. ? constraints.maxWidth
  297. : screenWidth),
  298. child: DataTable(
  299. columnSpacing: columnSpacing,
  300. sortAscending: true,
  301. sortColumnIndex: 1,
  302. columns: [
  303. DataColumn(label: Text(" ", style: estilo)),
  304. DataColumn(
  305. label: Text("FOLIO", style: estilo)),
  306. DataColumn(
  307. label: Text("NOMBRE", style: estilo)),
  308. DataColumn(
  309. label:
  310. Text("COMENTARIOS", style: estilo)),
  311. DataColumn(
  312. label: Text("ESTATUS", style: estilo)),
  313. DataColumn(
  314. label: Text("FECHA", style: estilo)),
  315. ],
  316. rows: registros,
  317. ),
  318. ),
  319. ),
  320. ),
  321. );
  322. }),
  323. ],
  324. ),
  325. ),
  326. const SizedBox(height: 15),
  327. if (!pvm.isLoading)
  328. Row(
  329. mainAxisAlignment: MainAxisAlignment.center,
  330. children: [
  331. TextButton(
  332. onPressed:
  333. pvm.currentPage > 1 ? pvm.previousPage : null,
  334. child: Text('Anterior'),
  335. style: ButtonStyle(
  336. backgroundColor:
  337. MaterialStateProperty.resolveWith<Color?>(
  338. (Set<MaterialState> states) {
  339. if (states.contains(MaterialState.disabled)) {
  340. return Colors.grey;
  341. }
  342. return AppTheme.tertiary;
  343. },
  344. ),
  345. foregroundColor:
  346. MaterialStateProperty.resolveWith<Color?>(
  347. (Set<MaterialState> states) {
  348. if (states.contains(MaterialState.disabled)) {
  349. return Colors.black;
  350. }
  351. return Colors.white;
  352. },
  353. ),
  354. ),
  355. ),
  356. SizedBox(width: 15),
  357. Text('Página ${pvm.currentPage} de ${pvm.totalPages}'),
  358. SizedBox(width: 15),
  359. TextButton(
  360. onPressed: pvm.currentPage < pvm.totalPages
  361. ? pvm.nextPage
  362. : null,
  363. child: Text('Siguiente'),
  364. style: ButtonStyle(
  365. backgroundColor:
  366. MaterialStateProperty.resolveWith<Color?>(
  367. (Set<MaterialState> states) {
  368. if (states.contains(MaterialState.disabled)) {
  369. return Colors.grey;
  370. }
  371. return AppTheme.tertiary;
  372. },
  373. ),
  374. foregroundColor:
  375. MaterialStateProperty.resolveWith<Color?>(
  376. (Set<MaterialState> states) {
  377. if (states.contains(MaterialState.disabled)) {
  378. return Colors.black;
  379. }
  380. return Colors.white;
  381. },
  382. ),
  383. ),
  384. ),
  385. ],
  386. ),
  387. const SizedBox(height: 15),
  388. ],
  389. ),
  390. ),
  391. ],
  392. ),
  393. );
  394. }
  395. Widget _buildDateRangePicker() {
  396. return Row(
  397. children: [
  398. Expanded(
  399. flex: 3,
  400. child: AppTextField(
  401. prefixIcon: const Icon(Icons.search),
  402. etiqueta: 'Búsqueda por folio...',
  403. controller: _busqueda,
  404. hintText: 'Búsqueda por folio...',
  405. ),
  406. ),
  407. const SizedBox(width: 5),
  408. Expanded(
  409. flex: 3,
  410. child: clase.FechaSelectWidget(
  411. fecha: fechaInicio,
  412. onFechaChanged: (d) {
  413. setState(() {
  414. fechaInicio = d;
  415. });
  416. },
  417. etiqueta: "Fecha Inicial",
  418. context: context,
  419. ),
  420. ),
  421. const SizedBox(width: 5),
  422. Expanded(
  423. flex: 3,
  424. child: clase.FechaSelectWidget(
  425. fecha: fechaFin,
  426. onFechaChanged: (d) {
  427. setState(() {
  428. fechaFin = d;
  429. });
  430. },
  431. etiqueta: "Fecha Final",
  432. context: context,
  433. ),
  434. ),
  435. ],
  436. );
  437. }
  438. Widget botonBuscar() {
  439. return Expanded(
  440. flex: 2,
  441. child: Row(
  442. children: [
  443. Expanded(
  444. flex: 2,
  445. child: Padding(
  446. padding: const EdgeInsets.only(bottom: 5),
  447. child: ElevatedButton(
  448. onPressed: clearSearchAndReset,
  449. style: ElevatedButton.styleFrom(
  450. shape: RoundedRectangleBorder(
  451. borderRadius: BorderRadius.circular(20.0),
  452. ),
  453. backgroundColor: AppTheme.tertiary,
  454. padding: const EdgeInsets.symmetric(vertical: 25),
  455. ),
  456. child: Text('Limpiar',
  457. style: TextStyle(color: AppTheme.quaternary)),
  458. ),
  459. ),
  460. ),
  461. const SizedBox(width: 8),
  462. Expanded(
  463. flex: 2,
  464. child: Padding(
  465. padding: const EdgeInsets.only(bottom: 5),
  466. child: ElevatedButton(
  467. onPressed: () async {
  468. if (_busqueda.text.isNotEmpty) {
  469. await Provider.of<PedidoViewModel>(context, listen: false)
  470. .buscarPedidosPorFolio(_busqueda.text.trim());
  471. } else if (fechaInicio != null && fechaFin != null) {
  472. await Provider.of<PedidoViewModel>(context, listen: false)
  473. .buscarPedidosPorFecha(fechaInicio!, fechaFin!);
  474. } else {
  475. ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
  476. content: Text(
  477. 'Introduce un folio o selecciona un rango de fechas para buscar.')));
  478. }
  479. },
  480. style: ElevatedButton.styleFrom(
  481. shape: RoundedRectangleBorder(
  482. borderRadius: BorderRadius.circular(20.0),
  483. ),
  484. backgroundColor: AppTheme.tertiary,
  485. padding: const EdgeInsets.symmetric(vertical: 25),
  486. ),
  487. child: Text('Buscar',
  488. style: TextStyle(color: AppTheme.quaternary)),
  489. ),
  490. ),
  491. ),
  492. ],
  493. ));
  494. }
  495. }