123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841 |
- import 'package:collection/collection.dart';
- import 'package:intl/intl.dart';
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../../models/models.dart';
- import '../../services/services.dart';
- import '../../themes/themes.dart';
- import '../../viewmodels/viewmodels.dart';
- import '../../widgets/widgets.dart';
- import '../pedido/pedido_ticket.dart';
- class PedidoDetalleScreen extends StatefulWidget {
- final Pedido pedido;
- PedidoDetalleScreen({Key? key, required this.pedido}) : super(key: key);
- @override
- _PedidoDetalleScreenState createState() => _PedidoDetalleScreenState();
- }
- class _PedidoDetalleScreenState extends State<PedidoDetalleScreen> {
- late Pedido pedido;
- @override
- void initState() {
- super.initState();
- pedido = widget.pedido;
- }
- String formatCurrency(double amount) {
- final format = NumberFormat("#,##0.00", "es_MX");
- return format.format(amount);
- }
- final List<String> tiposDePago = ['Efectivo', 'Tarjeta', 'Transferencia'];
- @override
- Widget build(BuildContext context) {
- double totalSinDescuento =
- pedido.productos.fold(0, (previousValue, element) {
- double productTotal =
- element.cantidad! * (element.producto?.precio ?? 0.0);
- double toppingsTotal = element.toppings.fold(0, (toppingTotal, topping) {
- return toppingTotal +
- (topping.topping?.precio ?? 0.0) * element.cantidad!;
- });
- return previousValue + productTotal + toppingsTotal;
- });
- double descuento = pedido.descuento?.toDouble() ?? 0.0;
- double precioDescuento = totalSinDescuento * (descuento / 100);
- double totalConDescuento = totalSinDescuento - precioDescuento;
- return Scaffold(
- appBar: AppBar(
- title: Text(
- 'Detalle del Pedido ${pedido.folio}',
- style: TextStyle(fontWeight: FontWeight.w500),
- ),
- ),
- body: SingleChildScrollView(
- padding: const EdgeInsets.all(12.0),
- child: Column(
- children: [
- Card(
- elevation: 4,
- color: Colors.white,
- child: Column(
- children: [
- // Colocamos la fecha y el cliente en una misma fila
- ListTile(
- title: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- 'Cliente: ${pedido.nombreCliente}',
- style: TextStyle(
- fontWeight: FontWeight.bold, fontSize: 22),
- ),
- Text(
- 'Fecha: ${_formatDateTime(pedido.peticion)}',
- style: TextStyle(
- fontWeight: FontWeight.bold, fontSize: 22),
- ),
- ],
- ),
- ),
- ListTile(
- subtitle: Text(
- 'Comentarios: ${pedido.comentarios}',
- style:
- TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
- ),
- ),
- ListTile(
- title: Text(
- 'Estado del Pedido: ${pedido.estatus}',
- style: TextStyle(
- fontSize: 22,
- fontWeight: FontWeight.bold,
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(height: 10),
- Card(
- elevation: 4,
- color: Colors.white,
- child: Padding(
- padding: const EdgeInsets.all(8.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text('Productos',
- style: TextStyle(
- fontSize: 22, fontWeight: FontWeight.bold)),
- const SizedBox(height: 15),
- ListView.builder(
- shrinkWrap: true,
- physics: NeverScrollableScrollPhysics(),
- itemCount: pedido.productos.length,
- itemBuilder: (context, index) {
- final producto = pedido.productos[index];
- return Padding(
- padding: const EdgeInsets.symmetric(vertical: 4.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- Expanded(
- flex: 6,
- child: Text(
- producto.producto?.nombre ??
- "Producto no especificado",
- style: TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 17),
- overflow: TextOverflow.ellipsis,
- ),
- ),
- Expanded(
- flex: 1,
- child: Text(
- 'x${producto.cantidad}',
- style: TextStyle(
- fontWeight: FontWeight.w500,
- fontSize: 17),
- textAlign: TextAlign.center,
- ),
- ),
- Expanded(
- flex: 2,
- child: Text(
- '\$${formatCurrency(producto.producto?.precio ?? 0.0)}',
- style: TextStyle(
- fontWeight: FontWeight.w500,
- fontSize: 17),
- textAlign: TextAlign.right,
- ),
- ),
- ],
- ),
- if (producto.comentario!.isNotEmpty)
- Text(
- 'Comentarios: ${producto.comentario!}',
- style: TextStyle(
- fontSize: 15, color: AppTheme.tertiary),
- ),
- if (producto.toppings.isNotEmpty)
- Padding(
- padding: const EdgeInsets.only(top: 4.0),
- child: Column(
- crossAxisAlignment:
- CrossAxisAlignment.start,
- children: producto.toppings.map((topping) {
- return Padding(
- padding: const EdgeInsets.symmetric(
- vertical: 2.0),
- child: Row(
- children: [
- Text(
- '- ${topping.topping?.nombre ?? "Topping no especificado"}',
- style: TextStyle(
- fontSize: 15,
- color: Colors.grey[600]),
- ),
- Spacer(),
- Text(
- '\$${formatCurrency(topping.topping?.precio ?? 0.0)}',
- style: TextStyle(
- fontSize: 15,
- color: Colors.grey[600]),
- ),
- ],
- ),
- );
- }).toList(),
- ),
- ),
- ],
- ),
- );
- },
- ),
- Divider(),
- Padding(
- padding: const EdgeInsets.symmetric(vertical: 8.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: [
- const Text('Subtotal:',
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- const SizedBox(width: 5),
- Text('\$${formatCurrency(totalSinDescuento)}',
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- ],
- ),
- if (descuento > 0) ...[
- Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: [
- Text(
- 'Descuento (${descuento.toStringAsFixed(0)}%):',
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- const SizedBox(width: 8),
- Text('-\$${formatCurrency(precioDescuento)}',
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- ],
- ),
- ],
- Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: [
- const Text('Total:',
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- const SizedBox(width: 5),
- Text('\$${formatCurrency(totalConDescuento)}',
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold)),
- ],
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- const SizedBox(height: 10),
- Card(
- elevation: 4,
- color: Colors.white,
- child: Padding(
- padding: const EdgeInsets.all(8.0),
- child: Consumer<PedidoViewModel>(
- builder: (context, viewModel, _) {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- Text('Pago',
- style: TextStyle(
- fontSize: 22, fontWeight: FontWeight.bold)),
- Spacer(),
- ElevatedButton(
- onPressed: () {
- showDialog(
- context: context,
- builder: (context) {
- return TotpCuadroConfirmacion(
- title: "Cambiar Método de Pago",
- content:
- "Por favor, ingresa el código de autenticación para continuar.",
- onSuccess: () {
- _mostrarModalCambiarMetodoPago(context);
- },
- );
- },
- );
- },
- child: Text('Cambiar Método Pago',
- style: TextStyle(
- color: AppTheme.quaternary,
- fontWeight: FontWeight.w500,
- fontSize: 16)),
- style: ElevatedButton.styleFrom(
- backgroundColor: AppTheme.tertiary,
- padding:
- const EdgeInsets.fromLTRB(20, 10, 20, 10),
- ),
- ),
- ],
- ),
- const SizedBox(height: 10),
- if ((pedido.cantEfectivo ?? 0) > 0)
- _buildReadOnlyPaymentRow(
- "Efectivo", pedido.cantEfectivo ?? 0.0),
- if ((pedido.cantTarjeta ?? 0) > 0)
- _buildReadOnlyPaymentRow(
- "Tarjeta", pedido.cantTarjeta ?? 0.0),
- if ((pedido.cantTransferencia ?? 0) > 0)
- _buildReadOnlyPaymentRow(
- "Transferencia", pedido.cantTransferencia ?? 0.0),
- ],
- );
- },
- ),
- ),
- ),
- const SizedBox(height: 20),
- Align(
- alignment: Alignment.centerLeft,
- child: ElevatedButton.icon(
- icon: Icon(
- Icons.receipt_long_outlined,
- color: AppTheme.quaternary,
- size: 30,
- ),
- onPressed: () => imprimirTicketsJuntos(context, pedido),
- label: Text(
- 'Imprimir Ticket',
- style: TextStyle(
- fontWeight: FontWeight.w500,
- fontSize: 18,
- color: AppTheme.quaternary),
- ),
- style: ElevatedButton.styleFrom(
- padding: const EdgeInsets.fromLTRB(50, 20, 50, 20),
- backgroundColor: AppTheme.tertiary,
- ),
- ),
- )
- ],
- ),
- ),
- );
- }
- Widget _buildReadOnlyPaymentRow(String paymentType, double amount) {
- return Padding(
- padding: const EdgeInsets.symmetric(vertical: 6.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(paymentType,
- style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
- Text('\$${formatCurrency(amount)}',
- style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
- ],
- ),
- );
- }
- Future<void> _mostrarModalCambiarMetodoPago(BuildContext context) async {
- double totalPedido = pedido.totalPedido ?? 0.0;
- TextEditingController efectivoController = TextEditingController(
- text: (pedido.cantEfectivo ?? 0) > 0
- ? pedido.cantEfectivo!.toStringAsFixed(2)
- : '');
- TextEditingController tarjetaController = TextEditingController(
- text: (pedido.cantTarjeta ?? 0) > 0
- ? pedido.cantTarjeta!.toStringAsFixed(2)
- : '');
- TextEditingController transferenciaController = TextEditingController(
- text: (pedido.cantTransferencia ?? 0) > 0
- ? pedido.cantTransferencia!.toStringAsFixed(2)
- : '');
- bool efectivoSeleccionado = (pedido.cantEfectivo ?? 0) > 0;
- bool tarjetaSeleccionada = (pedido.cantTarjeta ?? 0) > 0;
- bool transferenciaSeleccionada = (pedido.cantTransferencia ?? 0) > 0;
- bool efectivoCompleto = false;
- bool tarjetaCompleto = false;
- bool transferenciaCompleto = false;
- double cambio = 0.0;
- double faltante = totalPedido;
- bool totalCompletado = false;
- void _calcularCambio(StateSetter setState) {
- double totalPagado = (double.tryParse(efectivoController.text) ?? 0) +
- (double.tryParse(tarjetaController.text) ?? 0) +
- (double.tryParse(transferenciaController.text) ?? 0);
- setState(() {
- cambio = totalPagado - totalPedido;
- faltante = cambio < 0 ? totalPedido - totalPagado : 0;
- totalCompletado = cambio >= 0;
- });
- }
- bool? shouldSave = await showDialog<bool>(
- context: context,
- builder: (BuildContext context) {
- return StatefulBuilder(
- builder: (context, setState) {
- return AlertDialog(
- actionsPadding: EdgeInsets.fromLTRB(50, 10, 50, 30),
- title: const Text(
- 'Cambiar Método de Pago',
- style: TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
- ),
- content: SingleChildScrollView(
- child: AnimatedSize(
- duration: const Duration(milliseconds: 300),
- curve: Curves.easeInOut,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Align(
- alignment: Alignment.center,
- child: Text(
- 'Métodos de pago',
- style: TextStyle(
- fontWeight: FontWeight.bold, fontSize: 20),
- ),
- ),
- const SizedBox(height: 10),
- _buildPaymentMethodRow(
- setState,
- totalPedido,
- label: 'Efectivo',
- selected: efectivoSeleccionado,
- exactSelected: efectivoCompleto,
- controller: efectivoController,
- onSelected: (value) {
- setState(() {
- efectivoSeleccionado = value;
- if (!efectivoSeleccionado) {
- efectivoCompleto = false;
- efectivoController.clear();
- }
- _calcularCambio(setState);
- });
- },
- onExactSelected: (value) {
- setState(() {
- efectivoCompleto = value;
- if (efectivoCompleto) {
- efectivoController.text =
- totalPedido.toStringAsFixed(2);
- efectivoSeleccionado = true;
- tarjetaSeleccionada = false;
- transferenciaSeleccionada = false;
- tarjetaController.clear();
- transferenciaController.clear();
- } else {
- efectivoController.clear();
- }
- _calcularCambio(setState);
- });
- },
- disableOtherMethods:
- tarjetaCompleto || transferenciaCompleto,
- onChangedMonto: () => _calcularCambio(setState),
- ),
- _buildPaymentMethodRow(
- setState,
- totalPedido,
- label: 'Tarjeta',
- selected: tarjetaSeleccionada,
- exactSelected: tarjetaCompleto,
- controller: tarjetaController,
- sinCambio: true,
- onSelected: (value) {
- setState(() {
- tarjetaSeleccionada = value;
- if (!tarjetaSeleccionada) {
- tarjetaCompleto = false;
- tarjetaController.clear();
- }
- _calcularCambio(setState);
- });
- },
- onExactSelected: (value) {
- setState(() {
- tarjetaCompleto = value;
- if (tarjetaCompleto) {
- tarjetaController.text =
- totalPedido.toStringAsFixed(2);
- tarjetaSeleccionada = true;
- efectivoSeleccionado = false;
- transferenciaSeleccionada = false;
- efectivoController.clear();
- transferenciaController.clear();
- } else {
- tarjetaController.clear();
- }
- _calcularCambio(setState);
- });
- },
- disableOtherMethods:
- efectivoCompleto || transferenciaCompleto,
- onChangedMonto: () => _calcularCambio(setState),
- ),
- _buildPaymentMethodRow(
- setState,
- totalPedido,
- label: 'Transferencia',
- selected: transferenciaSeleccionada,
- exactSelected: transferenciaCompleto,
- controller: transferenciaController,
- sinCambio: true,
- onSelected: (value) {
- setState(() {
- transferenciaSeleccionada = value;
- if (!transferenciaSeleccionada) {
- transferenciaCompleto = false;
- transferenciaController.clear();
- }
- _calcularCambio(setState);
- });
- },
- onExactSelected: (value) {
- setState(() {
- transferenciaCompleto = value;
- if (transferenciaCompleto) {
- transferenciaController.text =
- totalPedido.toStringAsFixed(2);
- transferenciaSeleccionada = true;
- efectivoSeleccionado = false;
- tarjetaSeleccionada = false;
- efectivoController.clear();
- tarjetaController.clear();
- } else {
- transferenciaController.clear();
- }
- _calcularCambio(setState);
- });
- },
- disableOtherMethods:
- efectivoCompleto || tarjetaCompleto,
- onChangedMonto: () => _calcularCambio(setState),
- ),
- const SizedBox(height: 10),
- Align(
- alignment: Alignment.centerRight,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Text(
- 'Total del pedido: \$${totalPedido.toStringAsFixed(2)}',
- style: const TextStyle(
- fontWeight: FontWeight.bold, fontSize: 18),
- ),
- if (faltante > 0)
- Text(
- 'Faltante: \$${faltante.toStringAsFixed(2)}',
- style: const TextStyle(
- color: Colors.red,
- fontSize: 18,
- fontWeight: FontWeight.bold),
- )
- else if (cambio > 0)
- Text(
- 'Cambio: \$${cambio.toStringAsFixed(2)}',
- style: const TextStyle(
- color: Colors.green,
- fontSize: 18,
- fontWeight: FontWeight.bold),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- actions: [
- TextButton(
- child: const Text('Cancelar', style: TextStyle(fontSize: 18)),
- onPressed: () => Navigator.of(context).pop(false),
- style: ButtonStyle(
- padding: MaterialStateProperty.all(
- EdgeInsets.fromLTRB(30, 20, 30, 20)),
- backgroundColor: MaterialStateProperty.all(Colors.red),
- foregroundColor:
- MaterialStateProperty.all(AppTheme.secondary),
- ),
- ),
- const SizedBox(width: 100),
- TextButton(
- child: const Text('Guardar', style: TextStyle(fontSize: 18)),
- onPressed: totalCompletado
- ? () => Navigator.of(context).pop(true)
- : null,
- style: ButtonStyle(
- padding: MaterialStateProperty.all(
- EdgeInsets.fromLTRB(30, 20, 30, 20)),
- backgroundColor: MaterialStateProperty.all(
- totalCompletado ? AppTheme.tertiary : Colors.grey),
- foregroundColor:
- MaterialStateProperty.all(AppTheme.quaternary),
- ),
- ),
- ],
- );
- },
- );
- },
- );
- if (shouldSave ?? false) {
- double cantEfectivo = efectivoSeleccionado
- ? double.tryParse(efectivoController.text) ?? 0
- : 0;
- double cantTarjeta = tarjetaSeleccionada
- ? double.tryParse(tarjetaController.text) ?? 0
- : 0;
- double cantTransferencia = transferenciaSeleccionada
- ? double.tryParse(transferenciaController.text) ?? 0
- : 0;
- pedido.cantEfectivo = cantEfectivo;
- pedido.cantTarjeta = cantTarjeta;
- pedido.cantTransferencia = cantTransferencia;
- List<String> nuevosMetodos = [];
- if (cantEfectivo > 0) nuevosMetodos.add('Efectivo');
- if (cantTarjeta > 0) nuevosMetodos.add('Tarjeta');
- if (cantTransferencia > 0) nuevosMetodos.add('Transferencia');
- pedido.tipoPago =
- nuevosMetodos.isNotEmpty ? nuevosMetodos.join(',') : 'No Definido';
- pedido.sincronizado = null;
- await _guardarPedido(pedido, context);
- // Recargamos el pedido desde la BD para tener sus datos actualizados
- PedidoViewModel viewModel =
- Provider.of<PedidoViewModel>(context, listen: false);
- Pedido? pedidoActualizado =
- await viewModel.fetchPedidoConProductos(pedido.id!);
- if (pedidoActualizado != null) {
- setState(() {
- pedido = pedidoActualizado;
- });
- }
- }
- }
- Widget _buildPaymentMethodRow(
- StateSetter setState,
- double totalPedido, {
- required String label,
- required bool selected,
- required bool exactSelected,
- required TextEditingController controller,
- required Function(bool) onSelected,
- required Function(bool) onExactSelected,
- required bool disableOtherMethods,
- required Function() onChangedMonto,
- bool sinCambio = false,
- }) {
- return Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Row(
- children: [
- Checkbox(
- activeColor: AppTheme.primary,
- value: selected,
- onChanged: disableOtherMethods
- ? null
- : (value) {
- onSelected(value ?? false);
- },
- ),
- GestureDetector(
- onTap: disableOtherMethods
- ? null
- : () {
- onSelected(!selected);
- },
- child: Text(
- label,
- style:
- const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
- ),
- ),
- ],
- ),
- SizedBox(
- width: 180,
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Column(
- children: [
- const Text(
- 'Exacto',
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- color: Colors.black),
- ),
- const SizedBox(height: 17),
- Checkbox(
- activeColor: AppTheme.primary,
- value: exactSelected,
- onChanged: !disableOtherMethods
- ? (value) {
- onExactSelected(value ?? false);
- if (value == true) {
- setState(() {
- // nada especial, ya manejamos esto arriba
- });
- }
- }
- : null,
- ),
- ],
- ),
- const SizedBox(width: 5),
- Expanded(
- child: AppTextField(
- controller: controller,
- enabled: selected,
- etiqueta: 'Cantidad',
- hintText: '0.00',
- keyboardType: TextInputType.number,
- onChanged: (value) {
- if (sinCambio) {
- double? input = double.tryParse(value) ?? 0;
- if (input > totalPedido) {
- controller.text = totalPedido.toStringAsFixed(2);
- controller.selection = TextSelection.fromPosition(
- TextPosition(offset: controller.text.length),
- );
- }
- }
- onChangedMonto();
- },
- ),
- ),
- ],
- ),
- ),
- ],
- );
- }
- String _formatDateTime(String? dateTimeString) {
- if (dateTimeString == null) return "Sin fecha";
- DateTime parsedDate = DateTime.parse(dateTimeString);
- var formatter = DateFormat('dd-MM-yyyy HH:mm:ss');
- return formatter.format(parsedDate.toLocal());
- }
- Future<void> _guardarPedido(Pedido pedido, BuildContext buildContext) async {
- RepoService<Pedido> repoPedido = RepoService<Pedido>();
- RepoService<PedidoProducto> repoPedidoProducto =
- RepoService<PedidoProducto>();
- RepoService<PedidoProductoTopping> repoPedidoProductoTopping =
- RepoService<PedidoProductoTopping>();
- try {
- if (pedido.id != null && pedido.id! > 0) {
- await repoPedido.guardar(pedido);
- } else {
- pedido.id = await repoPedido.guardarLocal(pedido);
- }
- List<PedidoProducto> productosExistentes =
- await repoPedidoProducto.obtenerPorIdPedido(pedido.id!);
- for (var productoExistente in productosExistentes) {
- bool sigueExistiendo = pedido.productos.any(
- (producto) => producto.idProducto == productoExistente.idProducto);
- if (!sigueExistiendo) {
- productoExistente.eliminado = DateTime.now().toUtc();
- await repoPedidoProducto.guardar(productoExistente);
- }
- }
- for (var producto in pedido.productos) {
- PedidoProducto pedidoProducto = PedidoProducto(
- idPedido: pedido.id,
- idProducto: producto.idProducto,
- cantidad: producto.cantidad,
- costoUnitario: producto.costoUnitario,
- comentario: producto.comentario,
- );
- PedidoProducto? productoExistente = productosExistentes
- .firstWhereOrNull((p) => p.idProducto == pedidoProducto.idProducto);
- if (productoExistente != null) {
- pedidoProducto.id = productoExistente.id;
- await repoPedidoProducto.guardar(pedidoProducto);
- } else {
- int idPedidoProducto =
- await repoPedidoProducto.guardarLocal(pedidoProducto);
- for (var topping in producto.toppings) {
- PedidoProductoTopping pedidoProductoTopping = PedidoProductoTopping(
- idPedidoProducto: idPedidoProducto,
- idTopping: topping.idTopping,
- idCategoria: topping.idCategoria,
- );
- await repoPedidoProductoTopping.guardarLocal(pedidoProductoTopping);
- }
- }
- }
- ScaffoldMessenger.of(buildContext).showSnackBar(
- const SnackBar(content: Text("Pedido actualizado correctamente.")),
- );
- } catch (e) {
- print("Error al guardar el pedido: $e");
- ScaffoldMessenger.of(buildContext).showSnackBar(
- const SnackBar(content: Text("Error al actualizar el pedido.")),
- );
- }
- }
- }
|