pedido_form.dart 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:intl/intl.dart';
  6. import 'package:provider/provider.dart';
  7. import '../pedido/pedido_ticket.dart';
  8. import '../../themes/themes.dart';
  9. import '../../models/models.dart';
  10. import '../../viewmodels/viewmodels.dart';
  11. import 'package:collection/collection.dart';
  12. import '../../widgets/widgets.dart';
  13. class PedidoForm extends StatefulWidget {
  14. @override
  15. _PedidoFormState createState() => _PedidoFormState();
  16. }
  17. class _PedidoFormState extends State<PedidoForm> {
  18. final _busqueda = TextEditingController(text: '');
  19. final TextEditingController _descuentoController = TextEditingController();
  20. CategoriaProductoViewModel cvm = CategoriaProductoViewModel();
  21. ProductoViewModel pvm = ProductoViewModel();
  22. PedidoViewModel pedvm = PedidoViewModel();
  23. bool _isLoading = false;
  24. CategoriaProducto? categoriaSeleccionada;
  25. List<CategoriaProducto> categorias = [];
  26. List<Producto> productos = [];
  27. List<ItemCarrito> carrito = [];
  28. Producto? _productoActual;
  29. bool _estadoBusqueda = false;
  30. Pedido? pedidoActual;
  31. ScrollController _gridViewController = ScrollController();
  32. final _searchController = TextEditingController();
  33. final NumberFormat _numberFormat = NumberFormat.decimalPattern('es_MX');
  34. int? selectedDescuento = 0;
  35. double subtotal = 0;
  36. double precioDescuento = 0;
  37. double totalPedido = 0;
  38. bool efectivoSeleccionado = false;
  39. bool tarjetaSeleccionada = false;
  40. bool transferenciaSeleccionada = false;
  41. TextEditingController efectivoController = TextEditingController();
  42. TextEditingController tarjetaController = TextEditingController();
  43. TextEditingController transferenciaController = TextEditingController();
  44. double cambio = 0.0;
  45. double calcularTotalPedido() {
  46. double total = 0;
  47. for (var item in carrito) {
  48. total += double.parse(item.producto.precio!) * item.cantidad;
  49. item.selectedToppings.forEach((categoryId, selectedToppingIds) {
  50. for (int toppingId in selectedToppingIds) {
  51. Producto? topping = item.selectableToppings[categoryId]?.firstWhere(
  52. (topping) => topping.id == toppingId,
  53. orElse: () => Producto(precio: '0'));
  54. if (topping != null) {
  55. total += (double.tryParse(topping.precio!) ?? 0) * item.cantidad;
  56. }
  57. }
  58. });
  59. }
  60. return total;
  61. }
  62. double aplicarDescuento(double total, int? descuento) {
  63. if (descuento != null && descuento > 0) {
  64. double totalPedido = total * (1 - descuento / 100);
  65. // print(
  66. // 'Total con descuento: $totalPedido (Descuento aplicado: $descuento%)');
  67. return totalPedido;
  68. }
  69. // print('Sin descuento, total: $total');
  70. return total;
  71. }
  72. @override
  73. void initState() {
  74. super.initState();
  75. cargarCategoriasIniciales().then((_) {
  76. if (categorias.isNotEmpty) {
  77. categoriaSeleccionada = categorias.first;
  78. cargarProductosPorCategoria(categoriaSeleccionada!.id);
  79. }
  80. });
  81. Provider.of<DescuentoViewModel>(context, listen: false).cargarDescuentos();
  82. }
  83. _onSearchChanged(String value) {
  84. if (value.isEmpty) {
  85. cargarProductosIniciales();
  86. } else {
  87. Provider.of<ProductoViewModel>(context, listen: false)
  88. .fetchLocalByName(nombre: value);
  89. }
  90. }
  91. void _recalcularTotal() {
  92. subtotal = calcularTotalPedido();
  93. // print('Subtotal: $subtotal');
  94. // print('Descuento seleccionado: $selectedDescuento%');
  95. precioDescuento = subtotal * (selectedDescuento! / 100);
  96. totalPedido = subtotal - precioDescuento;
  97. setState(() {
  98. pedidoActual = pedidoActual ?? Pedido();
  99. pedidoActual!.totalPedido = totalPedido;
  100. pedidoActual!.descuento = selectedDescuento;
  101. });
  102. // print('Precio descuento: $precioDescuento');
  103. // print('Total con descuento aplicado: $totalPedido');
  104. }
  105. void _finalizeOrder() async {
  106. if (carrito.isEmpty) {
  107. showDialog(
  108. context: context,
  109. builder: (BuildContext context) {
  110. return AlertDialog(
  111. title: const Text('Pedido vacío',
  112. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22)),
  113. content: const Text(
  114. 'No puedes finalizar un pedido sin productos. Por favor, agrega al menos un producto.',
  115. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 18)),
  116. shape:
  117. RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
  118. actions: <Widget>[
  119. TextButton(
  120. style: TextButton.styleFrom(
  121. padding: EdgeInsets.fromLTRB(30, 20, 30, 20),
  122. foregroundColor: AppTheme.quaternary,
  123. backgroundColor: AppTheme.tertiary),
  124. onPressed: () {
  125. Navigator.of(context).pop();
  126. },
  127. child: const Text('Aceptar', style: TextStyle(fontSize: 18)),
  128. ),
  129. ],
  130. );
  131. },
  132. );
  133. return;
  134. }
  135. await _promptForCustomerName();
  136. }
  137. Future<void> _promptForCustomerName() async {
  138. TextEditingController nombreController = TextEditingController();
  139. TextEditingController comentarioController = TextEditingController();
  140. String errorMessage = '';
  141. double faltante = totalPedido;
  142. bool totalCompletado = false;
  143. void _calcularCambio(StateSetter setState) {
  144. double totalPagado = (double.tryParse(efectivoController.text) ?? 0) +
  145. (double.tryParse(tarjetaController.text) ?? 0) +
  146. (double.tryParse(transferenciaController.text) ?? 0);
  147. setState(() {
  148. cambio = totalPagado - totalPedido;
  149. if (cambio < 0) {
  150. faltante = totalPedido - totalPagado;
  151. cambio = 0;
  152. totalCompletado = false;
  153. } else {
  154. faltante = 0;
  155. totalCompletado = true;
  156. }
  157. });
  158. }
  159. void _validarCantidad(
  160. StateSetter setState, TextEditingController controller) {
  161. double cantidad = double.tryParse(controller.text) ?? 0;
  162. if (cantidad > totalPedido) {
  163. setState(() {
  164. controller.text = totalPedido.toStringAsFixed(2);
  165. });
  166. }
  167. _calcularCambio(setState);
  168. }
  169. bool? shouldSave = await showDialog<bool>(
  170. context: context,
  171. builder: (BuildContext context) {
  172. return StatefulBuilder(
  173. builder: (context, setState) {
  174. return AlertDialog(
  175. actionsPadding: EdgeInsets.fromLTRB(50, 10, 50, 30),
  176. title: const Text(
  177. 'Finalizar Pedido',
  178. style: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
  179. ),
  180. content: Column(
  181. mainAxisSize: MainAxisSize.min,
  182. children: [
  183. AppTextField(
  184. controller: nombreController,
  185. etiqueta: 'Nombre',
  186. hintText: "Nombre del Cliente",
  187. errorText: errorMessage.isEmpty ? null : errorMessage,
  188. onChanged: (value) {
  189. setState(() {
  190. errorMessage = value.trim().isEmpty
  191. ? "El nombre del cliente es obligatorio."
  192. : '';
  193. });
  194. },
  195. ),
  196. const SizedBox(height: 10),
  197. AppTextField(
  198. controller: comentarioController,
  199. etiqueta: 'Comentarios (opcional)',
  200. hintText: 'Comentarios',
  201. maxLines: 3,
  202. ),
  203. const SizedBox(height: 10),
  204. Align(
  205. alignment: Alignment.center,
  206. child: Text(
  207. 'Métodos de pago',
  208. style:
  209. TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
  210. ),
  211. ),
  212. const SizedBox(height: 10),
  213. // Efectivo
  214. Row(
  215. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  216. children: [
  217. Row(
  218. children: [
  219. Checkbox(
  220. activeColor: AppTheme.primary,
  221. value: efectivoSeleccionado,
  222. onChanged:
  223. (totalCompletado && !efectivoSeleccionado)
  224. ? null
  225. : (bool? value) {
  226. setState(() {
  227. efectivoSeleccionado = value ?? false;
  228. if (!efectivoSeleccionado) {
  229. efectivoController.clear();
  230. _calcularCambio(setState);
  231. }
  232. });
  233. },
  234. ),
  235. const Text(
  236. "Efectivo",
  237. style: TextStyle(
  238. fontSize: 18, fontWeight: FontWeight.bold),
  239. ),
  240. ],
  241. ),
  242. if (efectivoSeleccionado)
  243. SizedBox(
  244. width: 150,
  245. child: AppTextField(
  246. controller: efectivoController,
  247. etiqueta: 'Cantidad',
  248. hintText: '0.00',
  249. keyboardType: TextInputType.number,
  250. onChanged: (value) => _calcularCambio(setState),
  251. ),
  252. ),
  253. ],
  254. ),
  255. const SizedBox(height: 10),
  256. // Tarjeta
  257. Row(
  258. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  259. children: [
  260. Row(
  261. children: [
  262. Checkbox(
  263. activeColor: AppTheme.primary,
  264. value: tarjetaSeleccionada,
  265. onChanged: (totalCompletado && !tarjetaSeleccionada)
  266. ? null
  267. : (bool? value) {
  268. setState(() {
  269. tarjetaSeleccionada = value ?? false;
  270. if (!tarjetaSeleccionada) {
  271. tarjetaController.clear();
  272. _calcularCambio(setState);
  273. }
  274. });
  275. },
  276. ),
  277. const Text(
  278. "Tarjeta",
  279. style: TextStyle(
  280. fontSize: 18, fontWeight: FontWeight.bold),
  281. ),
  282. ],
  283. ),
  284. if (tarjetaSeleccionada)
  285. SizedBox(
  286. width: 150,
  287. child: AppTextField(
  288. controller: tarjetaController,
  289. etiqueta: 'Cantidad',
  290. hintText: '0.00',
  291. keyboardType: TextInputType.number,
  292. onChanged: (value) {
  293. _validarCantidad(setState, tarjetaController);
  294. },
  295. ),
  296. ),
  297. ],
  298. ),
  299. const SizedBox(height: 10),
  300. // Transferencia
  301. Row(
  302. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  303. children: [
  304. Row(
  305. children: [
  306. Checkbox(
  307. activeColor: AppTheme.primary,
  308. value: transferenciaSeleccionada,
  309. onChanged:
  310. (totalCompletado && !transferenciaSeleccionada)
  311. ? null
  312. : (bool? value) {
  313. setState(() {
  314. transferenciaSeleccionada =
  315. value ?? false;
  316. if (!transferenciaSeleccionada) {
  317. transferenciaController.clear();
  318. _calcularCambio(setState);
  319. }
  320. });
  321. },
  322. ),
  323. const Text(
  324. "Transferencia",
  325. style: TextStyle(
  326. fontSize: 18, fontWeight: FontWeight.bold),
  327. ),
  328. ],
  329. ),
  330. if (transferenciaSeleccionada)
  331. SizedBox(
  332. width: 150,
  333. child: AppTextField(
  334. controller: transferenciaController,
  335. etiqueta: 'Cantidad',
  336. hintText: '0.00',
  337. keyboardType: TextInputType.number,
  338. onChanged: (value) {
  339. _validarCantidad(
  340. setState, transferenciaController);
  341. },
  342. ),
  343. ),
  344. ],
  345. ),
  346. const SizedBox(height: 10),
  347. // Mostrar el total del pedido y la cantidad faltante
  348. Align(
  349. alignment: Alignment.centerRight,
  350. child: Column(
  351. crossAxisAlignment: CrossAxisAlignment.end,
  352. children: [
  353. Text(
  354. 'Total del pedido: \$${totalPedido.toStringAsFixed(2)}',
  355. style: const TextStyle(
  356. fontWeight: FontWeight.bold, fontSize: 18),
  357. ),
  358. if (faltante > 0)
  359. Text(
  360. 'Faltante: \$${faltante.toStringAsFixed(2)}',
  361. style: const TextStyle(
  362. color: Colors.red,
  363. fontSize: 18,
  364. fontWeight: FontWeight.bold),
  365. )
  366. else if (cambio > 0)
  367. Text('Cambio: \$${cambio.toStringAsFixed(2)}',
  368. style: const TextStyle(
  369. color: Colors.green,
  370. fontSize: 18,
  371. fontWeight: FontWeight.bold)),
  372. ]),
  373. ),
  374. ],
  375. ),
  376. actions: <Widget>[
  377. Row(
  378. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  379. children: [
  380. TextButton(
  381. child: const Text('Cancelar',
  382. style: TextStyle(fontSize: 18)),
  383. onPressed: () {
  384. Navigator.of(context).pop(false);
  385. },
  386. style: ButtonStyle(
  387. padding: MaterialStatePropertyAll(
  388. EdgeInsets.fromLTRB(30, 20, 30, 20)),
  389. backgroundColor: MaterialStatePropertyAll(Colors.red),
  390. foregroundColor:
  391. MaterialStatePropertyAll(AppTheme.secondary)),
  392. ),
  393. const SizedBox(width: 100),
  394. TextButton(
  395. child:
  396. const Text('Guardar', style: TextStyle(fontSize: 18)),
  397. onPressed: () {
  398. if (nombreController.text.trim().isEmpty) {
  399. setState(() => errorMessage =
  400. "El nombre del cliente es obligatorio.");
  401. return;
  402. }
  403. Navigator.of(context).pop(true);
  404. },
  405. style: ButtonStyle(
  406. padding: MaterialStatePropertyAll(
  407. EdgeInsets.fromLTRB(30, 20, 30, 20)),
  408. backgroundColor:
  409. MaterialStatePropertyAll(AppTheme.tertiary),
  410. foregroundColor:
  411. MaterialStatePropertyAll(AppTheme.quaternary)),
  412. ),
  413. ],
  414. )
  415. ],
  416. );
  417. },
  418. );
  419. },
  420. );
  421. if (shouldSave ?? false) {
  422. prepararPedidoActual(nombreController.text, comentarioController.text);
  423. }
  424. }
  425. void prepararPedidoActual(String nombreCliente, String comentarios) async {
  426. DateTime now = DateTime.now();
  427. String formattedDate = DateFormat('dd-MM-yyyy kk:mm:ss').format(now);
  428. Pedido nuevoPedido = Pedido(
  429. peticion: formattedDate,
  430. nombreCliente: nombreCliente,
  431. comentarios: comentarios,
  432. estatus: "NUEVO",
  433. totalPedido: totalPedido,
  434. descuento: pedidoActual?.descuento,
  435. tipoPago: _obtenerTipoPago(),
  436. cantEfectivo:
  437. efectivoSeleccionado ? double.tryParse(efectivoController.text) : 0,
  438. cantTarjeta:
  439. tarjetaSeleccionada ? double.tryParse(tarjetaController.text) : 0,
  440. cantTransferencia: transferenciaSeleccionada
  441. ? double.tryParse(transferenciaController.text)
  442. : 0,
  443. );
  444. List<PedidoProducto> listaPedidoProducto = carrito.map((item) {
  445. List<PedidoProductoTopping> selectedToppings = [];
  446. item.selectedToppings.forEach((categoryId, selectedToppingIds) {
  447. for (int toppingId in selectedToppingIds) {
  448. selectedToppings.add(PedidoProductoTopping(
  449. idTopping: toppingId,
  450. ));
  451. }
  452. });
  453. return PedidoProducto(
  454. idProducto: item.producto.id,
  455. producto: item.producto,
  456. costoUnitario: item.producto.precio,
  457. cantidad: item.cantidad,
  458. comentario: comentarios,
  459. toppings: selectedToppings,
  460. );
  461. }).toList();
  462. nuevoPedido.productos = listaPedidoProducto;
  463. bool result = await Provider.of<PedidoViewModel>(context, listen: false)
  464. .guardarPedidoLocal(pedido: nuevoPedido);
  465. if (!mounted) return;
  466. if (result) {
  467. Pedido? pedidoCompleto =
  468. await Provider.of<PedidoViewModel>(context, listen: false)
  469. .fetchPedidoConProductos(nuevoPedido.id!);
  470. if (pedidoCompleto != null) {
  471. imprimirTicketsJuntos(context, pedidoCompleto);
  472. }
  473. Navigator.of(context).pop();
  474. } else {
  475. print("Error al guardar el pedido");
  476. }
  477. }
  478. String _obtenerTipoPago() {
  479. List<String> tiposPago = [];
  480. if (efectivoSeleccionado) tiposPago.add('Efectivo');
  481. if (tarjetaSeleccionada) tiposPago.add('Tarjeta');
  482. if (transferenciaSeleccionada) tiposPago.add('Transferencia');
  483. return tiposPago.join(',');
  484. }
  485. void _limpiarBusqueda() async {
  486. setState(() {
  487. _busqueda.text = '';
  488. _estadoBusqueda = false;
  489. });
  490. await cargarCategoriasIniciales();
  491. }
  492. Future<void> cargarProductosIniciales() async {
  493. setState(() => _isLoading = true);
  494. await Provider.of<ProductoViewModel>(context, listen: false)
  495. .fetchLocalAll();
  496. productos =
  497. Provider.of<ProductoViewModel>(context, listen: false).productos;
  498. setState(() => _isLoading = false);
  499. }
  500. @override
  501. void dispose() {
  502. _gridViewController.dispose();
  503. _searchController.dispose();
  504. super.dispose();
  505. }
  506. Future<void> cargarCategoriasIniciales() async {
  507. setState(() => _isLoading = true);
  508. await Provider.of<CategoriaProductoViewModel>(context, listen: false)
  509. .fetchLocalAll();
  510. categorias = Provider.of<CategoriaProductoViewModel>(context, listen: false)
  511. .categoriaProductos;
  512. if (categorias.isNotEmpty) {
  513. categoriaSeleccionada = categorias.first;
  514. cargarProductosPorCategoria(categoriaSeleccionada!.id);
  515. }
  516. setState(() => _isLoading = false);
  517. if (categorias.isNotEmpty) {
  518. categoriaSeleccionada = categorias.first;
  519. }
  520. }
  521. void cargarProductosPorCategoria(int categoriaId) async {
  522. setState(() => _isLoading = true);
  523. await Provider.of<ProductoViewModel>(context, listen: false)
  524. .fetchAllByCategory(categoriaId);
  525. productos =
  526. Provider.of<ProductoViewModel>(context, listen: false).productos;
  527. setState(() => _isLoading = false);
  528. }
  529. void agregarAlCarrito(Producto producto) async {
  530. var existente = carrito.firstWhereOrNull((item) =>
  531. item.producto.id == producto.id &&
  532. mapEquals(item.selectedToppings, {}));
  533. if (existente != null) {
  534. setState(() {
  535. existente.cantidad++;
  536. });
  537. } else {
  538. Map<int, List<Producto>> toppingsSeleccionables =
  539. await obtenerToppingsSeleccionables(producto);
  540. setState(() {
  541. carrito.add(ItemCarrito(
  542. producto: producto,
  543. cantidad: 1,
  544. selectableToppings: toppingsSeleccionables,
  545. ));
  546. });
  547. }
  548. _recalcularTotal();
  549. }
  550. Future<Map<int, List<Producto>>> obtenerToppingsSeleccionables(
  551. Producto producto) async {
  552. Map<int, List<Producto>> toppingsSeleccionables = {};
  553. final toppingCategories =
  554. await pvm.obtenerToppingsPorProducto(producto.id!);
  555. for (int toppingId in toppingCategories) {
  556. Producto? topping = await pvm.obtenerProductoPorId(toppingId);
  557. if (topping != null && topping.idCategoria != null) {
  558. toppingsSeleccionables[topping.idCategoria!] ??= [];
  559. toppingsSeleccionables[topping.idCategoria]!.add(topping);
  560. }
  561. }
  562. return toppingsSeleccionables;
  563. }
  564. void quitarDelCarrito(Producto producto) {
  565. setState(() {
  566. var indice =
  567. carrito.indexWhere((item) => item.producto.id == producto.id);
  568. if (indice != -1) {
  569. if (carrito[indice].cantidad > 1) {
  570. carrito[indice].cantidad--;
  571. } else {
  572. carrito.removeAt(indice);
  573. }
  574. }
  575. });
  576. }
  577. void addToCart(Producto producto) {
  578. var existingIndex = carrito.indexWhere((item) =>
  579. item.producto.id == producto.id &&
  580. mapEquals(item.selectedToppings, {}));
  581. if (existingIndex != -1) {
  582. setState(() {
  583. carrito[existingIndex].cantidad++;
  584. });
  585. } else {
  586. setState(() {
  587. carrito.add(ItemCarrito(producto: producto, cantidad: 1));
  588. });
  589. }
  590. }
  591. void finalizeCustomization() {
  592. if (_productoActual != null) {
  593. addToCart(_productoActual!);
  594. setState(() {
  595. _productoActual = null;
  596. });
  597. }
  598. }
  599. Widget _buildDiscountSection() {
  600. return Padding(
  601. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  602. child: Row(
  603. crossAxisAlignment: CrossAxisAlignment.center,
  604. children: [
  605. const Text(
  606. 'Descuento',
  607. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  608. ),
  609. const Spacer(),
  610. ConstrainedBox(
  611. constraints: const BoxConstraints(
  612. maxWidth: 150,
  613. ),
  614. child: Consumer<DescuentoViewModel>(
  615. builder: (context, viewModel, child) {
  616. return AppDropdownModel<int>(
  617. hint: 'Seleccionar',
  618. items: viewModel.descuentos
  619. .map(
  620. (descuento) => DropdownMenuItem<int>(
  621. value: descuento.porcentaje,
  622. child: Text(
  623. '${descuento.porcentaje}%',
  624. style: const TextStyle(color: Colors.black),
  625. ),
  626. ),
  627. )
  628. .toList(),
  629. selectedValue: selectedDescuento,
  630. onChanged: (value) {
  631. setState(() {
  632. selectedDescuento = value;
  633. _recalcularTotal();
  634. });
  635. },
  636. );
  637. },
  638. ),
  639. ),
  640. ],
  641. ),
  642. );
  643. }
  644. Widget _buildTotalSection() {
  645. String formattedsubtotal = _numberFormat.format(subtotal);
  646. String formattedPrecioDescuento = _numberFormat.format(precioDescuento);
  647. String formattedtotalPedido = _numberFormat.format(totalPedido);
  648. return Padding(
  649. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  650. child: Column(
  651. crossAxisAlignment: CrossAxisAlignment.start,
  652. children: [
  653. if (precioDescuento > 0)
  654. Column(
  655. children: [
  656. Row(
  657. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  658. children: [
  659. const Text('Subtotal',
  660. style: TextStyle(
  661. fontWeight: FontWeight.bold, fontSize: 18)),
  662. Text("\$$formattedsubtotal",
  663. style: const TextStyle(
  664. fontWeight: FontWeight.bold, fontSize: 18)),
  665. ],
  666. ),
  667. const SizedBox(height: 10),
  668. Row(
  669. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  670. children: [
  671. const Text('Descuento',
  672. style: TextStyle(
  673. fontWeight: FontWeight.bold, fontSize: 18)),
  674. Text("-\$$formattedPrecioDescuento",
  675. style: const TextStyle(
  676. fontWeight: FontWeight.bold, fontSize: 18)),
  677. ],
  678. ),
  679. ],
  680. ),
  681. const SizedBox(height: 10),
  682. Row(
  683. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  684. children: [
  685. const Text('Total',
  686. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
  687. Text("\$$formattedtotalPedido",
  688. style: const TextStyle(
  689. fontWeight: FontWeight.bold, fontSize: 18)),
  690. ],
  691. ),
  692. ],
  693. ),
  694. );
  695. }
  696. @override
  697. Widget build(BuildContext context) {
  698. return Scaffold(
  699. appBar: AppBar(
  700. title:
  701. Text("Crear Pedido", style: TextStyle(color: AppTheme.secondary)),
  702. iconTheme: IconThemeData(color: AppTheme.secondary)),
  703. body: Row(
  704. children: [
  705. Flexible(
  706. flex: 3,
  707. child: _buildCartSection(),
  708. ),
  709. SizedBox(width: 35),
  710. Flexible(flex: 7, child: _buildProductsSection()),
  711. ],
  712. ),
  713. );
  714. }
  715. Widget _buildCartSection() {
  716. return Card(
  717. margin: const EdgeInsets.all(8.0),
  718. child: Column(
  719. children: [
  720. const Padding(
  721. padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 16.0),
  722. child: Row(
  723. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  724. children: [
  725. Text('Producto',
  726. style:
  727. TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
  728. Text('Cantidad',
  729. style:
  730. TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
  731. ],
  732. ),
  733. ),
  734. Expanded(
  735. child: ListView.builder(
  736. itemCount: carrito.length,
  737. itemBuilder: (context, index) {
  738. final item = carrito[index];
  739. return Column(
  740. children: [
  741. ListTile(
  742. title: Text(item.producto.nombre!,
  743. style: const TextStyle(fontWeight: FontWeight.w600)),
  744. subtitle: Text('\$${item.producto.precio}',
  745. style: const TextStyle(
  746. fontWeight: FontWeight.bold,
  747. color: Color(0xFF008000))),
  748. trailing: Row(
  749. mainAxisSize: MainAxisSize.min,
  750. children: [
  751. IconButton(
  752. icon: const Icon(Icons.delete, color: Colors.red),
  753. onPressed: () =>
  754. eliminarProductoDelCarrito(index)),
  755. IconButton(
  756. icon: const Icon(Icons.remove),
  757. onPressed: () => quitarProductoDelCarrito(item)),
  758. const SizedBox(width: 5),
  759. Text('${item.cantidad}',
  760. style: const TextStyle(
  761. fontWeight: FontWeight.bold, fontSize: 14)),
  762. const SizedBox(width: 5),
  763. IconButton(
  764. icon: const Icon(Icons.add),
  765. onPressed: () => incrementarProducto(item)),
  766. ],
  767. ),
  768. ),
  769. if (item.selectableToppings.isNotEmpty)
  770. ExpansionTile(
  771. title: Text('Toppings',
  772. style: const TextStyle(
  773. fontWeight: FontWeight.bold, fontSize: 16)),
  774. children: item.selectableToppings.entries.map((entry) {
  775. final categoryId = entry.key;
  776. final availableToppings = entry.value;
  777. final categoria =
  778. categorias.firstWhere((c) => c.id == categoryId);
  779. return Column(
  780. crossAxisAlignment: CrossAxisAlignment.start,
  781. children: [
  782. Padding(
  783. padding: const EdgeInsets.only(left: 16.0),
  784. child: Text(
  785. categoria.descripcion!,
  786. style: const TextStyle(
  787. fontWeight: FontWeight.bold,
  788. fontSize: 16),
  789. ),
  790. ),
  791. ...availableToppings.map((topping) {
  792. ValueNotifier<bool> isSelectedNotifier =
  793. ValueNotifier<bool>(
  794. item.selectedToppings[categoryId]
  795. ?.contains(topping.id) ??
  796. false,
  797. );
  798. return ValueListenableBuilder<bool>(
  799. valueListenable: isSelectedNotifier,
  800. builder: (context, isSelected, _) {
  801. return CheckboxListTile(
  802. activeColor: AppTheme.primary,
  803. title: Row(
  804. mainAxisAlignment:
  805. MainAxisAlignment.spaceBetween,
  806. children: [
  807. Text(topping.nombre!),
  808. if (double.tryParse(
  809. topping.precio!) !=
  810. 0.0)
  811. Text(
  812. '+\$${topping.precio}',
  813. style: TextStyle(
  814. color: Colors.black,
  815. fontSize: 13,
  816. ),
  817. ),
  818. ],
  819. ),
  820. value: isSelected,
  821. onChanged: (bool? value) {
  822. final maximoToppings =
  823. categoria.maximo ?? 0;
  824. if (value == true) {
  825. if ((item.selectedToppings[categoryId]
  826. ?.length ??
  827. 0) >=
  828. maximoToppings) {
  829. item.selectedToppings[categoryId]!
  830. .remove(item
  831. .selectedToppings[
  832. categoryId]!
  833. .first);
  834. }
  835. item.selectedToppings[categoryId] ??=
  836. <int>{};
  837. item.selectedToppings[categoryId]!
  838. .add(topping.id!);
  839. } else {
  840. item.selectedToppings[categoryId]
  841. ?.remove(topping.id!);
  842. if (item.selectedToppings[categoryId]
  843. ?.isEmpty ??
  844. false) {
  845. item.selectedToppings
  846. .remove(categoryId);
  847. }
  848. }
  849. setState(() {});
  850. _recalcularTotal();
  851. },
  852. );
  853. },
  854. );
  855. }).toList(),
  856. ],
  857. );
  858. }).toList(),
  859. ),
  860. Divider(),
  861. ],
  862. );
  863. },
  864. ),
  865. ),
  866. _buildDiscountSection(),
  867. const Divider(thickness: 5),
  868. _buildTotalSection(),
  869. const SizedBox(height: 25),
  870. Padding(
  871. padding: const EdgeInsets.all(8.0),
  872. child: ElevatedButton(
  873. onPressed: _finalizeOrder,
  874. style: ElevatedButton.styleFrom(
  875. primary: AppTheme.tertiary,
  876. textStyle: const TextStyle(fontSize: 22),
  877. fixedSize: const Size(250, 50),
  878. ),
  879. child: Text('Finalizar Pedido',
  880. style: TextStyle(color: AppTheme.quaternary)),
  881. ),
  882. ),
  883. ],
  884. ),
  885. );
  886. }
  887. void eliminarProductoDelCarrito(int index) {
  888. setState(() {
  889. carrito.removeAt(index);
  890. });
  891. _recalcularTotal();
  892. }
  893. void incrementarProducto(ItemCarrito item) {
  894. setState(() {
  895. item.cantidad++;
  896. });
  897. _recalcularTotal();
  898. }
  899. void quitarProductoDelCarrito(ItemCarrito item) {
  900. setState(() {
  901. if (item.cantidad > 1) {
  902. item.cantidad--;
  903. } else {
  904. carrito.remove(item);
  905. }
  906. });
  907. _recalcularTotal();
  908. }
  909. Widget _buildProductsSection() {
  910. return Column(
  911. children: [
  912. const SizedBox(height: 10),
  913. _buildCategoryButtons(),
  914. const SizedBox(height: 15),
  915. Expanded(
  916. child: Consumer<ProductoViewModel>(builder: (context, model, child) {
  917. productos = model.productos;
  918. return GridView.builder(
  919. controller: _gridViewController,
  920. key: ValueKey<int>(categoriaSeleccionada?.id ?? 0),
  921. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  922. crossAxisCount: 3,
  923. childAspectRatio: 3 / 2,
  924. ),
  925. itemCount: productos.length,
  926. itemBuilder: (context, index) {
  927. final producto = productos[index];
  928. if (producto.idCategoria != categoriaSeleccionada?.id) {
  929. return Container();
  930. }
  931. return Card(
  932. child: InkWell(
  933. onTap: () => agregarAlCarrito(producto),
  934. child: Column(
  935. mainAxisAlignment: MainAxisAlignment.center,
  936. children: [
  937. if (producto.imagen != null &&
  938. File(producto.imagen!).existsSync())
  939. Image.file(
  940. File(producto.imagen!),
  941. height: 120,
  942. fit: BoxFit.cover,
  943. )
  944. else
  945. const Icon(Icons.fastfood, size: 80),
  946. const SizedBox(height: 8),
  947. Padding(
  948. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  949. child: Text(
  950. producto.nombre ?? '',
  951. style: const TextStyle(
  952. fontSize: 16,
  953. fontWeight: FontWeight.bold,
  954. ),
  955. textAlign: TextAlign.center,
  956. ),
  957. ),
  958. const SizedBox(height: 8),
  959. Text(
  960. '\$${producto.precio}',
  961. style: const TextStyle(
  962. fontSize: 16,
  963. fontWeight: FontWeight.bold,
  964. color: Color(0xFF008000),
  965. ),
  966. ),
  967. ],
  968. ),
  969. ),
  970. );
  971. },
  972. );
  973. }),
  974. )
  975. ],
  976. );
  977. }
  978. Widget _buildCategoryButtons() {
  979. List<CategoriaProducto> categoriasFiltradas =
  980. categorias.where((categoria) => categoria.esToping == 0).toList();
  981. return Container(
  982. height: 50,
  983. child: ListView.builder(
  984. scrollDirection: Axis.horizontal,
  985. itemCount: categoriasFiltradas.length,
  986. itemBuilder: (context, index) {
  987. final categoria = categoriasFiltradas[index];
  988. bool isSelected = categoriaSeleccionada?.id == categoria.id;
  989. return Padding(
  990. padding: const EdgeInsets.symmetric(horizontal: 4.0),
  991. child: ElevatedButton(
  992. onPressed: () {
  993. cargarProductosPorCategoria(categoria.id);
  994. setState(() {
  995. categoriaSeleccionada = categoria;
  996. });
  997. },
  998. style: ElevatedButton.styleFrom(
  999. primary: isSelected ? AppTheme.tertiary : Colors.grey,
  1000. foregroundColor:
  1001. isSelected ? AppTheme.quaternary : AppTheme.secondary,
  1002. onPrimary: AppTheme.secondary,
  1003. ),
  1004. child: Text(categoria.nombre!),
  1005. ),
  1006. );
  1007. },
  1008. ),
  1009. );
  1010. }
  1011. Widget _buildSearchBar() {
  1012. return Padding(
  1013. padding: const EdgeInsets.all(8.0),
  1014. child: TextField(
  1015. controller: _searchController,
  1016. decoration: InputDecoration(
  1017. hintText: 'Buscar producto...',
  1018. prefixIcon: const Icon(Icons.search),
  1019. border: OutlineInputBorder(
  1020. borderRadius: BorderRadius.circular(12.0),
  1021. ),
  1022. ),
  1023. onChanged: _onSearchChanged,
  1024. ),
  1025. );
  1026. }
  1027. }