Files
lux/Lux.UI/Components/Compo/OfferRowMan.razor.cs
T
2026-01-13 15:10:47 +01:00

1647 lines
58 KiB
C#

using Egw.Window.Data;
using EgwCoreLib.Lux.Core;
using EgwCoreLib.Lux.Core.RestPayload;
using EgwCoreLib.Lux.Data.DbModel.Config;
using EgwCoreLib.Lux.Data.DbModel.Items;
using EgwCoreLib.Lux.Data.DbModel.Sales;
using EgwCoreLib.Lux.Data.DbModel.Utils;
using EgwCoreLib.Lux.Data.Services;
using Lux.UI.Components.Pages;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.IdentityModel.Tokens;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using NLog;
using System.Threading.Tasks;
using WebWindowComplex;
using WebWindowComplex.DTO;
using static EgwCoreLib.Lux.Core.Enums;
namespace Lux.UI.Components.Compo
{
public partial class OfferRowMan : IDisposable
{
#region Public Enums
/// <summary>
/// modalit modifica riga offerta
/// </summary>
public enum EditMode
{
None = 0,
/// <summary>
/// Dati generici del record
/// </summary>
RecData,
/// <summary>
/// Struttura serializzata (es JWD)
/// </summary>
SerStruc,
/// <summary>
/// BOM editing
/// </summary>
BOM,
/// <summary>
/// File editing (es BTL)
/// </summary>
File,
/// <summary>
/// Editing ciclo di lavoro
/// </summary>
JobCycle
}
#endregion Public Enums
#region Public Properties
[Parameter]
public OfferModel CurrRecord { get; set; } = null!;
[Parameter]
public DisplayMode DisplayMode { get; set; } = DisplayMode.Standard;
[Parameter]
public EventCallback<bool> EC_Updated { get; set; }
#endregion Public Properties
#region Public Methods
/// <summary>
/// Dispose sottoscrizione canale
/// </summary>
public void Dispose()
{
DLService.PipeUpdate.EA_NewMessage -= PipeUpdate_EA_NewMessage;
DLService.PipePng.EA_NewMessage -= PipePng_EA_NewMessage;
DLService.PipeSvg.EA_NewMessage -= PipeSvg_EA_NewMessage;
DLService.PipeHwOpt.EA_NewMessage -= PipeHwOpt_EA_NewMessage;
DLService.PipeProfList.EA_NewMessage -= PipeProfList_EA_NewMessage;
DLService.PipeShape.EA_NewMessage -= PipeShape_EA_NewMessage;
}
#endregion Public Methods
#region Protected Fields
/// <summary>
/// Predisposizione valori live SVG/JWD
/// </summary>
protected LivePayload CurrData = new LivePayload();
/// <summary>
/// Configurazione elenchi anagrafiche
/// </summary>
protected BaseListPayload SetupList = new BaseListPayload();
#endregion Protected Fields
#region Protected Properties
[Inject]
protected ConfigDataService CDService { get; set; } = null!;
[Inject]
protected IConfiguration Config { get; set; } = null!;
[Inject]
protected CalcRuidService CRService { get; set; } = null!;
[Inject]
protected CalcRequestService CService { get; set; } = null!;
[Inject]
protected IWebHostEnvironment CurrEnv { get; set; } = null!;
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
/// <summary>
/// Costo totale calcolato x offerta
/// </summary>
protected double GrandTotCost
{
get => AllRecords != null && AllRecords.Count > 0 ? AllRecords.Sum(x => x.TotalCost) : 0;
}
/// <summary>
/// Margine medio calcolato x offerta
/// </summary>
protected double GrandTotMargin
{
get
{
double answ = 0;
if (AllRecords != null && AllRecords.Count > 0)
{
double totPrice = AllRecords.Sum(x => x.TotalPrice);
double totCost = AllRecords.Sum(x => x.TotalCost);
if (totPrice > 0)
{
answ = (totPrice - totCost) / totPrice;
}
}
return answ;
}
}
/// <summary>
/// Importo totale calcolato x offerta
/// </summary>
protected double GrandTotNumItems
{
get
{
double answ = 0;
if (AllRecords != null && AllRecords.Count > 0)
{
answ = AllRecords.Sum(x => x.ProdItemQtyTot);
}
return answ;
}
}
/// <summary>
/// Importo totale calcolato x offerta
/// </summary>
protected double GrandTotPrice
{
get => AllRecords != null && AllRecords.Count > 0 ? AllRecords.Sum(x => x.TotalPrice) : 0;
}
/// <summary>
/// Num totale obj calcolato x offerta
/// </summary>
protected double GrandTotQty
{
get => AllRecords != null && AllRecords.Count > 0 ? AllRecords.Sum(x => x.Qty) : 0;
}
[Inject]
protected IWebHostEnvironment HostEnv { get; set; } = null!;
[Inject]
protected ImageCacheService ICService { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
/// <summary>
/// ID Offerta corrente
/// </summary>
protected int OfferID
{
get => CurrRecord.OfferID;
}
#endregion Protected Properties
#region Protected Methods
protected void ClosePopup()
{
CurrEditMode = EditMode.None;
EditRecord = null;
reqSellingItem = false;
}
/// <summary>
/// Aggiunge una nuova riga vuota come nota sotto il record selezionato oppure in coda...
/// </summary>
/// <returns></returns>
protected async Task DoAddNote()
{
int numRow = AllRecords.Count + 1;
if (EditRecord != null)
{
numRow = EditRecord.RowNum + 1;
}
OfferRowModel newNote = new OfferRowModel()
{
OfferID = CurrRecord.OfferID,
Envir = CurrRecord.Envir,
RowNum = numRow,
OfferRowUID = "",
Qty = 0,
BomCost = 0,
BomPrice = 0,
StepCost = 0,
StepPrice = 0
};
await DLService.OffertRowUpsert(newNote);
await ReloadData();
UpdateTable();
}
/// <summary>
/// Aggiunge una nuova riga ordine in coda...
/// </summary>
/// <returns></returns>
protected async Task DoAddOrderRow(int sellItemID)
{
int numRow = AllRecords.Count + 1;
if (EditRecord != null)
{
numRow = EditRecord.RowNum + 1;
}
OfferRowModel newSOR = new OfferRowModel()
{
AwaitBom = true,
AwaitPrice = true,
OfferID = CurrRecord.OfferID,
Envir = CurrRecord.Envir,
Inserted = DateTime.Now,
RowNum = numRow,
OfferRowUID = "",
Qty = 1,
SellingItemID = sellItemID,
BomCost = 0,
BomPrice = 0,
StepCost = 0,
StepPrice = 0
};
// se è window aggiungo "{}" come serStruct sennò non la prende bene...
if (CurrRecord.Envir == EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW)
{
newSOR.SerStruct = "{}";
}
reqSellingItem = false;
await DLService.OffertRowUpsert(newSOR);
await ReloadData();
UpdateTable();
}
/// <summary>
/// Annullamento modifica
/// </summary>
/// <param name="curRec"></param>
/// <returns></returns>
protected async Task DoCancel()
{
isLoading = true;
CurrEditMode = EditMode.None;
EditRecord = null;
await Task.Delay(20);
await DLService.FlushCacheOffersAsync();
await Task.Delay(20);
await ReloadData();
UpdateTable();
isLoading = false;
}
/// <summary>
/// Clona riga richiesta
/// </summary>
/// <param name="rec2clone"></param>
protected async Task DoClone(OfferRowModel rec2clone)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler duplicare la riga corrente?"))
return;
// calcolo indice riga...
int numRow = totalCount + 1;
OfferRowModel newRec = new OfferRowModel()
{
AwaitBom = true,
AwaitPrice = true,
Envir = rec2clone.Envir,
FileName = rec2clone.FileName,
FileResource = rec2clone.FileResource,
FileSize = rec2clone.FileSize,
Inserted = DateTime.Now,
ItemBOM = rec2clone.ItemBOM,
ItemSteps = rec2clone.ItemSteps,
Modified = DateTime.Now,
Note = rec2clone.Note,
OfferID = OfferID,
OfferRowUID = "",
Qty = rec2clone.Qty,
RowNum = numRow,
SellingItemID = rec2clone.SellingItemID,
SerStruct = rec2clone.SerStruct,
StepCost = rec2clone.StepCost,
StepPrice = rec2clone.StepPrice,
};
// salvo sul DB
await DLService.OffertRowUpsert(newRec);
// chiamo update record che non hanno UID x questo ordine
var list2fix = await DLService.OffertRowFixUid(OfferID);
if (list2fix != null && list2fix.Count > 0)
{
// rileggo i miei record...
await ReloadData();
var listCalc = SorListCalc();
foreach (var item in listCalc)
{
// se UID tra quelli da ricalcolare...
if (list2fix.Contains(item.OfferRowUID))
{
// chiedo BOM e immagine
await reqBomUpdate(item);
}
}
}
await ReloadData();
UpdateTable();
}
/// <summary>
/// Eliminazione riga offerta
/// </summary>
/// <param name="rec2del"></param>
/// <returns></returns>
protected async Task DoDelete(OfferRowModel rec2del)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler eliminare la riga corrente?<br/>Codice: {rec2del.OfferRowUID} | {rec2del.Note} | importo tot: {rec2del.TotalPrice}"))
return;
await DLService.OffertRowDelete(rec2del);
await ReloadData();
UpdateTable();
}
/// <summary>
/// Va in edit della riga richiesta
/// </summary>
/// <param name="curRec"></param>
protected void DoEdit(OfferRowModel curRec)
{
// imposto edit record
EditRecord = curRec;
/// modalita edit: gestione valori campi record
CurrEditMode = EditMode.RecData;
isLoading = false;
}
/// <summary>
/// Edit del file:
/// - abilitazione fileUpload
/// - anteprima grande (live)
/// </summary>
/// <param name="curRec"></param>
protected void DoEditFile(OfferRowModel curRec)
{
EditRecord = curRec;
/// modalitedit: gestione JWD
CurrEditMode = EditMode.File;
#if false
// preparazione dati da record corrente
PrepareWindowData(EditRecord.SerStruct);
// reset prev
prevJwd = "";
#endif
}
/// <summary>
/// Apre editor finestre del record richiesto
/// </summary>
/// <param name="curRec"></param>
protected void DoEditJwd(OfferRowModel curRec)
{
EditRecord = curRec;
/// modalitedit: gestione JWD
CurrEditMode = EditMode.SerStruc;
// preparazione dati da record corrente
PrepareWindowData(EditRecord.SerStruct);
// reset prev
prevJwd = "";
}
/// <summary>
/// Salvataggio edit record + reload
/// </summary>
/// <param name="curRec"></param>
/// <returns></returns>
protected async Task DoSave(OfferRowModel curRec)
{
isLoading = true;
// salvo record modificato...
await DLService.OffertRowUpsert(curRec);
// reset
CurrEditMode = EditMode.None;
EditRecord = null;
await DLService.FlushCacheOffersAsync();
await ReloadData();
UpdateTable();
isLoading = false;
}
/// <summary>
/// Seleziono riga senza cambiare modalit editing
/// </summary>
/// <param name="curRec"></param>
protected void DoSelect(OfferRowModel curRec)
{
// imposto edit record
EditRecord = curRec;
/// modalitedit: gestione valori campi record
CurrEditMode = EditMode.None;
}
protected async Task DoSelectItem()
{
reqSellingItem = !reqSellingItem;
ListSellItems = await DLService.SellingItemsByEnvir(CurrRecord.Envir);
}
/// <summary>
/// Imposta modalita edit ciclo di lavoro
/// </summary>
/// <param name="currRow"></param>
protected void DoSwapJobCycle(OfferRowModel currRow)
{
CurrEditMode = EditMode.JobCycle;
selectBom(currRow);
}
/// <summary>
/// Imposta modalita ad edit BOM
/// </summary>
/// <param name="currRow"></param>
protected void DoSwapMat(OfferRowModel currRow)
{
CurrEditMode = EditMode.BOM;
selectBom(currRow);
}
/// <summary>
/// Display fileSize scalato
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
protected string fSize(long size)
{
return EgwCoreLib.Utils.FileHelpers.SizeSuffix(size, 1);
}
/// <summary>
/// Formattazione testo come html x display
/// </summary>
protected MarkupString HtmlConv(string rawData)
{
return (MarkupString)rawData.Replace(Environment.NewLine, "<br />").Replace("\n", "<br />");//.Replace(" ", "&nbsp;");
}
/// <summary>
/// Calcolo URL immagine
/// </summary>
/// <param name="imgUid"></param>
/// <param name="env"></param>
/// <returns></returns>
protected string imgUrl(string imgUid, string env)
{
// cast string su env..
EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS envir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
Enum.TryParse(env, out envir);
return ICService.ImageUrl($"{apiUrl}/{imgBasePath}", false, imgUid, envir);
}
/// <summary>
/// Forza parametri generali selezionati nell'offerta
/// </summary>
/// <returns></returns>
protected async Task OfferForceParameters()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler impostare i parametri selezionati per l'offerta?"))
return;
// recupero obj dizionario x i parametri compresi...
ParamDict CurrSel = new ParamDict(CurrRecord.DictPresel);
// metto a waiting tutte le righe con bom...
var listCalc = SorListCalc();
foreach (var item in listCalc)
{
await DLService.OffertUpdateAwaitState(item.OfferRowID, true, true);
// poich non gestito evento ritorno update window interno si "scassa" --> try catch/ if FALSE
try
{
string rColor = CurrSel.GetVal("Color");
string rGlass = CurrSel.GetVal("Glass");
string rProfile = CurrSel.GetVal("Profile");
string rWood = CurrSel.GetVal("Wood");
var newSerStruct = SerialMan.MassUpdate((string)item.SerStruct, null, null, rColor, rWood, rGlass, rProfile);
await DLService.OffertRowUpdateSerStruct(item.OfferRowID, newSerStruct);
}
catch
{ }
}
await InvokeAsync(StateHasChanged);
// verifica preliminare UID
await DLService.OffertRowFixUid(OfferID);
// ricalcolo di tutte le BOM e relativi prezzi...
foreach (var item in listCalc)
{
// chiedo BOM e immagine
await reqBomUpdate(item);
}
//await DoRecalcOffer();
}
/// <summary>
/// Aggiornamento costing completo:
/// - verifica UID
/// - ricalcolo BOM
/// - update prezzi
/// </summary>
/// <returns></returns>
protected async Task OfferUpdateAllCosting()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler ricalcolare/validare in toto l'offerta?"))
return;
// metto a waiting tutte le righe con bom...
var listCalc = SorListCalc();
foreach (var item in listCalc)
{
await DLService.OffertUpdateAwaitState(item.OfferRowID, true, true);
}
await InvokeAsync(StateHasChanged);
// verifica preliminare UID
await DLService.OffertRowFixUid(OfferID);
// fixme todo da riverificare con calcolo BOM funzionante
#if false
// rileggo i record...
await ReloadData();
#endif
// ricalcolo di tutte le BOM e relativi prezzi...
foreach (var item in listCalc)
{
// chiedo BOM e immagine
await reqBomUpdate(item);
}
#if false
await DoRecalcOffer();
#endif
}
/// <summary>
/// Verifica e ricalcolo dei prezzi degli items nell'offerta
/// </summary>
/// <returns></returns>
protected async Task OfferUpdatePrices()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler ricalcolare a costi correnti offerta?"))
return;
await DoRecalcOffer(true);
}
/// <summary>
/// Verifica after render x stato interattivo pagina
/// </summary>
/// <param name="firstRender"></param>
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
// JS interop or data fetches go here
isInteractive = true;
}
}
/// <summary>
/// init obj
/// </summary>
protected override async Task OnInitializedAsync()
{
ConfInit();
prevJwd = "";
await ReloadBaseList();
DLService.PipeUpdate.EA_NewMessage += PipeUpdate_EA_NewMessage;
DLService.PipePng.EA_NewMessage += PipePng_EA_NewMessage;
DLService.PipeSvg.EA_NewMessage += PipeSvg_EA_NewMessage;
DLService.PipeHwOpt.EA_NewMessage += PipeHwOpt_EA_NewMessage;
DLService.PipeProfList.EA_NewMessage += PipeProfList_EA_NewMessage;
DLService.PipeShape.EA_NewMessage += PipeShape_EA_NewMessage;
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
UpdateTable();
}
/// <summary>
/// Prepara URL x download file JWD
/// </summary>
/// <param name="currUid"></param>
/// <param name="objType"></param>
/// <param name="envir"></param>
/// <returns></returns>
private string DownloadUrl(string currUid, string objType = "SOR", EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS envir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW)
{
return $"{apiUrl}/file/{currUid}?objType={objType}&env={envir}";
}
/// <summary>
/// Lancia la richiesta di ricaolo della BOM dal JWD (o equivalente)
/// </summary>
/// <returns></returns>
protected async Task RequestBom(OfferRowModel currRec)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler ricalcolare la BOM?"))
return;
await reqBomUpdate(currRec);
}
/// <summary>
/// Css di verifica riga selezionata
/// </summary>
/// <param name="selRow"></param>
/// <returns></returns>
protected string RowClass(OfferRowModel selRow)
{
return EditRecord != null && EditRecord.OfferRowID == selRow.OfferRowID ? "table-info" : "";
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private List<GenValueModel> AllColors = new();
private List<EnvirParamModel> AllConfEnvir = new();
private List<GlassModel> AllConfGlass = new();
private List<HardwareModel> AllConfHardware = new();
private List<WoodModel> AllConfWood = new();
private List<OfferRowModel> AllRecords = new List<OfferRowModel>();
private string apiUrl = "";
private List<string> AvailColorMaterialList = new List<string>();
private List<string> AvailFamilyHardwareList = new List<string>();
private List<string> AvailGlassList = new List<string>();
private List<Egw.Window.Data.Hardware> AvailHardwareList = new();
private List<string> AvailMaterialList = new List<string>();
private List<string> AvailProfileList = new List<string>();
private Dictionary<string, List<Threshold>> AvailThresholdDict = new Dictionary<string, List<Threshold>>();
/// <summary>
/// Base path x network share files
/// </summary>
private string basePath = "unsafe_uploads";
private string calcTag = "calc";
private EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS cEnvir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW;
/// <summary>
/// Channel update HwOptions
/// </summary>
private string chHwOpt = "";
/// <summary>
/// Channel update PNG
/// </summary>
private string chPng = "";
/// <summary>
/// Channel update Profile List
/// </summary>
private string chProfList = "";
/// <summary>
/// Channel update Shape
/// </summary>
private string chShape = "";
/// <summary>
/// Channel update SVG
/// </summary>
private string chSvg = "";
private List<BomItemDTO>? CurrBomList = null;
/// <summary>
/// Modalit editint attiva
/// </summary>
private EditMode CurrEditMode = EditMode.None;
private Dictionary<int, string> currGroupShape = new Dictionary<int, string>();
private Dictionary<int, string> currHwOption = new Dictionary<int, string>();
private int currPage = 1;
private string currPng = "";
private List<string> currProfList = new List<string>();
private string currSvg = "";
/// <summary>
/// Record in Edit corrente x modifica file/serializzato
/// </summary>
private OfferRowModel? EditRecord = null;
/// <summary>
/// Abilita edit massivo record ITEM
/// </summary>
private bool enableMassEdit = false;
private string genericBasePath = "";
private string imgBasePath = "";
/// <summary>
/// Semaforo x definire se sia gi in modalit ionterattiva o di prerendering
/// </summary>
private bool isInteractive = false;
private bool isLoading = false;
private List<OfferRowModel> ListRecords = new List<OfferRowModel>();
private List<SellingItemModel> ListSellItems = new List<SellingItemModel>();
private int numRecord = 10;
/// <summary>
/// Versione originale (pre edit)
/// </summary>
private string origJwd = "";
private List<string> PreparedFile = new();
/// <summary>
/// Versione precedente JWD x test e confronto
/// </summary>
private string prevJwd = "";
/// <summary>
/// Dizionario richieste
/// </summary>
private Dictionary<string, string> reqDict = new Dictionary<string, string>();
private bool reqSellingItem = false;
private int totalCount = 0;
#endregion Private Fields
#region Private Properties
private Dictionary<string, List<Threshold>> AvailThreshold { get; set; } = new Dictionary<string, List<Threshold>>()
{
{"Profilo78", new List<Threshold>() { new Threshold(3, "Bottom"), new Threshold(1, "Threshold")}},
{"ProfiloSaomad", new List<Threshold>(){ new Threshold(3, "Bottom"), new Threshold(2, "BottomWaterdrip"), new Threshold(1, "Threshold")}}
};
#endregion Private Properties
#region Private Methods
/// <summary>
/// Effettua vera richiesta della BOM
/// </summary>
/// <returns></returns>
private async Task callRefreshProfList()
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
var cMode = Egw.Window.Data.Enums.QuestionModes.CONFIG;
var cSubMode = Egw.Window.Data.Enums.QuestionConfSubModes.PROFILELIST;
// compongo righiesta
string reqUid = "Default";
DictExec.Add("Mode", $"{(int)cMode}");
DictExec.Add("UID", reqUid);
// creo registrazione richiesta...
var ruid = await CRService.AddRequestAsync($"{cEnvir}", $"{cMode}-{cSubMode}", reqUid);
// aggiungo RUID effettivo
DictExec.Add("RUID", ruid);
DictExec.Add("SubMode", $"{(int)cSubMode}");
CalcRequestDTO req = new CalcRequestDTO()
{
EnvType = cEnvir,
DictExec = DictExec
};
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await CService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{reqUid}", req);
}
/// <summary>
/// Chiude edit andando eventualmente a salvare
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private async Task CloseEdit(bool doSave)
{
// Proseguo solo se sono in interattivo (NO prerender pagina)
if (isInteractive)
{
// ...se ho editing
if (EditRecord != null)
{
bool updateBom = false;
// SE richiesto salvataggio...
if (doSave)
{
// salvo su DB!
await DLService.OffertRowUpdateSerStruct(EditRecord.OfferRowID, prevJwd);
// salvo nel record corrente!
EditRecord.SerStruct = prevJwd;
updateBom = true;
}
else
// altrimenti ricalcolo valore salvato
{
prevJwd = EditRecord.SerStruct;
CurrData.CurrJwd = EditRecord.SerStruct;
}
if (updateBom)
{
await reqBomUpdate(EditRecord);
}
// aggiorno nel dizionari
if (reqDict.ContainsKey("SerializedData"))
{
reqDict["SerializedData"] = prevJwd;
}
if (reqDict != null && reqDict.Count > 0)
{
// chiamo richiesta update
CalcRequestDTO calcRequestDTO = new CalcRequestDTO();
calcRequestDTO.EnvType = EditRecord.Envir;
calcRequestDTO.DictExec = reqDict;
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await ICService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{EditRecord.OfferRowUID}", calcRequestDTO);
}
EditRecord = null;
CurrEditMode = EditMode.None;
}
}
}
private void ConfInit()
{
basePath = Config.GetValue<string>("ServerConf:FileSharePath") ?? "unsafe_uploads";
apiUrl = Config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "";
imgBasePath = Config.GetValue<string>("ServerConf:ImageBaseUrl") ?? "";
genericBasePath = Config.GetValue<string>("ServerConf:GenericBaseUrl") ?? "";
calcTag = Config.GetValue<string>("ServerConf:CalcTag") ?? "calc";
chHwOpt = Config.GetValue<string>("ServerConf:ChannelHwOpt") ?? "";
chPng = Config.GetValue<string>("ServerConf:ChannelPng") ?? "";
chProfList = Config.GetValue<string>("ServerConf:ChannelProfList") ?? "";
chShape = Config.GetValue<string>("ServerConf:ChannelShape") ?? "";
chSvg = Config.GetValue<string>("ServerConf:ChannelSvg") ?? "";
}
/// <summary>
/// Esecuzione azione richiesta
/// </summary>
/// <param name="actReq">Azione richiesta</param>
private void DoAction(LayoutConst.DataAction actReq)
{
switch (actReq)
{
case LayoutConst.DataAction.None:
break;
case LayoutConst.DataAction.ResetDictShape:
CurrData.DictShape = new Dictionary<int, string>();
break;
case LayoutConst.DataAction.ResetHwOpt:
CurrData.DictOptionsXml = new Dictionary<int, string>();
break;
default:
break;
}
}
private async Task DoRecalcOffer(bool forceResetCalc)
{
isLoading = true;
if (forceResetCalc)
{
await setAwaitPrice(true, false);
UpdateTable();
await InvokeAsync(StateHasChanged);
await Task.Delay(300);
}
await DLService.OffertUpdateCost(OfferID);
if (forceResetCalc)
{
await Task.Delay(300);
await setAwaitPrice(false, true);
}
// rileggo dati
await ReloadData();
UpdateTable();
isLoading = false;
await EC_Updated.InvokeAsync(true);
}
/// <summary>
/// Salvataggio del JWD aggiornato nella mia riga di offerta
/// </summary>
/// <param name="CurrArgs"></param>
private async void ExecRequest(Dictionary<string, string> CurrArgs)
{
// Proseguo solo se sono in interattivo (NO prerender pagina)
if (isInteractive)
{
// ...se ho editing
if (EditRecord != null)
{
// SE contiene il mio Jwd...
if (CurrArgs.ContainsKey("SerializedData"))
{
string serStruct = CurrArgs["SerializedData"];
// controllo SE variato...
if (!prevJwd.Equals(serStruct) || !EgwCoreLib.Utils.DictUtils.DictAreEqual(reqDict, CurrArgs))
{
// aggiorno val prev
reqDict = CurrArgs;
prevJwd = serStruct;
// aggiorno live data
CurrData.CurrJwd = serStruct;
// chiamo richiesta update
CalcRequestDTO calcRequestDTO = new CalcRequestDTO();
calcRequestDTO.EnvType = EditRecord.Envir;
calcRequestDTO.DictExec = reqDict;
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await ICService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{EditRecord.OfferRowUID}", calcRequestDTO);
#if false
// salvo su DB!
await DLService.OffertRowUpdateSerStruct(EditRecord.OfferRowID, serStruct);
#endif
}
}
}
}
}
/// <summary>
/// Svuota cache corrente + rilegge dati
/// </summary>
private async Task ForceReloadData()
{
isLoading = true;
CurrEditMode = EditMode.None;
EditRecord = null;
await Task.Delay(20);
await DLService.FlushCacheOffersAsync();
await Task.Delay(20);
await ReloadData();
UpdateTable();
isLoading = false;
}
/// <summary>
/// Restituisce il contenuto del file salvato
/// </summary>
/// <param name="folderPath"></param>
/// <param name="secureName"></param>
/// <returns></returns>
private string LoadFileContent(string folderPath, string secureName)
{
string answ = "";
if (!string.IsNullOrEmpty(folderPath))
{
try
{
// calcolo path file...
string filePath = Path.Combine(basePath, folderPath, secureName);
if (File.Exists(filePath))
{
answ = File.ReadAllText(filePath);
}
}
catch (Exception exc)
{
Log.Error($"Exception on LoadFileContent{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Ricevuto HwOpt, processo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeHwOpt_EA_NewMessage(object? sender, EventArgs e)
{
// vale SOLO SE sono in editing...
if (EditRecord != null)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{chHwOpt}:{EditRecord.OfferRowUID}"))
{
var rawDict = JsonConvert.DeserializeObject<Dictionary<int, string>>(currArgs.newMessage) ?? new Dictionary<int, string>();
currHwOption = rawDict;
// salvo in live data...
CurrData.DictOptionsXml = currHwOption;
}
await InvokeAsync(StateHasChanged);
}
}
}
private async void PipePng_EA_NewMessage(object? sender, EventArgs e)
{
// vale SOLO SE sono in editing...
if (EditRecord != null)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{chPng}:{EditRecord.OfferRowUID}"))
{
currPng = currArgs.newMessage;
// non devo passarlo al componente...
#if false
// salvo in live data...
CurrData.SvgPreview = currSvg;
#endif
}
await InvokeAsync(StateHasChanged);
}
}
}
/// <summary>
/// Ricevuta profile list, processo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeProfList_EA_NewMessage(object? sender, EventArgs e)
{
// vale SOLO SE sono in editing...
if (EditRecord != null)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{chProfList}:{EditRecord.OfferRowUID}"))
{
try
{
// new List<string>();
currProfList = JsonConvert.DeserializeObject<List<string>>(currArgs.newMessage) ?? SetupList.Profile;
// salvo in live data...
SetupList.Profile = currProfList;
}
catch
{ }
}
await InvokeAsync(StateHasChanged);
}
}
}
/// <summary>
/// Ricevuta shape, procersso
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeShape_EA_NewMessage(object? sender, EventArgs e)
{
// vale SOLO SE sono in editing...
if (EditRecord != null)
{
#if false
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{shapeChannel}:{EditRecord.OfferRowUID}"))
{
currGroupShape = currArgs.newMessage;
// salvo in live data...
CurrData.DictShape = currGroupShape;
}
await InvokeAsync(StateHasChanged);
}
#endif
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.StartsWith($"{chShape}:{EditRecord.OfferRowUID}"))
{
// deserializzo il dizionario delle risposte...
var rawDict = JsonConvert.DeserializeObject<Dictionary<int, string>>(currArgs.newMessage);
#if false
int groupId = 0;
// verifica del groupID...
int.TryParse(currArgs.msgUid.Replace($"{shapeChannel}:{windowUid}:", ""), out groupId);
if (currGroupShape.ContainsKey(groupId))
{
currGroupShape[groupId] = currArgs.newMessage;
}
else
{
currGroupShape.Add(groupId, currArgs.newMessage);
}
#endif
currGroupShape = rawDict ?? new Dictionary<int, string>();
CurrData.DictShape = currGroupShape;
}
await InvokeAsync(StateHasChanged);
}
}
}
/// <summary>
/// Ricevuto SVG, se il mio lo aggiorno...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeSvg_EA_NewMessage(object? sender, EventArgs e)
{
// vale SOLO SE sono in editing...
if (EditRecord != null)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{chSvg}:{EditRecord.OfferRowUID}"))
{
currSvg = currArgs.newMessage;
// salvo in live data...
CurrData.SvgPreview = currSvg;
}
await InvokeAsync(StateHasChanged);
}
}
}
/// <summary>
/// Task verifica update ricevuti
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void PipeUpdate_EA_NewMessage(object? sender, EventArgs e)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
// cerco se faccia parte dei record correnti...
var recFound = AllRecords.Any(x => x.OfferRowUID == currArgs.newMessage);
if (recFound)
{
isLoading = true;
await Task.Delay(1);
// se si tratta dell'UID corrente --> fa update
await DoRecalcOffer(false);
await InvokeAsync(StateHasChanged);
}
}
await Task.Delay(1);
}
/// <summary>
/// Preparazione dati x componente edit JWD
/// </summary>
/// <param name="currJwd"></param>
private void PrepareWindowData(string currJwd)
{
// preparo conf oggetti x controllo
SetupList = new BaseListPayload()
{
ColorMaterial = AvailColorMaterialList,
FamilyHardware = AvailFamilyHardwareList,
Glass = AvailGlassList,
Hardware = AvailHardwareList,
Material = AvailMaterialList,
Profile = AvailProfileList,
Threshold = AvailThreshold
};
CurrData = new LivePayload()
{
CurrJwd = currJwd,
SvgPreview = currSvg
};
}
/// <summary>
/// init classi configurazione
/// </summary>
/// <returns></returns>
private async Task ReloadBaseList()
{
// lettura config setup varie da DB/Cache Redis
AllConfEnvir = await DLService.ConfEnvirParamGetAllAsync();
AllConfGlass = await DLService.ConfGlassGetAllAsync();
var rawProfiles = CDService.ProfileList(cEnvir, "Default");
// se fosse vuoto chiamo update...
if (rawProfiles == null || rawProfiles.Count == 0)
{
await callRefreshProfList();
// aspetto 200ms... e richiedo!
await Task.Delay(200);
rawProfiles = CDService.ProfileList(cEnvir, "Default");
}
AvailProfileList = rawProfiles;
#if false
// dizionario dei profili soglia
AvailThresholdDict = CDService.ProfileThreshDict(cEnvir);
#endif
var rawHw = CDService.HwModelList(cEnvir, "HW.AGB");
// hw filtro solo validi...
AllConfHardware = rawHw
.Where(x => !x.FamilyName.Equals(x.Description, StringComparison.OrdinalIgnoreCase))
.ToList();
AllConfWood = await DLService.ConfWoodGetAllAsync();
AllColors = await DLService.GenValGetFiltAsync("WoodCol");
// conversione tipi
AvailGlassList = AllConfGlass
.Select(x => x.Description)
.ToList();
AvailFamilyHardwareList = AllConfHardware
.DistinctBy(x => x.FamilyName)
.OrderBy(x => x.FamilyName)
.Select(x => x.FamilyName)
.ToList();
AvailHardwareList = AllConfHardware
.Select(x => new Egw.Window.Data.Hardware(x.Id, x.FamilyName, x.Description, x.OpeningType, x.Shape, x.SashQty, x.SashPosition))
.ToList();
AvailMaterialList = AllConfWood
.Select(x => x.Description)
.ToList();
// FixMe Todo: aggiunta profili (manca anche nel costruttore...)
AvailColorMaterialList = AllColors
.OrderBy(x => x.Index)
.Select(x => x.ValString)
.ToList();
}
/// <summary>
/// Legge i dati dei record completi
/// </summary>
private async Task ReloadData()
{
if (OfferID > 0)
{
AllRecords = await DLService.OfferRowGetByOffer(OfferID);
totalCount = AllRecords.Count();
}
}
/// <summary>
/// Eliminazione file (old)
/// </summary>
/// <param name="secureName">Nome secure da impiegare</param>
/// <param name="content">Contenuto file</param>
private bool DeleteOldFile(string folderPath, string secureName)
{
bool answ = false;
if (!string.IsNullOrEmpty(folderPath))
{
// calcolo path file...
string filePath = Path.Combine(basePath, folderPath, secureName);
// se esiste...
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
return answ;
}
/// <summary>
/// Effettua vera richiesta della BOM
/// </summary>
/// <param name="currRec"></param>
/// <returns></returns>
private async Task reqBomUpdate(OfferRowModel currRec)
{
// salvo richiesta BOM su record
currRec.AwaitBom = true;
currRec.AwaitPrice = true;
await DLService.OffertUpdateAwaitState(currRec.OfferRowID, true, true);
// preparo la domanda serializzata
Dictionary<string, string> DictExec = new Dictionary<string, string>();
// verifico parametri da conf envir...
var envRec = AllConfEnvir.FirstOrDefault(x => x.EnvirID == currRec.Envir);
string serKey = envRec != null ? envRec.SerStrucKey : "SerializedData";
// cablata la BOM
DictExec.Add("Mode", $"{(int)Egw.Window.Data.Enums.QuestionModes.BOM}");
// UID cablato x ora...
DictExec.Add("UID", currRec.OfferRowUID);
// aggiungo file secondo ambiente...
switch (currRec.Envir)
{
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW:
DictExec.Add(serKey, currRec.SerStruct);
break;
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.BEAM:
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WALL:
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.CABINET:
// rileggo da file...
string folderPath = $"SO-{currRec.OfferID:X8}";
string rawData = LoadFileContent(folderPath, currRec.FileResource);
DictExec.Add(serKey, rawData);
DictExec.Add("FileName", currRec.FileName);
break;
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.NULL:
default:
break;
}
CalcRequestDTO req = new CalcRequestDTO()
{
EnvType = currRec.Envir,
DictExec = DictExec
};
await InvokeAsync(StateHasChanged);
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
await CService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{currRec.OfferRowUID}", req);
}
/// <summary>
/// Salvataggio dei dati del file caricato
/// </summary>
/// <param name="fileDict">Dizionario info file</param>
private void SaveFile(Dictionary<string, string> fileDict)
{
// verifico di essere in edit...
if (EditRecord != null)
{
string folderPath = $"SO-{EditRecord.OfferID:X8}";
// verifico di avere parametri...
if (fileDict != null && fileDict.Count > 0)
{
string secureName = "";
string content = "";
if (fileDict.ContainsKey("secureName"))
{
secureName = fileDict["secureName"];
}
if (fileDict.ContainsKey("content"))
{
content = fileDict["content"];
}
if (!string.IsNullOrEmpty(folderPath) && !string.IsNullOrEmpty(secureName) && !string.IsNullOrEmpty(content))
{
// salvo!
saveFileContent(folderPath, secureName, content);
}
}
// altrimenti signifca cleanup eventuale vecchio file...
else
{
DeleteOldFile(folderPath, EditRecord.FileResource);
}
}
}
/// <summary>
/// Esegue salvataggio del file ricevuto
/// </summary>
/// <param name="folderPath">Path relativo x file (tipicamente UID parent order)</param>
/// <param name="secureName">Nome secure da impiegare</param>
/// <param name="content">Contenuto file</param>
private bool saveFileContent(string folderPath, string secureName, string content)
{
bool answ = false;
if (!string.IsNullOrEmpty(folderPath))
{
// calcolo path file...
string filePath = Path.Combine(basePath, folderPath, secureName);
string? directoryPath = Path.GetDirectoryName(filePath);
try
{
if (!string.IsNullOrEmpty(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(filePath, content);
}
catch (Exception exc)
{
Log.Error($"Exception on save{Environment.NewLine}{exc}");
}
}
return answ;
}
/// <summary>
/// Selezione e fix dati BOM
/// </summary>
/// <param name="currRow"></param>
/// <returns></returns>
private void selectBom(OfferRowModel currRow)
{
EditRecord = currRow;
CurrBomList = DLService.OffertGetBomList(EditRecord);
if (CurrBomList.Any(x => x.ItemID == 0))
{
CurrBomList = DLService.BomFixItemId(CurrBomList);
}
}
private async Task setAwaitPrice(bool awaitPrice, bool flushCache)
{
foreach (var item in AllRecords)
{
item.AwaitPrice = awaitPrice;
await DLService.OffertUpdateAwaitState(item.OfferRowID, null, awaitPrice, flushCache);
}
}
/// <summary>
/// Verifia ammissibilit display btn ricalcolo BOM da item
/// </summary>
/// <param name="reqItem"></param>
/// <returns></returns>
private bool ShowBom(OfferRowModel reqItem)
{
bool answ = false;
if (DisplayMode == EgwCoreLib.Lux.Core.Enums.DisplayMode.Edit)
{
switch (reqItem.Envir)
{
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW:
answ = !string.IsNullOrEmpty(reqItem.SerStruct) && reqItem.SerStruct.Length > 2;
break;
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.BEAM:
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WALL:
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.CABINET:
// da cambiare con ricerca file su disco?!?
answ = !string.IsNullOrEmpty(reqItem.FileResource) && !string.IsNullOrEmpty(reqItem.FileName);
break;
case EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.NULL:
default:
break;
}
}
return answ;
}
/// <summary>
/// Elenco SalesOfferRows calcolabili:
/// - contengono serializzazione come JWD
/// - contengono file come BTL
/// </summary>
private List<OfferRowModel> SorListCalc()
{
var rawList = AllRecords
.Where(x => (!string.IsNullOrEmpty(x.SerStruct) && x.SerStruct.Length > 2) || (!string.IsNullOrEmpty(x.FileName) && !string.IsNullOrEmpty(x.FileResource)))
.ToList();
return rawList ?? new List<OfferRowModel>();
}
/// <summary>
/// Toggle visibilit modifica file indicando ID della OfferRow corrente (o zero se deselect)
/// </summary>
private void ToggleFileEdit(OfferRowModel? currRec)
{
CurrEditMode = currRec == null ? EditMode.None : EditMode.File;
EditRecord = currRec;
}
/// <summary>
/// Salva nel record corrente la BOM aggiornata e poi ricalcola importo...
/// </summary>
/// <param name="newBomList"></param>
/// <exception cref="NotImplementedException"></exception>
private async void UpdateBom(List<BomItemDTO> newBomList)
{
if (EditRecord != null)
{
// salvo BOM nel record corrente...
bool fatto = await DLService.OffertRowUpdateBom(EditRecord.OfferRowID, newBomList);
// ricalcolo offerta completa
await ReloadData();
UpdateTable();
// rilegge il record da elenco appena rinfrescato...
int offerRowId = EditRecord.OfferRowID;
var updRec = AllRecords.FirstOrDefault(x => x.OfferRowID == offerRowId);
if (updRec != null)
{
CurrBomList = new List<BomItemDTO>();
// fa refresh dei dati della BOM visualizzata
selectBom(updRec);
await InvokeAsync(StateHasChanged);
}
}
}
/// <summary>
/// Filtro e paginazione
/// </summary>
private void UpdateTable()
{
// fix paginazione
ListRecords = AllRecords
.OrderBy(x => x.RowNum)
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
}
/// <summary>
/// Esegue lettura file + invio richiesta specifica
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private async Task UploadFile(InputFileChangeEventArgs e)
{
// Proseguo solo se sono in interattivo (NO prerender pagina)
if (isInteractive)
{
if (EditRecord != null)
{
// init dizionari arg richiesta update
Dictionary<string, string> fileArgs = new Dictionary<string, string>();
// leggo il contenuto del PRIMO (singolo) file
IBrowserFile file = e.File;
// limite file size (al momento 10 MB)
var maxAllowedSize = 10 * 1024 * 1024;
using var stream = file.OpenReadStream(maxAllowedSize);
using var reader = new StreamReader(stream);
string rawContent = await reader.ReadToEndAsync();
// calcolo il nome del file trusted...
string trustedFileName = Path.GetRandomFileName();
EditRecord.FileResource = trustedFileName;
EditRecord.FileName = file.Name;
EditRecord.FileSize = rawContent.LongCount();
// salvo sul DB i dati (nome, nome sicuro, size...)
await DLService.OffertRowUpdateFileData(EditRecord);
// parametri richiesta
fileArgs.Add("Mode", $"{(int)Egw.Window.Data.Enums.QuestionModes.PREVIEW}");
fileArgs.Add("SubMode", "2");
fileArgs.Add("FileName", $"{file.Name}");
fileArgs.Add("Height", "1200");
fileArgs.Add("Width", "1800");
//fileArgs.Add("Btl", rawContent);
fileArgs.Add("SerializedData", rawContent);
// invio!
CalcRequestDTO calcRequestDTO = new CalcRequestDTO();
calcRequestDTO.EnvType = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.BEAM;
calcRequestDTO.DictExec = fileArgs;
await ICService.CallRestPost($"{apiUrl}/{genericBasePath}", $"{calcTag}/{EditRecord.OfferRowUID}", calcRequestDTO);
// ora chiedo anche la BOM!
#if false
// salvo in locale il file: SISTEMARE PERMESSI
saveFileContent(EditFileRecord.OfferRowUID, trustedFileName, rawContent);
#endif
}
}
}
#endregion Private Methods
}
}