瀏覽代碼

Minor fixes

c90Beretta 2 月之前
父節點
當前提交
deb1ebe682

二進制
assets/Turquessa.png


+ 0 - 11
lib/mvvm/viewmodels/home_view_model.dart

@@ -1,11 +0,0 @@
-import 'package:flutter/material.dart';
-
-class HomeViewModel extends ChangeNotifier {
-  int _selectedIndex = 0;
-  int get selectedIndex => _selectedIndex;
-
-  void setIndex(int index) {
-    _selectedIndex = index;
-    notifyListeners();
-  }
-}

+ 1 - 1
lib/mvvm/viewmodels/mesa_view_model.dart

@@ -169,7 +169,7 @@ class MesaViewModel extends ChangeNotifier {
         "claveSucursal": claveSucursal!,
         "limite": "-1"
       };
-
+      //? peticion de Mesa
       final response = ApiResponse(await BaseService()
           .get('/pos/mesa', queryParameters: parametros, withAuth: true));
 

+ 83 - 0
lib/mvvm/viewmodels/pedido_view_model.dart

@@ -0,0 +1,83 @@
+import 'package:flutter/material.dart';
+import 'package:turquessa_mesas_hoster/core/services/database_service.dart';
+
+import '../../core/models/pedido_model.dart';
+import '../../core/services/api_response.dart';
+import '../../core/services/base_service.dart';
+
+class PedidoViewModel extends ChangeNotifier {
+  List<Pedido> _pedidos = [];
+  bool _isLoading = false;
+
+  List<Pedido> get pedidos => _pedidos;
+  bool get isLoading => _isLoading;
+
+  Future<void> fetchLocalPedidos() async {
+    var db = await DatabaseService().db;
+    var query = await db!
+        .query('Pedido', where: 'eliminado IS NULL', orderBy: 'id asc');
+    _pedidos = query.map((element) => Pedido.fromJson(element)).toList();
+    notifyListeners();
+  }
+
+  // Future<void> setSelectedPedido(Pedido pedido) async {
+  //   var db = await DatabaseService().db;
+  //   await db!.update(
+  //     'Pedido',
+  //     {'seleccionado': 0},
+  //   );
+  //   pedido.seleccionado = 1;
+  //   await DatabaseService().guardar(pedido);
+
+  //   await fetchLocalPedidos();
+  // }
+
+  Future<bool> sincronizarPedidos() async {
+    try {
+      final response =
+          ApiResponse(await BaseService().get('/pos/mesa?expand=pedidos'));
+      if (response.isOk && response.resultados != null) {
+        List<Pedido> pedidosApi =
+            response.resultados!.map((json) => Pedido.fromJson(json)).toList();
+
+        // if (pedidosApi.isNotEmpty) {
+        //   await DatabaseService().sincronizarPedidos(pedidosApi);
+        //   await fetchLocalPedidos();
+        //   notifyListeners();
+        //   return true;
+        // }
+      }
+      return false;
+    } catch (e) {
+      print('Error al sincronizar pedidos: $e');
+      return false;
+    }
+  }
+
+  // // Future<void> sincronizarPedidosDesdeApi() async {
+  //   setIsLoading(true);
+  //   try {
+  //     final response = ApiResponse(await BaseService().get('/pos/pedido'));
+
+  //     if (response.isOk && response.resultados != null) {
+  //       List<Pedido> pedidosApi =
+  //           response.resultados!.map((json) => Pedido.fromApi(json)).toList();
+
+  //       if (pedidosApi.isNotEmpty) {
+  //         await DatabaseService().sincronizarPedidos(pedidosApi);
+  //         await fetchLocalPedidos();
+  //         notifyListeners();
+  //       }
+  //     }
+  //   } catch (e) {
+  //     print('Error al sincronizar pedidos: $e');
+  //   } finally {
+  //     setIsLoading(false);
+  //   }
+  // }
+
+  void setIsLoading(bool value) {
+    _isLoading = value;
+    notifyListeners();
+  }
+}

+ 0 - 1
lib/mvvm/viewmodels/viewmodels.dart

@@ -1,7 +1,6 @@
 export '../viewmodels/login_view_model.dart';
 export '../viewmodels/sucursal_view_model.dart';
 export '../viewmodels/permiso_view_model.dart';
