using EgwCoreLib.Lux.Core.RestPayload; using EgwCoreLib.Lux.Data.DbModel.Items; using EgwCoreLib.Lux.Data.DbModel.Sales; using EgwCoreLib.Lux.Data.Services; using EgwMultiEngineManager.Data; using Microsoft.AspNetCore.Components; using RestSharp.Serializers.Xml; using System.Collections.Generic; using System.Threading.Tasks; namespace Lux.UI.Components.Compo { public partial class EditBom { #region Public Properties [Parameter] public List BomList { get; set; } = null!; [Parameter] public OfferRowModel CurrRowRec { get; set; } = null!; [Parameter] public EventCallback> EC_Updated { get; set; } [Parameter] public bool MassEdit { get; set; } = false; #endregion Public Properties #region Protected Properties [Inject] protected DataLayerServices DLService { get; set; } = null!; protected double ImportoTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.TotalCost); } return valTot; } } protected double QtyTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.Qty); } return valTot; } } protected int TotItemQty { get { int numTot = 0; if (bomDict != null) { numTot = bomDict.Sum(x => x.ItemQty); } return numTot; } } protected bool SelAll { get => selAll; set { if (selAll != value) { selAll = value; foreach (var item in bomDict) { item.isSelected = selAll; } } } } protected double VolTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.Volume); } return valTot; } } #endregion Protected Properties #region Protected Methods protected void DoCancel() { EditRecord = null; isLoading = false; } protected void DoEdit(BomDtoSel editRec) { if (editRec.ItemID > 0) { // carico elenco alternative... ListItemAlt = DLService.ItemGetAlt(editRec.ItemID); // riordino... ListItemAlt = ListItemAlt .OrderBy(x => x.ItemIDParent) .ThenBy(x => x.Description) .ToList(); } else { ListItemAlt = new List(); } EditRecord = editRec; } /// /// Conversione oggetti x editing /// protected override void OnParametersSet() { if (BomList != null && BomList.Count > 0) { int idx = 1; // Convert List to Dictionary with index as key bomDict = BomList .Select(x => new BomDtoSel() { isSelected = false, numRow = idx++, ClassCode = x.ClassCode, DescriptionCode = x.DescriptionCode, ItemCode = x.ItemCode, ItemID = x.ItemID, ItemQty = x.ItemQty, Price = x.Price, PriceEff = x.PriceEff, Qty = x.Qty, Volume = x.Volume }) .ToList(); totalCount = BomList.Count(); UpdateTable(); isLoading = false; } else { isLoading = true; } } protected void SaveNumRec(int newNum) { numRecord = newNum; UpdateTable(); } protected void SavePage(int newNum) { currPage = newNum; UpdateTable(); } #endregion Protected Methods #region Private Fields /// /// Dizionario interno oggetti x editare con indice /// private List bomDict = new List(); private List bomPaged = new List(); private int currPage = 1; private decimal defMargin = 0.2M; private double defQtyMax = 999; private int defRound = 5; private string defUM = "m"; private BomDtoSel? EditRecord = null; private bool isLoading = false; private List ListItemAlt = new List(); private int numRecord = 10; private bool selAll = false; private int totalCount = 0; private double totCost = 100; #endregion Private Fields protected async Task ForceItemPrice() { isLoading = true; // chiamo metodo update x i soli valori selezionati... var list2upd = bomDict.Where(x => x.isSelected).Cast().ToList(); await DLService.ItemMassUpdate(list2upd, totCost, (double)defMargin, defQtyMax, defUM, defRound); // ...e passo al controller parent LINQ projection to base type List baseList = bomDict.Cast().ToList(); await EC_Updated.InvokeAsync(baseList); SelAll = false; } protected bool showMassEditSave { get => bomDict != null && bomDict.Any(x => x.isSelected); } #region Protected Classes protected class BomDtoSel : BomItemDTO { #region Public Properties public bool isSelected { get; set; } = false; public int numRow { get; set; } = 1; #endregion Public Properties } #endregion Protected Classes #region Private Properties private bool ShowVolume { get => CurrRowRec.Envir == Constants.EXECENVIRONMENTS.BEAM || CurrRowRec.Envir == Constants.EXECENVIRONMENTS.WALL; } #endregion Private Properties #region Private Methods /// /// Salvataggio con conversion a list + ritorno a parent /// private async Task DoSave() { isLoading = true; // aggiorno l'oggetto nel mio dizionario if (EditRecord != null) { // modifico il valore ID con quello selezionato nel selettore x l'oggetto UpdateItem(EditRecord); } // deseleziono... DoCancel(); // ...e passo al controller parent LINQ projection to base type List baseList = bomDict.Cast().ToList(); await EC_Updated.InvokeAsync(baseList); } /// /// Update item nel dictionary /// /// /// private void UpdateItem(BomDtoSel updatedItem) { int index = bomDict.FindIndex(x => x.numRow == updatedItem.numRow); if (index >= 0) { bomDict[index] = updatedItem; } } /// /// Filtro e paginazione /// private void UpdateTable() { // fix paginazione bomPaged = bomDict .Skip(numRecord * (currPage - 1)) .Take(numRecord) .ToList(); isLoading = false; } #endregion Private Methods } }