175 lines
5.7 KiB
C#
175 lines
5.7 KiB
C#
using It.FattureInCloud.Sdk.Model;
|
|
using Microsoft.AspNetCore.Components;
|
|
using SHERPA.Data.DbModels;
|
|
using SHERPA.AD.Data;
|
|
|
|
namespace SHERPA.AD.Components
|
|
{
|
|
public partial class CustomerManager
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public int Anno { get; set; } = DateTime.Today.Year;
|
|
|
|
[Parameter]
|
|
public bool EditEnab { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public bool OnlyNeedSync { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public bool SelEnab { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public string TipoDoc { get; set; } = "*";
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Properties
|
|
|
|
protected bool IsLoading { get; set; } = false;
|
|
protected bool IsSynching { get; set; } = false;
|
|
protected List<CustomerModel> ListRecords { get; set; } = new List<CustomerModel>();
|
|
|
|
[Inject]
|
|
protected MessageService MService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected SIMDataService SDService { get; set; } = null!;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await ReloadData();
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
protected string modalCss { get; set; } = "alert alert-success";
|
|
protected string modalMessage { get; set; } = "";
|
|
protected async Task closeModal()
|
|
{
|
|
await Task.Delay(1);
|
|
IsSynching = false;
|
|
modalMessage = "";
|
|
modalCss = "alert alert-primary";
|
|
await ReloadData();
|
|
}
|
|
#region Private Methods
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
var allRec = await SDService.CustomersToSync(TipoDoc, Anno, false);
|
|
if (OnlyNeedSync)
|
|
{
|
|
ListRecords = allRec.Where(x => string.IsNullOrEmpty(x.IdExt)).ToList();
|
|
}
|
|
else
|
|
{
|
|
ListRecords = allRec;
|
|
}
|
|
}
|
|
|
|
/// <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
|
|
}
|
|
} |