Ver código fonte

15358: Poner rango en selector de Pedidos de dia , ajustar dos columnas incluirlas, fecha y propina, cambiar etiqueta del final de dia por rango y en ticket agregar total propinas https://prnt.sc/RFz4_6F7uIHS

ElPoteito 2 meses atrás
pai
commit
bbeb86fdc6
1 arquivos alterados com 176 adições e 59 exclusões
  1. 176 59
      lib/views/venta/venta_screen.dart

+ 176 - 59
lib/views/venta/venta_screen.dart

@@ -15,11 +15,14 @@ class VentaScreen extends StatefulWidget {
 }
 
 class _VentaScreenState extends State<VentaScreen> {
-  DateTime? fechaSeleccionada;
+  DateTime? fechaInicialSeleccionada;
+  DateTime? fechaFinalSeleccionada;
   List<Pedido> pedidosNoCancelados = [];
   List<Pedido> pedidosCancelados = [];
+  List<Propinas> listaPropinas = [];
   final _busqueda = TextEditingController(text: '');
   double totalDelDia = 0.0;
+  double totalPropinas = 0.0;
   double totalCancelados = 0.0;
 
   double totalEfectivoDelDia = 0.0;
@@ -36,7 +39,8 @@ class _VentaScreenState extends State<VentaScreen> {
   void clearSearchAndReset() {
     setState(() {
       _busqueda.clear();
-      fechaSeleccionada = null;
+      fechaInicialSeleccionada = null;
+      fechaFinalSeleccionada = null;
     });
   }
 
@@ -45,7 +49,7 @@ class _VentaScreenState extends State<VentaScreen> {
     return Scaffold(
       appBar: AppBar(
           title: Text(
-            "Resumen de Pedidos por Día",
+            "Resumen de Pedidos por Periodo",
             style: TextStyle(color: AppTheme.secondary),
           ),
           iconTheme: IconThemeData(color: AppTheme.secondary)),
@@ -62,16 +66,16 @@ class _VentaScreenState extends State<VentaScreen> {
                       child: tarjeta(
                         ListTile(
                             title: Text(
-                              "Fecha",
+                              "Fecha Inicial",
                               style: TextStyle(
                                   color: AppTheme.quaternary,
                                   fontWeight: FontWeight.bold),
                             ),
                             subtitle: Text(
-                              fechaSeleccionada == null
+                              fechaInicialSeleccionada == null
                                   ? ""
                                   : DateFormat("dd/MM/yyyy")
-                                      .format(fechaSeleccionada!),
+                                      .format(fechaInicialSeleccionada!),
                               style: TextStyle(
                                   color: AppTheme.quaternary,
                                   fontWeight: FontWeight.bold),
@@ -80,15 +84,58 @@ class _VentaScreenState extends State<VentaScreen> {
                                 color: AppTheme.quaternary),
                             onTap: () async {
                               DateTime? d = await showDatetimePicker(
-                                  context, fechaSeleccionada,
-                                  inicia: fechaSeleccionada,
+                                  context, fechaInicialSeleccionada,
+                                  inicia: fechaInicialSeleccionada,
                                   tipo: OmniDateTimePickerType.date,
                                   solofecha: true);
                               if (d == null) return;
                               setState(() {
-                                fechaSeleccionada = d;
+                                fechaInicialSeleccionada = d;
                               });
-                              cargarPedidos(fechaSeleccionada!);
+                              cargarPedidos(fechaInicialSeleccionada,
+                                  fechaFinalSeleccionada);
+                            }),
+                        color: AppTheme.tertiary,
+                      ),
+                    )),
+                const SizedBox(
+                  width: 10,
+                ),
+                Expanded(
+                    flex: 3,
+                    child: Padding(
+                      padding: const EdgeInsets.symmetric(horizontal: 0.0),
+                      child: tarjeta(
+                        ListTile(
+                            title: Text(
+                              "Fecha Final",
+                              style: TextStyle(
+                                  color: AppTheme.quaternary,
+                                  fontWeight: FontWeight.bold),
+                            ),
+                            subtitle: Text(
+                              fechaFinalSeleccionada == null
+                                  ? ""
+                                  : DateFormat("dd/MM/yyyy")
+                                      .format(fechaFinalSeleccionada!),
+                              style: TextStyle(
+                                  color: AppTheme.quaternary,
+                                  fontWeight: FontWeight.bold),
+                            ),
+                            trailing: Icon(Icons.calendar_month,
+                                color: AppTheme.quaternary),
+                            onTap: () async {
+                              DateTime? d = await showDatetimePicker(
+                                  context, fechaFinalSeleccionada,
+                                  inicia: fechaFinalSeleccionada,
+                                  tipo: OmniDateTimePickerType.date,
+                                  solofecha: true);
+                              if (d == null) return;
+                              setState(() {
+                                fechaFinalSeleccionada = d;
+                              });
+                              cargarPedidos(fechaInicialSeleccionada,
+                                  fechaFinalSeleccionada);
                             }),
                         color: AppTheme.tertiary,
                       ),
@@ -136,6 +183,16 @@ class _VentaScreenState extends State<VentaScreen> {
                                 fontSize: 20, fontWeight: FontWeight.w500),
                           ),
                           Text(
+                            "Fecha/Hora: ${_formatDateTime(pedido.peticion)}",
+                            style: TextStyle(
+                                fontSize: 20, fontWeight: FontWeight.w500),
+                          ),
+                          Text(
+                            "Propina: \$${_propinaPedido(pedido.id)}",
+                            style: TextStyle(
+                                fontSize: 20, fontWeight: FontWeight.w500),
+                          ),
+                          Text(
                               "Total: \$${formatCurrency(pedido.totalPedido ?? 0)}",
                               style: TextStyle(
                                   fontSize: 20, fontWeight: FontWeight.w500)),
@@ -194,13 +251,25 @@ class _VentaScreenState extends State<VentaScreen> {
                 Column(
                   crossAxisAlignment: CrossAxisAlignment.end,
                   children: [
-                    Padding(
-                      padding: const EdgeInsets.all(16.0),
-                      child: Text(
-                        "Total del día: \$${formatCurrency(totalDelDia)}",
-                        style: TextStyle(
-                            fontSize: 20, fontWeight: FontWeight.bold),
-                      ),
+                    Row(
+                      children: [
+                        Padding(
+                          padding: const EdgeInsets.all(16.0),
+                          child: Text(
+                            "Total Propina: \$${formatCurrency(totalPropinas)}",
+                            style: TextStyle(
+                                fontSize: 20, fontWeight: FontWeight.bold),
+                          ),
+                        ),
+                        Padding(
+                          padding: const EdgeInsets.all(16.0),
+                          child: Text(
+                            "Total: \$${formatCurrency(totalDelDia)}",
+                            style: TextStyle(
+                                fontSize: 20, fontWeight: FontWeight.bold),
+                          ),
+                        ),
+                      ],
                     ),
                     Row(
                       children: [
@@ -263,62 +332,110 @@ class _VentaScreenState extends State<VentaScreen> {
     );
   }
 
-  void cargarPedidos(DateTime fecha) async {
-    // Convertir el inicio y fin del día local a UTC
-    final inicioDelDia = DateTime(fecha.year, fecha.month, fecha.day)
-        .toUtc(); // Convierte la fecha local de inicio del día a UTC
+  void cargarPedidos(DateTime? fechaInicial, DateTime? fechaFinal) async {
+    if (fechaInicial != null && fechaFinal != null) {
+      // Convertir el inicio y fin del día local a UTC
+      final inicioDelDia =
+          DateTime(fechaInicial.year, fechaInicial.month, fechaInicial.day)
+              .toUtc(); // Convierte la fecha local de inicio del día a UTC
 
-    final finDelDia = DateTime(fecha.year, fecha.month, fecha.day, 23, 59, 59)
-        .toUtc(); // Convierte la fecha local de fin del día a UTC
+      final finDelDia = DateTime(
+              fechaFinal.year, fechaFinal.month, fechaFinal.day, 23, 59, 59)
+          .toUtc(); // Convierte la fecha local de fin del día a UTC
 
-    print('Buscando pedidos desde: $inicioDelDia hasta: $finDelDia (en UTC)');
+      print('Buscando pedidos desde: $inicioDelDia hasta: $finDelDia (en UTC)');
 
-    // Realizar la búsqueda en UTC
-    final pedidos = await Provider.of<PedidoViewModel>(context, listen: false)
-        .buscarPorFecha(inicioDelDia, finDelDia);
+      // Realizar la búsqueda en UTC
+      final pedidos = await Provider.of<PedidoViewModel>(context, listen: false)
+          .buscarPorFecha(inicioDelDia, finDelDia);
 
-    print('Pedidos obtenidos: ${pedidos.length}');
-    pedidos.forEach((pedido) => print(
-        'Pedido: ${pedido.folio}, Total: ${pedido.totalPedido}, Estatus: ${pedido.estatus}'));
+      print('Pedidos obtenidos: ${pedidos.length}');
+      pedidos.forEach((pedido) => print(
+          'Pedido: ${pedido.folio}, Total: ${pedido.totalPedido}, Estatus: ${pedido.estatus}, Propinas: ${pedido.propinas.length}'));
 
-    final pedidosNoCancelados =
-        pedidos.where((p) => p.estatus != "CANCELADO").toList();
-    final pedidosCancelados =
-        pedidos.where((p) => p.estatus == "CANCELADO").toList();
+      final pedidosNoCancelados =
+          pedidos.where((p) => p.estatus != "CANCELADO").toList();
+      final pedidosCancelados =
+          pedidos.where((p) => p.estatus == "CANCELADO").toList();
 
-    totalDelDia = 0.0;
-    totalCancelados = 0.0;
-    totalEfectivoDelDia = 0.0;
-    totalTarjetaDelDia = 0.0;
-    totalTransferenciaDelDia = 0.0;
-    cambio = 0.0;
-    totalSinCambio = 0.0;
+      totalDelDia = 0.0;
+      totalPropinas = 0.0;
+      totalCancelados = 0.0;
+      totalEfectivoDelDia = 0.0;
+      totalTarjetaDelDia = 0.0;
+      totalTransferenciaDelDia = 0.0;
+      cambio = 0.0;
+      totalSinCambio = 0.0;
 
-    for (var pedido in pedidosNoCancelados) {
-      totalDelDia += pedido.totalPedido ?? 0.0;
-      totalEfectivoDelDia += pedido.cantEfectivo ?? 0.0;
-      totalTarjetaDelDia += pedido.cantTarjeta ?? 0.0;
-      totalTransferenciaDelDia += pedido.cantTransferencia ?? 0.0;
-      totalSinCambio =
-          totalEfectivoDelDia + totalTarjetaDelDia + totalTransferenciaDelDia;
+      for (var pedido in pedidosNoCancelados) {
+        totalDelDia += pedido.totalPedido ?? 0.0;
+        totalEfectivoDelDia += pedido.cantEfectivo ?? 0.0;
+        totalTarjetaDelDia += pedido.cantTarjeta ?? 0.0;
+        totalTransferenciaDelDia += pedido.cantTransferencia ?? 0.0;
+        totalSinCambio =
+            totalEfectivoDelDia + totalTarjetaDelDia + totalTransferenciaDelDia;
 
-      cambio = totalSinCambio - totalDelDia;
-    }
+        cambio = totalSinCambio - totalDelDia;
 
-    totalCancelados = pedidosCancelados.fold(
-        0.0, (sum, current) => sum + (current.totalPedido ?? 0.0));
+        print("Propinas: ${pedido.propinas.length}");
+        // if (pedido.propinas.length > 0) {
+        //   for (var propina in pedido.propinas) {
+        //     totalPropinas += propina.cantidad ?? 0.0;
+        //   }
+        // }
 
-    setState(() {
-      this.pedidosNoCancelados = pedidosNoCancelados;
-      this.pedidosCancelados = pedidosCancelados;
-    });
+        final propinas =
+            await Provider.of<PropinaViewModel>(context, listen: false)
+                .obtenerPropinasPorPedido(pedido.id);
+
+        listaPropinas = propinas;
+        for (var propina in propinas) {
+          totalPropinas += propina.cantidad ?? 0.0;
+        }
+        print("Total propinas: ${totalPropinas}");
+      }
+
+      totalCancelados = pedidosCancelados.fold(
+          0.0, (sum, current) => sum + (current.totalPedido ?? 0.0));
+
+      setState(() {
+        this.pedidosNoCancelados = pedidosNoCancelados;
+        this.pedidosCancelados = pedidosCancelados;
+      });
+    }
   }
 
   Future<void> imprimirResumenPedidos(List<Pedido> pedidos) async {
-    if (fechaSeleccionada == null) {
-      print("No se ha seleccionado una fecha.");
+    if (fechaInicialSeleccionada == null) {
+      print("No se ha seleccionado una fecha inicial.");
+      return;
+    }
+    if (fechaFinalSeleccionada == null) {
+      print("No se ha seleccionado una fecha final.");
       return;
     }
-    await VentaTicket.imprimirResumenPedidos(pedidos, fechaSeleccionada!);
+    await VentaTicket.imprimirResumenPedidos(
+        pedidos, fechaInicialSeleccionada!);
+  }
+
+  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());
+  }
+
+  double? _propinaPedido(int? idPedido) {
+    double? propina = 0.0;
+
+    for (var p in listaPropinas) {
+      if (p.idPedido == idPedido) {
+        propina = p.cantidad;
+      }
+    }
+
+    return propina;
   }
 }