607 lines
23 KiB
C#
607 lines
23 KiB
C#
using GPW.CORE.Data.DbModels;
|
|
using GPW.CORE.Data.DTO;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
|
|
namespace GPW.CORE.Data.Controllers
|
|
{
|
|
public class GPWController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public GPWController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco Clienti (tutti)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<AnagClientiModel> AnagClientiAll()
|
|
{
|
|
// init dati necessari
|
|
List<AnagClientiModel> dbResult = new List<AnagClientiModel>();
|
|
// 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco delle fasi (tutte)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<AnagFasiModel> AnagFasiAll()
|
|
{
|
|
// init dati necessari
|
|
List<AnagFasiModel> dbResult = new List<AnagFasiModel>();
|
|
// cerco su DB recuperando set di dati....
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
// recupero intero set...
|
|
dbResult = localDbCtx
|
|
.DbSetAnagFasi
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in AnagFasiAll:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<AnagGruppiModel> AnagGruppiAll()
|
|
{
|
|
// init dati necessari
|
|
List<AnagGruppiModel> dbResult = new List<AnagGruppiModel>();
|
|
// 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<AnagProgettiModel> AnagProjAll()
|
|
{
|
|
// init dati necessari
|
|
List<AnagProgettiModel> dbResult = new List<AnagProgettiModel>();
|
|
// cerco su DB recuperando set di dati....
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
// recupero intero set....
|
|
dbResult = localDbCtx
|
|
.DbSetAnagProgetti
|
|
.Include(c => c.ClienteNav)
|
|
.OrderBy(x => x.NomeProj)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in AnagProjAll:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vista dati per progetto con totalizzaizone ore budget/real
|
|
/// </summary>
|
|
/// <param name="idxProj"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei Rilievi temperatura nel periodo indicato x dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<CheckVc19Model> CheckVC19List(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
// init dati necessari
|
|
List<CheckVc19Model> dbResult = new List<CheckVc19Model>();
|
|
// 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei dettagli giornalieri attività di un dipendente, dato periodo riferimento
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<DailyDataDTO> DailyDetails(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
// init dati necessari
|
|
List<DailyDataDTO> dbResult = new List<DailyDataDTO>();
|
|
List<TimbratureExplModel> rawTimbExp = new List<TimbratureExplModel>();
|
|
List<TimbratureModel> rawTimbr = new List<TimbratureModel>();
|
|
List<RegAttivitaModel> rawRegAtt = new List<RegAttivitaModel>();
|
|
List<RilievoTempModel> rawRilTemp = new List<RilievoTempModel>();
|
|
List<CheckVc19Model> rawCheckC19 = new List<CheckVc19Model>();
|
|
|
|
// 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)
|
|
.ToList();
|
|
|
|
rawRegAtt = localDbCtx
|
|
.DbSetRegAttivita
|
|
.Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.Inizio && x.Inizio <= dtFine)
|
|
.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)
|
|
.ToList();
|
|
|
|
rawCheckC19 = localDbCtx
|
|
.DbSetCheckVc19
|
|
.Where(x => x.IdxDipendente == idxDipendente && dtInizio <= x.DtCheck && x.DtCheck <= 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))
|
|
.ToList(),
|
|
ListTimbr = rawTimbr
|
|
.Where(x => thisDay <= x.DataOra && x.DataOra <= thisDay.AddDays(1))
|
|
.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()
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
public List<DipendentiModel> DipendentiGetAll()
|
|
{
|
|
List<DipendentiModel> dbResult = new List<DipendentiModel>();
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetDipendenti
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<AnagKeyValueModel> AnagKeyValGetAll()
|
|
{
|
|
List<AnagKeyValueModel> dbResult = new List<AnagKeyValueModel>();
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetAnagKeyValue
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Log.Info("Dispose di GPWController");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco pareto progetti ordinati da filtro
|
|
/// </summary>
|
|
/// <param name="idxDip"></param>
|
|
/// <param name="dataStart"></param>
|
|
/// <param name="dataEnd"></param>
|
|
/// <param name="maxResult"></param>
|
|
/// <returns></returns>
|
|
public List<ParetoRegAttModel> ParetoRegAtt(int idxDip, DateTime dataStart, DateTime dataEnd, int maxResult)
|
|
{
|
|
List<ParetoRegAttModel> dbResult = new List<ParetoRegAttModel>();
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public RegAttivitaModel RegAttLastByDip(int IdxDipendente)
|
|
{
|
|
RegAttivitaModel dbResult = new RegAttivitaModel();
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetRegAttivita
|
|
.OrderByDescending(x => x.IdxRa)
|
|
.FirstOrDefault(x => x.IdxDipendente == IdxDipendente);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RegAttLastByDip{Environment.NewLine}{exc}");
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new RegAttivitaModel();
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public bool RegAttUpdate(RegAttivitaModel currItem)
|
|
{
|
|
bool answ = false;
|
|
using (GPWContext localDbCtx = new GPWContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
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
|
|
.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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco dei Rilievi temperatura nel periodo indicato x dipendente
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtInizio">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="dtFine">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<RilievoTempModel> RilTempList(int idxDipendente, DateTime dtInizio, DateTime dtFine)
|
|
{
|
|
// init dati necessari
|
|
List<RilievoTempModel> dbResult = new List<RilievoTempModel>();
|
|
// 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Annulla modifiche su una specifica entity (cancel update)
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera overview di un periodo settimanale x dipendente, data riferimento, numero settimane precedenti
|
|
/// </summary>
|
|
/// <param name="idxDipendente">Dipendente interessato</param>
|
|
/// <param name="dtRif">Data di riferimento (ultima/corrente)</param>
|
|
/// <param name="numWeek">NUm settimane precedenti da recuperare</param>
|
|
/// <returns></returns>
|
|
public List<WeekStatDTO> WeekOverview(int idxDipendente, DateTime dtRif, int numWeek)
|
|
{
|
|
// init dati necessari
|
|
List<WeekStatDTO> dbResult = new List<WeekStatDTO>();
|
|
List<WeekData> weekList = new List<WeekData>();
|
|
|
|
// aggiungo sett corrente + quelle richeiste...
|
|
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
|
|
}
|
|
} |