Hello Everyone,
I have been battling on what the best way is to get unit costs to auto-populate onto the Sales Quotes (CR304500) screen.
Initially I had used a FieldUpdated handler for inventory ID but as we import plenty of worksheets from excel, this breaks and pushes the values a line down.
Yesterday, I was doing the T230 course and I was inspired so I recreated it but for recalculating unit costs on Sales Quotes.
Here is the code for the button, hope it helps someone!
public class QuoteMaintCostExt : PXGraphExtension<PX.Objects.CR.QuoteMaint>
{
public static bool IsActive() => true;
//Make sure you have the views
public PXOrderedSelect<CRQuote, CROpportunityProducts,
Where<CROpportunityProducts.quoteID, Equal<Current<CRQuote.quoteID>>>,
OrderBy<Asc<CROpportunityProducts.sortOrder>>>
Products;
public PXSelect<CRQuote,
Where<CRQuote.opportunityID, Equal<Optional<CRQuote.opportunityID>>,
And<CRQuote.quoteType, Equal<CRQuoteTypeAttribute.distribution>>>> Quote;
#region Actions
public PXAction<CRQuote> UpdateCosts;
PXButton(DisplayOnMainToolbar = true)]
PXUIField(DisplayName = "Update Costs")]
protected virtual void updateCosts()
{
var quote = Products.Current;
if (quote.QuoteID == null || quote.InventoryID == null) return;
var lines = Products.Select();
foreach(CROpportunityProducts product in lines)
{
if (quote.POCreate == true) return;
if (quote.POCreate == false)
{
INItemSite site = SelectFrom<INItemSite>
.Where<INItemSite.inventoryID.IsEqual<@P.AsInt>>
.View
.Select(Base, product.InventoryID);
if (site != null)
{
product.CuryUnitCost = site.TranUnitCost;
Products.Update(product);
}
}
}
}
#endregion
}