pedido_mesa_screen.dart 19 KB

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