-export '../viewmodels/home_view_model.dart';
 export '../viewmodels/usuarios_view_model.dart';
 export '../viewmodels/mesa_view_model.dart';
 export '../viewmodels/producto_view_model.dart';

+ 77 - 102
lib/mvvm/views/home/home_screen.dart

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
 import 'package:provider/provider.dart';
 import 'package:turquessa_mesas_hoster/core/models/mesa_model.dart';
 import 'package:turquessa_mesas_hoster/utils/widgets/custom_appbar.dart';
+import 'package:turquessa_mesas_hoster/utils/widgets/navigation_rail.dart';
 
 import '../../../utils/widgets/ordenes_card.dart';
 import '../../viewmodels/viewmodels.dart';
@@ -40,36 +41,7 @@ class Formulario extends State<HomeScreen> {
       ),
       body: Row(
         children: [
-          NavigationRail(
-            backgroundColor: Color.fromARGB(255, 25, 30, 41),
-            selectedIndex: _selectedIndex,
-            onDestinationSelected: (int index) {
-              setState(() {
-                _selectedIndex = index;
-              });
-            },
-            labelType: NavigationRailLabelType.all,
-            destinations: const [
-              NavigationRailDestination(
-                icon: Icon(Icons.home, color: Colors.white),
-                selectedIcon: Icon(Icons.home_filled),
-                label: Text('Inicio'),
-              ),
-              NavigationRailDestination(
-                icon: Icon(Icons.search),
-                selectedIcon: Icon(
-                  Icons.search_rounded,
-                  color: Colors.white,
-                ),
-                label: Text('Buscar'),
-              ),
-              NavigationRailDestination(
-                icon: Icon(Icons.settings),
-                selectedIcon: Icon(Icons.settings_rounded, color: Colors.white),
-                label: Text('Ajustes'),
-              ),
-            ],
-          ),
+          CustomNavigationRail(selectedIndex: _selectedIndex),
           Expanded(
             child: Center(
               child: GridView.builder(
@@ -90,10 +62,7 @@ class Formulario extends State<HomeScreen> {
                       });
                     },
                     child: TableCard(
-                      icon: Icons.table_chart,
-                      //TODO: Agregar campo de estatus de la mesa para definir los colores
-                      color: (mesa.activa == true) ? Colors.blue : Colors.grey,
-                      title: mesa.nombre ?? 'Mesa sin nombre',
+                      mesa: mesa,
                     ),
                   );
                 },
@@ -130,38 +99,6 @@ class Formulario extends State<HomeScreen> {
   }
 }
 
-class TableCard extends StatelessWidget {
-  final IconData icon;
-  final Color color;
-  final String title;
-  const TableCard(
-      {super.key,
-      required this.icon,
-      required this.color,
-      required this.title});
-
-  @override
-  Widget build(BuildContext context) {
-    return Card(
-      color: color,
-      child: Column(
-        mainAxisAlignment: MainAxisAlignment.center,
-        children: [
-          Icon(
-            icon,
-            size: 50,
-            color: Colors.white,
-          ),
-          Text(
-            title,
-            style: const TextStyle(color: Colors.white, fontSize: 20),
-          )
-        ],
-      ),
-    );
-  }
-}
-
 class TablaDetalles extends StatelessWidget {
   final Mesa table;
   final EstadoPedido status;
@@ -259,9 +196,9 @@ class TablaDetalles extends StatelessWidget {
                       ],
                     ),
                     const SizedBox(height: 16),
-                    IconDataByStatus(status: status),
+                    // IconDataByStatus(status: status),
                     const SizedBox(height: 16),
-                    const OrdenesScreen(),
+                    OrdenesScreen(),
                   ],
                 ),
               ),
@@ -273,49 +210,87 @@ class TablaDetalles extends StatelessWidget {
   }
 }
 
