Skip to main content
Solved

Warehouse filter in code to retrieve unit cost

  • August 26, 2021
  • 1 reply
  • 124 views

Good day,

We are trying to retrieve the unit cost, based on the warehouse selected inline on the sales quote screen. 

How would I go about properly filtering in code to get to the right value, currently I have the following code to retrieve the code based on conditions:

We want the process to look as follow:

  1. First look at valuation method
    1. If it is standard
      1. Retrieve the current cost from the item warehouse details screen that links to the chosen warehouse on the sales quote screen
      2. If the cost is 0, retrieve from the item screen
    2. If it is average
      1. Retrieve the average cost from the item warehouse details screen that links to the chosen warehouse on the sales quote screen
      2. If the cost is 0, retrieve from the item screen
    3. Else
      1. Throw exception
 protected void CROpportunityProducts_SiteID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
        {

            var row = (CROpportunityProducts)e.Row;

            InventoryItem temp = new InventoryItem();
            temp = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.InventoryID);

            INItemCost tempCost = new INItemCost();
            tempCost = PXSelect<INItemCost, Where<INItemCost.inventoryID, Equal<Required<INItemCost.inventoryID>>>>.Select(Base, row.InventoryID);

            INItemSite tempWharehouseCost = new INItemSite();
            tempWharehouseCost = PXSelect<INItemSite, Where<INItemSite.inventoryID, Equal<Required<INItemCost.inventoryID>>>>.Select(Base, row.InventoryID);


            var wharehouse = row.SiteID;

            if (temp.ValMethod == "A")
            {
                if (tempWharehouseCost.AvgCost != 0)
                {
                    row.CuryUnitCost = tempWharehouseCost.AvgCost;
                }
                else {
                    row.CuryUnitCost = tempCost.AvgCost;
                }
            }
            else if (temp.ValMethod == "S")
            {
                if (tempWharehouseCost.StdCost != 0)
                {
                    row.CuryUnitCost = tempWharehouseCost.StdCost;

                }
                else
                {
                    row.CuryUnitCost = temp.StdCost;
                }
            }
            else
            {
                throw new Exception("Please choose correct valuation method");
            }

 

 

The reason for this is that stock items, can be in mulitple warehouses which in return can have different costs to the item.

Any help would be greatly appreciated

Best answer by Naveen Boga

Hi, @charlbester34 I have modified your code like below, please verify.

May this help you!!

protected void CROpportunityProducts_SiteID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
        {
            CROpportunityProducts row = (CROpportunityProducts)e.Row;
            if (row != null)
            {
                InventoryItem objInventoryItem = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row?.InventoryID);
                INItemCost objINItemCost = PXSelect<INItemCost, Where<INItemCost.inventoryID, Equal<Required<INItemCost.inventoryID>>>>.Select(Base, row?.InventoryID);
                INItemSite tempWharehouseCost = PXSelect<INItemSite, Where<INItemSite.inventoryID, Equal<Required<INItemCost.inventoryID>>,
                                                And<INItemSite.siteID, Equal<Required<INItemSite.siteID>>>>>.Select(Base, row.InventoryID, row.InventoryID);

                if (objInventoryItem != null && objInventoryItem.ValMethod == "A")
                {
                    if (tempWharehouseCost?.AvgCost != 0)
                        row.CuryUnitCost = tempWharehouseCost?.AvgCost;
                    else
                        row.CuryUnitCost = objINItemCost?.AvgCost;
                }
                else if (objInventoryItem != null && !string.IsNullOrEmpty(objInventoryItem.ValMethod) && objInventoryItem.ValMethod == "S")
                {
                    if (tempWharehouseCost?.StdCost != 0)                      
                        row.CuryUnitCost = tempWharehouseCost?.StdCost;
                    else                    
                        row.CuryUnitCost = objInventoryItem?.StdCost;                    
                }
                else
                {
                    throw new Exception("Please choose correct valuation method");
                }
            }
        }

 

 

View original
Did this topic help you find an answer to your question?

1 reply

Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3417 replies
  • Answer
  • August 26, 2021

Hi, @charlbester34 I have modified your code like below, please verify.

May this help you!!

protected void CROpportunityProducts_SiteID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
        {
            CROpportunityProducts row = (CROpportunityProducts)e.Row;
            if (row != null)
            {
                InventoryItem objInventoryItem = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row?.InventoryID);
                INItemCost objINItemCost = PXSelect<INItemCost, Where<INItemCost.inventoryID, Equal<Required<INItemCost.inventoryID>>>>.Select(Base, row?.InventoryID);
                INItemSite tempWharehouseCost = PXSelect<INItemSite, Where<INItemSite.inventoryID, Equal<Required<INItemCost.inventoryID>>,
                                                And<INItemSite.siteID, Equal<Required<INItemSite.siteID>>>>>.Select(Base, row.InventoryID, row.InventoryID);

                if (objInventoryItem != null && objInventoryItem.ValMethod == "A")
                {
                    if (tempWharehouseCost?.AvgCost != 0)
                        row.CuryUnitCost = tempWharehouseCost?.AvgCost;
                    else
                        row.CuryUnitCost = objINItemCost?.AvgCost;
                }
                else if (objInventoryItem != null && !string.IsNullOrEmpty(objInventoryItem.ValMethod) && objInventoryItem.ValMethod == "S")
                {
                    if (tempWharehouseCost?.StdCost != 0)                      
                        row.CuryUnitCost = tempWharehouseCost?.StdCost;
                    else                    
                        row.CuryUnitCost = objInventoryItem?.StdCost;                    
                }
                else
                {
                    throw new Exception("Please choose correct valuation method");
                }
            }
        }

 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings