using EgwMultiEngineManager.Data; namespace Lux.UI.Components.Compo.Common { 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; } #endregion Public Properties #region Protected Methods /// /// 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; } } #endregion Protected Methods #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 Fields /// /// Dizionario interno oggetti x editare con indice /// private List bomDict = new List(); private List bomPaged = new List(); private int currPage = 1; private bool massEdit = false; 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 #region Private Properties private double ImportoTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.TotalCost); } return valTot; } } [Inject] private IItemService IService { get; set; } = null!; private double QtyTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.Qty); } return valTot; } } private bool SelAll { get => selAll; set { if (selAll != value) { selAll = value; foreach (var item in bomDict) { item.isSelected = selAll; } } } } private bool showMassEditSave { get => bomDict != null && bomDict.Any(x => x.isSelected); } private void ButtonMassEdit() { massEdit = !massEdit; } private bool ShowVolume { get => CurrRowRec.Envir == Constants.EXECENVIRONMENTS.BEAM || CurrRowRec.Envir == Constants.EXECENVIRONMENTS.WALL; } private int TotItemQty { get { int numTot = 0; if (bomDict != null) { numTot = bomDict.Sum(x => x.ItemQty); } return numTot; } } private double VolTotale { get { double valTot = 0; if (bomDict != null) { valTot = bomDict.Sum(x => x.Volume); } return valTot; } } #endregion Private Properties #region Private Methods private void DoCancel() { EditRecord = null; isLoading = false; } private async Task DoEdit(BomDtoSel editRec) { if (editRec.ItemID > 0) { // carico elenco alternative... ListItemAlt = await IService.GetAltAsync(editRec.ItemID); // riordino... ListItemAlt = ListItemAlt .OrderBy(x => x.ItemIDParent) .ThenBy(x => x.Description) .ToList(); } else { ListItemAlt = new List(); } EditRecord = editRec; } /// /// Salvataggio con conversion a list + ritorno a parent /// private 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(); return EC_Updated.InvokeAsync(baseList); } private async Task ForceItemPrice() { isLoading = true; // chiamo metodo update x i soli valori selezionati... var list2upd = bomDict.Where(x => x.isSelected).Cast().ToList(); await IService.MassUpdateAsync(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; } private void SaveNumRec(int newNum) { numRecord = newNum; currPage = 1; UpdateTable(); } private void SavePage(int newNum) { currPage = newNum; UpdateTable(); } /// /// 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 } }