From 282a959f88233e35952569dc6e68b328bc6c247d Mon Sep 17 00:00:00 2001 From: Mark M2 Macbook Date: Sun, 6 Sep 2026 10:03:27 +0200 Subject: [PATCH] a --- lib/core/enums_and_consts/consts.dart | 2 + .../targets/data/targets_repository.dart | 271 ++++++++++++++++++ .../models/individual_target_model.dart | 108 +++++++ .../targets/models/target_metrics.dart | 219 ++++++++++++++ lib/features/targets/models/target_model.dart | 136 +++++++++ lib/service_locator.dart | 2 + 6 files changed, 738 insertions(+) create mode 100644 lib/features/targets/data/targets_repository.dart create mode 100644 lib/features/targets/models/individual_target_model.dart create mode 100644 lib/features/targets/models/target_metrics.dart create mode 100644 lib/features/targets/models/target_model.dart diff --git a/lib/core/enums_and_consts/consts.dart b/lib/core/enums_and_consts/consts.dart index e0a3ee3..93658c1 100644 --- a/lib/core/enums_and_consts/consts.dart +++ b/lib/core/enums_and_consts/consts.dart @@ -18,6 +18,8 @@ class Tables { static const String staffInStores = 'staff_in_stores'; static const String staffMembers = 'staff_members'; static const String stores = 'stores'; + static const String targets = 'targets'; + static const String individualTargets = 'individual_targets'; static const String tasks = 'tasks'; static const String taskAssignments = 'task_assignments'; static const String taskReminders = 'task_reminders'; diff --git a/lib/features/targets/data/targets_repository.dart b/lib/features/targets/data/targets_repository.dart new file mode 100644 index 0000000..b4572f0 --- /dev/null +++ b/lib/features/targets/data/targets_repository.dart @@ -0,0 +1,271 @@ +import 'dart:async'; +import 'package:flutter/foundation.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; +import 'package:flux/features/targets/models/individual_target_model.dart'; +import 'package:flux/features/targets/models/target_model.dart'; +import 'package:flux/service_locator.dart'; +import 'package:supabase_flutter/supabase_flutter.dart'; + +class TargetsRepository { + final SupabaseClient _supabase = sl(); + + final StreamController _targetsChangedController = + StreamController.broadcast(); + + Stream get onTargetsChanged => _targetsChangedController.stream; + + void notifyTargetsChanged() { + if (!_targetsChangedController.isClosed) { + _targetsChangedController.add(null); + } + } + + // ========================================== + // STORE TARGETS (Tabella 'targets') + // ========================================== + + Future getStoreTargetById(String id) async { + try { + final response = await _supabase + .from(Tables.targets) + .select(''' + *, + ${Tables.stores}(*), + ${Tables.individualTargets}( + *, + ${Tables.staffMembers}(*) + ) + ''') + .eq('id', id) + .single(); + return TargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore getStoreTargetById: $e'); + return null; + } + } + + Future> getStoreTargets({ + required String companyId, + String? storeId, + DateTime? fromDate, + DateTime? toDate, + }) async { + try { + var query = _supabase + .from(Tables.targets) + .select(''' + *, + ${Tables.stores}(*), + ${Tables.individualTargets}( + *, + ${Tables.staffMembers}(*) + ) + ''') + .eq('company_id', companyId); + + if (storeId != null && storeId.isNotEmpty) { + query = query.eq('store_id', storeId); + } + + if (fromDate != null) { + query = query.gte('end_day', fromDate.toIso8601String()); + } + + if (toDate != null) { + query = query.lte('start_day', toDate.toIso8601String()); + } + + final response = await query.order('start_day', ascending: false); + return (response as List) + .map((e) => TargetModel.fromMap(e as Map)) + .toList(); + } on Exception catch (e) { + debugPrint('Errore getStoreTargets: $e'); + throw Exception('Errore nel recupero degli obiettivi di negozio: $e'); + } + } + + Future getStoreTargetForMonth({ + required String companyId, + required String storeId, + required DateTime month, + }) async { + try { + final firstDay = DateTime(month.year, month.month, 1); + final lastDay = DateTime(month.year, month.month + 1, 0, 23, 59, 59); + + final response = await _supabase + .from(Tables.targets) + .select(''' + *, + ${Tables.stores}(*), + ${Tables.individualTargets}( + *, + ${Tables.staffMembers}(*) + ) + ''') + .eq('company_id', companyId) + .eq('store_id', storeId) + .gte('end_day', firstDay.toIso8601String()) + .lte('start_day', lastDay.toIso8601String()) + .order('start_day', ascending: false) + .limit(1) + .maybeSingle(); + + if (response == null) return null; + return TargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore getStoreTargetForMonth: $e'); + return null; + } + } + + Future saveStoreTarget(TargetModel target) async { + try { + final Map data = target.toMap(); + final response = await _supabase + .from(Tables.targets) + .upsert(data) + .select(''' + *, + ${Tables.stores}(*), + ${Tables.individualTargets}( + *, + ${Tables.staffMembers}(*) + ) + ''') + .single(); + + notifyTargetsChanged(); + return TargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore saveStoreTarget: $e'); + throw Exception('Errore nel salvataggio dell\'obiettivo di negozio: $e'); + } + } + + Future deleteStoreTarget(String id) async { + try { + await _supabase.from(Tables.targets).delete().eq('id', id); + notifyTargetsChanged(); + } on Exception catch (e) { + debugPrint('Errore deleteStoreTarget: $e'); + throw Exception('Errore nell\'eliminazione dell\'obiettivo di negozio: $e'); + } + } + + // ========================================== + // INDIVIDUAL TARGETS (Tabella 'individual_targets') + // ========================================== + + Future getIndividualTargetById(String id) async { + try { + final response = await _supabase + .from(Tables.individualTargets) + .select(''' + *, + ${Tables.staffMembers}(*) + ''') + .eq('id', id) + .single(); + return IndividualTargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore getIndividualTargetById: $e'); + return null; + } + } + + Future> getIndividualTargets({ + required String companyId, + String? targetId, + String? staffId, + }) async { + try { + var query = _supabase + .from(Tables.individualTargets) + .select(''' + *, + ${Tables.staffMembers}(*) + ''') + .eq('company_id', companyId); + + if (targetId != null && targetId.isNotEmpty) { + query = query.eq('target_id', targetId); + } + + if (staffId != null && staffId.isNotEmpty) { + query = query.eq('staff_id', staffId); + } + + final response = await query.order('created_at', ascending: false); + return (response as List) + .map((e) => IndividualTargetModel.fromMap(e as Map)) + .toList(); + } on Exception catch (e) { + debugPrint('Errore getIndividualTargets: $e'); + throw Exception('Errore nel recupero degli obiettivi individuali: $e'); + } + } + + Future getIndividualTargetForStaffAndTarget({ + required String companyId, + required String targetId, + required String staffId, + }) async { + try { + final response = await _supabase + .from(Tables.individualTargets) + .select(''' + *, + ${Tables.staffMembers}(*) + ''') + .eq('company_id', companyId) + .eq('target_id', targetId) + .eq('staff_id', staffId) + .maybeSingle(); + + if (response == null) return null; + return IndividualTargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore getIndividualTargetForStaffAndTarget: $e'); + return null; + } + } + + Future saveIndividualTarget( + IndividualTargetModel target, + ) async { + try { + final Map data = target.toMap(); + final response = await _supabase + .from(Tables.individualTargets) + .upsert(data) + .select(''' + *, + ${Tables.staffMembers}(*) + ''') + .single(); + + notifyTargetsChanged(); + return IndividualTargetModel.fromMap(response); + } on Exception catch (e) { + debugPrint('Errore saveIndividualTarget: $e'); + throw Exception('Errore nel salvataggio dell\'obiettivo individuale: $e'); + } + } + + Future deleteIndividualTarget(String id) async { + try { + await _supabase.from(Tables.individualTargets).delete().eq('id', id); + notifyTargetsChanged(); + } on Exception catch (e) { + debugPrint('Errore deleteIndividualTarget: $e'); + throw Exception('Errore nell\'eliminazione dell\'obiettivo individuale: $e'); + } + } + + void dispose() { + _targetsChangedController.close(); + } +} diff --git a/lib/features/targets/models/individual_target_model.dart b/lib/features/targets/models/individual_target_model.dart new file mode 100644 index 0000000..0071a6d --- /dev/null +++ b/lib/features/targets/models/individual_target_model.dart @@ -0,0 +1,108 @@ +import 'package:equatable/equatable.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; +import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; +import 'target_metrics.dart'; + +class IndividualTargetModel extends Equatable { + final String? id; + final DateTime? createdAt; + final String targetId; + final String companyId; + final String staffId; + final StaffMemberModel? staff; + final String? description; + final TargetMetrics metrics; + + const IndividualTargetModel({ + this.id, + this.createdAt, + required this.targetId, + required this.companyId, + required this.staffId, + this.staff, + this.description, + this.metrics = const TargetMetrics(), + }); + + @override + List get props => [ + id, + createdAt, + targetId, + companyId, + staffId, + staff, + description, + metrics, + ]; + + IndividualTargetModel copyWith({ + String? id, + DateTime? createdAt, + String? targetId, + String? companyId, + String? staffId, + StaffMemberModel? staff, + String? description, + TargetMetrics? metrics, + }) { + return IndividualTargetModel( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + targetId: targetId ?? this.targetId, + companyId: companyId ?? this.companyId, + staffId: staffId ?? this.staffId, + staff: staff ?? this.staff, + description: description ?? this.description, + metrics: metrics ?? this.metrics, + ); + } + + factory IndividualTargetModel.empty({ + String targetId = '', + String companyId = '', + String staffId = '', + }) { + return IndividualTargetModel( + targetId: targetId, + companyId: companyId, + staffId: staffId, + metrics: TargetMetrics.empty(), + ); + } + + factory IndividualTargetModel.fromMap(Map map) { + return IndividualTargetModel( + id: map['id'] as String?, + createdAt: + map['created_at'] != null + ? DateTime.parse(map['created_at'] as String) + : null, + targetId: map['target_id'] as String? ?? '', + companyId: map['company_id'] as String? ?? '', + staffId: map['staff_id'] as String? ?? '', + staff: + map[Tables.staffMembers] != null + ? StaffMemberModel.fromMap( + map[Tables.staffMembers] as Map, + ) + : null, + description: map['description'] as String?, + metrics: + map['metrics'] != null && map['metrics'] is Map + ? TargetMetrics.fromMap(map['metrics'] as Map) + : TargetMetrics.fromMap(map), + ); + } + + Map toMap() { + return { + if (id != null && id!.isNotEmpty) 'id': id, + 'target_id': targetId, + 'company_id': companyId, + 'staff_id': staffId, + 'description': description, + 'metrics': metrics.toMap(), + }; + } +} diff --git a/lib/features/targets/models/target_metrics.dart b/lib/features/targets/models/target_metrics.dart new file mode 100644 index 0000000..883dd36 --- /dev/null +++ b/lib/features/targets/models/target_metrics.dart @@ -0,0 +1,219 @@ +import 'package:equatable/equatable.dart'; + +class TargetMetrics extends Equatable { + final int mnp1; + final int mnp2; + final int unica1; + final int unica2; + final int nip1; + final int nip2; + final int business1; + final int business2; + final int contenuti1; + final int contenuti2; + final int fin1; + final int fin2; + final int upselling1; + final int upselling2; + final int cb1; + final int cb2; + final int al1; + final int al2; + final int telepass1; + final int telepass2; + final int energiaTim; + final int energiaAltri; + + const TargetMetrics({ + this.mnp1 = 0, + this.mnp2 = 0, + this.unica1 = 0, + this.unica2 = 0, + this.nip1 = 0, + this.nip2 = 0, + this.business1 = 0, + this.business2 = 0, + this.contenuti1 = 0, + this.contenuti2 = 0, + this.fin1 = 0, + this.fin2 = 0, + this.upselling1 = 0, + this.upselling2 = 0, + this.cb1 = 0, + this.cb2 = 0, + this.al1 = 0, + this.al2 = 0, + this.telepass1 = 0, + this.telepass2 = 0, + this.energiaTim = 0, + this.energiaAltri = 0, + }); + + @override + List get props => [ + mnp1, + mnp2, + unica1, + unica2, + nip1, + nip2, + business1, + business2, + contenuti1, + contenuti2, + fin1, + fin2, + upselling1, + upselling2, + cb1, + cb2, + al1, + al2, + telepass1, + telepass2, + energiaTim, + energiaAltri, + ]; + + TargetMetrics copyWith({ + int? mnp1, + int? mnp2, + int? unica1, + int? unica2, + int? nip1, + int? nip2, + int? business1, + int? business2, + int? contenuti1, + int? contenuti2, + int? fin1, + int? fin2, + int? upselling1, + int? upselling2, + int? cb1, + int? cb2, + int? al1, + int? al2, + int? telepass1, + int? telepass2, + int? energiaTim, + int? energiaAltri, + }) { + return TargetMetrics( + mnp1: mnp1 ?? this.mnp1, + mnp2: mnp2 ?? this.mnp2, + unica1: unica1 ?? this.unica1, + unica2: unica2 ?? this.unica2, + nip1: nip1 ?? this.nip1, + nip2: nip2 ?? this.nip2, + business1: business1 ?? this.business1, + business2: business2 ?? this.business2, + contenuti1: contenuti1 ?? this.contenuti1, + contenuti2: contenuti2 ?? this.contenuti2, + fin1: fin1 ?? this.fin1, + fin2: fin2 ?? this.fin2, + upselling1: upselling1 ?? this.upselling1, + upselling2: upselling2 ?? this.upselling2, + cb1: cb1 ?? this.cb1, + cb2: cb2 ?? this.cb2, + al1: al1 ?? this.al1, + al2: al2 ?? this.al2, + telepass1: telepass1 ?? this.telepass1, + telepass2: telepass2 ?? this.telepass2, + energiaTim: energiaTim ?? this.energiaTim, + energiaAltri: energiaAltri ?? this.energiaAltri, + ); + } + + factory TargetMetrics.empty() { + return const TargetMetrics(); + } + + factory TargetMetrics.fromMap(Map map) { + return TargetMetrics( + mnp1: (map['mnp_1'] as num?)?.toInt() ?? 0, + mnp2: (map['mnp_2'] as num?)?.toInt() ?? 0, + unica1: (map['unica_1'] as num?)?.toInt() ?? 0, + unica2: (map['unica_2'] as num?)?.toInt() ?? 0, + nip1: (map['nip_1'] as num?)?.toInt() ?? 0, + nip2: (map['nip_2'] as num?)?.toInt() ?? 0, + business1: (map['business_1'] as num?)?.toInt() ?? 0, + business2: (map['business_2'] as num?)?.toInt() ?? 0, + contenuti1: (map['contenuti_1'] as num?)?.toInt() ?? 0, + contenuti2: (map['contenuti_2'] as num?)?.toInt() ?? 0, + fin1: (map['fin_1'] as num?)?.toInt() ?? 0, + fin2: (map['fin_2'] as num?)?.toInt() ?? 0, + upselling1: (map['upselling_1'] as num?)?.toInt() ?? 0, + upselling2: (map['upselling_2'] as num?)?.toInt() ?? 0, + cb1: (map['cb_1'] as num?)?.toInt() ?? 0, + cb2: (map['cb_2'] as num?)?.toInt() ?? 0, + al1: (map['al_1'] as num?)?.toInt() ?? 0, + al2: (map['al_2'] as num?)?.toInt() ?? 0, + telepass1: (map['telepass_1'] as num?)?.toInt() ?? 0, + telepass2: (map['telepass_2'] as num?)?.toInt() ?? 0, + energiaTim: + (map['energia_tim'] as num?)?.toInt() ?? + (map['Energia_tim'] as num?)?.toInt() ?? + 0, + energiaAltri: + (map['energia_altri'] as num?)?.toInt() ?? + (map['Energia_altri'] as num?)?.toInt() ?? + 0, + ); + } + + Map toMap() { + return { + 'mnp_1': mnp1, + 'mnp_2': mnp2, + 'unica_1': unica1, + 'unica_2': unica2, + 'nip_1': nip1, + 'nip_2': nip2, + 'business_1': business1, + 'business_2': business2, + 'contenuti_1': contenuti1, + 'contenuti_2': contenuti2, + 'fin_1': fin1, + 'fin_2': fin2, + 'upselling_1': upselling1, + 'upselling_2': upselling2, + 'cb_1': cb1, + 'cb_2': cb2, + 'al_1': al1, + 'al_2': al2, + 'telepass_1': telepass1, + 'telepass_2': telepass2, + 'energia_tim': energiaTim, + 'energia_altri': energiaAltri, + }; + } + + /// Somma totale di tutti i target del Livello 1 (inclusa energia) + int get totalLevel1 => + mnp1 + + unica1 + + nip1 + + business1 + + contenuti1 + + fin1 + + upselling1 + + cb1 + + al1 + + telepass1 + + energiaTim + + energiaAltri; + + /// Somma totale di tutti i target del Livello 2 + int get totalLevel2 => + mnp2 + + unica2 + + nip2 + + business2 + + contenuti2 + + fin2 + + upselling2 + + cb2 + + al2 + + telepass2; +} diff --git a/lib/features/targets/models/target_model.dart b/lib/features/targets/models/target_model.dart new file mode 100644 index 0000000..7a35622 --- /dev/null +++ b/lib/features/targets/models/target_model.dart @@ -0,0 +1,136 @@ +import 'package:equatable/equatable.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; +import 'package:flux/features/master_data/store/models/store_model.dart'; +import 'individual_target_model.dart'; +import 'target_metrics.dart'; + +class TargetModel extends Equatable { + final String? id; + final DateTime? createdAt; + final DateTime startDay; + final DateTime endDay; + final String? description; + final String companyId; + final String? storeId; + final StoreModel? store; + final TargetMetrics metrics; + final List individualTargets; + + const TargetModel({ + this.id, + this.createdAt, + required this.startDay, + required this.endDay, + this.description, + required this.companyId, + this.storeId, + this.store, + this.metrics = const TargetMetrics(), + this.individualTargets = const [], + }); + + @override + List get props => [ + id, + createdAt, + startDay, + endDay, + description, + companyId, + storeId, + store, + metrics, + individualTargets, + ]; + + TargetModel copyWith({ + String? id, + DateTime? createdAt, + DateTime? startDay, + DateTime? endDay, + String? description, + String? companyId, + String? storeId, + StoreModel? store, + TargetMetrics? metrics, + List? individualTargets, + }) { + return TargetModel( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + startDay: startDay ?? this.startDay, + endDay: endDay ?? this.endDay, + description: description ?? this.description, + companyId: companyId ?? this.companyId, + storeId: storeId ?? this.storeId, + store: store ?? this.store, + metrics: metrics ?? this.metrics, + individualTargets: individualTargets ?? this.individualTargets, + ); + } + + factory TargetModel.empty({ + String companyId = '', + String? storeId, + DateTime? startDay, + DateTime? endDay, + }) { + final now = DateTime.now(); + final firstDayOfMonth = DateTime(now.year, now.month, 1); + final lastDayOfMonth = DateTime(now.year, now.month + 1, 0, 23, 59, 59); + + return TargetModel( + startDay: startDay ?? firstDayOfMonth, + endDay: endDay ?? lastDayOfMonth, + companyId: companyId, + storeId: storeId, + metrics: TargetMetrics.empty(), + individualTargets: const [], + ); + } + + factory TargetModel.fromMap(Map map) { + return TargetModel( + id: map['id'] as String?, + createdAt: + map['created_at'] != null + ? DateTime.parse(map['created_at'] as String) + : null, + startDay: DateTime.parse(map['start_day'] as String), + endDay: DateTime.parse(map['end_day'] as String), + description: map['description'] as String?, + companyId: map['company_id'] as String? ?? '', + storeId: map['store_id'] as String?, + store: + map[Tables.stores] != null + ? StoreModel.fromMap(map[Tables.stores] as Map) + : null, + metrics: + map['metrics'] != null && map['metrics'] is Map + ? TargetMetrics.fromMap(map['metrics'] as Map) + : TargetMetrics.fromMap(map), + individualTargets: + map[Tables.individualTargets] != null && + map[Tables.individualTargets] is List + ? (map[Tables.individualTargets] as List) + .map( + (e) => + IndividualTargetModel.fromMap(e as Map), + ) + .toList() + : const [], + ); + } + + Map toMap() { + return { + if (id != null && id!.isNotEmpty) 'id': id, + 'start_day': startDay.toIso8601String(), + 'end_day': endDay.toIso8601String(), + 'description': description, + 'company_id': companyId, + if (storeId != null && storeId!.isNotEmpty) 'store_id': storeId, + 'metrics': metrics.toMap(), + }; + } +} diff --git a/lib/service_locator.dart b/lib/service_locator.dart index c110694..07c90a4 100644 --- a/lib/service_locator.dart +++ b/lib/service_locator.dart @@ -27,6 +27,7 @@ import 'package:flux/features/tasks/data/task_repository.dart'; import 'package:flux/features/tickets/blocs/ticket_list_cubit.dart'; import 'package:flux/features/tickets/data/ticket_repository.dart'; import 'package:flux/features/tickets/data/tickets_shipping_repository.dart'; +import 'package:flux/features/targets/data/targets_repository.dart'; import 'package:flux/features/tracking/blocs/tracking_cubit.dart'; import 'package:flux/features/tracking/data/tracking_repository.dart'; import 'package:get_it/get_it.dart'; @@ -69,6 +70,7 @@ Future init() async { sl.registerLazySingleton( () => DocumentSequenceRepository(), ); + sl.registerLazySingleton(() => TargetsRepository()); //Cubit globali sl.registerLazySingleton(