Files
gpw_next/GPW.CORE.SMART/Components/Pages/DayOff.razor.cs
T
2024-09-05 15:16:39 +02:00

510 lines
18 KiB
C#

using EgwCoreLib.Razor;
using EgwCoreLib.Razor.Data;
using GPW.CORE.Data.DbModels;
using GPW.CORE.Smart.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis.CodeFixes;
namespace GPW.CORE.Smart.Components.Pages
{
public partial class DayOff : IDisposable
{
#region Public Properties
public CalendarModeEnum.modoControllo currMode { get; set; } = CalendarModeEnum.modoControllo.showCalendar;
public int MoveBlock { get; set; } = 7;
public int NumDays { get; set; } = 5;
#endregion Public Properties
#region Public Methods
public void Dispose()
{
ListDate = new List<DateTime>();
DateCFF = new Dictionary<DateTime, string>();
eventList = new List<CalendarEvent>();
GC.Collect();
}
#endregion Public Methods
#region Protected Fields
protected int anno = DateTime.Today.Year;
protected DateTime dtMax = DateTime.Today.AddMonths(3);
protected DateTime dtMin = DateTime.Today;
protected List<DateTime> ListDate = new List<DateTime>();
protected int numDays = 5;
protected int numMesi = 3;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected MessageService AppMServ { get; set; } = null!;
[Inject]
protected CoreSmartDataService CDService { get; set; } = null!;
protected DateTime DtReq { get; set; } = DateTime.Today;
protected List<CalendarEvent> EventListWeek { get; set; } = new List<CalendarEvent>();
protected int idxDipendente
{
get => AppMServ.IdxDipendente;
}
#endregion Protected Properties
#region Protected Methods
protected async Task backToCal(bool isMode)
{
showDetail = false;
await Task.Delay(1);
}
protected string btnSel(string bName)
{
bool answ = false;
switch (bName)
{
case "FER":
answ = showFer;
break;
case "MAL":
answ = showMal;
break;
case "PER":
answ = showPer;
break;
default:
break;
}
return answ ? "btn-primary" : "btn-secondary";
}
protected async Task ChangePeriod(int numStep)
{
ListDate = new List<DateTime>();
await Task.Delay(1);
DtReq = DtReq.AddDays(MoveBlock * numStep);
ReloadWeekData();
await Task.Delay(1);
}
protected override void OnInitialized()
{
initToggler();
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected void SetDayDetail(DateTime selDate)
{
var tmpDays = ListDate.Where(x => x.Equals(selDate)).ToList();
var tmpEv = EventListWeek.Where(x => x.Inizio.Date.Equals(selDate)).ToList();
// salvo valori simulati
ListDate = tmpDays;
EventListWeek = tmpEv;
}
protected async Task toggFer()
{
showFer = !showFer;
await ReloadData();
ReloadWeekData();
}
protected void toggleFiltEv()
{
showFiltEv = !showFiltEv;
}
protected async Task toggMal()
{
showMal = !showMal;
await ReloadData();
ReloadWeekData();
}
protected async Task toggPer()
{
showPer = !showPer;
await ReloadData();
ReloadWeekData();
}
#endregion Protected Methods
#region Private Fields
private Dictionary<DateTime, string> DateCFF = new Dictionary<DateTime, string>();
private Dictionary<DateTime, string> DateRDip = new Dictionary<DateTime, string>();
private List<CalendarEvent> eventList = new List<CalendarEvent>();
private bool isLoading = false;
#endregion Private Fields
#region Private Properties
private List<CalFesteFerieModel> ListFermateAzienda { get; set; } = new List<CalFesteFerieModel>();
private List<RegMalattieModel> ListMalattie { get; set; } = new List<RegMalattieModel>();
private List<RegRichiesteModel> ListRichiesteDip { get; set; } = new List<RegRichiesteModel>();
[Inject]
private MessageService MService { get; set; } = null!;
private bool showDetail { get; set; } = false;
private bool showFer { get; set; } = true;
private bool showFiltEv { get; set; } = false;
private bool showMal { get; set; } = true;
private bool showMalattie { get; set; } = false;
private bool showPer { get; set; } = true;
private bool showRichieste { get; set; } = false;
private Toggler.SelectGlobalToggle ToggleShow { get; set; } = new Toggler.SelectGlobalToggle();
private Toggler.SelectGlobalToggle ToggleTipo { get; set; } = new Toggler.SelectGlobalToggle();
#endregion Private Properties
#region Private Methods
private string colorByGiustStato(string codGiust, bool conf)
{
string answ = "#EDEDED";
switch (codGiust)
{
case "PERM":
answ = conf ? "#9966DE" : "#CDAAFF";
break;
case "FER":
answ = conf ? "#11CD44" : "#AAFFCD";
break;
case "104":
answ = conf ? "#DE00AB" : "#FFAACD";
break;
case "MAL":
answ = conf ? "#000" : "#696969";
break;
default:
break;
}
return answ;
}
private string colorTextByGiustStato(string codGiust, bool conf)
{
string answ = "#EDEDED";
switch (codGiust)
{
case "PERM":
answ = conf ? "#DEDEDE" : "#000000";
break;
case "FER":
answ = conf ? "#FFFFFF" : "#000000";
break;
case "104":
answ = conf ? "#DEDEDE" : "#000000";
break;
case "MAL":
answ = conf ? "#DEDEDE" : "#ABABAB";
break;
default:
break;
}
return answ;
}
private void DisplayDate(DateTime dtSel)
{
DtReq = dtSel;
ReloadWeekData();
showDetail = true;
MService.ReportDateChange();
}
private async Task evTogShow(Toggler.SelectGlobalToggle newTogData)
{
ToggleShow = newTogData;
showRichieste = !ToggleShow.isActive;
await Task.Delay(1);
}
private async Task evTogTipo(Toggler.SelectGlobalToggle newTogData)
{
ToggleTipo = newTogData;
showMalattie = !ToggleTipo.isActive;
await Task.Delay(1);
}
private async Task ForceReloadCal()
{
await Task.Delay(1);
await ReloadData();
}
private void initToggler()
{
ToggleTipo = new Toggler.SelectGlobalToggle()
{
leftString = "Malattie",
rightString = "Ferie e Permessi",
placardCss = "bg-light border-light text-dark",
};
ToggleShow = new Toggler.SelectGlobalToggle()
{
leftString = "Mostra Richieste",
rightString = "Solo Calendario",
placardCss = "bg-light border-light text-dark",
};
}
private async void MService_EA_DipUpdated()
{
if (idxDipendente > 0)
{
await ReloadData();
await Task.Delay(1);
await InvokeAsync(StateHasChanged);
}
}
private async Task ReloadData()
{
isLoading = true;
int iCnt = 0;
DateCFF = new Dictionary<DateTime, string>();
eventList = new List<CalendarEvent>();
DateTime oggi = DateTime.Today;
// prendo mese prec x partenza
dtMin = new DateTime(DtReq.Year, DtReq.Month, 1).AddMonths(-1);
// prendo sempre 3 mesi (prec, corrente, successivo)
dtMax = dtMin.AddMonths(numMesi);
// dichiarazione codici CSS di base
string cssFest = "bg-danger text-warning rounded-pill ";
string cssChius = "bg-warning text-dark rounded-pill ";
string cssRichFer = "bg-warning text-dark rounded-circle p-2";
string cssRichPerm = "bg-info text-light rounded-circle p-2";
string cssMal = "bg-dark text-light rounded-circle p-2";
string cssTd = "bg-info text-dark rounded-pill";
DateCFF.Add(DateTime.Today, cssTd);
// recupero le fermate aziendali
ListFermateAzienda = await CDService.CalFestFeriePeriodo(dtMin, dtMax);
if (ListFermateAzienda.Count > 0)
{
string cssSpec = "";
foreach (var item in ListFermateAzienda)
{
cssSpec = item.codGiust == "FEST" ? cssFest : cssChius;
if (!DateCFF.ContainsKey(item.data))
{
if (item.data.DayOfWeek != DayOfWeek.Saturday && item.data.DayOfWeek != DayOfWeek.Sunday)
{
DateCFF.Add(item.data, cssSpec);
}
}
eventList.Add(new CalendarEvent()
{
id = iCnt++,
Inizio = item.data.Date.AddHours(9),
Fine = item.data.Date.AddHours(19),
Type = item.codGiust,
Description = item.descrizione,
CssClass = item.codGiust == "FEST" ? "blockFest" : "blockChius"
});
}
}
// recupero e converto le richieste dipendente... RAGGUPPANDOLE
ListRichiesteDip = await CDService.RegRichiesteGetByDip(0, dtMin, dtMax);
if (ListRichiesteDip.Count > 0)
{
string cssSpec = "";
DateTime dtIns = DateTime.Today;
foreach (var item in ListRichiesteDip)
{
// verifico se abilitato per tipo
if ((item.CodGiust == "FER" && showFer) || (showPer && item.CodGiust != "FER"))
{
switch (item.CodGiust)
{
case "PERM":
case "104":
cssSpec = cssRichPerm;
break;
case "FER":
cssSpec = cssRichFer;
break;
default:
break;
}
// aggiungo x tutte le date richieste...
int numGG = item.DtEnd.Subtract(item.DtStart).Days + 1;
for (int i = 0; i < numGG; i++)
{
dtIns = item.DtStart.Date.AddDays(i);
if (!DateRDip.ContainsKey(dtIns))
{
DateRDip.Add(dtIns, cssSpec);
}
}
string css = "";
DateTime dtFine = item.DtEnd;
if (item.CodGiust == "PERM")
{
css = "blockPerm";
}
else if (item.CodGiust == "104")
{
css = "block104";
}
else if (item.CodGiust == "FER")
{
css = "blockFerie";
}
if (item.CodGiust == "FER")
{
for (int i = 0; i < numGG; i++)
{
var dtEvInizio = item.DtStart.AddDays(i).AddHours(9);
var dtEvFine = item.DtStart.AddDays(i).AddHours(19);
var evDescr = item.DipNav.Abbrev;// $"{item.DipNav.Nome.Substring(0, 1)}. {item.DipNav.Cognome}";
// 2024.08.27 cerco se esistesse già...
var prevRec = eventList
.Where(x => x.Inizio == dtEvInizio && x.Fine == dtEvFine && x.Type == item.CodGiust)
.FirstOrDefault();
// se manca aggiungo
if (prevRec == null)
{
eventList.Add(new CalendarEvent()
{
id = iCnt++,
Inizio = item.DtStart.AddDays(i).AddHours(9),
Fine = item.DtStart.AddDays(i).AddHours(19),
Description = evDescr,
Type = item.CodGiust,
CssClass = css
});
}
// altrimenti accodo....
else
{
prevRec.Description += $", {evDescr}";
}
}
}
else
{
var evDescr = item.DipNav.Abbrev;
eventList.Add(new CalendarEvent()
{
id = iCnt++,
Inizio = item.DtStart.Hour > 0 ? item.DtStart : item.DtStart.AddHours(9),
Fine = item.DtEnd.Hour > 0 ? item.DtEnd : item.DtStart.AddHours(19),
Type = item.CodGiust,
Description = item.DipNav.Abbrev,
CssClass = css
});
}
}
}
}
if (showMal)
{
// recupero e converto le malattie...
ListMalattie = await CDService.RegMalattieGetAll(100);
if (ListMalattie.Count > 0)
{
string cssSpec = cssMal;
DateTime dtIns = DateTime.Today;
foreach (var item in ListMalattie)
{
// aggiungo x tutte le date richieste...
for (int i = 0; i < item.NumGG; i++)
{
dtIns = item.DtInizio.Date.AddDays(i - 1);
if (!DateRDip.ContainsKey(dtIns))
{
DateRDip.Add(dtIns, cssSpec);
}
}
// esplosione malattie...
for (int i = 0; i < item.NumGG; i++)
{
eventList.Add(new CalendarEvent()
{
id = iCnt++,
Inizio = item.DtInizio.AddDays(i).AddHours(9),
Fine = item.DtInizio.AddDays(i).AddHours(19),
Title = "MAL",
Description = $"{item.DipNav.Nome.Substring(0, 1)}. {item.DipNav.Cognome}",
Type = "MAL",
ToolTip = "MAL",
CssClass = "blockMal"
});
}
}
}
}
isLoading = false;
await Task.Delay(1);
}
private async Task ReloadMonth(DateTime dtSel)
{
DtReq = dtSel;
MService.targetDate = dtSel;
showDetail = false;
await ReloadData();
await Task.Delay(1);
MService.ReportDateChange();
}
private void ReloadWeekData()
{
ListDate = new List<DateTime>();
EventListWeek = new List<CalendarEvent>();
int nWeek = DtReq.DayOfWeek - DayOfWeek.Monday;
if (NumDays < 5 || NumDays > 7)
{
nWeek = 0;
}
for (int i = 0; i < NumDays; i++)
{
ListDate.Add(DtReq.AddDays(i - nWeek));
}
// ciclo giorno x giorno e controllo se devo "ritardare" x visualizzare...
EventListWeek = eventList.Where(x => ListDate.Contains(x.Inizio.Date)).ToList();
}
#endregion Private Methods
}
}