mah....volare

This commit is contained in:
2026-06-03 19:16:15 +02:00
parent f27ede7625
commit 01515910b6
7 changed files with 560 additions and 243 deletions

View File

@@ -35,6 +35,82 @@ class OperationsRepository {
}
}
// 🥷 2. RECUPERO PAGINATO ASSOLUTO CON CONTEGGIO TOTALI
Future<PaginatedOperations> fetchPaginatedOperations({
required String companyId,
String? storeId,
String? staffId,
String? providerId,
required int page, // Usiamo 'page' (1, 2, 3...) invece di 'offset'
int itemsPerPage = 25, // Default a 25 elementi per pagina
String? searchTerm,
DateTimeRange? dateRange,
}) async {
try {
// Calcoliamo il range di partenza e fine per Supabase
// Es. Pagina 1, 25 items -> range(0, 24)
// Es. Pagina 2, 25 items -> range(25, 49)
final from = (page - 1) * itemsPerPage;
final to = from + itemsPerPage - 1;
var query = _supabase
.from(Tables.operations)
.select('''
*,
${Tables.customers}(*),
${Tables.stores}(name),
${Tables.providers}(name, color_hex),
${Tables.models}(name_with_brand),
${Tables.staffMembers}(name),
${Tables.attachments}(*)
''')
.eq('company_id', companyId);
// Filtro Range Date
if (dateRange != null) {
query = query
.gte('created_at', dateRange.start.toIso8601String())
.lte('created_at', dateRange.end.toIso8601String());
}
if (storeId != null) {
query = query.or('store_id.eq.$storeId,store_id.is.null');
}
if (staffId != null) {
query = query.or('staff_id.eq.$staffId,staff_id.is.null');
}
if (providerId != null) {
query = query.or('provider_id.eq.$providerId,provider_id.is.null');
}
if (searchTerm != null && searchTerm.isNotEmpty) {
query = query.or(
'reference.ilike.%$searchTerm%,note.ilike.%$searchTerm%,customer.name.ilike.%$searchTerm%',
);
}
final response = await query
.order('created_at', ascending: false)
.range(from, to)
.count(CountOption.exact);
// 3. Estrazione dei dati
final List<OperationModel> operations = (response.data as List)
.map((map) => OperationModel.fromMap(map))
.toList();
final int totalCount = response.count;
return PaginatedOperations(
operations: operations,
totalCount: totalCount,
);
} catch (e) {
throw Exception('Errore nel recupero della pagina $page: $e');
}
}
// --- RECUPERO PAGINATO CON FILTRI E JOIN ---
Future<List<OperationModel>> fetchOperations({
required String companyId,
@@ -325,3 +401,10 @@ class OperationsRepository {
}
}
}
class PaginatedOperations {
final List<OperationModel> operations;
final int totalCount;
PaginatedOperations({required this.operations, required this.totalCount});
}