From 66c7094ada534d33c02e9f665e3c7d114cf64c2f Mon Sep 17 00:00:00 2001 From: Mark M2 Macbook Date: Fri, 9 Oct 2026 15:36:06 +0200 Subject: [PATCH] =?UTF-8?q?possibilit=C3=A0=20di=20disattivare=20staff=20e?= =?UTF-8?q?=20altre=20migliorie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../widgets/shared_forms/staff_section.dart | 4 +- lib/core/widgets/staff_selector_modal.dart | 16 +- .../dashboard_daily_ricontatti_cubit.dart | 36 ++-- .../staff/models/staff_member_model.dart | 9 + .../master_data/staff/ui/staff_screen.dart | 174 ++++++++++++++++-- .../targets/blocs/target_form_cubit.dart | 11 +- .../targets/data/target_progress_service.dart | 11 +- .../targets/ui/target_detail_screen.dart | 4 +- lib/features/tasks/blocs/task_form_cubit.dart | 3 + .../dashboard_daily_ricontatti_test.dart | 42 +++++ 10 files changed, 269 insertions(+), 41 deletions(-) diff --git a/lib/core/widgets/shared_forms/staff_section.dart b/lib/core/widgets/shared_forms/staff_section.dart index ac52971..101fd14 100644 --- a/lib/core/widgets/shared_forms/staff_section.dart +++ b/lib/core/widgets/shared_forms/staff_section.dart @@ -53,7 +53,9 @@ class StaffSection extends StatelessWidget { ); } - final staffMembers = state.storeStaff; + final staffMembers = state.storeStaff + .where((staff) => staff.isActive || staff.id == selectedStaffId) + .toList(); // FIX: Feedback visivo se la lista è vuota if (staffMembers.isEmpty) { diff --git a/lib/core/widgets/staff_selector_modal.dart b/lib/core/widgets/staff_selector_modal.dart index 44e1b67..d4e9ca7 100644 --- a/lib/core/widgets/staff_selector_modal.dart +++ b/lib/core/widgets/staff_selector_modal.dart @@ -61,7 +61,21 @@ class StaffSelectorModal extends StatelessWidget { if (state.status == StaffStatus.loading) { return const CircularProgressIndicator(); } - final staffList = state.storeStaff; + final rawList = state.storeStaff.isNotEmpty + ? state.storeStaff + : state.allStaff; + final staffList = rawList.where((s) => s.isActive).toList(); + + if (staffList.isEmpty) { + return Padding( + padding: const EdgeInsets.symmetric(vertical: 24.0), + child: Text( + 'Nessun collaboratore attivo trovato.', + style: TextStyle(color: Colors.grey.shade600), + ), + ); + } + return _buildStaffGrid(context, staffList); }, ), diff --git a/lib/features/home/dashboard_daily_ricontatti/blocs/dashboard_daily_ricontatti_cubit.dart b/lib/features/home/dashboard_daily_ricontatti/blocs/dashboard_daily_ricontatti_cubit.dart index 9709ffd..ca7ccc7 100644 --- a/lib/features/home/dashboard_daily_ricontatti/blocs/dashboard_daily_ricontatti_cubit.dart +++ b/lib/features/home/dashboard_daily_ricontatti/blocs/dashboard_daily_ricontatti_cubit.dart @@ -107,7 +107,17 @@ class DashboardDailyRicontattiCubit extends Cubit storeId: storeId, ); - final activeStaff = allStaff.where((s) => s.isActive).toList(); + // 1. Escludiamo i collaboratori con qualifica Manager o Tester, o disattivati + final Set excludedStaffIds = {}; + for (final s in allStaff) { + if (s.isExcludedFromRicontattiFocus && s.id != null) { + excludedStaffIds.add(s.id!); + } + } + + final activeEligibleStaff = allStaff + .where((s) => !s.isExcludedFromRicontattiFocus) + .toList(); // 2. Recupero conteggi mensili per addetto del negozio final monthlyCountsByStaff = @@ -117,11 +127,6 @@ class DashboardDailyRicontattiCubit extends Cubit month: now, ); - int grandMonthTotal = 0; - for (final mVal in monthlyCountsByStaff.values) { - grandMonthTotal += mVal; - } - // 3. Recupero operazioni create oggi nel negozio final paginatedOps = await _operationsRepository.fetchPaginatedOperations( companyId: companyId!, @@ -137,12 +142,11 @@ class DashboardDailyRicontattiCubit extends Cubit return op.details.any((d) => d.type == OperationType.ricontatto); }).toList(); - // 5. Raggruppamento per addetto + // 5. Raggruppamento per addetto ammissibile final List summaries = []; - final Set processedStaffIds = {}; - int grandTotal = 0; + final Set processedStaffIds = Set.from(excludedStaffIds); - for (final staff in activeStaff) { + for (final staff in activeEligibleStaff) { final staffId = staff.id ?? ''; processedStaffIds.add(staffId); @@ -154,7 +158,6 @@ class DashboardDailyRicontattiCubit extends Cubit staffCount += countInOp > 0 ? countInOp : 1; } - grandTotal += staffCount; final staffMonthCount = monthlyCountsByStaff[staffId] ?? 0; summaries.add( @@ -182,7 +185,6 @@ class DashboardDailyRicontattiCubit extends Cubit final countInOp = o.details.where((d) => d.type == OperationType.ricontatto).length; staffCount += countInOp > 0 ? countInOp : 1; } - grandTotal += staffCount; final staffMonthCount = monthlyCountsByStaff[opStaffId] ?? 0; summaries.add( @@ -197,7 +199,15 @@ class DashboardDailyRicontattiCubit extends Cubit } } - // 7. Ordinamento: per numero di ricontatti oggi decrescente, poi per mese decrescente, poi per nome + // 7. Calcolo totali coerenti con gli addetti visibili + int grandTotal = 0; + int grandMonthTotal = 0; + for (final s in summaries) { + grandTotal += s.count; + grandMonthTotal += s.monthCount; + } + + // 8. Ordinamento: per numero di ricontatti oggi decrescente, poi per mese decrescente, poi per nome summaries.sort((a, b) { final countCmp = b.count.compareTo(a.count); if (countCmp != 0) return countCmp; diff --git a/lib/features/master_data/staff/models/staff_member_model.dart b/lib/features/master_data/staff/models/staff_member_model.dart index 5b8cfea..cf546da 100644 --- a/lib/features/master_data/staff/models/staff_member_model.dart +++ b/lib/features/master_data/staff/models/staff_member_model.dart @@ -77,6 +77,15 @@ class StaffMemberModel extends Equatable { ); } + /// Esclude da ricontatti e target operativi i profili non addetti (manager e tester) o disattivati + bool get isExcludedFromRicontattiFocus { + if (!isActive) return true; + final title = (jobTitle ?? '').trim().toLowerCase(); + if (title.contains('manager') || title.contains('tester')) return true; + if (systemRole == SystemRole.manager || systemRole == SystemRole.admin) return true; + return false; + } + factory StaffMemberModel.empty() { return const StaffMemberModel( companyId: '', diff --git a/lib/features/master_data/staff/ui/staff_screen.dart b/lib/features/master_data/staff/ui/staff_screen.dart index 8b8c154..2b3dc32 100644 --- a/lib/features/master_data/staff/ui/staff_screen.dart +++ b/lib/features/master_data/staff/ui/staff_screen.dart @@ -142,24 +142,64 @@ class _StaffScreenState extends State { myRole == SystemRole.admin || myRole == SystemRole.manager; final hasEmail = member.email != null && member.email!.trim().isNotEmpty; + final bool isActive = member.isActive; + return Card( elevation: 0, + color: isActive ? null : context.surface.withValues(alpha: 0.6), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), - side: BorderSide(color: context.accent.withValues(alpha: 0.1)), + side: BorderSide( + color: isActive + ? context.accent.withValues(alpha: 0.1) + : Colors.red.withValues(alpha: 0.2), + ), ), child: ListTile( contentPadding: const EdgeInsets.all(12), leading: CircleAvatar( - backgroundColor: context.accent.withValues(alpha: 0.1), + backgroundColor: isActive + ? context.accent.withValues(alpha: 0.1) + : Colors.grey.withValues(alpha: 0.2), child: Text( member.name[0].toUpperCase(), - style: TextStyle(color: context.accent), + style: TextStyle( + color: isActive ? context.accent : Colors.grey, + ), ), ), - title: Text( - member.name, - style: const TextStyle(fontWeight: FontWeight.bold), + title: Row( + children: [ + Flexible( + child: Text( + member.name, + style: TextStyle( + fontWeight: FontWeight.bold, + color: isActive ? null : context.secondaryText, + ), + overflow: TextOverflow.ellipsis, + ), + ), + if (!isActive) ...[ + const SizedBox(width: 8), + Container( + padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), + decoration: BoxDecoration( + color: Colors.red.withValues(alpha: 0.1), + borderRadius: BorderRadius.circular(4), + border: Border.all(color: Colors.red.withValues(alpha: 0.3)), + ), + child: const Text( + 'DISATTIVATO', + style: TextStyle( + fontSize: 10, + fontWeight: FontWeight.bold, + color: Colors.red, + ), + ), + ), + ], + ], ), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, @@ -177,19 +217,18 @@ class _StaffScreenState extends State { Text( 'Qualifica: ${member.jobTitle!}', style: TextStyle( - color: context.accent, + color: isActive ? context.accent : Colors.grey, fontWeight: FontWeight.w500, ), ), ], ], ), - // MODIFICA UX: Menu a tendina per le azioni (Salva spazio e previene overflow) - trailing: canManageStaff && hasEmail + trailing: canManageStaff ? PopupMenuButton( icon: const Icon(Icons.more_vert), - onSelected: (value) { - if (value == 'invite_reset') { + onSelected: (value) async { + if (value == 'invite_reset' && hasEmail) { if (!member.hasJoined) { context.read().inviteStaffMember( member: member, @@ -207,22 +246,97 @@ class _StaffScreenState extends State { } else { context.read().resetPassword(member.email!); } + } else if (value == 'toggle_active') { + final newActiveState = !member.isActive; + final confirm = await showDialog( + context: context, + builder: (ctx) => AlertDialog( + title: Text( + newActiveState + ? "Riattivare ${member.name}?" + : "Disattivare ${member.name}?", + ), + content: Text( + newActiveState + ? "L'addetto tornerà attivo e selezionabile per nuove operazioni, ticket e gare." + : "L'addetto non comparirà più nella selezione per nuove operazioni o ticket e non comparirà nelle gare e nelle loro ripartizioni. I dati storici rimarranno intatti.", + ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(ctx, false), + child: const Text("Annulla"), + ), + FilledButton( + style: FilledButton.styleFrom( + backgroundColor: + newActiveState ? Colors.green : Colors.red, + ), + onPressed: () => Navigator.pop(ctx, true), + child: Text( + newActiveState ? "Riattiva" : "Disattiva", + ), + ), + ], + ), + ); + + if (confirm != true || !mounted) return; + + context.read().saveStaffMember( + member.copyWith(isActive: newActiveState), + ); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + newActiveState + ? "${member.name} riattivato con successo!" + : "${member.name} disattivato con successo.", + ), + backgroundColor: + newActiveState ? Colors.green : Colors.orange.shade800, + ), + ); } }, itemBuilder: (context) => [ + if (hasEmail) + PopupMenuItem( + value: 'invite_reset', + child: Row( + children: [ + Icon( + !member.hasJoined ? Icons.send : Icons.lock_reset, + size: 20, + ), + const SizedBox(width: 12), + Text( + !member.hasJoined + ? "Re-invia Invito" + : "Reset Password", + ), + ], + ), + ), PopupMenuItem( - value: 'invite_reset', + value: 'toggle_active', child: Row( children: [ Icon( - !member.hasJoined ? Icons.send : Icons.lock_reset, + member.isActive + ? Icons.person_off_outlined + : Icons.person_add_alt_outlined, size: 20, + color: member.isActive ? Colors.redAccent : Colors.green, ), const SizedBox(width: 12), Text( - !member.hasJoined - ? "Re-invia Invito" - : "Reset Password", + member.isActive + ? "Disattiva Addetto" + : "Riattiva Addetto", + style: TextStyle( + color: + member.isActive ? Colors.redAccent : Colors.green, + ), ), ], ), @@ -241,6 +355,7 @@ class _StaffScreenState extends State { final emailController = TextEditingController(text: member?.email); final phoneController = TextEditingController(text: member?.phoneNumber); final jobTitleController = TextEditingController(text: member?.jobTitle); + bool isActive = member?.isActive ?? true; SystemRole selectedRole = member?.systemRole ?? SystemRole.user; List tempSelectedStores = @@ -345,7 +460,31 @@ class _StaffScreenState extends State { ), ], ), - const SizedBox(height: 24), + const SizedBox(height: 16), + + // SWITCH ATTIVO / DISATTIVATO + SwitchListTile( + contentPadding: EdgeInsets.zero, + value: isActive, + title: const Text( + "Collaboratore Attivo", + style: TextStyle(fontWeight: FontWeight.w600), + ), + subtitle: Text( + isActive + ? "Visibile per nuove operazioni, ticket e gare" + : "Disattivato (invisibile per nuove operazioni, ticket e gare)", + style: TextStyle( + fontSize: 12, + color: context.secondaryText, + ), + ), + activeThumbColor: context.accent, + onChanged: (val) { + setModalState(() => isActive = val); + }, + ), + const SizedBox(height: 16), const Text( "Assegna ai Negozi", @@ -412,6 +551,7 @@ class _StaffScreenState extends State { phoneNumber: phoneController.text.trim(), jobTitle: jobTitleController.text.trim(), systemRole: selectedRole, + isActive: isActive, companyId: GetIt.I .get() .state diff --git a/lib/features/targets/blocs/target_form_cubit.dart b/lib/features/targets/blocs/target_form_cubit.dart index 035a6af..bd220b7 100644 --- a/lib/features/targets/blocs/target_form_cubit.dart +++ b/lib/features/targets/blocs/target_form_cubit.dart @@ -53,10 +53,10 @@ class TargetFormCubit extends Cubit { final storeId = target.storeId ?? defaultStoreId; final storeStaff = await _loadStoreStaff(companyId, storeId); - // Costruisci o allinea individualTargets per ogni membro dello staff + // Costruisci o allinea individualTargets per ogni membro dello staff attivo final List individuals = []; for (final staff in storeStaff) { - if (staff.id == null) continue; + if (staff.id == null || !staff.isActive) continue; final existing = target.individualTargets.firstWhere( (it) => it.staffId == staff.id, orElse: @@ -119,9 +119,8 @@ class TargetFormCubit extends Cubit { } // 5. Se trovati addetti assegnati al negozio, ritorniamo quelli attivi - final activeStoreStaff = staff.where((s) => s.isActive).toList(); - if (activeStoreStaff.isNotEmpty) { - return activeStoreStaff; + if (staff.isNotEmpty) { + return staff.where((s) => s.isActive).toList(); } // 6. Fallback: controlliamo se in allStaff di StaffCubit qualcuno ha questo store associato @@ -163,7 +162,7 @@ class TargetFormCubit extends Cubit { final List individuals = []; for (final staff in storeStaff) { - if (staff.id == null) continue; + if (staff.id == null || !staff.isActive) continue; individuals.add( IndividualTargetModel( companyId: state.target.companyId, diff --git a/lib/features/targets/data/target_progress_service.dart b/lib/features/targets/data/target_progress_service.dart index cdd5256..be36824 100644 --- a/lib/features/targets/data/target_progress_service.dart +++ b/lib/features/targets/data/target_progress_service.dart @@ -138,9 +138,15 @@ class TargetProgressService { ); } - // 4. Costruzione avanzamento per singoli addetti + // 4. Costruzione avanzamento per singoli addetti (solo addetti attivi) final staffProgressMap = {}; - final individualTargetsList = target.individualTargets; + final individualTargetsList = target.individualTargets + .where((it) => it.staff == null || it.staff!.isActive) + .toList(); + final inactiveStaffIds = target.individualTargets + .where((it) => it.staff != null && !it.staff!.isActive) + .map((it) => it.staffId) + .toSet(); final bool hasExplicitIndividualTargets = individualTargetsList.any( (it) => it.metrics.totalLevel1 > 0 || it.metrics.totalLevel2 > 0, @@ -200,6 +206,7 @@ class TargetProgressService { // Se ci sono addetti con operazioni che non compaiono in individualTargets, creiamo la loro scheda for (final sId in staffCounts.keys) { + if (inactiveStaffIds.contains(sId)) continue; if (!staffProgressMap.containsKey(sId)) { final sCounts = staffCounts[sId] ?? {}; final sCategories = {}; diff --git a/lib/features/targets/ui/target_detail_screen.dart b/lib/features/targets/ui/target_detail_screen.dart index ef7c132..f949db2 100644 --- a/lib/features/targets/ui/target_detail_screen.dart +++ b/lib/features/targets/ui/target_detail_screen.dart @@ -703,7 +703,9 @@ class _TargetDetailScreenState extends State TargetProgressModel progress, bool isDesktop, ) { - final staffMembers = target.individualTargets; + final staffMembers = target.individualTargets + .where((it) => it.staff == null || it.staff!.isActive) + .toList(); if (staffMembers.isEmpty) { return Center( diff --git a/lib/features/tasks/blocs/task_form_cubit.dart b/lib/features/tasks/blocs/task_form_cubit.dart index 7f92553..5243f20 100644 --- a/lib/features/tasks/blocs/task_form_cubit.dart +++ b/lib/features/tasks/blocs/task_form_cubit.dart @@ -96,6 +96,9 @@ class TaskFormCubit extends Cubit { final Map> grouped = {}; for (var staff in staffList) { + if (!staff.isActive && !state.selectedStaffIds.contains(staff.id)) { + continue; + } if (!state.isGlobal) { final belongsToCurrentStore = staff.assignedStores.any( (store) => store.id == _currentStoreId, diff --git a/test/features/home/dashboard_daily_ricontatti/dashboard_daily_ricontatti_test.dart b/test/features/home/dashboard_daily_ricontatti/dashboard_daily_ricontatti_test.dart index 16539ad..51d7769 100644 --- a/test/features/home/dashboard_daily_ricontatti/dashboard_daily_ricontatti_test.dart +++ b/test/features/home/dashboard_daily_ricontatti/dashboard_daily_ricontatti_test.dart @@ -122,4 +122,46 @@ void main() { expect(OperationType.fromValue('RICONTATTO'), equals(OperationType.ricontatto)); }); }); + + group('StaffMemberModel.isExcludedFromRicontattiFocus', () { + test('excludes manager and tester by qualification jobTitle', () { + final manager = StaffMemberModel.empty().copyWith(jobTitle: 'Manager'); + final storeManager = StaffMemberModel.empty().copyWith(jobTitle: 'Store Manager'); + final tester = StaffMemberModel.empty().copyWith(jobTitle: 'Tester'); + final betaTester = StaffMemberModel.empty().copyWith(jobTitle: 'BETA TESTER'); + + expect(manager.isExcludedFromRicontattiFocus, isTrue); + expect(storeManager.isExcludedFromRicontattiFocus, isTrue); + expect(tester.isExcludedFromRicontattiFocus, isTrue); + expect(betaTester.isExcludedFromRicontattiFocus, isTrue); + }); + + test('excludes manager by systemRole and inactive members', () { + final roleManager = StaffMemberModel.empty().copyWith( + jobTitle: 'Addetto', + systemRole: SystemRole.manager, + ); + final roleAdmin = StaffMemberModel.empty().copyWith( + jobTitle: 'Addetto', + systemRole: SystemRole.admin, + ); + final inactive = StaffMemberModel.empty().copyWith( + jobTitle: 'Addetto', + isActive: false, + ); + + expect(roleManager.isExcludedFromRicontattiFocus, isTrue); + expect(roleAdmin.isExcludedFromRicontattiFocus, isTrue); + expect(inactive.isExcludedFromRicontattiFocus, isTrue); + }); + + test('includes standard active addetti', () { + final addetto = StaffMemberModel.empty().copyWith( + jobTitle: 'Addetto Vendite', + systemRole: SystemRole.user, + isActive: true, + ); + expect(addetto.isExcludedFromRicontattiFocus, isFalse); + }); + }); }