328 lines
10 KiB
C#
328 lines
10 KiB
C#
using It.FattureInCloud.Sdk.Model;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
using SHERPA.AD.Data;
|
|
using SHERPA.Data.DbModels;
|
|
|
|
namespace SHERPA.AD.Components
|
|
{
|
|
public partial class CustomerManager
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public SelectCli CurrFilter { get; set; } = new SelectCli();
|
|
|
|
[Parameter]
|
|
public bool ForceReload
|
|
{
|
|
set
|
|
{
|
|
if (value)
|
|
{
|
|
SelRecord = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
[Parameter]
|
|
public EventCallback<CustomerModel?> RecCloned { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback<CustomerModel?> RecSelected { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback<CustomerModel?> RecUpdated { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback<int> UpdateCount { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Properties
|
|
|
|
protected bool IsLoading { get; set; } = false;
|
|
protected bool IsSynching { get; set; } = false;
|
|
|
|
[Inject]
|
|
protected IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
protected List<CustomerModel> ListDeletable { get; set; } = new List<CustomerModel>();
|
|
protected List<CustomerModel> ListRecords { get; set; } = new List<CustomerModel>();
|
|
protected string modalCss { get; set; } = "alert alert-success";
|
|
protected string modalMessage { get; set; } = "";
|
|
|
|
[Inject]
|
|
protected MessageService MService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected SADDataService SDService { get; set; } = null!;
|
|
|
|
protected List<CustomerModel> SearchRecords { get; set; } = new List<CustomerModel>();
|
|
protected int totalCount { get; set; } = 0;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected async Task closeModal()
|
|
{
|
|
await Task.Delay(1);
|
|
IsSynching = false;
|
|
modalMessage = "";
|
|
modalCss = "alert alert-primary";
|
|
await ReloadData();
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
// test token locale
|
|
var tokenData = await MService.getUserTokenAsync();
|
|
hasToken = tokenData != null;
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
LastFilter.Anno = -1;
|
|
}
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
// verifica cambio parametri...
|
|
if (!CurrFilter.Equals(LastFilter))
|
|
{
|
|
LastFilter = CurrFilter.clone();
|
|
await ReloadData();
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Properties
|
|
|
|
private bool hasToken
|
|
{
|
|
get => MService.hasToken;
|
|
set => MService.hasToken = value;
|
|
}
|
|
|
|
private SelectCli LastFilter { get; set; } = new SelectCli();
|
|
private CustomerModel? SelRecord { get; set; } = null;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private string CheckSel(CustomerModel curItem)
|
|
{
|
|
string answ = "";
|
|
if (SelRecord != null)
|
|
{
|
|
answ = curItem.IdxCli == SelRecord.IdxCli ? "table-info" : "";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clona il record
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
private async Task doCloneRec(CustomerModel currItem)
|
|
{
|
|
// chiedo verifica
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler DUPLICARE il cliente selezionato??"))
|
|
return;
|
|
|
|
await SDService.CustomerClona(currItem);
|
|
SelRecord = null;
|
|
await RecCloned.InvokeAsync(currItem);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina record
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
private async Task doDelRec(CustomerModel currItem)
|
|
{
|
|
// chiedo verifica
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler ELIMINARE il cliente selezionato??"))
|
|
return;
|
|
|
|
await SDService.CustomerDelete(currItem);
|
|
SelRecord = null;
|
|
await ReloadData();
|
|
await RecUpdated.InvokeAsync(currItem);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Seleziona record
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
private async Task doEditRec(CustomerModel currItem)
|
|
{
|
|
SelRecord = currItem;
|
|
await Task.Delay(1);
|
|
await RecSelected.InvokeAsync(currItem);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Seleziona record
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
private async Task doUpdateRec(CustomerModel currItem)
|
|
{
|
|
await SDService.CustomerUpdate(currItem);
|
|
SelRecord = null;
|
|
await Task.Delay(1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indica che è cancellabile (NON HA fatture relative)
|
|
/// </summary>
|
|
/// <param name="IdxCli"></param>
|
|
/// <returns></returns>
|
|
private bool isDeletable(int IdxCli)
|
|
{
|
|
return ListDeletable.Where(x => x.IdxCli == IdxCli).Count() > 0;
|
|
}
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
IsLoading = true;
|
|
await Task.Delay(1);
|
|
List<CustomerModel> allRec = new List<CustomerModel>();
|
|
// verifico se sia "showAll"..
|
|
if (CurrFilter.ShowFilt)
|
|
{
|
|
allRec = await SDService.CustomersToSync(CurrFilter.CodTipo, CurrFilter.Anno, false);
|
|
}
|
|
else
|
|
{
|
|
allRec = await SDService.CustomersGetAll(CurrFilter.SearchVal);
|
|
}
|
|
if (CurrFilter.OnlySync)
|
|
{
|
|
SearchRecords = allRec.Where(x => string.IsNullOrEmpty(x.IdExt)).ToList();
|
|
}
|
|
else
|
|
{
|
|
SearchRecords = allRec;
|
|
}
|
|
totalCount = SearchRecords.Count;
|
|
// paginazione
|
|
ListRecords = SearchRecords.Skip(CurrFilter.NumRecord * (CurrFilter.CurrPage - 1)).Take(CurrFilter.NumRecord).ToList();
|
|
ListDeletable = await SDService.CustomersDeletable();
|
|
await UpdateCount.InvokeAsync(totalCount);
|
|
IsLoading = false;
|
|
await Task.Delay(1);
|
|
}
|
|
|
|
private async Task ResetSel()
|
|
{
|
|
SelRecord = null;
|
|
await RecSelected.InvokeAsync(null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sincronizzazione di tutti i record che la richiedono
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task SyncAll()
|
|
{
|
|
IsSynching = true;
|
|
// prendo elenco dei clienti NON in sync e processo 1:1
|
|
var data2sync = ListRecords.Where(x => string.IsNullOrEmpty(x.IdExt)).ToList();
|
|
if (data2sync != null && data2sync.Count > 0)
|
|
{
|
|
foreach (var item in data2sync)
|
|
{
|
|
await SyncCurrent(item);
|
|
}
|
|
}
|
|
IsSynching = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sincronizzazioner bidirezionale dati cliente locale e su cloud
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
private async Task SyncCurrent(CustomerModel currItem)
|
|
{
|
|
modalMessage = "";
|
|
modalCss = "alert alert-primary";
|
|
IsSynching = true;
|
|
// prova a cercare il customer...
|
|
var (searchResult, cloudRec) = await MService.cloudClientSearch(currItem);
|
|
if (!searchResult.success)
|
|
{
|
|
modalCss = "alert alert-danger";
|
|
modalMessage = $"Errore in fase di ricerca: {searchResult.message}";
|
|
if (!string.IsNullOrEmpty(searchResult.description))
|
|
{
|
|
modalMessage += $" | {searchResult.description}";
|
|
}
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
else
|
|
{
|
|
// se ho trovato il record --> aggiorno sul DB
|
|
if (cloudRec != null)
|
|
{
|
|
await UpdateDbRecord(currItem, cloudRec);
|
|
}
|
|
else
|
|
{
|
|
// se non trovato DEVO creare...
|
|
var (addResult, newCloudRec) = await MService.cloudClientCreate(currItem);
|
|
// verifico sia avvenuto con successo..
|
|
if (!addResult.success)
|
|
{
|
|
modalCss = "alert alert-danger";
|
|
modalMessage = $"Errore in fase di creazione record remoto: {addResult.message}";
|
|
if (!string.IsNullOrEmpty(addResult.description))
|
|
{
|
|
modalMessage += $" | {addResult.description}";
|
|
}
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
else
|
|
{
|
|
// se ho trovato il record --> aggiorno sul DB
|
|
if (newCloudRec != null)
|
|
{
|
|
await UpdateDbRecord(currItem, newCloudRec);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
modalMessage = "Done!";
|
|
modalCss = "alert alert-success";
|
|
await InvokeAsync(StateHasChanged);
|
|
await Task.Delay(500);
|
|
IsSynching = false;
|
|
await ReloadData();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue update record su DB dai dati cloud
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <param name="cloudRec"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> UpdateDbRecord(CustomerModel currItem, ModelClient cloudRec)
|
|
{
|
|
bool fatto = false;
|
|
currItem.IdExt = $"{cloudRec.Id}";
|
|
// salvo sul DB
|
|
fatto = await SDService.CustomerUpdate(currItem);
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |