Files

409 lines
12 KiB
C#

using GPW.CORE.Data.DbModels;
using GPW.CORE.Data.DTO;
using GPW.CORE.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.JSInterop;
using NLog.Targets.Wrappers;
namespace GPW.CORE.ADM.Components.Compo
{
public partial class FasiMan
{
#region Public Properties
[Parameter]
public bool CanSelProj { get; set; } = false;
[Parameter]
public bool ChkLicOk { get; set; } = true;
[Parameter]
public EventCallback<AnagFasiExplModel> EC_FaseSel { get; set; }
[Parameter]
public EventCallback<int> EC_ProjSel { get; set; }
[Parameter]
public EventCallback<bool> EC_update { get; set; }
[Parameter]
public bool EnableAddFasi { get; set; } = true;
[Parameter]
public string HeadCss { get; set; } = "";
[Parameter]
public List<AnagFasiExplModel>? ListRecords
{
get => listRecords;
set => listRecords = value;
}
[Parameter]
public bool ShowOnlyActive { get; set; } = true;
[Parameter]
public bool ShowSelect { get; set; } = true;
[Parameter]
public string Title { get; set; } = "";
#endregion Public Properties
#region Protected Properties
[Inject]
protected MessageService AppMServ { get; set; } = null!;
[Inject]
protected GpwDataService GDataServ { get; set; } = null!;
protected int IdxCli
{
get => idxCli;
set
{
if (idxCli != value)
{
idxCli = value;
}
}
}
protected int IdxPrj
{
get => idxPrj;
set
{
if (idxPrj != value)
{
idxPrj = value;
}
}
}
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected List<AnagClientiModel> ListClientiFilt
{
get
{
List<AnagClientiModel> answ = new List<AnagClientiModel>();
if (ShowOnlyActive)
{
answ = ListClienti.Where(x => x.Attivo).ToList();
}
else
{
answ = ListClienti;
}
return answ;
}
}
protected List<AnagProgettiModel> ListProgettiFilt
{
get
{
List<AnagProgettiModel> answ = new List<AnagProgettiModel>();
if (IdxCli > 0)
{
ListProgetti = GDataServ.AnagProjByCli(IdxCli);
answ = ListProgetti.Where(x => (x.Attivo ?? false) || !ShowOnlyActive).ToList();
}
return answ;
}
}
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Aggiunta nuova fase/sottofase (se idxFaseAnc &gt; 0)
/// </summary>
/// <param name="idxFaseAnc"></param>
/// <returns></returns>
protected async Task AddFase(int idxFaseAnc)
{
int idxProj = 0;
if (ListRecords != null && ListRecords.Count > 0)
{
idxProj = ListRecords.FirstOrDefault().IdxProgetto ?? 0;
}
if (idxProj > 0)
{
// creo nuovo record...
var newRec = new AnagFasiModel()
{
IdxProgetto = idxProj,
IdxFaseAncest = idxFaseAnc,
NomeFase = "__Nuova Fase",
DescrizioneFase = "Descrizione Fase"
};
// eseguo aggiunta nuovo record...
bool fatto = await GDataServ.AnagFasiUpsert(newRec);
if (fatto)
{
await EC_update.InvokeAsync(true);
}
}
}
protected string CheckSel(AnagFasiExplModel curItem)
{
string answ = "";
if (RecordSel != null)
{
answ = curItem.IdxFase == RecordSel.IdxFase ? "table-info" : "";
}
// verifico stato attivo
answ += !curItem.Attivo ? " striked" : "";
return answ;
}
protected async Task DoDelete(AnagFasiExplModel? selItem)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler forzare il ricalcolo dei record?"))
return;
if (selItem != null)
{
bool fatto = await GDataServ.AnagFasiDelete(selItem.IdxFase);
if (fatto)
{
await EC_update.InvokeAsync(true);
}
}
}
protected async Task DoEdit(AnagFasiExplModel? selItem)
{
RecordSel = null;
if (selItem != null)
{
RecordEdit = await GDataServ.AnagFasiByKey(selItem.IdxFase);
}
else
{
RecordEdit = null;
}
}
protected async Task DoMassTagUpd()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler forzare il tag per tutte le fasi?"))
return;
int idxProj = 0;
if (ListRecords != null && ListRecords.Count > 0)
{
idxProj = ListRecords.FirstOrDefault().IdxProgetto ?? 0;
if (idxProj > 0)
{
bool fatto = await GDataServ.AnagFasiForceTag(idxProj, NewCodTagFase);
if (fatto)
{
await EC_update.InvokeAsync(true);
}
}
}
}
protected async Task DoSelect(AnagFasiExplModel? selItem)
{
RecordSel = selItem;
RecordEdit = null;
await EC_FaseSel.InvokeAsync(selItem);
}
protected async Task ForceReload()
{
RecordEdit = null;
RecordSel = null;
await EC_FaseSel.InvokeAsync(null);
await EC_update.InvokeAsync(true);
await Task.Delay(1);
isLoading = false;
}
/// <summary>
/// init valori da config
/// </summary>
/// <returns></returns>
protected void InitConf()
{
// leggo conf standard controllo RegAtt
var sWarningRatioPerc = GDataServ.ConfigGetKey("WarningRatioPerc");
if (sWarningRatioPerc != null)
{
double.TryParse(sWarningRatioPerc.valore, out warnRatio);
}
}
protected override async Task OnInitializedAsync()
{
InitConf();
await ReloadSel();
ReloadAnagBase();
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
/// <summary>
/// Tentativo rilettura selezione se possibile..
/// </summary>
/// <returns></returns>
protected async Task ReloadSel()
{
// verifico di essere in modalità con selezione permessa x preselezione
if (CanSelProj)
{
IdxCli = await AppMServ.UserPrefGet<int>($"FasiMan_{Title}_idxCli");
IdxPrj = await AppMServ.UserPrefGet<int>($"FasiMan_{Title}_idxPrj");
await EC_ProjSel.InvokeAsync(IdxPrj);
}
}
#endregion Protected Methods
#region Private Fields
private int idxCli = 0;
private int idxPrj = 0;
private AnagFasiModel? RecordEdit = null;
private AnagFasiExplModel? RecordSel = null;
private double warnRatio = 50;
#endregion Private Fields
#region Private Properties
private bool isLoading { get; set; } = false;
private List<AnagClientiModel> ListClienti { get; set; } = new List<AnagClientiModel>();
private List<AnagProgettiModel> ListProgetti { get; set; } = new List<AnagProgettiModel>();
private List<AnagFasiExplModel>? listRecords { get; set; } = new List<AnagFasiExplModel>();
private List<TagFasiDTO> ListTagFasi { get; set; } = new List<TagFasiDTO>();
private string NewCodTagFase { get; set; } = "ND";
private int totalCount { get; set; } = 0;
#endregion Private Properties
#region Private Methods
/// <summary>
/// restituisce una classe css a seconda dei valori passati:
/// green: bdgt &gt; real
/// orange: real &gt; bdgt*warning
/// red: real &gt; bdgt
/// std: errore...
/// </summary>
/// <param name="real"></param>
/// <param name="bdgt"></param>
/// <returns></returns>
private string ColorByVal(object real, object bdgt)
{
string specClass = "default";
try
{
double valoreReal = Convert.ToDouble(real);
double valoreBdget = Convert.ToDouble(bdgt);
double valoreWarn = Convert.ToDouble(bdgt) * warnRatio / 100;
if (valoreReal >= valoreBdget)
{
specClass = "danger";
}
else if (valoreReal >= valoreWarn)
{
specClass = "warning";
}
else
{
specClass = "success";
}
}
catch
{ }
return $" bg-{specClass} bg-opacity-50 bg-gradient border border-{specClass} rounded";
}
private bool DelEnabled(decimal TotOre, int idxFase, int idxFaseAnc)
{
// se ha ore NON è eliminabile...
bool answ = TotOre == 0;
// se zero ore --> deve esere child oppure ancestor senza fasi...
if (answ)
{
// se ancestor
if (idxFaseAnc == 0)
{
// cerco fasi dato ancestor...
var listChild = GDataServ.AnagFasiByAncestor(idxFase);
// solo se NON ha fasi child
answ = listChild == null || listChild.Count == 0;
}
}
return answ;
}
private void ReloadAnagBase()
{
ListTagFasi = GDataServ.AnagTagFasiAll();
ListClienti = GDataServ.AnagClientiAll();
}
private async Task ReloadData()
{
if (CanSelProj)
{
listRecords = new List<AnagFasiExplModel>();
ListProgetti = new List<AnagProgettiModel>();
// se abilitato x rilettura locale --> leggo fasi!
if (CanSelProj && IdxCli > 0 && IdxPrj > 0)
{
listRecords = await GDataServ.AnagFasiExplByProj(IdxPrj);
}
}
totalCount = listRecords.Count;
isLoading = false;
}
private async Task SaveCli()
{
IdxPrj = 0;
await ReloadData();
await EC_ProjSel.InvokeAsync(IdxPrj);
await SaveSelection();
}
private async Task SaveProj()
{
await ReloadData();
await EC_ProjSel.InvokeAsync(IdxPrj);
await SaveSelection();
}
private async Task SaveSelection()
{
await AppMServ.UserPrefSet($"FasiMan_{Title}_idxCli", $"{IdxCli}");
await AppMServ.UserPrefSet($"FasiMan_{Title}_idxPrj", $"{IdxPrj}");
}
#endregion Private Methods
}
}