Files
Gestionale-Turni/gestionaleTurni/FasciaRowControl.vb
luca.mustacchio@outlook.it c8978b3795 Backup prima del merge
2026-03-25 17:56:28 +01:00

515 lines
21 KiB
VB.net

Imports System.Drawing.Text
Public Class FasciaRowControl
' ✅ TIPO PER TRACCIARE LE COMBOBOX
Private Class ComboInfo
Public Property ComboBox As ComboBox
Public Property FasciaID As Integer
Public Property Slot As Integer
Public Property ListaOriginale As List(Of Dipendente)
End Class
Private _staAggiornando As Boolean = False
' ✅ TRACCIA TUTTE LE COMBOBOX
Private _tutteCombo As New List(Of ComboInfo)()
Public righeTabella As RigheTabella
Public Data As Date
Public W As Integer = 0
Private Property HRow As Integer = 35
Private Property WCol As Single = 100.0F
' ✅ EVENTI PUBBLICI PER IL FORM
Public Event InizializzazioneCompletata(larghezza As Integer)
Public Event ComboBoxDipendenteCambiato(data As Date, fasciaID As Integer, slot As Integer,
dipendenteID As Integer, haConflitto As Boolean)
Public Sub New(data As Date, righeTabella As RigheTabella)
Me.righeTabella = righeTabella
Me.Data = data
InitializeComponent()
End Sub
Private Sub FasciaRowControl_NEW_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' BeginInvoke necessario per il rendering corretto
Me.BeginInvoke(New MethodInvoker(AddressOf InizializzaTabella))
End Sub
Private Sub InizializzaTabella()
Try
CreaColonne()
CreaTimelineFasce()
CreaAltezzaTabella()
' ✅ CALCOLA E SALVA W
Me.W = tlpFasce.Width
Debug.WriteLine($"[Controllo {Data:dd/MM}] Inizializzato | W={Me.W}")
' ✅ NOTIFICA AL FORM
RaiseEvent InizializzazioneCompletata(Me.W)
Catch ex As Exception
Debug.WriteLine($"[ERRORE] InizializzaTabella: {ex.Message}")
End Try
End Sub
Private Sub CreaColonne()
Dim numColonne As Integer = CalcolaNumeroColonne() + 1 ' +1 per descrizione
If numColonne <= 1 Then Return
tlpFasce.SuspendLayout()
tlpFasce.Controls.Clear()
tlpFasce.ColumnStyles.Clear()
tlpFasce.RowStyles.Clear()
tlpFasce.AutoSize = False
tlpFasce.Dock = DockStyle.None
tlpFasce.Anchor = AnchorStyles.Top Or AnchorStyles.Left
tlpFasce.ColumnCount = numColonne
tlpFasce.RowCount = 2 ' Data + Ore
' Colonna 0: Descrizione (fissa)
tlpFasce.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 100))
' Colonne 1-N: Orari (fisse)
For i As Integer = 1 To numColonne - 1
tlpFasce.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, WCol))
Next
' Righe
tlpFasce.RowStyles.Add(New RowStyle(SizeType.Absolute, HRow)) ' Data
tlpFasce.RowStyles.Add(New RowStyle(SizeType.Absolute, HRow)) ' Ore
' Label Data
Dim lblData As New Label With {
.Text = Data.ToString("dddd dd MMMM yyyy"),
.BackColor = Color.FromArgb(200, 200, 200),
.TextAlign = ContentAlignment.MiddleCenter,
.Dock = DockStyle.Fill,
.Font = New Font("Segoe UI", 10, FontStyle.Bold),
.BorderStyle = BorderStyle.FixedSingle
}
tlpFasce.Controls.Add(lblData, 0, 0)
tlpFasce.SetColumnSpan(lblData, numColonne)
' Label Descrizione Colonna
Dim lblDesc As New Label With {
.Text = "Orario" & vbCrLf & "Fascia",
.Font = New Font("Segoe UI", 9, FontStyle.Bold),
.BackColor = Color.AliceBlue,
.TextAlign = ContentAlignment.MiddleCenter,
.BorderStyle = BorderStyle.FixedSingle,
.Dock = DockStyle.Fill
}
tlpFasce.Controls.Add(lblDesc, 0, 1)
' Label Ore
Dim minInizio As TimeSpan = TimeSpan.MaxValue
For Each f As Fascia In righeTabella.Fasce
Dim inizio As TimeSpan
If TimeSpan.TryParse(f.FasciaOraria.OraInizio, inizio) AndAlso inizio < minInizio Then
minInizio = inizio
End If
Next
For i As Integer = 0 To numColonne - 2
Dim oraSlot As TimeSpan = minInizio.Add(TimeSpan.FromMinutes(i * 30))
Dim lbl As New Label With {
.Text = oraSlot.ToString("hh\:mm"),
.Font = New Font("Segoe UI", 9, FontStyle.Bold),
.BackColor = Color.AliceBlue,
.TextAlign = ContentAlignment.MiddleCenter,
.BorderStyle = BorderStyle.FixedSingle,
.Dock = DockStyle.Fill
}
tlpFasce.Controls.Add(lbl, i + 1, 1)
Next
' Calcola larghezza totale
Dim larghezzaTotale As Integer = 0
For Each cs As ColumnStyle In tlpFasce.ColumnStyles
If cs.SizeType = SizeType.Absolute Then
larghezzaTotale += CInt(cs.Width)
End If
Next
tlpFasce.Width = larghezzaTotale
tlpFasce.Height = HRow * 2
tlpFasce.ResumeLayout()
End Sub
Private Sub CreaTimelineFasce()
If righeTabella.Fasce Is Nothing OrElse righeTabella.Fasce.Count = 0 Then Return
_tutteCombo.Clear() ' Reset tracking
tlpFasce.SuspendLayout()
' Calcola minimo inizio
Dim minInizio As TimeSpan = TimeSpan.MaxValue
For Each f As Fascia In righeTabella.Fasce
Dim inizio As TimeSpan
If TimeSpan.TryParse(f.FasciaOraria.OraInizio, inizio) AndAlso inizio < minInizio Then
minInizio = inizio
End If
Next
' Ordina fasce
Dim fasceOrdinate = righeTabella.Fasce.OrderBy(Function(f)
Dim t As TimeSpan
TimeSpan.TryParse(f.FasciaOraria.OraInizio, t)
Return t
End Function).ToList()
Dim rigaAttuale As Integer = 2 ' Dopo intestazione
For Each fascia As Fascia In fasceOrdinate
Dim inizioFascia As TimeSpan
Dim fineFascia As TimeSpan
If Not TimeSpan.TryParse(fascia.FasciaOraria.OraInizio, inizioFascia) Then Continue For
If Not TimeSpan.TryParse(fascia.FasciaOraria.OraFine, fineFascia) Then Continue For
If fineFascia < inizioFascia Then
fineFascia = fineFascia.Add(New TimeSpan(24, 0, 0))
End If
' Calcola posizione e larghezza
Dim colonnaInizio As Integer = CInt(Math.Floor((inizioFascia - minInizio).TotalMinutes / 30)) + 1
Dim colonneDaCoprire As Integer = CInt(Math.Ceiling((fineFascia - inizioFascia).TotalMinutes / 30))
If colonneDaCoprire < 1 Then colonneDaCoprire = 1
' Lista dipendenti
Dim listaDip As List(Of Dipendente) = fascia.DipentiPresenti
Dim listaDipCopia As New List(Of Dipendente)(listaDip)
listaDipCopia.Insert(0, New Dipendente With {.ID = 0, .Nome = "", .Cognome = "-- VUOTO --"})
For slot As Integer = 1 To fascia.PersoneNecessarie
' Label Fascia (solo primo slot)
If slot = 1 Then
Dim lblFascia As New Label With {
.Text = $"{fascia.FasciaNome}{vbCrLf}{inizioFascia:hh\:mm} - {fineFascia:hh\:mm}",
.TextAlign = ContentAlignment.MiddleCenter,
.BackColor = Color.FromArgb(220, 240, 255),
.BorderStyle = BorderStyle.FixedSingle,
.Font = New Font("Segoe UI", 9, FontStyle.Bold),
.Dock = DockStyle.Fill,
.AutoSize = False
}
tlpFasce.SetRowSpan(lblFascia, fascia.PersoneNecessarie)
tlpFasce.Controls.Add(lblFascia, 0, rigaAttuale)
End If
' ComboBox
Dim cmb As New ComboBox With {
.DropDownStyle = ComboBoxStyle.DropDown,
.Dock = DockStyle.Fill,
.Tag = New Tuple(Of Integer, Integer)(fascia.FasciaOraria.ID, slot),
.BackColor = Color.White,
.FlatStyle = FlatStyle.Standard,
.Font = New Font("Segoe UI", 11, FontStyle.Regular),
.Margin = New Padding(1)
}
' ✅ SALVA LISTA ORIGINALE
Dim listaCopia As New List(Of Dipendente)(listaDipCopia)
cmb.DataSource = New BindingSource(listaDipCopia, Nothing)
cmb.DataSource = New BindingSource(listaCopia, Nothing)
cmb.DisplayMember = "NomeCompleto"
cmb.ValueMember = "ID"
' ✅ TRACCIA LA COMBOBOX
_tutteCombo.Add(New ComboInfo With {
.ComboBox = cmb,
.FasciaID = fascia.FasciaOraria.ID,
.Slot = slot,
.ListaOriginale = listaCopia
})
' ✅ HANDLER
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
AddHandler cmb.DropDown, AddressOf ComboBoxDropDown_FiltraLista
tlpFasce.Controls.Add(cmb, colonnaInizio, rigaAttuale)
tlpFasce.SetColumnSpan(cmb, colonneDaCoprire)
tlpFasce.RowStyles.Add(New RowStyle(SizeType.Absolute, HRow))
rigaAttuale += 1
Next
Next
tlpFasce.ResumeLayout()
End Sub
Private Sub ComboBoxDipendente_SelectedIndexChanged(sender As Object, e As EventArgs)
' ✅ ESCI SE STA GIA' AGGIORNANDO
If _staAggiornando Then Return
Dim cmb As ComboBox = CType(sender, ComboBox)
Dim comboInfo = _tutteCombo.FirstOrDefault(Function(c) c.ComboBox Is cmb)
If comboInfo Is Nothing Then Return
Dim fasciaID As Integer = comboInfo.FasciaID
Dim slot As Integer = comboInfo.Slot
Dim dipendenteID As Integer = If(cmb.SelectedValue IsNot Nothing, CInt(cmb.SelectedValue), 0)
' ✅ CONTROLLA CONFLITTO
Dim haConflitto As Boolean = False
If dipendenteID > 0 Then
For Each c In _tutteCombo.Where(Function(x) x.FasciaID <> fasciaID)
If c.ComboBox.SelectedValue IsNot Nothing AndAlso CInt(c.ComboBox.SelectedValue) = dipendenteID Then
haConflitto = True
Exit For
End If
Next
End If
' ✅ APPLICA COLORE
If dipendenteID = 0 Then
cmb.BackColor = Color.White
ElseIf haConflitto Then
cmb.BackColor = Color.Yellow
Else
cmb.BackColor = Color.LightGreen
End If
' ✅ AGGIORNA ALTRE COMBO (SOLO FILTRO, NON DATASOURCE)
AggiornaFiltriComboEsclusa(cmb)
' ✅ NOTIFICA AL FORM
RaiseEvent ComboBoxDipendenteCambiato(Me.Data, fasciaID, slot, dipendenteID, haConflitto)
Debug.WriteLine($"[Controllo] Combo: Fascia={fasciaID}, Slot={slot}, Dip={dipendenteID}, Conflitto={haConflitto}")
End Sub
' ✅ AGGIORNA SOLO I FILTRI DELLE ALTRE COMBO (SENZA CAMBIARE DATASOURCE)
Private Sub AggiornaFiltriComboEsclusa(comboEsclusa As ComboBox)
_staAggiornando = True
Try
' ✅ OTTIENI DIPENDENTI SELEZIONATI IN TUTTE LE FASCE
Dim selezionatiPerFascia As New Dictionary(Of Integer, List(Of Integer))()
For Each ci In _tutteCombo
If ci.ComboBox.SelectedValue IsNot Nothing AndAlso CInt(ci.ComboBox.SelectedValue) > 0 Then
If Not selezionatiPerFascia.ContainsKey(ci.FasciaID) Then
selezionatiPerFascia(ci.FasciaID) = New List(Of Integer)()
End If
selezionatiPerFascia(ci.FasciaID).Add(CInt(ci.ComboBox.SelectedValue))
End If
Next
' ✅ AGGIORNA OGNI COMBO (TRANNE QUELLA CHE HA TRIGGERATO)
For Each ci In _tutteCombo
If ci.ComboBox Is comboEsclusa Then Continue For
' ✅ SALVA SELEZIONE CORRENTE
Dim selezioneCorrente = ci.ComboBox.SelectedValue
' ✅ FILTRA LISTA
Dim selezionatiNellaStessaFascia As New List(Of Integer)() ' ✅ CORRETTO
If selezionatiPerFascia.ContainsKey(ci.FasciaID) Then
selezionatiNellaStessaFascia = selezionatiPerFascia(ci.FasciaID) _
.Where(Function(id) id <> CInt(If(selezioneCorrente IsNot Nothing, selezioneCorrente, 0))) _
.ToList()
End If
Dim listaFiltrata = ci.ListaOriginale.Where(Function(d) d.ID = 0 OrElse Not selezionatiNellaStessaFascia.Contains(d.ID)).ToList()
' ✅ AGGIORNA DATASOURCE SENZA TRIGGERARE EVENTO
RemoveHandler ci.ComboBox.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
ci.ComboBox.DataSource = New BindingSource(listaFiltrata, Nothing)
ci.ComboBox.DisplayMember = "NomeCompleto"
ci.ComboBox.ValueMember = "ID"
If selezioneCorrente IsNot Nothing Then
ci.ComboBox.SelectedValue = selezioneCorrente
End If
AddHandler ci.ComboBox.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
Next
Finally
_staAggiornando = False
End Try
End Sub
Private Sub ComboBoxDropDown_FiltraLista(sender As Object, e As EventArgs)
' ✅ ESCI SE STA GIA' AGGIORNANDO
If _staAggiornando Then Return
Dim cmb As ComboBox = CType(sender, ComboBox)
Dim comboInfo = _tutteCombo.FirstOrDefault(Function(c) c.ComboBox Is cmb)
If comboInfo Is Nothing Then Return
' ✅ OTTIENI DIPENDENTI GIA' SELEZIONATI NELLA STESSA FASCIA
Dim selezionatiNellaFascia As New List(Of Integer)()
For Each c In _tutteCombo.Where(Function(x) x.FasciaID = comboInfo.FasciaID AndAlso x.ComboBox IsNot cmb)
If c.ComboBox.SelectedValue IsNot Nothing AndAlso CInt(c.ComboBox.SelectedValue) > 0 Then
selezionatiNellaFascia.Add(CInt(c.ComboBox.SelectedValue))
End If
Next
' ✅ SALVA SELEZIONE CORRENTE
Dim selezioneCorrente = cmb.SelectedValue
' ✅ FILTRA LISTA
Dim listaFiltrata = comboInfo.ListaOriginale.Where(Function(d) d.ID = 0 OrElse Not selezionatiNellaFascia.Contains(d.ID)).ToList()
' ✅ AGGIORNA DATASOURCE SENZA TRIGGERARE EVENTO
RemoveHandler cmb.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
cmb.DataSource = New BindingSource(listaFiltrata, Nothing)
cmb.DisplayMember = "NomeCompleto"
cmb.ValueMember = "ID"
' ✅ RIPRISTINA SELEZIONE
If selezioneCorrente IsNot Nothing Then
cmb.SelectedValue = selezioneCorrente
End If
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
End Sub
' ✅ AGGIORNA TUTTE LE COMBOBOX DOPO UN CAMBIAMENTO
Private Sub AggiornaTutteLeCombo()
For Each comboInfo In _tutteCombo
Dim cmb = comboInfo.ComboBox
Dim selezioneCorrente = cmb.SelectedValue
' Riapplica il datasource per aggiornare il filtro
cmb.DataSource = New BindingSource(comboInfo.ListaOriginale, Nothing)
cmb.DisplayMember = "NomeCompleto"
cmb.ValueMember = "ID"
If selezioneCorrente IsNot Nothing Then
cmb.SelectedValue = selezioneCorrente
End If
Next
End Sub
' ✅ METODO PUBBLICO PER TROVARE E MODIFICARE UNA COMBOBOX SPECIFICA
Public Sub AggiornaComboBox(fasciaID As Integer, slot As Integer, dipendenteID As Integer)
For Each ctrl As Control In tlpFasce.Controls
If TypeOf ctrl Is ComboBox Then
Dim cmb = CType(ctrl, ComboBox)
If cmb.Tag IsNot Nothing Then
Dim tagData = CType(cmb.Tag, Tuple(Of Integer, Integer))
If tagData.Item1 = fasciaID AndAlso tagData.Item2 = slot Then
' Trovata! Aggiorna la selezione
cmb.SelectedValue = dipendenteID
Debug.WriteLine($"[Controllo] ComboBox aggiornata: Fascia={fasciaID}, Slot={slot}, Dip={dipendenteID}")
Exit For
End If
End If
End If
Next
End Sub
' ✅ METODO PER AGGIORNARE LA LISTA DIPENDENTI DI UNA FASCIA
Public Sub AggiornaListaDipendenti(fasciaID As Integer, nuovaLista As List(Of Dipendente))
For Each ctrl As Control In tlpFasce.Controls
If TypeOf ctrl Is ComboBox Then
Dim cmb = CType(ctrl, ComboBox)
If cmb.Tag IsNot Nothing Then
Dim tagData = CType(cmb.Tag, Tuple(Of Integer, Integer))
If tagData.Item1 = fasciaID Then
' Mantieni la selezione corrente se possibile
Dim selezioneCorrente = cmb.SelectedValue
Dim listaCopia As New List(Of Dipendente)(nuovaLista)
listaCopia.Insert(0, New Dipendente With {.ID = 0, .Nome = "", .Cognome = "-- VUOTO --"})
cmb.DataSource = New BindingSource(listaCopia, Nothing)
cmb.DisplayMember = "NomeCompleto"
cmb.ValueMember = "ID"
' Ripristina selezione
If selezioneCorrente IsNot Nothing Then
cmb.SelectedValue = selezioneCorrente
End If
End If
End If
End If
Next
End Sub
Private Sub CreaAltezzaTabella()
Dim altezzaTotale As Integer = 0
For Each rs As RowStyle In tlpFasce.RowStyles
If rs.SizeType = SizeType.Absolute Then
altezzaTotale += CInt(rs.Height)
End If
Next
tlpFasce.Height = altezzaTotale
Me.Height = altezzaTotale
End Sub
Private Function CalcolaNumeroColonne() As Integer
If righeTabella.Fasce.Count = 0 Then Return 0
Dim minInizio As TimeSpan = TimeSpan.MaxValue
Dim maxFine As TimeSpan = TimeSpan.MinValue
For Each f As Fascia In righeTabella.Fasce
Dim inizio As TimeSpan
Dim fine As TimeSpan
If TimeSpan.TryParse(f.FasciaOraria.OraInizio, inizio) AndAlso
TimeSpan.TryParse(f.FasciaOraria.OraFine, fine) Then
If fine < inizio Then fine = fine.Add(New TimeSpan(24, 0, 0))
If inizio < minInizio Then minInizio = inizio
If fine > maxFine Then maxFine = fine
End If
Next
Dim durata As TimeSpan = maxFine - minInizio
Dim numeroColonne As Integer = CInt(Math.Ceiling(durata.TotalMinutes / 30))
If numeroColonne < 1 AndAlso righeTabella.Fasce.Count > 0 Then numeroColonne = 1
Return numeroColonne
End Function
' ✅ PULIZIA HANDLER QUANDO IL CONTROLLO VIENE DISTRUTTO
Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
For Each comboInfo In _tutteCombo
RemoveHandler comboInfo.ComboBox.SelectedIndexChanged, AddressOf ComboBoxDipendente_SelectedIndexChanged
RemoveHandler comboInfo.ComboBox.DropDown, AddressOf ComboBoxDropDown_FiltraLista
Next
_tutteCombo.Clear()
MyBase.OnHandleDestroyed(e)
End Sub
Private Sub tlpFasce_Paint(sender As Object, e As PaintEventArgs) Handles tlpFasce.Paint
End Sub
End Class
Public Class RigheTabella
Private _fascia As List(Of Fascia)
Public Property Fasce As List(Of Fascia)
Get
Return _fascia
End Get
Set(value As List(Of Fascia))
_fascia = value
End Set
End Property
' ✅ METODO UTILE PER CONTARE LE RIGHE TOTALI
Public Function GetTotaleRighe() As Integer
If _fascia Is Nothing Then Return 0
Return _fascia.Sum(Function(f) f.PersoneNecessarie)
End Function
' ✅ METODO PER TROVARE UNA FASCIA PER ID
Public Function GetFasciaById(fasciaID As Integer) As Fascia
If _fascia Is Nothing Then Return Nothing
Return _fascia.FirstOrDefault(Function(f) f.FasciaOraria.ID = fasciaID)
End Function
End Class