using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MP.Data.DatabaseModels; using NLog; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MP.Data.Controllers { public class MpInveController : IDisposable { #region Public Constructors public MpInveController(IConfiguration configuration) { _configuration = configuration; Log.Info("Avviata classe MpInveController"); } #endregion Public Constructors #region Public Methods public void Dispose() { _configuration = null; } #region gestione lotti esterni /// /// Elenco lotti esterni presenti sul db di ARCA /// /// Codice articolo /// Codice lotto /// Codice magazzino /// public List ListLottiEsterni(string codArt, string codLotto, string codMagazzino) { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { var DataGiac = new SqlParameter("@DataGiac", DateTime.Now); var CodArt = new SqlParameter("@CodArt", codArt); var CodLotto = new SqlParameter("@CodLotto", codLotto); var CodMagaz = new SqlParameter("@CodMagaz", codMagazzino); var OnlyTest = new SqlParameter("@OnlyTest", false); dbResult = dbCtx .DbLottoArca .FromSqlRaw("exec dbo.stp_GIAC_getByDate @DataGiac,@CodArt,@CodLotto,@CodMagaz,@OnlyTest", DataGiac, CodArt, CodLotto, CodMagaz, OnlyTest) .AsNoTracking() //.AsEnumerable() .ToList(); } return dbResult; } #endregion gestione lotti esterni #region gestione articoli /// /// articolo MAG corrispondente all' articolo selezionato /// /// /// public AnagArticoli_MAG artBySearch(string artSearch) { AnagArticoli_MAG dbResult = new AnagArticoli_MAG(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbArtMag .AsNoTracking() .Where(x => x.CodArt == artSearch) .FirstOrDefault(); } return dbResult; } #endregion #region gestione config /// /// Elenco da tabella Config /// /// public List ConfigGetAll() { List dbResult = new List(); using (var dbCtx = new MoonProContext(_configuration)) { dbResult = dbCtx .DbSetConfig .AsNoTracking() .OrderBy(x => x.Chiave) .ToList(); } return dbResult; } #endregion gestione config #region gestione scansioni /// /// Elenco Scansioni Totali /// /// public List ScanList() { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbScanData .AsNoTracking() .OrderByDescending(x => x.DtScan) .ToList(); } return dbResult; } #if false /// /// Elenco Scansioni Totali /// /// /// public List ScanBySession(int InveSessId) { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbScanData .AsNoTracking() .Where(x => x.InveSessID == InveSessId) .OrderByDescending(x => x.DtScan) .ToList(); } return dbResult; } #endif /// /// Elenco Scansioni dato Id sessione inventario /// /// /// public async Task updateScan(ScanDataModel editRec) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { var currRec = dbCtx .DbScanData .Where(x => x.ScanID == editRec.ScanID) .FirstOrDefault(); if (currRec != null) { currRec.ScanID = editRec.ScanID; currRec.CodArticolo = editRec.CodArticolo; currRec.DtScan = editRec.DtScan; currRec.UserScan = editRec.UserScan; currRec.ScanValue = editRec.ScanValue; currRec.IsForced = editRec.IsForced; currRec.Lotto = editRec.Lotto; currRec.RifExt = editRec.RifExt; currRec.Qty = editRec.Qty; currRec.Note = editRec.Note; currRec.IsKnown = editRec.IsKnown; currRec.IsUnique = editRec.IsUnique; currRec.InveSessID = editRec.InveSessID; dbCtx.Entry(currRec).State = EntityState.Modified; } else { dbCtx .DbScanData .Add(editRec); } await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante ScanBySession{Environment.NewLine}{exc}"); } } return fatto; } /// /// Inserisco nuova scansione /// /// /// /// /// insert di un record sessione /// /// public async Task InsertNewScansione(ScanDataModel newRec) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { dbCtx .DbScanData .Add(newRec); await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante InsertNewScansione{Environment.NewLine}{exc}"); } } return fatto; } #endregion gestione scansioni #region gestione operatori /// /// Elenco operatori /// /// /// /// /// /// public List ElencoOperatori() { List dbResult = new List(); using (var dbCtx = new MoonProContext(_configuration)) { dbResult = dbCtx .DbOperatori .Where(s => s.MatrOpr > 0) .AsNoTracking() .OrderBy(x => x.MatrOpr) .ToList(); } return dbResult; } /// /// login operatori /// /// /// /// /// /// public bool LoginOperatore(int matrOpr, string authKey) { List dbResult = new List(); bool answ = false; using (var dbCtx = new MoonProContext(_configuration)) { dbResult = dbCtx .DbOperatori .Where(s => (s.MatrOpr > 0) && (s.MatrOpr == matrOpr) && (s.authKey == authKey)) .AsNoTracking() .ToList(); if (dbResult.Count == 1) { answ = true; } } return answ; } #endregion gestione operatori #region gestione magazzini /// /// Elenco Magazzini /// /// public List ElencoMagazzini() { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbAnagMag .AsNoTracking() .OrderBy(x => x.MagID) .ToList(); } return dbResult; } /// /// insert di un record magazzino /// /// public async Task InsertNewMag(AnagMagModel newRec) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { dbCtx .DbAnagMag .Add(newRec); await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante InsertNewMag{Environment.NewLine}{exc}"); } } return fatto; } /// /// modifica di un record magazzino /// /// public async Task UpdateMag(AnagMagModel magRec) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { var dbResult = dbCtx .DbAnagMag .AsNoTracking() .Where(x => x.MagID == magRec.MagID) .FirstOrDefault(); if (dbResult != null) { if (dbResult.DescMag != magRec.DescMag) { dbResult.DescMag = magRec.DescMag.ToUpper(); dbCtx.Entry(dbResult).State = EntityState.Modified; } } await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante UpdateMag{Environment.NewLine}{exc}"); } } return fatto; } /// /// delete magazzino /// /// public async Task DeleteMag(AnagMagModel record) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { dbCtx .DbAnagMag .Remove(record); await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante deleteMag{Environment.NewLine}{exc}"); } } return fatto; } #endregion gestione magazzini #region gestione UDC /// /// elenco udc /// /// public List ElencoUdc() { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbUdcData .Include(m => m.lottoNav) .AsNoTracking() .OrderByDescending(x => x.UDC) .ToList(); } return dbResult; } /// /// check udc /// /// /// public bool IsUDC(string Udc) { List dbResult = new List(); bool answ = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbUdcData .Where(x => x.UDC == Udc) .AsNoTracking() .OrderByDescending(x => x.UDC) .ToList(); if (dbResult.Count == 1) { answ = true; } } return answ; } #endregion gestione UDC #region gestione lotti /// /// elenco lotti /// /// public List ElencoLotti() { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbLottoData .AsNoTracking() .OrderByDescending(x => x.Lotto) .ToList(); } return dbResult; } /// /// check lotto /// /// /// public bool IsLotto(string lotto) { List dbResult = new List(); bool answ = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbLottoData .Where(x => x.Lotto == lotto) .AsNoTracking() .OrderByDescending(x => x.Lotto) .ToList(); if (dbResult.Count == 1) { answ = true; } } return answ; } #endregion gestione lotti #region gestione sessione /// /// delete sessione /// /// public async Task deleteSessione(InventorySessionModel record) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { dbCtx .DbInveSess .Remove(record); await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante deleteSessione{Environment.NewLine}{exc}"); } } return fatto; } /// /// insert di un record sessione /// /// public async Task InsertNewSessione(InventorySessionModel newRec) { bool fatto = false; using (var dbCtx = new MoonPro_InveContext(_configuration)) { try { dbCtx .DbInveSess .Add(newRec); await dbCtx.SaveChangesAsync(); fatto = true; } catch (Exception exc) { Log.Error($"Eccezione durante InsertNewSessione{Environment.NewLine}{exc}"); } } return fatto; } /// /// Elenco Inventari tipo Azienda (TUTTI, chiusi e paerti) filtrati x data /// /// /// /// public List InventSessHistList(DateTime FromDate, DateTime ToDate) { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbInveSess .Where(x => x.DtStart >= FromDate && x.DtStart <= ToDate && x.DtEnd != null) .Include(m => m.AnagMagNav) .AsNoTracking() .OrderByDescending(x => x.DtStart) .ToList(); } return dbResult; } /// /// Elenco Inventari CORRENTI (=aperti, senza data fine) /// /// public List InventSessCurrList() { List dbResult = new List(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { dbResult = dbCtx .DbInveSess .Where(x => x.DtEnd == null) .Include(m => m.AnagMagNav) .AsNoTracking() .OrderByDescending(x => x.DtStart) .ToList(); } return dbResult; } #endregion gestione sessione #endregion Public Methods #region Private Fields private static IConfiguration _configuration; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields } }