Files
mapo-core/MP.SPEC/Pages/Articoli.razor.cs
T
2026-05-27 16:31:30 +02:00

317 lines
8.6 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DbModels;
using MP.SPEC.Data;
namespace MP.SPEC.Pages
{
public partial class Articoli : ComponentBase, IDisposable
{
#region Public Methods
public string CheckSelect(string codArticolo)
{
return currRecord?.CodArticolo == codArticolo
? "table-info"
: "";
}
public void Dispose()
{
currRecord = null;
ListTipoArt = null;
ListAziende = null;
SearchRecords = null;
ListRecords = null;
GC.Collect();
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MpDataService MDService { get; set; } = null!;
[Inject]
protected NavigationManager NavManager { get; set; } = null!;
private string searchCss => string.IsNullOrEmpty(searchVal) ? "btn-secondary" : "btn-primary";
protected string SearchVal
{
get => searchVal;
set
{
// salvo solo se 3+ chars
if (value.Length > 2 || string.IsNullOrEmpty(value))
{
searchVal = value;
}
}
}
protected int totalCount = 0;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Crea nuovo record e va in editing...
/// </summary>
/// <returns></returns>
protected void AddNew()
{
isNewArt = true;
currRecord = new AnagArticoliModel()
{
CodArticolo = $"_NEW_{DateTime.Now:yyyyMMdd.HHmmss}",
DescArticolo = "Nuovo articolo",
Azienda = !selAzienda.Equals("*") ? selAzienda : "MAPO",
Disegno = "",
Tipo = !selTipoArt.Equals("*") ? selTipoArt : "ART",
CurrRev = "",
ProdRev = ""
};
}
/// <summary>
/// Cloning record
/// </summary>
/// <param name="selRec"></param>
protected void cloneRecord(AnagArticoliModel selRec)
{
isNewArt = true;
// creo record duplicato...
AnagArticoliModel newRec = new AnagArticoliModel()
{
Azienda = selRec.Azienda,
CodArticolo = $"clone-{selRec.CodArticolo}",
DescArticolo = $"CLONE - {selRec.DescArticolo}",
Disegno = selRec.Disegno,
Tipo = selRec.Tipo,
CurrRev = "",
ProdRev = ""
};
currRecord = newRec;
}
/// <summary>
/// Eliminazione record selezionato (previa conferma)
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
protected async Task deleteRecord(AnagArticoliModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Eliminazione Articolo: sei sicuro di voler procedere?"))
return;
await Task.Delay(1);
var done = await MDService.ArticoliDeleteRecord(selRec);
currRecord = null;
await ReloadDataAsync();
await Task.Delay(1);
}
protected void ForceReload(int newNum)
{
numRecord = newNum;
}
protected void ForceReloadPage(int newNum)
{
currPage = newNum;
}
protected override async Task OnInitializedAsync()
{
numRecord = 10;
await ReloadBaseData();
}
protected override async Task OnParametersSetAsync()
{
await ReloadDataAsync();
}
protected void ResetData()
{
isNewArt = false;
currRecord = null;
}
protected async Task ResetSearch()
{
SearchVal = "";
await ResetDataAsync();
}
protected async Task resetSel()
{
isNewArt = false;
currRecord = null;
await Task.Delay(1);
}
protected async Task selRecord(AnagArticoliModel selRec)
{
isNewArt = false;
currRecord = selRec;
await Task.Delay(1);
}
protected async Task UpdateAsync(AnagArticoliModel selRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Confermi di voler salvare le modifiche?"))
return;
var done = await MDService.ArticoliUpdateRecord(selRec);
await ResetDataAsync();
}
protected async Task ResetDataAsync()
{
currPage = 1;
currRecord = null;
await ReloadDataAsync();
}
#endregion Protected Methods
#region Private Fields
private string _selAzienda = "*";
private SelectArticoliParams currFilter = new SelectArticoliParams();
private AnagArticoliModel? currRecord = null;
private bool isNewArt = false;
private List<AnagGruppiModel>? ListAziende;
private List<AnagArticoliModel>? ListRecords;
private List<ListValuesModel>? ListTipoArt;
private int maxNumRecord = 5000;
private List<AnagArticoliModel>? SearchRecords;
#endregion Private Fields
#region Private Properties
private int _currPage = 1;
private int _numRecord = 10;
private int currPage
{
get => _currPage;
set
{
if (_currPage != value)
{
_currPage = value;
UpdateTable();
}
}
}
private bool isLoading { get; set; } = false;
private int numRecord
{
get => _numRecord;
set
{
if (_numRecord != value)
{
_numRecord = value;
UpdateTable();
}
}
}
private string searchVal { get; set; } = "";
private string selAzienda
{
get => _selAzienda;
set
{
if (value != _selAzienda)
{
_selAzienda = value;
}
}
}
/// <summary>
/// Tipo articolo selezionato
/// </summary>
private string selTipoArt = "*";
private async Task ReloadAziendaAsync()
{
isLoading = true;
// svuoto cache redis...
ConfigModel updRec = new ConfigModel()
{
Chiave = "AZIENDA",
Valore = selAzienda
};
await MDService.ConfigUpdateAsync(updRec);
await MDService.ConfigResetCache();
// ricarico
await ResetDataAsync();
}
private bool ShowCharts { get; set; } = false;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Verifica cancellabilità record
/// </summary>
/// <param name="selRec"></param>
/// <returns></returns>
private bool ArticoloDelEnabled(AnagArticoliModel currRec)
{
if (currRec.Tipo.Equals("KIT"))
return false;
return MDService.ArticoloDelEnabled(currRec.CodArticolo);
}
private async Task ReloadBaseData()
{
await MDService.EnsureArtCacheLoadedAsync(true);
selAzienda = await MDService.ConfigTryGetAsync("AZIENDA");
if (string.IsNullOrEmpty(selAzienda))
{
selAzienda = "*";
}
ListAziende = await MDService.ElencoAziendeAsync();
ListTipoArt = await MDService.AnagTipoArtLvAsync();
}
/// <summary>
/// Verifica cablata x add tutto tranne KIT
/// </summary>
private bool CanAdd => !selTipoArt.Equals("KIT");
private async Task ReloadDataAsync()
{
isLoading = true;
SearchRecords = await MDService.ArticoliGetSearchAsync(maxNumRecord, selTipoArt, selAzienda, SearchVal);
totalCount = SearchRecords.Count;
UpdateTable();
}
private void UpdateTable()
{
ListRecords = SearchRecords?.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList() ?? new();
isLoading = false;
}
#endregion Private Methods
}
}