0
votes

I have overwritten the Records data view on ARSalesPriceMaint with one of my own in order to also overwrite the data view delegate records. This is necessary because I have customized the page filter. This works great.

I have added an action which should take the resulting records (after filtering) and update a custom field in the DAC extension. This does indeed update records, but it is not respecting the filters on the records, especially the grid filters. For instance, filtering the UOM column affects the view, but my action updates all UOMs.

So here's the Q: Why is my action's foreach statement being given more records than are in the grid?

I believe this is the problem area, but the entire code is below also:

(this is from the UpdateDifs action, not the data view delegate)

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
        }

Entire 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
}
1

1 Answers

1
votes

Turns out it was not the action with incorrect code.

The following change to the data view delegate has fixed the problem:

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