pedido_screen.dart 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. import 'package:flutter/material.dart';
  2. import 'package:intl/intl.dart';
  3. import 'package:omni_datetime_picker/omni_datetime_picker.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:yoshi_papas_app/themes/themes.dart';
  6. import 'package:yoshi_papas_app/views/pedido/pedido_csv.dart';
  7. import 'package:yoshi_papas_app/views/pedido/pedido_detalle_screen.dart';
  8. import '../../models/models.dart';
  9. import '../../viewmodels/viewmodels.dart';
  10. import '../../widgets/app_textfield.dart';
  11. import '../../widgets/widgets_components.dart' as clase;
  12. import 'pedido_form.dart';
  13. class PedidoScreen extends StatefulWidget {
  14. const PedidoScreen({Key? key}) : super(key: key);
  15. @override
  16. State<PedidoScreen> createState() => _PedidoScreenState();
  17. }
  18. class _PedidoScreenState extends State<PedidoScreen> {
  19. final _busqueda = TextEditingController(text: '');
  20. DateTime? fechaInicio;
  21. DateTime? fechaFin;
  22. ScrollController horizontalScrollController = ScrollController();
  23. @override
  24. void initState() {
  25. super.initState();
  26. WidgetsBinding.instance.addPostFrameCallback((_) {
  27. Provider.of<PedidoViewModel>(context, listen: false)
  28. .fetchLocalPedidosForScreen();
  29. });
  30. }
  31. void exportCSV() async {
  32. final pedidosViewModel =
  33. Provider.of<PedidoViewModel>(context, listen: false);
  34. List<Pedido> pedidosConProductos;
  35. // Si hay un rango de fechas seleccionado, obtenemos los pedidos de ese rango
  36. if (fechaInicio != null && fechaFin != null) {
  37. pedidosConProductos = await pedidosViewModel.fetchPedidosPorFechaSinLimit(
  38. fechaInicio!, fechaFin!);
  39. } else {
  40. // De lo contrario, obtenemos todos los pedidos
  41. pedidosConProductos = await pedidosViewModel.fetchAllLocalPedidos();
  42. }
  43. List<Pedido> pedidosConDetalles = [];
  44. for (Pedido pedido in pedidosConProductos) {
  45. Pedido? pedidoConProductos =
  46. await pedidosViewModel.fetchPedidoConProductos(pedido.id);
  47. if (pedidoConProductos != null) {
  48. pedidosConDetalles.add(pedidoConProductos);
  49. }
  50. }
  51. if (pedidosConDetalles.isNotEmpty) {
  52. String fileName = 'Pedidos_JoshiPapas_POS';
  53. if (fechaInicio != null && fechaFin != null) {
  54. String startDateStr = DateFormat('dd-MM-yyyy').format(fechaInicio!);
  55. String endDateStr = DateFormat('dd-MM-yyyy').format(fechaFin!);
  56. fileName += '_${startDateStr}_al_${endDateStr}';
  57. }
  58. fileName += '.csv';
  59. await exportarPedidosACSV(pedidosConDetalles, fileName);
  60. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  61. content: Text('Archivo CSV descargado! Archivo: $fileName')));
  62. } else {
  63. ScaffoldMessenger.of(context).showSnackBar(
  64. SnackBar(content: Text('No hay pedidos para exportar.')));
  65. }
  66. }
  67. void clearSearchAndReset() {
  68. setState(() {
  69. _busqueda.clear();
  70. fechaInicio = null;
  71. fechaFin = null;
  72. Provider.of<PedidoViewModel>(context, listen: false)
  73. .fetchLocalPedidosForScreen();
  74. });
  75. }
  76. void go(Pedido item) async {
  77. Pedido? pedidoCompleto =
  78. await Provider.of<PedidoViewModel>(context, listen: false)
  79. .fetchPedidoConProductos(item.id);
  80. if (pedidoCompleto != null) {
  81. Navigator.push(
  82. context,
  83. MaterialPageRoute(
  84. builder: (context) => PedidoDetalleScreen(pedido: pedidoCompleto),
  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. AppTheme.primary),
  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.folio.toString()),
  180. onTap: () => go(item),
  181. ),
  182. DataCell(
  183. Text(item.nombreCliente ?? "Sin nombre"),
  184. onTap: () => go(item),
  185. ),
  186. DataCell(
  187. Text(item.comentarios ?? "Sin comentarios"),
  188. onTap: () => go(item),
  189. ),
  190. DataCell(
  191. Text(item.estatus ?? "Sin Estatus"),
  192. onTap: () => go(item),
  193. ),
  194. DataCell(
  195. Text(item.peticion ?? "Sin fecha"),
  196. onTap: () => go(item),
  197. ),
  198. ]));
  199. }
  200. return Scaffold(
  201. appBar: AppBar(
  202. title: Text(
  203. 'Pedidos',
  204. style: TextStyle(color: AppTheme.secondary),
  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) => PedidoForm(),
  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. children: [
  250. Expanded(
  251. flex: 7,
  252. child: _buildDateRangePicker(),
  253. ),
  254. SizedBox(width: 5),
  255. botonBuscar()
  256. ],
  257. );
  258. } else {
  259. return Column(
  260. children: [
  261. Row(
  262. children: [_buildDateRangePicker()],
  263. ),
  264. SizedBox(height: 15),
  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: 2,
  410. child: clase.tarjeta(
  411. ListTile(
  412. title: Text(
  413. "Fecha Inicial",
  414. style: TextStyle(
  415. color: AppTheme.quaternary,
  416. fontWeight: FontWeight.bold),
  417. ),
  418. subtitle: Text(
  419. fechaInicio == null
  420. ? ""
  421. : DateFormat("dd/MM/yyyy").format(fechaInicio!),
  422. style: TextStyle(
  423. color: AppTheme.quaternary,
  424. fontWeight: FontWeight.bold),
  425. ),
  426. trailing:
  427. Icon(Icons.calendar_month, color: AppTheme.quaternary),
  428. onTap: () async {
  429. DateTime? d = await clase.showDatetimePicker(
  430. context, fechaInicio,
  431. tipo: OmniDateTimePickerType.date, solofecha: true);
  432. if (d == null) return;
  433. setState(() {
  434. fechaInicio = d;
  435. });
  436. }),
  437. color: AppTheme.tertiary)),
  438. const SizedBox(width: 5),
  439. Expanded(
  440. flex: 2,
  441. child: clase.tarjeta(
  442. ListTile(
  443. title: Text(
  444. "Fecha Final",
  445. style: TextStyle(
  446. color: AppTheme.quaternary,
  447. fontWeight: FontWeight.bold),
  448. ),
  449. subtitle: Text(
  450. fechaFin == null
  451. ? ""
  452. : DateFormat("dd/MM/yyyy").format(fechaFin!),
  453. style: TextStyle(
  454. color: AppTheme.quaternary,
  455. fontWeight: FontWeight.bold),
  456. ),
  457. trailing:
  458. Icon(Icons.calendar_month, color: AppTheme.quaternary),
  459. onTap: () async {
  460. DateTime? d = await clase.showDatetimePicker(
  461. context, fechaInicio,
  462. inicia: fechaInicio,
  463. tipo: OmniDateTimePickerType.date,
  464. solofecha: true);
  465. if (d == null) return;
  466. setState(() {
  467. fechaFin = d;
  468. });
  469. }),
  470. color: AppTheme.tertiary)),
  471. ],
  472. );
  473. }
  474. Widget botonBuscar() {
  475. return Expanded(
  476. flex: 2,
  477. child: Row(
  478. children: [
  479. Expanded(
  480. flex: 2,
  481. child: Padding(
  482. padding: const EdgeInsets.only(top: 0),
  483. child: ElevatedButton(
  484. onPressed: clearSearchAndReset,
  485. style: ElevatedButton.styleFrom(
  486. shape: RoundedRectangleBorder(
  487. borderRadius: BorderRadius.circular(20.0),
  488. ),
  489. primary: AppTheme.tertiary,
  490. padding: const EdgeInsets.symmetric(vertical: 25),
  491. ),
  492. child: Text('Limpiar',
  493. style: TextStyle(color: AppTheme.quaternary)),
  494. ),
  495. ),
  496. ),
  497. const SizedBox(width: 8),
  498. Expanded(
  499. flex: 2,
  500. child: Padding(
  501. padding: const EdgeInsets.only(top: 0),
  502. child: ElevatedButton(
  503. onPressed: () async {
  504. if (_busqueda.text.isNotEmpty) {
  505. await Provider.of<PedidoViewModel>(context, listen: false)
  506. .buscarPedidosPorFolio(_busqueda.text.trim());
  507. } else if (fechaInicio != null && fechaFin != null) {
  508. await Provider.of<PedidoViewModel>(context, listen: false)
  509. .buscarPedidosPorFecha(fechaInicio!, fechaFin!);
  510. } else {
  511. ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
  512. content: Text(
  513. 'Introduce un folio o selecciona un rango de fechas para buscar.')));
  514. }
  515. },
  516. style: ElevatedButton.styleFrom(
  517. shape: RoundedRectangleBorder(
  518. borderRadius: BorderRadius.circular(20.0),
  519. ),
  520. primary: AppTheme.tertiary,
  521. padding: const EdgeInsets.symmetric(vertical: 25),
  522. ),
  523. child: Text('Buscar',
  524. style: TextStyle(color: AppTheme.quaternary)),
  525. ),
  526. ),
  527. ),
  528. ],
  529. ));
  530. }
  531. }