using EgwCoreLib.Utils; using GPW.CORE.Data.DbModels; using GPW.CORE.Data.DTO; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using NLog; namespace GPW.CORE.Data.Controllers { public class GPWController : IDisposable { #region Public Constructors public GPWController(IConfiguration configuration) { _configuration = configuration; } #endregion Public Constructors #region Public Methods /// /// Recupera l'elenco Clienti (tutti) /// /// public List AnagClientiAll() { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagClienti .OrderBy(x => x.RagSociale) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagClientiAll:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elimina il cliente (se non ci sono relazioni con fasi attive, in tal caso rende disattivo...) /// /// public bool AnagClientiDelete(AnagClientiModel remRec) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagClienti .Where(x => x.IdxCliente == remRec.IdxCliente) .FirstOrDefault(); if (dbRec != null) { // controllo se abbia progetti associati var listFasi = localDbCtx .DbSetAnagProgetti .Where(x => x.IdxCliente == remRec.IdxCliente) .ToList(); // --> in tal caso disattivo... if (listFasi != null && listFasi.Count > 0) { dbRec.Attivo = false; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } else { // altrimenti elimino davvero... localDbCtx .DbSetAnagClienti .Remove(dbRec); } } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagClientiDelete:{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera l'elenco Clienti (tutti) /// /// public bool AnagClientiUpsert(AnagClientiModel upsRec) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagClienti .Where(x => x.IdxCliente == upsRec.IdxCliente) .FirstOrDefault(); if (dbRec == null) { localDbCtx .DbSetAnagClienti .Add(upsRec); } else { dbRec.Attivo = upsRec.Attivo; dbRec.Cap = upsRec.Cap; dbRec.Cf = upsRec.Cf; dbRec.Citta = upsRec.Citta; dbRec.CodExt = upsRec.CodExt; dbRec.Email = upsRec.Email; dbRec.Indirizzo = upsRec.Indirizzo; dbRec.Nota = upsRec.Nota; dbRec.PIva = upsRec.PIva; dbRec.Prov = upsRec.Prov; dbRec.RagSociale = upsRec.RagSociale; dbRec.Tel = upsRec.Tel; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagClientiUpsert:{Environment.NewLine}{exc}"); } } return answ; } /// /// Cerca un device dato il suo secret /// /// /// public AnagDeviceModel? AnagDeviceByKey(string devSecret) { // init dati necessari AnagDeviceModel? dbResult = new AnagDeviceModel(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagDevices .Where(x => x.DeviceSecret == devSecret) .FirstOrDefault(); } catch (Exception exc) { Log.Error($"Errore in AnagDeviceByKey:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Crea un record device /// /// /// public bool AnagDeviceInsert(AnagDeviceModel newRecord) { // init dati necessari bool done = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { localDbCtx .DbSetAnagDevices .Add(newRecord); // ora salvo! localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Log.Error($"Errore in AnagDeviceInsert:{Environment.NewLine}{exc}"); } } return done; } /// /// Crea un record device /// /// /// public bool AnagDeviceUpdate(AnagDeviceModel currItem) { // init dati necessari bool done = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { if (currItem.IdxDevice == 0) { localDbCtx.Entry(currItem).State = EntityState.Added; } else { // recupero vecchio var oldRec = localDbCtx .DbSetAnagDevices .Where(x => x.IdxDevice == currItem.IdxDevice) .FirstOrDefault(); if (oldRec != null) { oldRec.LastIpv4 = currItem.LastIpv4; oldRec.DataOraLastSeen = DateTime.Now; oldRec.Description = currItem.Description; oldRec.ScreenSize = currItem.ScreenSize; localDbCtx .DbSetAnagDevices .Update(oldRec); } // altrimenti aggiungo else { localDbCtx .DbSetAnagDevices .Update(currItem); } } // ora salvo! localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Log.Error($"Errore in AnagDeviceUpdate:{Environment.NewLine}{exc}"); } } return done; } /// /// Recupera l'elenco delle fasi (tutte) /// /// public List AnagFasiAll(bool onlyActive) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagFasi .Where(x => (x.ProgettoNav != null && x.ProgettoNav.Attivo == true) && (((bool)x.Attivo == true && onlyActive == true) || !onlyActive)) .Include(p => p.ProgettoNav) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagFasiAll:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco fasi dato ancestor /// /// /// public List AnagFasiByAncestor(int idxFaseAnc) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... var searchRecord = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFaseAncest == idxFaseAnc) .Include(p => p.ProgettoNav) .ToList(); if (searchRecord != null) { dbResult = searchRecord; } } catch (Exception exc) { Log.Error($"Errore in AnagFasiByAncestor:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Recupera singola fase /// /// /// public AnagFasiModel AnagFasiByKey(int idxFase) { // init dati necessari AnagFasiModel dbResult = new AnagFasiModel(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... var searchRecord = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFase == idxFase) .Include(p => p.ProgettoNav) .FirstOrDefault(); if (searchRecord != null) { dbResult = searchRecord; } } catch (Exception exc) { Log.Error($"Errore in AnagFasiByKey:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco fasi dato chiave progetto /// /// /// public List AnagFasiByProj(int idxProj) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagFasi .Where(x => x.IdxProgetto == idxProj) .Include(p => p.ProgettoNav) .OrderBy(x => x.CodFase) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagFasiByProj:{Environment.NewLine}{exc}"); } } return dbResult; } public bool AnagFasiDelete(int IdxFase) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFase == IdxFase) .FirstOrDefault(); if (dbRec != null) { // verifico NON abbia child... var listChild = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFaseAncest == IdxFase) .ToList(); if (listChild == null || listChild.Count == 0) { localDbCtx .DbSetAnagFasi .Remove(dbRec); } } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagFasiDelete:{Environment.NewLine}{exc}"); } } return answ; } /// /// Elenco FasiExpl dato chiave progetto /// /// /// public List AnagFasiExplByProj(int idxProj) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var pIdxPrj = new SqlParameter("@idxProgetto", idxProj); dbResult = localDbCtx .DbSetAnagFasiExpl .FromSqlRaw("EXEC stp_AF_getByIdxProj @idxProgetto", pIdxPrj) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagFasiExplByProj:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Aggiorno tutte le fasi del proj con il tag indicato /// /// /// /// public bool AnagFasiForceTag(int idxProj, string CodTagFase) { int result = 0; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var pIdxPrj = new SqlParameter("@idxProgetto", idxProj); var pCodTag = new SqlParameter("@CodTagFase", CodTagFase); result = localDbCtx .Database .ExecuteSqlRaw("EXEC stp_AF_updCodFaseByProj @idxProgetto, @CodTagFase", pIdxPrj, pCodTag); } catch (Exception exc) { Log.Error($"Errore in AnagFasiForceTag:{Environment.NewLine}{exc}"); } } return result != 0; } /// /// Esecuzione upsert fase /// /// /// public bool AnagFasiUpsert(AnagFasiModel upsRec) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFase == upsRec.IdxFase && upsRec.IdxFase > 0) .FirstOrDefault(); if (dbRec == null) { localDbCtx .DbSetAnagFasi .Add(upsRec); } else { dbRec.Attivo = upsRec.Attivo; dbRec.BudgetMoney = upsRec.BudgetMoney; dbRec.BudgetTime = upsRec.BudgetTime; dbRec.CodExt = upsRec.CodExt; dbRec.CodTagFase = upsRec.CodTagFase; dbRec.DescrizioneFase = upsRec.DescrizioneFase; dbRec.EnableMoney = upsRec.EnableMoney; dbRec.EnableTime = upsRec.EnableTime; dbRec.IdxFaseAncest = upsRec.IdxFaseAncest; dbRec.IdxProgetto = upsRec.IdxProgetto; dbRec.NomeFase = upsRec.NomeFase; dbRec.PercOpen = upsRec.PercOpen; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagFasiUpsert:{Environment.NewLine}{exc}"); } } return answ; } /// /// Elenco Giustificativi /// /// public List AnagGiust() { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagGiust .OrderBy(x => x.codGiust) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagGiust:{Environment.NewLine}{exc}"); } } return dbResult; } public List AnagGruppiAll() { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagGruppi .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagGruppiAll:{Environment.NewLine}{exc}"); } } return dbResult; } public List AnagGruppiUser(int idxDipendente) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagGruppi .Where(x => x.Dipendenti2GruppiNav.Contains(new Dipendenti2GruppiModel() { IdxDipendente = idxDipendente, Gruppo = x.Gruppo })) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagGruppiUser:{Environment.NewLine}{exc}"); } } return dbResult; } public List AnagKeyValGetAll() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetAnagKeyValue .ToList(); } return dbResult; } /// /// Dati orario del dipendente indicato /// /// /// public AnagOrariModel AnagOrarioByDip(int idxDip) { AnagOrariModel? dbResult = new AnagOrariModel(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set.... dbResult = localDbCtx .DbSetAnagOrari .Where(x => x.DipendentiNav.Count > 0 && x.DipendentiNav.First().IdxDipendente == idxDip) .FirstOrDefault(); } catch (Exception exc) { Log.Error($"Errore in AnagOrarioByDip:{Environment.NewLine}{exc}"); } } if (dbResult == null) { dbResult = new AnagOrariModel(); } return dbResult; } /// /// Tabella setup dati orari - elenco completo /// /// /// public List AnagOrarioGetAll() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set.... dbResult = localDbCtx .DbSetAnagOrari .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagOrarioGetAll:{Environment.NewLine}{exc}"); } } if (dbResult == null) { dbResult = new List(); } return dbResult; } public List AnagProjAll(bool onlyActive) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set.... dbResult = localDbCtx .DbSetAnagProgetti .Where(x => ((bool)(x.Attivo ?? false) == true && onlyActive == true) || !onlyActive) .Include(c => c.ClienteNav) .Include(g => g.GruppiNav) .OrderBy(x => x.NomeProj) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagProjAll:{Environment.NewLine}{exc}"); } } return dbResult; } public AnagProgettiModel AnagProjByKey(int IdxProj) { // init dati necessari AnagProgettiModel dbResult = new AnagProgettiModel(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set.... var dbRec = localDbCtx .DbSetAnagProgetti .Where(x => (x.IdxProgetto == IdxProj)) .FirstOrDefault(); if (dbRec != null) { dbResult = dbRec; } } catch (Exception exc) { Log.Error($"Errore in AnagProjByKey:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco progetti calcolati con ore avanzamento filtrata /// /// /// /// /// /// /// public List AnagProjCalcFilt(string gruppo, int idxCliente, bool showPrjArch, bool showPrjZeroH, bool showPrjStar) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { var pGruppo = new SqlParameter("@gruppo", gruppo); var pIdxCli = new SqlParameter("@idxCliente", idxCliente); var pShowPAr = new SqlParameter("@showPrjArch", showPrjArch); var pShowPZH = new SqlParameter("@showPrjZeroH", showPrjZeroH); var pShowPSt = new SqlParameter("@showPrjStar", showPrjStar); dbResult = localDbCtx .DbSetCalcOreProj .FromSqlRaw("EXEC stp_AP_Expl_getData @gruppo, @idxCliente, @showPrjArch, @showPrjZeroH, @showPrjStar", pGruppo, pIdxCli, pShowPAr, pShowPZH, pShowPSt) .ToList(); } return dbResult; } public bool AnagProjDelete(AnagProgettiModel remRec) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagProgetti .Where(x => x.IdxProgetto == remRec.IdxProgetto) .FirstOrDefault(); if (dbRec != null) { // controllo se abbia progetti associati var listFasi = localDbCtx .DbSetAnagFasi .Where(x => x.IdxProgetto == remRec.IdxProgetto) .ToList(); // --> in tal caso disattivo... if (listFasi != null && listFasi.Count > 0) { dbRec.Attivo = false; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } else { // altrimenti elimino davvero... localDbCtx .DbSetAnagProgetti .Remove(dbRec); } } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagProjDelete:{Environment.NewLine}{exc}"); } } return answ; } /// /// Clona record Progetti con stored /// /// /// /// /// public bool AnagProjDoClone(int IdxProj, string NewName, bool DoYearSubst) { int result = 0; using (GPWContext localDbCtx = new GPWContext(_configuration)) { var pIdxProj = new SqlParameter("@idxProgetto", IdxProj); var pNomeProj = new SqlParameter("@nomeProj", NewName); var pSostAnnoFasi = new SqlParameter("@sostAnnoFasi", DoYearSubst); result = localDbCtx .Database .ExecuteSqlRaw("EXEC stp_AP_duplicateProj @idxProgetto, @nomeProj, @sostAnnoFasi", pIdxProj, pNomeProj, pSostAnnoFasi); } return result != 0; } /// /// Esegue update calcolo dati proj x tutti (anche archiviati) /// /// public bool AnagProjForceUpdate() { int result = 0; using (GPWContext localDbCtx = new GPWContext(_configuration)) { // imposto default x ricalcolo di tutti int IdxProj = 0; string gruppo = ""; int IdxCli = 0; bool DoArch = true; var pIdxProj = new SqlParameter("@idxProgetto", IdxProj); var pGruppo = new SqlParameter("@gruppo", gruppo); var pIdxCli = new SqlParameter("@idxCliente", IdxCli); var pPrjArch = new SqlParameter("@PrjArch", DoArch); result = localDbCtx .Database .ExecuteSqlRaw("EXEC stp_AP_Summary_Update @idxProgetto, @gruppo, @idxCliente, @PrjArch", pIdxProj, pGruppo, pIdxCli, pPrjArch); } return result != 0; } public bool AnagProjUpdStatus(int IdxProj, bool Attivo, bool Starred) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagProgetti .Where(x => x.IdxProgetto == IdxProj) .FirstOrDefault(); if (dbRec != null) { dbRec.Attivo = Attivo; dbRec.Starred = Starred; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagProjUpdStatus:{Environment.NewLine}{exc}"); } } return answ; } public bool AnagProjUpsert(AnagProgettiModel upsRec) { // init bool answ = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero cliente in primis... var dbRec = localDbCtx .DbSetAnagProgetti .Where(x => x.IdxProgetto == upsRec.IdxProgetto) .FirstOrDefault(); if (dbRec == null) { localDbCtx .DbSetAnagProgetti .Add(upsRec); } else { dbRec.Attivo = upsRec.Attivo; dbRec.Avvio = upsRec.Avvio; dbRec.Chiusura = upsRec.Chiusura; dbRec.CodExt = upsRec.CodExt; dbRec.DescrProj = upsRec.DescrProj; dbRec.Gruppo = upsRec.Gruppo; dbRec.IdxCliente = upsRec.IdxCliente; dbRec.NomeProj = upsRec.NomeProj; dbRec.OldIdx = upsRec.OldIdx; dbRec.Starred = upsRec.Starred; //set modificato localDbCtx.Entry(dbRec).State = EntityState.Modified; } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Errore in AnagProjUpsert:{Environment.NewLine}{exc}"); } } return answ; } /// /// Elenco Tags /// /// public List AnagTagAll() { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagTag .OrderBy(x => x.Descrizione) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagTagAll:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Elenco Tags /// /// public List AnagTagFasiAll() { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... dbResult = localDbCtx .DbSetAnagTagFasi .OrderBy(x => x.Descrizione) .ToList(); } catch (Exception exc) { Log.Error($"Errore in AnagTagFasiAll:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Vista dati per FASE con totalizzaizone ore budget/real /// /// /// public CalcOreFasiModel CalcOreFase(int idxFase) { CalcOreFasiModel dbResult = new CalcOreFasiModel(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { var parIdxFase = new SqlParameter("@idxFaseAnc", idxFase); var rawResult = localDbCtx .DbSetCalcOreFasi .FromSqlRaw("EXEC stp_AF_getByIdxFase_Child @idxFaseAnc", parIdxFase) .ToList(); if (rawResult != null && rawResult.Count > 0) { dbResult = rawResult[0]; } } return dbResult; } /// /// Vista dati per progetto con totalizzaizone ore budget/real /// /// /// public CalcOreProgettiModel CalcOreProj(int idxProj) { CalcOreProgettiModel dbResult = new CalcOreProgettiModel(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { var idxProgetto = new SqlParameter("@idxProgetto", idxProj); var rawResult = localDbCtx .DbSetCalcOreProj .FromSqlRaw("EXEC stp_AP_getByIdxPrj @idxProgetto", idxProgetto) .ToList(); if (rawResult != null && rawResult.Count > 0) { dbResult = rawResult[0]; } } return dbResult; } /// /// Lista chiusure aziendali dato periodo /// /// /// /// public List CalFestFeriePeriodo(DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetCalFesteFerie .Where(x => x.data >= dtStart && x.data <= dtEnd) .OrderBy(x => x.data) .ToList(); } return dbResult; } /// /// Recupera l'elenco dei Rilievi temperatura nel periodo indicato x dipendente /// /// Dipendente interessato /// Data di riferimento (ultima/corrente) /// NUm settimane precedenti da recuperare /// public List CheckVC19List(int idxDipendente, DateTime dtInizio, DateTime dtFine) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetCheckVc19 .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DtCheck && x.DtCheck <= dtFine) .ToList(); } return dbResult; } /// /// Lista configurazione /// /// /// /// public List ConfigGetAll() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetConfig .OrderBy(x => x.chiave) .ToList(); } return dbResult; } /// /// Recupera l'elenco dei dettaglio giornaliero attività di tutti i dipendenti, dato giorno riferimento /// /// Data di riferimento /// public List DailyDetailAllDip(DateTime dtRif) { //resetto valoredtRif a giornata (mezzanotte) dtRif = dtRif.Date; // init dati necessari List dbResult = new List(); List rawTimbExp = new List(); List rawTimbr = new List(); List rawRegAtt = new List(); List rawRilTemp = new List(); List rawCheckC19 = new List(); // fermate/festività/ferie List rawFermateAz = new List(); List rawMalattie = new List(); List rawRichDip = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { // recupero intero set dati timbrature e attività.... rawTimbExp = localDbCtx .DbSetTimbratureExpl .Where(x => dtRif == x.DataLav) .ToList(); rawTimbr = localDbCtx .DbSetTimbrature .Where(x => dtRif == x.DataOra.Date) .ToList(); rawRegAtt = localDbCtx .DbSetRegAttivita .Where(x => dtRif == x.Inizio.Date) .Include(a => a.FasiNav).ThenInclude(p => p.ProgettoNav).ThenInclude(c => c.ClienteNav) .ToList(); rawRilTemp = localDbCtx .DbSetRilievoTemp .Where(x => dtRif <= x.DtRilievo.Date) .ToList(); rawCheckC19 = localDbCtx .DbSetCheckVc19 .Where(x => dtRif <= x.DtCheck.Date) .ToList(); // recupero fermate e malattie/permessi del dipendente... rawFermateAz = localDbCtx .DbSetCalFesteFerie .Where(x => x.data == dtRif) .ToList(); rawMalattie = localDbCtx .DbSetRegMalattie .Where(x => x.DtInizio.Date <= dtRif && x.DtInizio.AddDays(x.NumGG) >= dtRif) .ToList(); rawRichDip = localDbCtx .DbSetRegRichieste .Where(x => x.DtStart.Date <= dtRif && x.DtEnd >= dtRif) .ToList(); // calcolo elenco DIP List listDip = rawTimbExp.Select(x => x.IdxDipendente).Distinct().ToList(); // x ogni giorno scompongo il set di info... foreach (var idxDip in listDip) { DailyDataDTO currDayDto = new DailyDataDTO() { IdxDipendente = idxDip, DtRif = dtRif, ListRA = rawRegAtt .Where(x => x.IdxDipendente == idxDip) .OrderBy(x => x.Inizio) .ToList(), ListTimbr = rawTimbr .Where(x => x.IdxDipendente == idxDip) .OrderBy(x => x.DataOra) .ToList(), TimbrExpl = rawTimbExp .Where(x => x.IdxDipendente == idxDip) .FirstOrDefault(), ListRilTemp = rawRilTemp .Where(x => x.IdxDipendente == idxDip) .ToList(), ListCheckC19 = rawCheckC19 .Where(x => x.IdxDipendente == idxDip) .ToList(), ListFermateAzienda = rawFermateAz, ListMalattie = rawMalattie .Where(x => x.IdxDipendente == idxDip) .ToList(), ListFerieDip = rawRichDip .Where(x => x.IdxDipendente == idxDip) .ToList(), ListRichiesteDip = rawRichDip .Where(x => x.IdxDipendente == idxDip) .ToList() }; dbResult.Add(currDayDto); } } return dbResult; } /// /// Recupera l'elenco dei dettagli giornalieri attività di un dipendente, dato periodo riferimento /// /// Dipendente interessato /// Data di riferimento (ultima/corrente) /// NUm settimane precedenti da recuperare /// public List DailyDetails(int idxDipendente, DateTime dtInizio, DateTime dtFine) { // init dati necessari List dbResult = new List(); List rawTimbExp = new List(); List rawTimbr = new List(); List rawRegAtt = new List(); List rawRilTemp = new List(); List rawCheckC19 = new List(); // fermate/festività/ferie List rawFermateAz = new List(); List rawMalattie = new List(); List rawRichDip = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { // recupero intero set dati timbrature e attività.... rawTimbExp = localDbCtx .DbSetTimbratureExpl .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataLav && x.DataLav <= dtFine) .ToList(); rawTimbr = localDbCtx .DbSetTimbrature .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataOra && x.DataOra <= dtFine.AddDays(1)) .ToList(); rawRegAtt = localDbCtx .DbSetRegAttivita .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.Inizio && x.Inizio <= dtFine.AddDays(1)) .Include(a => a.FasiNav).ThenInclude(p => p.ProgettoNav).ThenInclude(c => c.ClienteNav) .ToList(); rawRilTemp = localDbCtx .DbSetRilievoTemp .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DtRilievo && x.DtRilievo <= dtFine.AddDays(1)) .ToList(); rawCheckC19 = localDbCtx .DbSetCheckVc19 .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DtCheck && x.DtCheck <= dtFine.AddDays(1)) .ToList(); // recupero fermate e malattie/permessi del dipendente... rawFermateAz = localDbCtx .DbSetCalFesteFerie .Where(x => x.data >= dtInizio && x.data <= dtFine) .ToList(); rawMalattie = localDbCtx .DbSetRegMalattie .Where(x => (x.DtInizio >= dtInizio && x.DtInizio <= dtFine) || (x.DtInizio.AddDays(x.NumGG) >= dtInizio.AddDays(x.NumGG) && x.DtInizio <= dtFine)) .ToList(); rawRichDip = localDbCtx .DbSetRegRichieste .Where(x => (idxDipendente == 0 || x.IdxDipendente == idxDipendente) && ((x.DtStart >= dtInizio && x.DtStart <= dtFine) || (x.DtStart <= dtInizio && dtFine <= x.DtEnd) || (x.DtEnd >= dtInizio && x.DtEnd <= dtFine))) .ToList(); // calcolo intervallo date... int numDays = dtFine.Subtract(dtInizio).Days; // x ogni giorno scompongo il set di info... for (int i = 0; i <= numDays; i++) { DateTime thisDay = dtInizio.AddDays(i); DailyDataDTO currDayDto = new DailyDataDTO() { IdxDipendente = idxDipendente, DtRif = thisDay, ListRA = rawRegAtt .Where(x => thisDay <= x.Inizio && x.Inizio < thisDay.AddDays(1)) .OrderBy(x => x.Inizio) .ToList(), ListTimbr = rawTimbr .Where(x => thisDay <= x.DataOra && x.DataOra < thisDay.AddDays(1)) .OrderBy(x => x.DataOra) .ToList(), TimbrExpl = rawTimbExp .Where(x => thisDay == x.DataLav) .FirstOrDefault(), ListRilTemp = rawRilTemp .Where(x => thisDay <= x.DtRilievo && x.DtRilievo < thisDay.AddDays(1)) .ToList(), ListCheckC19 = rawCheckC19 .Where(x => thisDay <= x.DtCheck && x.DtCheck < thisDay.AddDays(1)) .ToList(), ListFermateAzienda = rawFermateAz .Where(x => thisDay == x.data) .ToList(), ListMalattie = rawMalattie .Where(x => thisDay >= x.DtInizio && thisDay <= x.DtInizio.AddDays(x.NumGG)) .ToList(), ListFerieDip = rawRichDip .Where(x => x.CodGiust == "FER" && (thisDay >= x.DtStart && thisDay <= x.DtEnd)) .ToList(), ListRichiesteDip = rawRichDip .Where(x => x.CodGiust != "FER" && (thisDay.Date == x.DtStart.Date)) .ToList() }; dbResult.Add(currDayDto); } } return dbResult; } public bool DbForceMigrate() { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { localDbCtx.DbForceMigrate(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in DbForceMigrate{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera record anomalie dipendenti /// /// /// /// /// /// /// public List DipAndAnomGetAll(DateTime inizio, DateTime fine, bool notOkApp, bool notOkTim, bool notOkLav) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { SqlParameter parInizio = new SqlParameter("@inizio", inizio); SqlParameter parFine = new SqlParameter("@fine", fine); SqlParameter parNotOkApp = new SqlParameter("@notOkApp", notOkApp); SqlParameter parNotOkTim = new SqlParameter("@notOkTim", notOkTim); SqlParameter parNotOkLav = new SqlParameter("@notOkLav", notOkLav); var rawResult = localDbCtx .DbSetDipAndAnom .FromSqlRaw("EXEC stp_DipendentiAndAnomalie @inizio,@fine,@notOkApp,@notOkTim,@notOkLav", parInizio, parFine, parNotOkApp, parNotOkTim, parNotOkLav) .ToList(); if (rawResult == null) { dbResult = new List(); } else { dbResult = rawResult; } } return dbResult; } public List Dipendenti2RuoliGetAll() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetDip2Ruoli .Include(o => o.DipNav) .ToList(); } return dbResult; } public List DipendentiGetAll() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetDipendenti .Include(o => o.OrarioNav) .ToList(); } return dbResult; } public bool DipendentiUpdate(DipendentiModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { if (currItem.IdxDipendente > 0) { var currRec = localDbCtx .DbSetDipendenti .FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente); if (currRec != null) { currRec.Cognome = currItem.Cognome; currRec.Nome = currItem.Nome; currRec.AuthKey = currItem.AuthKey; currRec.Cf = currItem.Cf; currRec.Email = currItem.Email; currRec.DataNascita = currItem.DataNascita; currRec.LuogoNascita = currItem.LuogoNascita; localDbCtx .DbSetDipendenti .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetDipendenti .Update(currItem); } } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in DipendentiUpdate{Environment.NewLine}{exc}"); } } return answ; } public void Dispose() { Log.Info("Dispose di GPWController"); } /// /// Imposta stato di una fase /// /// ID /// /// Stato attivo true/false (se è amster -_> aggiorna sottofasi x trigger su DB /// /// public async Task FasiSetActive(int idxFase, bool isActive) { // init dati necessari bool fatto = false; // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // recupero intero set... var dbResult = localDbCtx .DbSetAnagFasi .Where(x => x.IdxFase == idxFase) .FirstOrDefault(); if (dbResult != null) { dbResult.Attivo = isActive; await localDbCtx.SaveChangesAsync(); fatto = true; } } catch (Exception exc) { Log.Error($"Errore in FasiSetActive:{Environment.NewLine}{exc}"); } } await Task.Delay(1); return fatto; } public List GetChecksVC19(int maxNum) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { int numRec = localDbCtx .DbSetCheckVc19 .Count(); // se ho meno record che quelli richiesti --> riduco maxNum = numRec > maxNum ? maxNum : numRec; dbResult = localDbCtx .DbSetCheckVc19 .OrderByDescending(x => x.DtCheck) .Take(maxNum) .ToList(); } return dbResult; } public List GetChecksVC19Filt(int idxDip, DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetCheckVc19 .Where(x => (idxDip == 0 || x.IdxDipendente == idxDip) && (x.DtCheck >= dtStart && x.DtCheck <= dtEnd)) .OrderByDescending(x => x.DtCheck) .ToList(); } return dbResult; } public bool InsertCheck(DCCDecode updItem, string clientIp) { bool done = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { int idxDip = 0; DipendentiModel? currUser = new DipendentiModel() { IdxDipendente = 0 }; // per prima cosa cerca il dipendente x cognome/nome... var userList = localDbCtx .DbSetDipendenti .Where(x => x.Cognome.ToUpper() == updItem.nam.fn.ToUpper() && x.Nome.ToUpper() == updItem.nam.gn.ToUpper()) .ToList(); // se ne ho solo 1 ok... if (userList.Count > 1) { currUser = userList .Where(x => x.DataNascita != null && updItem.dob.Date == ((DateTime)x.DataNascita).Date) .FirstOrDefault(); } // altrimenti cerco anche con DOB... else if (userList.Count == 1) { // se lo trova inserisce check... currUser = userList[0]; } if (currUser != null) { // fisso dipendente idxDip = currUser.IdxDipendente; } string rawPayload = JsonConvert.SerializeObject(updItem); CheckVc19Model newItem = new CheckVc19Model() { IdxDipendente = idxDip, DtCheck = DateTime.Now, Cognome = updItem.nam.fn.ToUpper(), Nome = updItem.nam.gn.ToUpper(), Payload = rawPayload, Dob = updItem.dob.Date }; // aggiungo! localDbCtx .DbSetCheckVc19 .Add(newItem); // ora salvo! localDbCtx.SaveChanges(); // se trovato procedo ANCHE x timbratura if (idxDip > 0) { // verifico se ho timbratura ingresso var listTimbra = localDbCtx .DbSetTimbrature .Where(x => x.IdxDipendente == idxDip && x.DataOra.Date == DateTime.Today && x.Entrata == true) .ToList(); // se non ci fosse aggiungo if (listTimbra == null || listTimbra.Count == 0) { var newIngresso = new TimbratureModel() { IdxDipendente = idxDip, DataOra = DateTime.Now, CodTipoTimb = "Web", Ipv4 = clientIp, Entrata = true, Approv = true }; // aggiungo! localDbCtx .DbSetTimbrature .Add(newIngresso); } // ora salvo! localDbCtx.SaveChanges(); done = true; } // altrimenti NON FA NULLA else { Log.Info($"Attenzione, dipendente non trovato: {updItem.nam.fn} {updItem.nam.gn}"); } } catch (Exception exc) { Log.Error($"Eccezione in InsertCheck:{Environment.NewLine}{exc}"); } } return done; } public bool InsertManual(int idxDip, string clientIp) { bool done = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { // se trovato procedo if (idxDip > 0) { string rawPayload = "MANUAL"; CheckVc19Model newItem = new CheckVc19Model() { IdxDipendente = idxDip, DtCheck = DateTime.Now, Payload = rawPayload }; // aggiungo! localDbCtx .DbSetCheckVc19 .Add(newItem); // ora salvo! localDbCtx.SaveChanges(); done = true; } // altrimenti NON FA NULLA else { Log.Info($"Attenzione, dipendente non trovato: {idxDip}"); } } catch (Exception exc) { Log.Error($"Eccezione in InsertManual:{Environment.NewLine}{exc}"); } } return done; } /// /// Elenco pareto progetti ordinati da filtro /// /// /// /// /// /// public List ParetoRegAtt(int idxDip, DateTime dataStart, DateTime dataEnd, int maxResult) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { var idxDipendente = new SqlParameter("@idxDipendente", idxDip); var inizio = new SqlParameter("@inizio", dataStart); var fine = new SqlParameter("@fine", dataEnd); var maxRes = new SqlParameter("@maxRes", maxResult); dbResult = localDbCtx .DbSetParetoRegAtt .FromSqlRaw("EXEC stp_freqProjByDipPeriodo @idxDipendente, @inizio,@fine,@maxRes", idxDipendente, inizio, fine, maxRes) .ToList(); } return dbResult; } /// /// Righe presenze giornaliere /// /// DataOra riferimento per richiesta presenze /// public List PresenzeDay(DateTime dtRif) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { var idxProgetto = new SqlParameter("@dataRif", dtRif); dbResult = localDbCtx .DbSetStatsDayPres .FromSqlRaw("EXEC stp_statsDayPres @dataRif", idxProgetto) .ToList(); } return dbResult; } public bool RegAttDelete(RegAttivitaModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { localDbCtx.Remove(currItem); localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegAttUpdate{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera ultima attività dipendente /// /// /// cerca ultima solo tra attive (=true) o tutte (=false) /// public RegAttivitaModel RegAttLastByDip(int IdxDipendente, bool onlyActive) { RegAttivitaModel? dbResult = new RegAttivitaModel(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { dbResult = localDbCtx .DbSetRegAttivita .Where(x => x.FasiNav.Attivo || !onlyActive) .OrderByDescending(x => x.Inizio) .FirstOrDefault(x => x.IdxDipendente == IdxDipendente); } catch (Exception exc) { Log.Error($"Eccezione in RegAttLastByDip{Environment.NewLine}{exc}"); } if (dbResult == null) { dbResult = new RegAttivitaModel(); } } if (dbResult == null) { dbResult = new RegAttivitaModel(); } return dbResult; } /// /// Insert/Update attività /// /// /// public bool RegAttUpdate(RegAttivitaModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { if (currItem.IdxRa == 0) { localDbCtx.Entry(currItem).State = EntityState.Added; } else { var currRec = localDbCtx .DbSetRegAttivita .FirstOrDefault(x => x.IdxRa == currItem.IdxRa); if (currRec != null) { currRec.IdxDipendente = currItem.IdxDipendente; currRec.IdxFase = currItem.IdxFase; currRec.Inizio = currItem.Inizio; currRec.Fine = currItem.Fine; currRec.Descrizione = currItem.Descrizione; localDbCtx.Entry(currRec).State = EntityState.Modified; localDbCtx .DbSetRegAttivita .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetRegAttivita .Update(currItem); } } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegAttUpdate{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera eventi dato criteri filtro /// /// /// /// /// public List RegEventiGetFilt(DateTime inizio, DateTime fine, string evento) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetRegEventi .Where(x => x.DataOra >= inizio && x.DataOra <= fine && (string.IsNullOrEmpty(evento) || x.Evento == evento)) .ToList(); } return dbResult; } /// /// Insert/Update di un record in Registro Eventi /// /// /// public bool RegEventiUpdate(RegistroEventiModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegEventi .FirstOrDefault(x => x.DataOra == currItem.DataOra && x.Evento == currItem.Evento); if (currRec != null) { currRec.Evento = currItem.Evento; currRec.Commento = currItem.Commento; localDbCtx .DbSetRegEventi .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetRegEventi .Add(currItem); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegEventiUpdate{Environment.NewLine}{exc}"); } } return answ; } /// /// Elimina record registro malattie (SE non confermato) /// /// public bool RegMalattieDelete(RegMalattieModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegMalattie .FirstOrDefault(x => x.IdxRegMal == currItem.IdxRegMal && !x.Conf); if (currRec != null) { localDbCtx .DbSetRegMalattie .Remove(currRec); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegMalattieDelete{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera elenco reg malattie (tutte, dato max dalla + recente) /// /// /// public List RegMalattieGetAll(int maxRecord) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetRegMalattie .Include(d => d.DipNav) .OrderByDescending(x => x.DtInizio) .Take(maxRecord) .ToList(); } return dbResult; } /// /// recupera elenco reg malattie del dipendente /// /// /// /// /// public List RegMalattieGetByDip(int idxDipendente, int maxRecord) { List dbResult = new List(); if (idxDipendente > 0) { using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetRegMalattie .Where(x => x.IdxDipendente == idxDipendente) .Include(d => d.DipNav) .OrderByDescending(x => x.DtInizio) .Take(maxRecord) .ToList(); } } return dbResult; } /// /// Recupera elenco reg malattie dato periodo /// /// /// /// public List RegMalattieGetByPeriod(DateTime DtFrom, DateTime DtTo) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetRegMalattie .Where(x => (x.DtInizio.AddDays(x.NumGG) >= DtFrom && x.DtInizio <= DtTo)) .Include(d => d.DipNav) .OrderByDescending(x => x.DtInizio) .ToList(); } return dbResult; } /// /// Inserisce/Aggiorna record registro malattie /// /// public bool RegMalattieUpsert(RegMalattieModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegMalattie .FirstOrDefault(x => x.IdxRegMal == currItem.IdxRegMal); if (currRec != null) { // aggiorno i campi currRec.IdxDipendente = currItem.IdxDipendente; currRec.DtInizio = currItem.DtInizio; currRec.NumGG = currItem.NumGG; currRec.CodCert = currItem.CodCert ?? "ND"; currRec.Conf = currItem.Conf; localDbCtx .DbSetRegMalattie .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetRegMalattie .Add(currItem); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegMalattieUpsert{Environment.NewLine}{exc}"); } } return answ; } /// /// Aggiorna record registro richieste approvando la richiesta /// /// public bool RegRichiesteApprova(RegRichiesteModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegRichieste .FirstOrDefault(x => x.IdxRegRich == currItem.IdxRegRich); if (currRec != null) { currRec.Conf = true; localDbCtx .DbSetRegRichieste .Update(currRec); localDbCtx.SaveChanges(); answ = true; } } catch (Exception exc) { Log.Error($"Eccezione in RegRichiesteApprova{Environment.NewLine}{exc}"); } } return answ; } /// /// Elimina record registro richieste (SE non confermato) /// /// public bool RegRichiesteDelete(RegRichiesteModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegRichieste .FirstOrDefault(x => x.IdxRegRich == currItem.IdxRegRich && !x.Conf); if (currRec != null) { localDbCtx .DbSetRegRichieste .Remove(currRec); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegRichiesteDelete{Environment.NewLine}{exc}"); } } return answ; } /// /// recupera elenco reg richieste del dipendente /// /// /// Inizio periodo richiesto /// Fine periodo richiesto /// /// public List RegRichiesteGetByDip(int idxDipendente, DateTime dtFrom, DateTime dtTo) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { DateTime oggi = DateTime.Today; dbResult = localDbCtx .DbSetRegRichieste .Where(x => (idxDipendente == 0 || x.IdxDipendente == idxDipendente) && (x.DtStart >= dtFrom && x.DtStart <= dtTo)) .Include(d => d.DipNav) .OrderByDescending(x => x.DtStart) .ToList(); } return dbResult; } /// /// Inserisce/Aggiorna record registro richieste /// /// public bool RegRichiesteUpsert(RegRichiesteModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRegRichieste .FirstOrDefault(x => x.IdxRegRich == currItem.IdxRegRich); if (currRec != null) { // aggiorno i campi currRec.IdxDipendente = currItem.IdxDipendente; currRec.DtStart = currItem.DtStart; currRec.DtEnd = currItem.DtEnd; currRec.CodGiust = currItem.CodGiust; currRec.Note = currItem.Note ?? ""; currRec.Conf = currItem.Conf; localDbCtx .DbSetRegRichieste .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetRegRichieste .Add(currItem); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RegRichiesteUpsert{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera l'elenco dei dettagli attività e timbrature di un dipendente, dato periodo riferimento /// /// Dipendente interessato /// Periodo di riferimento /// public ReportDataDTO ReportDetails(int idxDipendente, DtUtils.Periodo periodo) { // init dati necessari DateTime dtInizio = periodo.Inizio; DateTime dtFine = periodo.Fine; ReportDataDTO dbResult = new ReportDataDTO(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { // recupero intero set dati timbrature e attività.... dbResult.TimbrExpl = localDbCtx .DbSetTimbratureExpl .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataLav && x.DataLav <= dtFine) .ToList(); dbResult.ListTimbr = localDbCtx .DbSetTimbrature .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DataOra && x.DataOra <= dtFine.AddDays(1)) .ToList(); dbResult.ListRA = localDbCtx .DbSetRegAttivita .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.Inizio && x.Inizio <= dtFine.AddDays(1)) .Include(a => a.FasiNav).ThenInclude(p => p.ProgettoNav).ThenInclude(c => c.ClienteNav) .ToList(); // effettuo calcoli! dbResult.DoCalc(); } return dbResult; } /// /// Recupera l'elenco dei Rilievi temperatura nel periodo indicato x dipendente /// /// Dipendente interessato /// Data di riferimento (ultima/corrente) /// NUm settimane precedenti da recuperare /// public List RilTempList(int idxDipendente, DateTime dtInizio, DateTime dtFine) { // init dati necessari List dbResult = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { dbResult = localDbCtx .DbSetRilievoTemp .Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DtRilievo && x.DtRilievo <= dtFine) .ToList(); } return dbResult; } public bool RilTempUpdate(RilievoTempModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetRilievoTemp .FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente && x.DtRilievo == currItem.DtRilievo); if (currRec != null) { // aggiorno solo entrata/uscita currRec.TempRil = currItem.TempRil; localDbCtx .DbSetRilievoTemp .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetRilievoTemp .Add(currItem); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in RilTempUpdate{Environment.NewLine}{exc}"); } } return answ; } /// /// Annulla modifiche su una specifica entity (cancel update) /// /// /// public bool rollBackEntity(object item) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { if (localDbCtx.Entry(item).State == EntityState.Deleted || localDbCtx.Entry(item).State == EntityState.Modified) { localDbCtx.Entry(item).Reload(); } } catch (Exception exc) { Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera record anomalie dipendenti /// /// singolo idx oppure 0 = tutti /// /// /// /// /// /// /// public List TeRaExplGetAll(int idxDipendente, DateTime dataFrom, DateTime dataTo, bool inatt, bool showWE, int maxErrMin, int maxErrPlus) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { SqlParameter parIdxDip = new SqlParameter("@idxDipendente", idxDipendente); SqlParameter parDataFrom = new SqlParameter("@dataFrom", dataFrom); SqlParameter parDataTo = new SqlParameter("@dataTo", dataTo); SqlParameter parInatt = new SqlParameter("@parInatt", inatt); SqlParameter parShowWE = new SqlParameter("@showWE", showWE); SqlParameter parMaxErrMin = new SqlParameter("@maxErrMin", maxErrMin); SqlParameter parMaxErrPlus = new SqlParameter("@maxErrPlus", maxErrPlus); dbResult = localDbCtx .DbSetTeRaExpl .FromSqlRaw("EXEC stp_TE_RA_ByUserDate @idxDipendente,@dataFrom,@dataTo,@parInatt,@showWE,@maxErrMin,@maxErrPlus", parIdxDip, parDataFrom, parDataTo, parInatt, parShowWE, parMaxErrMin, parMaxErrPlus) .ToList(); } return dbResult; } /// /// Recupera record anomalie dipendenti /// /// singolo idx oppure 0 = tutti /// /// /// /// /// /// public List TimbExplGetAnomalie(int idxDipendente, DateTime inizio, DateTime fine, bool notOkApp, bool notOkTim, bool notOkLav) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { SqlParameter parIdxDip = new SqlParameter("@idxDipendente", idxDipendente); SqlParameter parInizio = new SqlParameter("@inizio", inizio); SqlParameter parFine = new SqlParameter("@fine", fine); SqlParameter parNotOkApp = new SqlParameter("@notOkApp", notOkApp); SqlParameter parNotOkTim = new SqlParameter("@notOkTim", notOkTim); SqlParameter parNotOkLav = new SqlParameter("@notOkLav", notOkLav); dbResult = localDbCtx .DbSetTimbratureExpl .FromSqlRaw("EXEC stp_timbratureExpl_getByAnomalia @idxDipendente,@inizio,@fine,@notOkApp,@notOkTim,@notOkLav", parIdxDip, parInizio, parFine, parNotOkApp, parNotOkTim, parNotOkLav) .ToList(); } return dbResult; } /// /// Recupera record anomalie dipendenti x orario continuato /// /// singolo idx oppure 0 = tutti /// /// /// public List TimbExplGetContinuato(int idxDipendente, DateTime inizio, DateTime fine) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { SqlParameter parIdxDip = new SqlParameter("@idxDipendente", idxDipendente); SqlParameter parInizio = new SqlParameter("@inizio", inizio); SqlParameter parFine = new SqlParameter("@fine", fine); dbResult = localDbCtx .DbSetTimbratureExpl .FromSqlRaw("EXEC stp_timbratureExpl_getContinuato @idxDipendente,@inizio,@fine", parIdxDip, parInizio, parFine) .ToList(); } return dbResult; } /// /// Elenco timbrature successive alla data odierna x ogni dipendente (x recupero errori /// timbrature future) /// - per il check imposta la mezzanotte del giorno successivo /// /// public List TimbratureAnomalieFuture() { // imposta data a domani DateTime dateRif = DateTime.Today.AddDays(1); List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { dbResult = localDbCtx .DbSetTimbrature .Where(x => x.DataOra.Date >= dateRif.Date) .OrderBy(x => x.IdxDipendente) .ThenByDescending(x => x.DataOra) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione in TimbratureAnomalieFuture{Environment.NewLine}{exc}"); } } return dbResult; } public List TimbratureDay(DateTime dateRif, int IdxDip) { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { dbResult = localDbCtx .DbSetTimbrature .Where(x => x.DataOra.Date == dateRif.Date && x.IdxDipendente == IdxDip) .OrderByDescending(x => x.DataOra) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione in TimbratureDay{Environment.NewLine}{exc}"); } } return dbResult; } public bool TimbratureDelete(TimbratureModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { localDbCtx.Remove(currItem); localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in TimbratureDelete{Environment.NewLine}{exc}"); } } return answ; } /// /// Elenco di tutte le timbrature richieste (= NON approvate) /// /// public List TimbratureRichieste() { List dbResult = new List(); using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { dbResult = localDbCtx .DbSetTimbrature .Where(x => x.Approv == false) .Include(d => d.DipNav) .OrderByDescending(x => x.DataOra) .ToList(); } catch (Exception exc) { Log.Error($"Eccezione in TimbratureRichieste{Environment.NewLine}{exc}"); } } return dbResult; } public bool TimbratureUpdate(TimbratureModel currItem) { bool answ = false; using (GPWContext localDbCtx = new GPWContext(_configuration)) { try { var currRec = localDbCtx .DbSetTimbrature .FirstOrDefault(x => x.IdxDipendente == currItem.IdxDipendente && x.DataOra == currItem.DataOra); if (currRec != null) { // aggiorno solo entrata/uscita currRec.Entrata = currItem.Entrata; currRec.Approv = currItem.Approv; currRec.Ipv4 = currItem.Ipv4; currRec.DataOra = currItem.DataOra; localDbCtx .DbSetTimbrature .Update(currRec); } // altrimenti aggiungo else { localDbCtx .DbSetTimbrature .Add(currItem); } localDbCtx.SaveChanges(); answ = true; } catch (Exception exc) { Log.Error($"Eccezione in TimbratureUpdate{Environment.NewLine}{exc}"); } } return answ; } /// /// Recupera l'elenco delle ultime attività inserite da un dipendente /// /// Dipendente interessato /// Num record da recuperare /// public List UserLastRec(int idxDipendente, int numRec) { List listRec = new List(); // cerco su DB recuperando set di dati.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { listRec = localDbCtx .DbSetRegAttivita .Where(x => x.IdxDipendente == idxDipendente) .Include(a => a.FasiNav).ThenInclude(p => p.ProgettoNav).ThenInclude(c => c.ClienteNav) .OrderByDescending(x => x.IdxRa) .Take(numRec) .AsNoTracking() .ToList(); } return listRec; } /// /// Recupera overview di un periodo settimanale x dipendente, data riferimento, numero /// settimane precedenti /// /// Dipendente interessato /// Data di riferimento (ultima/corrente) /// NUm settimane precedenti da recuperare /// public List WeekOverview(int idxDipendente, DateTime dtRif, int numWeek) { // init dati necessari List dbResult = new List(); List weekList = new List(); // aggiungo sett corrente + quelle richieste... for (int i = numWeek; i >= 0; i--) { var currWeek = new WeekData(dtRif.AddDays(-i * 7)); weekList.Add(currWeek); } // cerco su DB settimana x settimana.... using (GPWContext localDbCtx = new GPWContext(_configuration)) { foreach (var item in weekList) { var totOreTim = localDbCtx .DbSetTimbratureExpl .Where(x => x.IdxDipendente == idxDipendente && x.DataLav >= item.inizio && x.DataLav <= item.fine) .Sum(x => (double)x.HLav); var totMinLav = localDbCtx .DbSetRegAttivitaExpl .Where(x => x.IdxDipendente == idxDipendente && x.DataLav >= item.inizio && x.DataLav <= item.fine) .Sum(x => (int)x.MinRegAtt); // aggiungo alla lista... var weekData = new WeekStatDTO() { IdxDipendente = idxDipendente, Anno = item.anno, WeekNumber = item.weekNumber, Inizio = item.inizio, Fine = item.fine, SumOreLav = totOreTim, SumOreComm = (double)totMinLav / 60, }; dbResult.Add(weekData); } } return dbResult; } #endregion Public Methods #region Private Fields private static IConfiguration _configuration = null!; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }