Fabio Dalle Ave il y a 2 mois
Parent
commit
7bc7473b0a

+ 6 - 19
lib/services/sync/ordine_sync_service.dart

@@ -88,9 +88,6 @@ class OrdineSyncService {
         '/rest/ordini/updates/$codAgente/$lastUpdate',
       );
       final db = _ordiniDao.db;
-      final cacheOrdineById = <int, Ordine?>{};
-      final cacheOrdineByIdSede = <int, Ordine?>{};
-      final cacheOrdineByNumSede = <String, Ordine?>{};
       final cacheCliente = <String, int?>{};
       final cacheFilialeById = <int, int?>{};
       final cacheFilialeByRemoteId = <int, int?>{};
@@ -105,26 +102,14 @@ class OrdineSyncService {
 
           Ordine? existing;
           if (idOrdineAgente != null) {
-            existing = cacheOrdineById[idOrdineAgente];
-            if (existing == null) {
-              existing = await _ordiniDao.getById(idOrdineAgente);
-              cacheOrdineById[idOrdineAgente] = existing;
-            }
+            existing ??= await _ordiniDao.getById(idOrdineAgente);
           }
           if (existing == null && idOrdine != null) {
-            existing = cacheOrdineByIdSede[idOrdine];
-            if (existing == null) {
-              existing = await _ordiniDao.findByIdOrdineSede(idOrdine);
-              cacheOrdineByIdSede[idOrdine] = existing;
-            }
+            existing ??= await _ordiniDao.findByIdOrdineSede(idOrdine);
           }
           if (existing == null && numeroOrdineSede != null) {
             final numKey = numeroOrdineSede.toString();
-            existing = cacheOrdineByNumSede[numKey];
-            if (existing == null) {
-              existing = await _ordiniDao.findByNumeroOrdineSede(numKey);
-              cacheOrdineByNumSede[numKey] = existing;
-            }
+            existing ??= await _ordiniDao.findByNumeroOrdineSede(numKey);
           }
 
           if (stato == 'X') {
@@ -169,7 +154,9 @@ class OrdineSyncService {
           }
 
           final ordineCompanion = OrdiniCompanion(
-            idOrdine: existing != null ? Value(existing.idOrdine) : Value.absent(),
+            idOrdine: existing != null
+                ? Value(existing.idOrdine)
+                : Value.absent(),
             idOrdineSede: Value(idOrdine),
             numeroOrdine: Value(json['numeroOrdine'] as int?),
             numeroOrdineSede: Value(numeroOrdineSede?.toString()),

+ 127 - 41
lib/ui/pages/dashboard/dashboard_page.dart

@@ -14,7 +14,9 @@ class DashboardPage extends ConsumerWidget {
   Widget build(BuildContext context, WidgetRef ref) {
     final authState = ref.watch(authProvider);
     final agente = authState.agente;
-    final dashboardAsync = agente != null ? ref.watch(dashboardProvider(agente.idAgente)) : null;
+    final dashboardAsync = agente != null
+        ? ref.watch(dashboardProvider(agente.idAgente))
+        : null;
     final theme = Theme.of(context);
 
     return Scaffold(
@@ -31,11 +33,14 @@ class DashboardPage extends ConsumerWidget {
         onSync: () => context.go('/dashboard/sync'),
         onLogout: () => ref.read(authProvider.notifier).logout(),
       ),
-      body: dashboardAsync?.when(
-        data: (data) => _DashboardContent(data: data, theme: theme, context: context),
-        loading: () => const Center(child: CircularProgressIndicator()),
-        error: (e, _) => Center(child: Text('Errore: $e')),
-      ) ?? const Center(child: CircularProgressIndicator()),
+      body:
+          dashboardAsync?.when(
+            data: (data) =>
+                _DashboardContent(data: data, theme: theme, context: context),
+            loading: () => const Center(child: CircularProgressIndicator()),
+            error: (e, _) => Center(child: Text('Errore: $e')),
+          ) ??
+          const Center(child: CircularProgressIndicator()),
     );
   }
 }
@@ -45,7 +50,11 @@ class _DashboardContent extends StatelessWidget {
   final ThemeData theme;
   final BuildContext context;
 
-  const _DashboardContent({required this.data, required this.theme, required this.context});
+  const _DashboardContent({
+    required this.data,
+    required this.theme,
+    required this.context,
+  });
 
   @override
   Widget build(BuildContext context) {
@@ -92,24 +101,58 @@ class _DashboardContent extends StatelessWidget {
             spacing: 12,
             runSpacing: 12,
             children: [
-              _StatCard(label: 'Nuovi Ordini', value: data.newOrdersCount.toString(), icon: Icons.receipt_long, color: theme.colorScheme.primary),
-              _StatCard(label: 'Ordini in Sospeso', value: data.ordersOnHoldCount.toString(), icon: Icons.pause_circle, color: theme.colorScheme.tertiary),
-              _StatCard(label: 'Nuovi Clienti', value: data.newCustomersCount.toString(), icon: Icons.person_add, color: theme.colorScheme.secondary),
+              _StatCard(
+                label: 'Ordini',
+                value: data.newOrdersCount.toString(),
+                icon: Icons.receipt_long,
+                color: theme.colorScheme.primary,
+                onTap: () => context.go('/dashboard/orders'),
+              ),
+              _StatCard(
+                label: 'Ordini in Sospeso',
+                value: data.ordersOnHoldCount.toString(),
+                icon: Icons.pause_circle,
+                color: theme.colorScheme.tertiary,
+              ),
+              _StatCard(
+                label: 'Nuovi Clienti',
+                value: data.newCustomersCount.toString(),
+                icon: Icons.person_add,
+                color: theme.colorScheme.secondary,
+              ),
             ],
           ),
           const SizedBox(height: 24),
 
           // Top items
           if (data.topItems.isNotEmpty) ...[
-            Text('Articoli Più Venduti', style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold)),
+            Text(
+              'Articoli Più Venduti',
+              style: theme.textTheme.titleMedium?.copyWith(
+                fontWeight: FontWeight.bold,
+              ),
+            ),
             const SizedBox(height: 8),
             Card(
               child: Column(
-                children: data.topItems.map((item) => ListTile(
-                  leading: CircleAvatar(child: Text(item.codArticolo?.substring(0, 2) ?? '??')),
-                  title: Text(item.descrizione ?? ''),
-                  trailing: Text(NumberFormat.currency(locale: 'it', symbol: '€').format(item.totale ?? 0)),
-                )).toList(),
+                children: data.topItems
+                    .map(
+                      (item) => ListTile(
+                        leading: CircleAvatar(
+                          child: Text(
+                            item.codArticolo?.substring(0, 2) ?? '??',
+                          ),
+                        ),
+                        title: Text(item.descrizione ?? ''),
+                        trailing: Text(
+                          NumberFormat.currency(
+                            locale: 'it',
+                            symbol: '€',
+                          ).format(item.totale ?? 0),
+                        ),
+                      ),
+                    )
+                    .toList(),
               ),
             ),
           ],
@@ -117,15 +160,31 @@ class _DashboardContent extends StatelessWidget {
           // Top customers
           if (data.topCustomers.isNotEmpty) ...[
             const SizedBox(height: 16),
-            Text('Top Clienti', style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold)),
+            Text(
+              'Top Clienti',
+              style: theme.textTheme.titleMedium?.copyWith(
+                fontWeight: FontWeight.bold,
+              ),
+            ),
             const SizedBox(height: 8),
             Card(
               child: Column(
-                children: data.topCustomers.map((c) => ListTile(
-                  leading: CircleAvatar(child: Text(c.codCliente?.substring(0, 2) ?? '??')),
-                  title: Text(c.ragioneSociale ?? ''),
-                  trailing: Text(NumberFormat.currency(locale: 'it', symbol: '€').format(c.totale ?? 0)),
-                )).toList(),
+                children: data.topCustomers
+                    .map(
+                      (c) => ListTile(
+                        leading: CircleAvatar(
+                          child: Text(c.codCliente?.substring(0, 2) ?? '??'),
+                        ),
+                        title: Text(c.ragioneSociale ?? ''),
+                        trailing: Text(
+                          NumberFormat.currency(
+                            locale: 'it',
+                            symbol: '€',
+                          ).format(c.totale ?? 0),
+                        ),
+                      ),
+                    )
+                    .toList(),
               ),
             ),
           ],
@@ -141,12 +200,19 @@ class _ActionCard extends StatelessWidget {
   final Color color;
   final VoidCallback onTap;
 
-  const _ActionCard({required this.icon, required this.label, required this.color, required this.onTap});
+  const _ActionCard({
+    required this.icon,
+    required this.label,
+    required this.color,
+    required this.onTap,
+  });
 
   @override
   Widget build(BuildContext context) {
     return SizedBox(
-      width: ResponsiveBreakpoints.isPhone(context) ? (MediaQuery.of(context).size.width - 48) / 2 : 160,
+      width: ResponsiveBreakpoints.isPhone(context)
+          ? (MediaQuery.of(context).size.width - 48) / 2
+          : 160,
       child: Card(
         child: InkWell(
           borderRadius: BorderRadius.circular(12),
@@ -158,7 +224,11 @@ class _ActionCard extends StatelessWidget {
               children: [
                 Icon(icon, size: 32, color: color),
                 const SizedBox(height: 8),
-                Text(label, textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelLarge),
+                Text(
+                  label,
+                  textAlign: TextAlign.center,
+                  style: Theme.of(context).textTheme.labelLarge,
+                ),
               ],
             ),
           ),
@@ -173,27 +243,43 @@ class _StatCard extends StatelessWidget {
   final String value;
   final IconData icon;
   final Color color;
+  final VoidCallback? onTap;
 
-  const _StatCard({required this.label, required this.value, required this.icon, required this.color});
+  const _StatCard({
+    required this.label,
+    required this.value,
+    required this.icon,
+    required this.color,
+    this.onTap,
+  });
 
   @override
   Widget build(BuildContext context) {
     return Card(
-      child: Padding(
-        padding: const EdgeInsets.all(16),
-        child: Row(
-          mainAxisSize: MainAxisSize.min,
-          children: [
-            Icon(icon, color: color),
-            const SizedBox(width: 12),
-            Column(
-              crossAxisAlignment: CrossAxisAlignment.start,
-              children: [
-                Text(value, style: Theme.of(context).textTheme.headlineSmall?.copyWith(fontWeight: FontWeight.bold)),
-                Text(label, style: Theme.of(context).textTheme.bodySmall),
-              ],
-            ),
-          ],
+      child: InkWell(
+        borderRadius: BorderRadius.circular(12),
+        onTap: onTap,
+        child: Padding(
+          padding: const EdgeInsets.all(16),
+          child: Row(
+            mainAxisSize: MainAxisSize.min,
+            children: [
+              Icon(icon, color: color),
+              const SizedBox(width: 12),
+              Column(
+                crossAxisAlignment: CrossAxisAlignment.start,
+                children: [
+                  Text(
+                    value,
+                    style: Theme.of(context).textTheme.headlineSmall?.copyWith(
+                      fontWeight: FontWeight.bold,
+                    ),
+                  ),
+                  Text(label, style: Theme.of(context).textTheme.bodySmall),
+                ],
+              ),
+            ],
+          ),
         ),
       ),
     );

+ 6 - 3
lib/ui/pages/orders/order_list_page.dart

@@ -1,3 +1,4 @@
+import 'dart:math' as math;
 import 'package:flutter/material.dart';
 import 'package:flutter_riverpod/flutter_riverpod.dart';
 import 'package:go_router/go_router.dart';
@@ -133,7 +134,8 @@ class _OrderListPageState extends ConsumerState<OrderListPage> {
                     itemBuilder: (_, i) => _OrderCard(order: orders[i]),
                   );
                 }
-                return _OrderTable(orders: orders);
+                final screenWidth = MediaQuery.of(context).size.width;
+                return _OrderTable(orders: orders, availableWidth: screenWidth);
               },
               loading: () => const Center(child: CircularProgressIndicator()),
               error: (e, _) => Center(child: Text('Errore: $e')),
@@ -245,7 +247,8 @@ class _OrderCard extends StatelessWidget {
 
 class _OrderTable extends StatelessWidget {
   final List<VOrdiniData> orders;
-  const _OrderTable({required this.orders});
+  final double availableWidth;
+  const _OrderTable({required this.orders, required this.availableWidth});
 
   @override
   Widget build(BuildContext context) {
@@ -257,7 +260,7 @@ class _OrderTable extends StatelessWidget {
     return SingleChildScrollView(
       scrollDirection: Axis.horizontal,
       child: SizedBox(
-        width: 900,
+        width: math.max(900, availableWidth),
         child: SingleChildScrollView(
           scrollDirection: Axis.vertical,
           child: Column(

+ 1 - 0
lib/ui/pages/sync/sync_settings_page.dart

@@ -93,6 +93,7 @@ class _SyncSettingsPageState extends ConsumerState<SyncSettingsPage> {
         await db.delete(db.clientiAgente).go();
         await db.delete(db.scadenze).go();
         await db.delete(db.statisticheVendita).go();
+        await (db.delete(db.parametri)..where((t) => t.nome.equals('lastUpdate'))).go();
       });
 
       if (!mounted) return;