using GPW.CORE.Data.DbModels; using GPW.CORE.Data.Services; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; namespace GPW.CORE.WRKLOG.Components.Compo { public partial class RegRichieste { #region Public Properties [Parameter] public int IdxDipendente { get; set; } [Parameter] public bool isLoading { get => loading; set => loading = value; } [Parameter] public int months { get; set; } [Parameter] public EventCallback ReportUpdate { get; set; } #endregion Public Properties #region Protected Properties [Inject] protected MessageService AppMServ { get; set; } = null!; [Inject] protected GpwDataService GDataServ { get; set; } = null!; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; protected bool loading { get; set; } = false; #endregion Protected Properties #region Protected Methods protected override async Task OnInitializedAsync() { dtInizio = new DateTime(DateTime.Today.Year, 1, 1); dtFine = dtInizio.AddYears(2); currRecord.IdxDipendente = AppMServ.IdxDipendente; CodGiust = ""; var rawGiust = await GDataServ.AnagGiust(); if (rawGiust != null) { ListGiust = rawGiust.Where(x => x.showReq).ToList(); } await ReloadData(); } #endregion Protected Methods #region Private Fields private bool isSendingData = false; private List? ListGiust = null; private List? ListRecords = null; /// /// Numero giorni minimo per richiesta ferie /// private int NumDayFerieRichAntic = 14; /// /// NUmero giorni massimo per richiesta permessi /// private int NumDayPermMax = 21; private int sendDataMaxVal = 100; private int sendDataNextVal = 0; private int sendDataVal = 0; private bool showAdd = false; #endregion Private Fields #region Private Properties private string CodGiust { get => currRecord.CodGiust; set { if (currRecord.CodGiust != value) { currRecord.CodGiust = value; // calcolo limite date DateTime mDtStart = minDate(value); DateTime mDtEnd = maxDate(value); // imposto dataOra min/max da tipo cod Giust if (value != "FER") { int ora = DateTime.Now.Hour; mDtStart = minDate(value).AddHours(ora); mDtEnd = minDate(value).AddHours(ora + 4); } // modifico SE fossero extra periodo if (mDtStart > dtStart) { dtStart = mDtStart; } if (mDtEnd < dtEnd) { dtStart = mDtStart; dtEnd = mDtEnd; } } } } private RegRichiesteModel currRecord { get; set; } = new RegRichiesteModel(); private DateTime dtEnd { get => currRecord.DtEnd; set { currRecord.DtEnd = value; // verifico coerenza date... if (dtStart >= dtEnd && dtEnd.Hour > 2) { dtStart = dtEnd.AddHours(-1); } } } private DateTime dtFine { get; set; } = DateTime.Today; private DateTime dtInizio { get; set; } = DateTime.Today; private DateTime dtStart { get => currRecord.DtStart; set { currRecord.DtStart = value; // verifico coerenza date... if (dtEnd <= dtStart) { dtEnd = dtStart.AddHours(1); } } } private string NomeDip { get { string answ = "per cortesia"; if (AppMServ != null && AppMServ.RigaDip != null) { answ = $"{AppMServ.RigaDip.Nome}"; } return answ; } } #endregion Private Properties #region Private Methods private async Task DoDelete(RegRichiesteModel selItem) { // chiedo verifica if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il record selezionato??")) return; await Task.Delay(1); isSendingData = true; sendDataVal = 0; sendDataNextVal = 90; await Task.Delay(1); await InvokeAsync(StateHasChanged); await GDataServ.RegRichiesteDelete(selItem); sendDataVal = 100; sendDataNextVal = 100; await Task.Delay(1); await ReloadData(); await ReportUpdate.InvokeAsync(true); isSendingData = false; } private void editRec(RegRichiesteModel selItem) { currRecord = selItem; showAdd = true; } /// /// init valori da config /// /// private async Task initConf() { // leggo conf standard giorni permessi/ferie var sNumDayFerieRichAntic = GDataServ.ConfigGetKey("NumDayFerieRichAntic"); if (sNumDayFerieRichAntic != null) { int.TryParse(sNumDayFerieRichAntic.valore, out NumDayFerieRichAntic); } var sNumDayPermMax = GDataServ.ConfigGetKey("NumDayPermMax"); if (sNumDayPermMax != null) { int.TryParse(sNumDayPermMax.valore, out NumDayPermMax); } } /// /// Registra il record /// /// private async Task insertRecord() { isSendingData = true; sendDataVal = 0; sendDataNextVal = 5; await Task.Delay(1); // verifica preliminare delle date compatibili... var dtCheckMin = minDate(currRecord.CodGiust); var dtCheckMax = maxDate(currRecord.CodGiust); if (currRecord.DtStart < dtCheckMin) { currRecord.DtStart = dtCheckMin; } else if (currRecord.DtStart > dtCheckMax) { currRecord.DtStart = dtCheckMax; currRecord.DtEnd = dtCheckMax; } if (currRecord.DtEnd > dtCheckMax) { currRecord.DtEnd = dtCheckMax; } else if (currRecord.DtEnd < dtCheckMin) { currRecord.DtStart = dtCheckMin; currRecord.DtEnd = dtCheckMin; } // effettuo insert... sendDataVal = 5; sendDataNextVal = 90; await Task.Delay(1); var updTask = Task.Run(async () => await GDataServ.RegRichiesteUpsert(currRecord) ); await updTask; // effettuo insert... sendDataVal = 100; sendDataNextVal = 100; await Task.Delay(1); showAdd = false; await ReloadData(); await ReportUpdate.InvokeAsync(true); isSendingData = false; } private DateTime maxDate(string codGiust) { DateTime answ = DateTime.Today.AddDays(NumDayPermMax); switch (codGiust) { case "FER": answ = new DateTime(answ.Year, answ.Month, 1).AddMonths(months); break; case "104": case "PERM": default: DateTime.Today.AddDays(NumDayPermMax); break; } return answ; } private DateTime minDate(string codGiust) { DateTime answ = DateTime.Today; switch (codGiust) { case "FER": answ = DateTime.Today.AddDays(NumDayFerieRichAntic); break; case "104": case "PERM": default: answ = DateTime.Today; break; } return answ; } private async Task ReloadData() { ListRecords = null; await initConf(); ListRecords = GDataServ.RegRichiesteGetByDip(AppMServ.IdxDipendente, dtInizio, dtFine); } private async Task toggleAddNew() { showAdd = !showAdd; await Task.Delay(1); if (showAdd) { currRecord = new RegRichiesteModel() { IdxDipendente = AppMServ.IdxDipendente }; } } #endregion Private Methods } }