首页 > 解决方案 > 为什么我的操作比当前过滤的记录影响更多?

问题描述

我已经用我自己的一个覆盖了Records数据视图ARSalesPriceMaint,以便也覆盖数据视图委托records。这是必要的,因为我已经自定义了页面过滤器。这很好用。

我添加了一个操作,它应该获取结果记录(过滤后)并更新 DAC 扩展中的自定义字段。这确实更新了记录,但它不尊重记录上的过滤器,尤其是网格过滤器。例如,过滤UOM列会影响视图,但我的操作会更新所有 UOM。

那么问题来了为什么我的操作foreach语句的记录比网格中的记录多?

我相信这是问题区域,但整个代码也在下面:

(这是来自UpdateDifs操作,而不是数据视图委托)

foreach (PXResult<ARSalesPrice> result in Records.View.Select(null, null,
            PXView.Searches,
            Base.Records.View.GetExternalSorts(),
            Base.Records.View.GetExternalDescendings(),
            Base.Records.View.GetExternalFilters() ?? new PXFilterRow[0],
            ref startRow, 0, ref totalRows))
        {
            // more code
        }

完整代码:

public class ARSalesPriceMaint_Extension : PXGraphExtension<ARSalesPriceMaint>
{
    public PXFilter<ARSalesPricesExtDialog> UpdatePriceFactorsDialog;
    
    [PXFilterable]
    public SelectFrom<ARSalesPrice>.View Records;

    #region Data View Delegates

    protected IEnumerable records()
    {
        int startRow = 0;
        int totalRows = 0;

        if (PXView.MaximumRows == 1
            && PXView.SortColumns?.Length > 0 && PXView.SortColumns[0].Equals(nameof(ARSalesPrice.RecordID), StringComparison.OrdinalIgnoreCase)
            && PXView.Searches?.Length > 0 && PXView.Searches[0] != null)
        {
            var cached = Records.Cache.Locate(new ARSalesPrice { RecordID = Convert.ToInt32(PXView.Searches[0]) });
            if (cached != null)
                return new[] { cached };
        }

        ARSalesPriceFilter filter = Base.Filter.Current;
        ARSalesPriceFilterExt filterExt = PXCache<ARSalesPriceFilter>.GetExtension<ARSalesPriceFilterExt>(filter);

        var priceCode = ParsePriceCode(Base, filter.PriceType, filter.PriceCode);

        var list = new ArrayList();

        // Set filters
        var filterParams = new object[]
        {
                filter.PriceType, filter.PriceType,
                filter.PriceType == PriceTypes.Customer ? priceCode : null,
                filter.PriceType == PriceTypes.CustomerPriceClass ? priceCode : null,
                priceCode,
                filter.InventoryID, filter.InventoryID,
                filter.SiteID, filter.SiteID,
                filter.EffectiveAsOfDate, filter.EffectiveAsOfDate, filter.EffectiveAsOfDate,
                filter.ItemClassCD, filter.ItemClassCDWildcard,
                filter.InventoryPriceClassID, filter.InventoryPriceClassID,
                filter.OwnerID, filter.OwnerID,
                filter.MyWorkGroup,
                filter.WorkGroupID, filter.WorkGroupID
        };

        var records = Base.Records.View.Select(null, filterParams,
            PXView.Searches,
            Base.Records.View.GetExternalSorts(),
            Base.Records.View.GetExternalDescendings(),
            Base.Records.View.GetExternalFilters() ?? new PXFilterRow[0],
            ref startRow, 0, ref totalRows);

        if (filterExt.UsrComponentID != null)
        {
            foreach (PXResult<ARSalesPrice, INItemClass, BAccount> result in records)
            {
                ARSalesPrice salesPrice = result;
                InventoryItem inItem = ARSalesPrice.FK.InventoryItem.FindParent(Base, salesPrice);
                if ((inItem.KitItem ?? false) == false) continue;

                // Get kits containing the selected component
                var kitsContainingComps = SelectFrom<INKitSpecStkDet>.
                    InnerJoin<INKitSpecHdr>.
                        On<INKitSpecHdr.kitInventoryID.IsEqual<INKitSpecStkDet.kitInventoryID>.
                        And<INKitSpecHdr.revisionID.IsEqual<INKitSpecStkDet.revisionID>>>.
                    Where<INKitSpecStkDet.kitInventoryID.IsEqual<@P.AsInt>.
                        And<INKitSpecStkDet.compInventoryID.IsEqual<@P.AsInt>>.
                        And<INKitSpecHdr.isActive.IsEqual<boolTrue>>>.View.Select(new PXGraph(), salesPrice.InventoryID, filterExt.UsrComponentID);

                // If kits exist with that component, add the item to the list
                if (kitsContainingComps != null && kitsContainingComps.Count > 0)
                    list.Add(salesPrice);
            }
            return list;
        }
        else
            return records;
    }

