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

232 lines
8.3 KiB
VB.net

Public Class FormTurni
'Private _turniCorrenti As List(Of Turno)
'Private _tuttiIDipendenti As List(Of Dipendente)
Private _ruoloSelezionato As Ruolo
Private _dataInizio As Date
Private _dataFine As Date
Private _maxComboPerGiorno As Integer = 0
Private _larghezzaMax As Integer = 0 ' ← TRACK LARGHEZZA
Private _pannelliCreati As Integer = 0 ' ← TRACK PANNELLI
Private _totalePannelli As Integer = 0 ' ← TRACK TOTALE
Private Sub FormTurni_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Debug.WriteLine("=== DEBUG VISIBILITÀ ===")
Debug.WriteLine($"PanelGiorni.Visible: {PanelGiorni.Visible}")
Debug.WriteLine($"PanelGiorni.BackColor: {PanelGiorni.BackColor}")
Debug.WriteLine($"PanelGiorni.Dock: {PanelGiorni.Dock}")
Debug.WriteLine($"PanelGiorni.Size: {PanelGiorni.Size}")
Debug.WriteLine($"Form.Size: {Me.Size}")
Debug.WriteLine($"PanelGiorni.Parent: {PanelGiorni.Parent?.GetType().Name}")
Debug.WriteLine("====================")
With PanelGiorni
.Dock = DockStyle.Fill
.AutoScroll = True
.AutoSize = False
.Visible = True ' ⚠️ FORZA VISIBILE
.BringToFront() ' ⚠️ PORTA IN PRIMO PIANO
End With
ImpostaPeriodoDefault()
CaricaComboRuoli()
End Sub
Private Sub OnComboBoxDipendenteCambiato(data As Date, fasciaID As Integer, slot As Integer, dipendenteID As Integer)
Debug.WriteLine($"[Form] Combo: {data:dd/MM} | Fascia={fasciaID} | Slot={slot} | Dip={dipendenteID}")
' Aggiorna visivamente la combo
Dim pannello = PanelGiorni.Controls.OfType(Of FasciaRowControl)() _
.FirstOrDefault(Function(p) p.Data = data)
If pannello IsNot Nothing Then
pannello.AggiornaComboBox(fasciaID, slot, dipendenteID)
End If
End Sub
Private Sub ImpostaPeriodoDefault()
DtpDal.Value = Date.Today
DtpAl.Value = Date.Today.AddDays(6)
End Sub
Private Sub CaricaComboRuoli()
Dim listaRuoli = Gestionale.gestioneRuoli.GetRuoli()
ComboRuolo.DataSource = Nothing
ComboRuolo.DisplayMember = "Nome"
ComboRuolo.ValueMember = "ID"
ComboRuolo.DataSource = listaRuoli
End Sub
Private Sub btnCarica_Click(sender As Object, e As EventArgs) Handles btnCarica.Click
CaricaDatiPeriodo()
'CreaPannelliPerGiorno()
' Crea pannelli per ogni giorno
_pannelliCreati = 0
_totalePannelli = 0
Dim dataCorrente As Date = _dataInizio
While dataCorrente <= _dataFine
_totalePannelli += 1
'CaricaPannelloTurni(dataCorrente)
creaPannello(dataCorrente)
dataCorrente = dataCorrente.AddDays(1)
End While
'resizeGriglie()
End Sub
Private Sub CaricaDatiPeriodo()
Try
_ruoloSelezionato = Gestionale.gestioneRuoli.GetRuolo(CInt(ComboRuolo.SelectedValue))
If _ruoloSelezionato Is Nothing Then Return
Debug.WriteLine("ID cambiato: " & _ruoloSelezionato.ID)
_dataInizio = DtpDal.Value.Date
_dataFine = DtpAl.Value.Date
' Carica dipendenti
'_tuttiIDipendenti = Gestionale.gestioneDipendenti.GetDipendentiDisponibiliPerRuolo(_ruoloSelezionato.ID, _dataInizio)
' Carica turni esistenti
'_turniCorrenti = Gestionale.gestioneTurni.GetTurniPeriodo(_dataInizio, _dataFine, _ruoloSelezionato.ID)
' Pulisci il pannello
PanelGiorni.Controls.Clear()
' Forza refresh finale
PanelGiorni.PerformLayout()
PanelGiorni.Refresh()
Catch ex As Exception
MessageBox.Show("Errore: " & ex.Message)
End Try
End Sub
Private Sub creaPannello(data As Date)
Dim R As New RigheTabella
Dim fasce = Gestionale.gestioneOrariFasce.GetFasce_PerGiorno_Ruolo(data.DayOfWeek, _ruoloSelezionato.ID)
R.Fasce = fasce
For Each f As Fascia In fasce
f.DipentiPresenti = Gestionale.gestioneDipendenti.GetDipendentiDisponibiliPerFascia(_ruoloSelezionato.ID, data, f.FasciaOraria.ID)
Next
Dim pannello As New FasciaRowControl(data, R)
'pannello.Dock = DockStyle.Fill
PanelGiorni.Controls.Add(pannello)
Dim dipendentiPerFascia As New Dictionary(Of Integer, List(Of Dipendente))
Dim turniSalvati = Gestionale.gestioneTurni.GetTurniPerGiorno(data, _ruoloSelezionato.ID)
' ✅ AGGIUNGI HANDLER PRIMA DI AGGIUNGERE AL PANEL
AddHandler pannello.InizializzazioneCompletata, AddressOf OnPannelloPronto
AddHandler pannello.ComboBoxDipendenteCambiato, AddressOf OnComboBoxDipendenteCambiato
'pannello.InizializzaTimeline(data, fasce, dipendentiPerFascia, turniSalvati)
End Sub
Private Sub OnComboBoxDipendenteCambiato(data As Date, fasciaID As Integer, slot As Integer,
dipendenteID As Integer, haConflitto As Boolean)
Debug.WriteLine($"[Form] Combo: {data:dd/MM} | Fascia={fasciaID} | Slot={slot} | Dip={dipendenteID}")
If haConflitto Then
Debug.WriteLine($"⚠️ ATTENZIONE: Dipendente {dipendenteID} è già assegnato in altre fasce!")
' Puoi mostrare un messaggio o loggare il conflitto
End If
'If dipendenteID > 0 Then
' SalvaTurno(data, fasciaID, slot, dipendenteID)
'End If
End Sub
Private Sub OnPannelloPronto(larghezza As Integer)
' ✅ TRACK DELLA LARGHEZZA MASSIMA
If larghezza > _larghezzaMax Then
_larghezzaMax = larghezza
End If
_pannelliCreati += 1
' ✅ QUANDO TUTTI I PANNELLI HANNO FINITO, RIDIMENSIONA IL FORM
If _pannelliCreati = _totalePannelli Then
Me.Width = _larghezzaMax + 50 ' +50 per i bordi del Form
Debug.WriteLine($"=== FORM RIDIMENSIONATO ===")
Debug.WriteLine($"Larghezza Max: {_larghezzaMax}")
Debug.WriteLine($"Form.Width: {Me.Width}")
End If
End Sub
'Private Sub SalvaTurni(sender As Object, e As EventArgs)
' Dim btn = CType(sender, Button)
' Dim tag = btn.Tag
' Dim data As Date = tag.Data
' Dim fasciaID As Integer = tag.FasciaID
' Dim comboList As List(Of ComboBox) = tag.Combo
' Dim turniEsistenti = _turniCorrenti.
' Where(Function(t) t.Data.Date = data.Date AndAlso t.FasciaID = fasciaID).
' OrderBy(Function(t) t.ID).
' ToList()
' For i As Integer = 0 To comboList.Count - 1
' Dim dipID = CInt(comboList(i).SelectedValue)
' Dim turnoEsistente As Turno = Nothing
' If i < turniEsistenti.Count Then turnoEsistente = turniEsistenti(i)
' If dipID = 0 Then
' If turnoEsistente IsNot Nothing Then
' Gestionale.gestioneTurni.EliminaTurno(turnoEsistente.ID)
' End If
' Else
' Dim turno As New Turno With {
' .ID = If(turnoEsistente IsNot Nothing, turnoEsistente.ID, 0),
' .DipendenteID = dipID,
' .RuoloID = _ruoloSelezionato.ID,
' .FasciaID = fasciaID,
' .Data = data
' }
' Gestionale.gestioneTurni.SalvaTurno(turno)
' End If
' Next
' MessageBox.Show("Turni salvati")
' CaricaDatiPeriodo()
'End Sub
Private Sub ComboRuolo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboRuolo.SelectedIndexChanged
If ComboRuolo.SelectedValue IsNot Nothing Then
_ruoloSelezionato = New Ruolo With {
.ID = CInt(ComboRuolo.SelectedValue),
.Nome = ComboRuolo.Text
}
Debug.WriteLine("ID ruolo selezionato: " & _ruoloSelezionato.ID)
End If
End Sub
Private Sub PanelGiorni_Paint(sender As Object, e As PaintEventArgs) Handles PanelGiorni.Paint
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
End Class
Public Class ComboTag
Public Property Data As Date
Public Property FasciaID As Integer
End Class