pedido_mesa_screen.dart 20 KB

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