    #endregion

    #region Event Handlers

    protected virtual void _(Events.FieldUpdated<ARSalesPriceFilterExt.usrComponentID> e)
    {
        Records.View.RequestRefresh();
    }

    #endregion

    #region Actions

    /// <summary>
    /// Mass updates pricing factors
    /// </summary>
    public PXAction<ARSalesPriceFilter> updateDifs;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Update Difs")]
    protected virtual void UpdateDifs()
    {
        int startRow = 0;
        int totalRows = 0;

        // Check for filter
        if (Base.Records.View.GetExternalFilters() == null && Base.Filter.Current.GetExtension<ARSalesPriceFilterExt>().UsrComponentID == null)
        {
            if (Base.Filter.Ask("No Filter Set", "A filter has not been set. Make sure you have filtered to the items you wish to update. Continue?", MessageButtons.YesNo, MessageIcon.Question) != WebDialogResult.Yes)
                throw new PXException();
        }

        // Show dialog
        UpdatePriceFactorsDialog.View.Clear();
        if (UpdatePriceFactorsDialog.AskExt((graph, view) =>
        {
            UpdatePriceFactorsDialog.Cache.Clear();
            UpdatePriceFactorsDialog.Current = new ARSalesPricesExtDialog();
        }, true) != WebDialogResult.OK) return;

        foreach (PXResult<ARSalesPrice> result in Records.View.Select(null, null,
            PXView.Searches,
            Base.Records.View.GetExternalSorts(),
            Base.Records.View.GetExternalDescendings(),
            Base.Records.View.GetExternalFilters() ?? new PXFilterRow[0],
            ref startRow, 0, ref totalRows))
        {
            ARSalesPrice salesPrice = result;
            ARSalesPriceExt salesPriceExt = salesPrice.GetExtension<ARSalesPriceExt>();

            if (UpdatePriceFactorsDialog.Current.Calculation != null && UpdatePriceFactorsDialog.Current.Calculation != "UC")
                Base.Records.SetValueExt<ARSalesPriceExt.usrCalculation>(result, UpdatePriceFactorsDialog.Current.Calculation);
            
            if (UpdatePriceFactorsDialog.Current.DifPctSelected ?? false)
            {
                switch (UpdatePriceFactorsDialog.Current.DifPctAction)
                {
                    case "EQ":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifPct>(result, UpdatePriceFactorsDialog.Current.DifPct);
                        break;

                    case "PL":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifPct>(result, decimal.Add(salesPriceExt.UsrDifPct ?? 0, UpdatePriceFactorsDialog.Current.DifPct ?? 0));
                        break;

                    case "MI":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifPct>(result, decimal.Subtract(salesPriceExt.UsrDifPct ?? 0, UpdatePriceFactorsDialog.Current.DifPct ?? 0));
                        break;
                }
            }
            
            if (UpdatePriceFactorsDialog.Current.DifAmtSelected ?? false)
            {
                switch (UpdatePriceFactorsDialog.Current.DifAmtAction)
                {
                    case "EQ":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifAmt>(result, UpdatePriceFactorsDialog.Current.DifAmt);
                        break;

                    case "PL":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifAmt>(result, decimal.Add(salesPriceExt.UsrDifAmt ?? 0, UpdatePriceFactorsDialog.Current.DifAmt ?? 0));
                        break;

                    case "MI":
                        Base.Records.SetValueExt<ARSalesPriceExt.usrDifAmt>(result, decimal.Subtract(salesPriceExt.UsrDifAmt ?? 0, UpdatePriceFactorsDialog.Current.DifAmt ?? 0));
                        break;
                }
            }
            Base.Records.Update(result);
        };
        Base.Actions.PressSave();
    }

    #endregion
      
    #region Methods
    
    private static string ParsePriceCode(PXGraph graph, string priceType, string priceCode)
    {
        if (priceCode != null)
        {
            if (priceType == PriceTypes.Customer)
            {
                var customerRepository = new CustomerRepository(graph);

                Customer customer = customerRepository.FindByCD(priceCode);
                if (customer != null)
                {
                    return customer.BAccountID.ToString();
                }
            }
            return priceType == PriceTypes.CustomerPriceClass ? priceCode : null;
        }
        else
            return null;
    }

    #endregion
}

标签: c#acumatica

解决方案


原来这不是代码不正确的动作。

对数据视图委托的以下更改已解决该问题:

var records = Base.Records.View.Select(PXView.Currents, filterParams,
            PXView.Searches,
            PXView.SortColumns,
            PXView.Descendings,
            Records.View.GetExternalFilters(),
            ref startRow, 0, ref totalRows);

推荐阅读