-class IconDataByStatus extends StatelessWidget {
-  final EstadoPedido status;
-  const IconDataByStatus({Key? key, required this.status});
+class TableCard extends StatelessWidget {
+  final Mesa mesa;
+  const TableCard({
+    super.key,
+    required this.mesa,
+  });
 
   @override
   Widget build(BuildContext context) {
+    final status = mesa.estado ?? EstadoPedido.disponible;
+
+    Color backgroundColor;
+    Color iconColor;
+    IconData icon;
+    Color cardColor;
+
     switch (status) {
       case EstadoPedido.disponible:
-        return IconButton(
-          onPressed: () {
-            final mesaViewModel =
-                Provider.of<MesaViewModel>(context, listen: false);
-            mesaViewModel.CambiarEstadoPedidoMesa(EstadoPedido.preparacion);
-          },
-          style: ButtonStyle(
-            backgroundColor: MaterialStateProperty.all(
-                const Color.fromARGB(255, 220, 252, 232)),
-          ),
-          icon: const Icon(Icons.table_restaurant_rounded, color: Colors.green),
-        );
+        backgroundColor = const Color.fromARGB(255, 220, 252, 232);
+        iconColor = Colors.green;
+        icon = Icons.table_restaurant_rounded;
+        cardColor = const Color.fromARGB(255, 220, 252, 232);
+        break;
       case EstadoPedido.surtida:
-        return IconButton(
-          onPressed: () {},
-          icon: const Icon(Icons.coffee_rounded,
-              color: Color.fromARGB(255, 220, 234, 254)),
-        );
+        backgroundColor = const Color.fromARGB(255, 220, 234, 254);
+        iconColor = Colors.blue;
+        icon = Icons.coffee_rounded;
+        cardColor = const Color.fromARGB(255, 220, 234, 254);
+        break;
       case EstadoPedido.preparacion:
-        return IconButton(
-          onPressed: () {},
-          icon: const Icon(Icons.kitchen_rounded,
-              color: Color.fromARGB(255, 243, 232, 255)),
-        );
+        backgroundColor = const Color.fromARGB(255, 243, 232, 255);
+        iconColor = Colors.deepPurple;
+        icon = Icons.kitchen_rounded;
+        cardColor = const Color.fromARGB(255, 243, 232, 255);
+        break;
       case EstadoPedido.cobrado:
-        return IconButton(
-          onPressed: () {},
-          icon: const Icon(Icons.money_rounded,
-              color: Color.fromARGB(255, 255, 238, 213)),
-        );
+        backgroundColor = const Color.fromARGB(255, 255, 238, 213);
+        iconColor = Colors.amber;
+        icon = Icons.attach_money_rounded;
+        cardColor = const Color.fromARGB(255, 255, 238, 213);
+        break;
       default:
-        return IconButton(
-          onPressed: () {},
-          icon: const Icon(Icons.check_circle, color: Colors.grey),
-        );
+        backgroundColor = Colors.grey.shade200;
+        iconColor = Colors.grey;
+        icon = Icons.settings;
+        cardColor = Colors.white;
+        break;
     }
+
+    return Card(
+      color: cardColor,
+      child: Column(
+        mainAxisAlignment: MainAxisAlignment.center,
+        children: [
+          IconButton(
+            onPressed: () {
+              if (status == EstadoPedido.disponible) {
+                final mesaViewModel =
+                    Provider.of<MesaViewModel>(context, listen: false);
+                mesaViewModel.CambiarEstadoPedidoMesa(EstadoPedido.preparacion);
+              }
+            },
+            iconSize: 48,
+            style: ButtonStyle(
+              backgroundColor: MaterialStateProperty.all(backgroundColor),
+            ),
+            icon: Icon(icon, color: iconColor),
+          ),
+          const SizedBox(height: 8),
+          Text(
+            mesa.nombre ?? 'Mesa sin nombre',
+            style: TextStyle(
+              color: status == EstadoPedido.disponible
+                  ? Colors.black
+                  : Colors.black87,
+              fontSize: 20,
+              fontWeight: FontWeight.w500,
+            ),
+          )
+        ],
+      ),
+    );
   }
 }

+ 62 - 19
lib/utils/widgets/custom_appbar.dart

@@ -1,4 +1,8 @@
 import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+import 'package:turquessa_mesas_hoster/core/models/sucursal_model.dart';
+
+import '../../mvvm/viewmodels/sucursal_view_model.dart';
 
 class CustomAppbar extends StatefulWidget {
   const CustomAppbar({super.key});
@@ -9,33 +13,72 @@ class CustomAppbar extends StatefulWidget {
 
 class _CustomAppbarState extends State<CustomAppbar> {
   @override
+  void initState() {
+    super.initState();
+    final _sucursalViewModel =
+        Provider.of<SucursalViewModel>(context, listen: false);
+    _sucursalViewModel.fetchLocalSucursales();
+
+    WidgetsBinding.instance.addPostFrameCallback((_) async {
+      Provider.of<SucursalViewModel>(context, listen: false)
+          .sincronizarSucursalesDesdeApi();
+
+      await _sucursalViewModel.sincronizarSucursales();
+      await _sucursalViewModel.fetchLocalSucursales();
+    });
+  }
+
+  String? _selectedValue;
+
+  @override
   Widget build(BuildContext context) {
+    final sucursalViewModel = Provider.of<SucursalViewModel>(context);
+    final sucursales = sucursalViewModel.sucursales;
     return Row(
       children: [
         Image.asset(
-          'assets/logo.png',
-          height: 40,
+          'assets/Turquessa.png',
+          height: 100,
         ),
         const SizedBox(width: 10),
-        const SizedBox(width: 10),
-        DropdownButton(
-            hint: const Text(
-              "Mesas Interiores",
-              style:
-                  TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
-            ),
-            icon: const Icon(Icons.arrow_drop_down),
-            items: const [
-              DropdownMenuItem(
-                value: "ES",
-                child: Text("Palomeras"),
+        SizedBox(
+          width: 240,
+          child: Expanded(
+            child: DropdownButtonFormField<String>(
+              value: _selectedValue,
+              hint: Text(
+                _selectedValue?.isEmpty ?? true
+                    ? 'Selecciona una sucursal'
+                    : '',
+                style:
+                    TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
               ),
-              DropdownMenuItem(
-                value: "EN",
-                child: Text("Sombrillera"),
+              icon: const Icon(Icons.arrow_drop_down),
+              decoration: InputDecoration(
+                contentPadding:
+                    const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
+                border: OutlineInputBorder(
+                  borderRadius: BorderRadius.circular(15),
+                  borderSide: BorderSide.none,
+                ),
+                filled: true,
+                fillColor: Colors.white,
               ),
-            ],
-            onChanged: (e) => {})
+              items: sucursales
+                  .map((e) => DropdownMenuItem(
+                        value: e.nombre,
+                        child: Text(e.nombre ?? ""),
+                      ))
+                  .toList(),
+              onChanged: (value) {
+                setState(() {
+                  _selectedValue = value;
+                  // setSelectedSucursal(value as Sucursal);
+                });
+              },
+            ),
+          ),
+        )
       ],
     );
   }

+ 45 - 0
lib/utils/widgets/navigation_rail.dart

@@ -0,0 +1,45 @@
+import 'package:flutter/material.dart';
+
+class CustomNavigationRail extends StatefulWidget {
+  const CustomNavigationRail({super.key, required selectedIndex});
+
+  @override
+  State<CustomNavigationRail> createState() => Custom_NavigationRailState();
+}
+
+class Custom_NavigationRailState extends State<CustomNavigationRail> {
+  int selectedIndex = 0;
+  @override
+  Widget build(BuildContext context) {
+    return NavigationRail(
+      backgroundColor: Color.fromARGB(255, 25, 30, 41),
+      selectedIndex: selectedIndex,
+      onDestinationSelected: (int index) {
+        setState(() {
+          selectedIndex = index;
+        });
+      },
+      labelType: NavigationRailLabelType.all,
+      destinations: const [
+        NavigationRailDestination(
+          icon: Icon(Icons.home, color: Colors.white),
+          selectedIcon: Icon(Icons.home_filled),
+          label: Text('Inicio'),
+        ),
+        NavigationRailDestination(
+          icon: Icon(Icons.search),
+          selectedIcon: Icon(
+            Icons.search_rounded,
+            color: Colors.white,
+          ),
+          label: Text('Buscar'),
+        ),
+        NavigationRailDestination(
+          icon: Icon(Icons.settings),
+          selectedIcon: Icon(Icons.settings_rounded, color: Colors.white),
+          label: Text('Ajustes'),
+        ),
+      ],
+    );
+  }
+}

+ 1 - 0
pubspec.yaml

@@ -76,6 +76,7 @@ flutter:
 
   # To add assets to your application, add an assets section, like this:
   assets:
+    - assets/Turquessa.png
     - assets/logo.png
     - assets/igLogo.png
     - assets/logo-BN.png