#if false using Blazored.LocalStorage; #endif using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data.DbModels; using MP.SPEC.Components; using MP.SPEC.Data; using MP.SPEC.Services; using NLog; using System.Reflection.PortableExecutable; using EgwCoreLib.Razor; using Microsoft.AspNetCore.DataProtection; using MP.Data.Services; namespace MP.SPEC.Pages { public partial class PODL { #region Protected Fields protected bool enableForceSync = true; protected bool enableStartPODL = true; protected bool enableStopODL = true; protected DataPager? pagerODL = null!; protected bool reqNew = false; #endregion Protected Fields #region Protected Properties protected string addMessage { get => addEnabled ? "" : "Manca Selezione Impianto / Fase"; } protected bool canSaveEdit { get { bool answ = false; if (currRecord != null) { // controllo le condizioni di selezione fase bool okSelReparto = (useFasi4KeyRich == "FASE" && selReparto != "*") || useFasi4KeyRich != "FASE"; // controllo condizione record valido bool okCurrRecord = currRecord.CodArticolo != "" && currRecord.CodFase != "" && currRecord.IdxMacchina != ""; answ = okCurrRecord && okSelReparto; } return answ; } } protected string header { get => currFilter.Header; set => currFilter.Header = value; } [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; #if false [Inject] protected ILocalStorageService localStorage { get; set; } = null!; #endif [Inject] protected ILocalStorageService localStorage { get; set; } = null!; [Inject] protected MpDataService MDService { get; set; } = null!; [Inject] protected IOApiService MpIoApiCall { get; set; } = null!; [Inject] protected NavigationManager NavManager { get; set; } = null!; protected string useFasi4KeyRich { get; set; } = ""; #endregion Protected Properties #region Protected Methods protected async Task cancel() { currRecord = null; await ReloadData(); await Task.Delay(1); } protected async Task editRecord(PODLExpModel selRec) { canEdit = true; // preseleziono ricerca articolo if (selRec != null) { artSearch = selRec.CodArticolo.Length > nArtSearch ? selRec.CodArticolo.Substring(0, nArtSearch) : selRec.CodArticolo; } currRecord = selRec; await Task.Delay(1); } protected async Task forceSyncDb() { if (!await JSRuntime.InvokeAsync("confirm", "Sei sicuro di voler (re)inviare i dati (Articoli, PODL) all'impianto?")) return; var clonedRec = MP.Data.Utils.POdlExt.convertToPOdl(currRecord); await callSyncDb(clonedRec); currRecord = null; NavManager.NavigateTo(NavManager.Uri, true); } protected async Task getReparto() { string keyStor = "reparto"; string localReparto = await localStorage.GetItemAsync(keyStor, "") ?? ""; if (!string.IsNullOrEmpty(localReparto)) { selReparto = localReparto; } else { selReparto = "*"; await localStorage.SetItemAsync(keyStor, selReparto); } } protected override async Task OnInitializedAsync() { await getReparto(); ListAziende = MDService.ElencoAziende(); var allGruppiData = MDService.ElencoGruppiFase(); if (allGruppiData != null) { ListGruppiFase = allGruppiData.Where(x => x.SelEnabled).ToList(); } ListStati = await MDService.AnagStatiComm(); currAzienda = await MDService.ConfigTryGetAsync("AZIENDA"); padCodXdl = await MDService.ConfigTryGetAsync("padCodXdl"); useFasi4KeyRich = await MDService.ConfigTryGetAsync("SPEC_KeyRichiesta"); // carico opzioni gestione PODL/ODL/sync string SPEC_PODL_gest = await MDService.ConfigTryGetAsync("SPEC_PODL_gest"); if (!string.IsNullOrEmpty(SPEC_PODL_gest)) { bool.TryParse(SPEC_PODL_gest, out enableStartPODL); } string SPEC_ODL_gest = await MDService.ConfigTryGetAsync("SPEC_ODL_gest"); if (!string.IsNullOrEmpty(SPEC_ODL_gest)) { bool.TryParse(SPEC_ODL_gest, out enableStopODL); } string SPEC_XODL_sync = await MDService.ConfigTryGetAsync("SPEC_XODL_sync"); if (!string.IsNullOrEmpty(SPEC_XODL_sync)) { bool.TryParse(SPEC_XODL_sync, out enableForceSync); } string SPEC_nArtSearch = await MDService.ConfigTryGetAsync("SPEC_nArtSearch"); if (!string.IsNullOrEmpty(SPEC_nArtSearch)) { int.TryParse(SPEC_nArtSearch, out nArtSearch); } } protected override async Task OnParametersSetAsync() { // carico dati await ReloadData(); } protected async Task pgResetReq(bool doReset) { if (doReset) { await Task.Delay(1); if (pagerODL != null) { pagerODL.resetCurrPage(); } } } /// /// Crea nuovo record e va in editing... /// /// protected async Task reqNewPODL() { canEdit = true; header = "Nuovo PODL"; artSearch = ""; string codExt = $"{currFase}"; string codGruppo = ""; if (ListGruppiFase != null && ListGruppiFase.Count > 0) { var firstFase = ListGruppiFase.FirstOrDefault(x => x.CodGruppo.StartsWith(currAzienda)); if (firstFase != null) { codGruppo = firstFase.CodGruppo; } } string codMacc = ""; if (ListMacchine != null && ListMacchine.Count > 0) { var firstMacc = ListMacchine.FirstOrDefault(x => x.Nome.Contains(currAzienda)); if (firstMacc != null) { codMacc = firstMacc.IdxMacchina; } } currRecord = new PODLExpModel() { CodArticolo = "",//currArticolo, KeyBCode = codExt, KeyRichiesta = codExt, CodGruppo = codGruppo, IdxMacchina = codMacc, NumPezzi = 1, DueDate = DateTime.Now.AddDays(30) }; await Task.Delay(1); } protected void resetFase() { StatoSel = "*"; } protected void resetMacchina() { macchina = "*"; } protected async Task resetReparto() { string keyStor = "reparto"; selReparto = "*"; await localStorage.SetItemAsync(keyStor, selReparto); } protected async Task selRecord(PODLExpModel selRec) { canEdit = false; currRecord = selRec; await Task.Delay(1); } protected void SetNumRec(int newNum) { currPage = 1; numRecord = newNum; } protected void SetPage(int newNum) { currPage = newNum; } protected async Task toggleClosed() { hasOdl = !hasOdl; await Task.Delay(1); } protected async Task update(PODLExpModel selRec) { if (!await JSRuntime.InvokeAsync("confirm", "Confermi di voler salvare le modifiche?")) return; await Task.Delay(1); var clonedRec = MP.Data.Utils.POdlExt.convertToPOdl(selRec); // se x qualche motivo mancasse codGruppo --> sistemo! if (string.IsNullOrEmpty(clonedRec.CodGruppo)) { clonedRec.CodGruppo = currGruppoSel.CodGruppo; Log.Error($"CodGruppo mancante: messo valore selezionato: {currGruppoSel.CodGruppo}"); } // se selezionato sovrascrivo... if (useFasi4KeyRich == "FASE") { clonedRec.CodGruppo = currGruppoSel.CodGruppo; } var done = await MDService.POdlUpdateRecord(clonedRec); // se attivabile chiamo sync if (clonedRec.Attivabile) { await callSyncDb(clonedRec); } currRecord = null; NavManager.NavigateTo(NavManager.Uri, true); } protected void UpdateTotCount(int newTotCount) { totalCount = newTotCount; } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private PODLExpModel? _currRecord = null; private AnagGruppiModel currGruppoSel = new AnagGruppiModel(); private PODLExpModel currRecordControlli = new PODLExpModel(); private List? ListArticoli; private List? ListAziende; private List? ListGruppiFase; private List? ListMacchine; private List? ListStati; private int nArtSearch = 5; #endregion Private Fields #region Private Properties private string _artSearch { get; set; } = ""; private string _currAzienda { get; set; } = "*"; private bool addEnabled { get { //bool answ = currFase != "*"; bool answ = (useFasi4KeyRich == "FASE" && currFase != "*") || (useFasi4KeyRich != "FASE" && selReparto != "*"); return answ; } } private string artSearch { get => _artSearch; set { if (!_artSearch.Equals(value)) { _artSearch = value; var pUpd = Task.Run(async () => { if (currRecord != null) { currRecord.CodArticolo = ""; } await ReloadData(); }); pUpd.Wait(); } } } private bool canEdit { get; set; } = false; private string currAzienda { get => _currAzienda; set { if (!_currAzienda.Equals(value)) { _currAzienda = value; var pUpd = Task.Run(async () => { await ReloadData(); }); pUpd.Wait(); } } } private string currFase { get => currFilter.CodFase; set { if (!currFilter.CodFase.Equals(value)) { currFilter.CodFase = value; currPage = 1; } } } private SelectXdlParams currFilter { get; set; } = new SelectXdlParams(); private int currPage { get => currFilter.CurrPage; set => currFilter.CurrPage = value; } private PODLExpModel? currRecord { get => _currRecord; set { _currRecord = value; } } private bool filtActive { get => macchina != "*" || StatoSel != "*" || selReparto != "*"; } private bool hasOdl { get => currFilter.HasOdl; set { if (currFilter.HasOdl != value) { currFilter.HasOdl = value; } } } private bool isLoading { get; set; } = false; private string macchina { get => currFilter.IdxMacchina; set => currFilter.IdxMacchina = value; } private int numRecord { get => currFilter.NumRec; set => currFilter.NumRec = value; } private string padCodXdl { get; set; } = "00000"; private DateTime selDtEnd { get => currFilter.DtEnd; set { if (!currFilter.DtEnd.Equals(value)) { currFilter.DtEnd = value; currPage = 1; } } } private DateTime selDtStart { get => currFilter.DtStart; set { if (!currFilter.DtStart.Equals(value)) { currFilter.DtStart = value; currPage = 1; } } } private string selReparto { get => currFilter.CodReparto; set => currFilter.CodReparto = value; } private string StatoSel { get => currFilter.CodFase; set => currFilter.CodFase = value; } private int totalCount { get => currFilter.TotCount; set => currFilter.TotCount = value; } #endregion Private Properties #region Private Methods /// /// Chiama metodo x chiedere sync DB /// /// /// private async Task callSyncDb(PODLModel selRec) { // chiamo aggiunta task SyncDb... string idxMacc = selRec.IdxMacchina; string restUrl = $"IOB/addTask2Exe/{idxMacc}?taskName=syncDbData&taskVal="; try { var response = await MpIoApiCall.callMpIoUrlGet(restUrl); } catch (Exception exc) { Log.Error($"Errore durante chiamata: {Environment.NewLine}{exc}"); } } private async Task ReloadData() { isLoading = true; await Task.Delay(1); ListMacchine = MDService.MacchineGetFilt(selReparto); ListArticoli = await MDService.ArticoliGetSearch(100, currAzienda, artSearch); if (ListGruppiFase != null) { var firstGroup = ListGruppiFase.Where(x => x.CodGruppo == selReparto).FirstOrDefault(); if (firstGroup != null) { currGruppoSel = firstGroup; } } isLoading = false; } private async Task UpdateFilter(SelectXdlParams newParams) { isLoading = true; await Task.Delay(1); currPage = 1; // salvo comunque filtro reparto x utente await localStorage.SetItemAsync("reparto", selReparto); await ReloadData(); await Task.Delay(1); await InvokeAsync(() => StateHasChanged()); currFilter = newParams; isLoading = false; } #endregion Private Methods } }