I am working on recalculating percentage value of each rows when a field value on a row is updated. I have created two custom fields PoundsUsed and PercentOfWIP for INComponentTran DAC. When PoundsUsed is updated I would like to recalculate the PercentOfWIP for all INComponentTran records in Kit Assembly.
I have also created a custom field that is the sum of INComponentTran's PoundsUsed and calculating percentage based on that. Below is the code snippet that I have written to accomplish but it's only updating the latest record.
private decimal CalculateTotalLBSProduced()
{
decimal totalPortions = 0M;
decimal totalLBs = 0M;
foreach (INComponentTran tran in Base.Components.Select())
{
INTranExt tranExt = tran.GetExtension<INTranExt>();
if (tranExt.UsrPoundsUsed.HasValue)
{
if (tran.UOM == "PRTION")
{
totalPortions = totalPortions + tranExt.UsrPoundsUsed.Value;
}
if (tran.UOM == "LB")
{
totalLBs = totalLBs + tranExt.UsrPoundsUsed.Value;
}
}
}
return totalPortions - totalLBs;
}
private void CalculatePercentOfWIP()
{
decimal totalLBSProduced = CalculateTotalLBSProduced();
Base.Document.Cache.SetValueExt<INKitRegisterExt.usrLBSProduced>(Base.Document.Current, totalLBSProduced);
foreach (INComponentTran tran in Base.Components.Select())
{
decimal percentOfWIP = 0M;
Base.Components.Current = tran;
INTranExt tranExt = tran.GetExtension<INTranExt>();
if (tran.UOM == "PRTION")
{
decimal? poundsUsed = tran.GetExtension<INTranExt>().UsrPoundsUsed.GetValueOrDefault();
if (totalLBSProduced != 0 && poundsUsed.HasValue && poundsUsed.Value !=0)
{
percentOfWIP = (poundsUsed.Value / totalLBSProduced) * 100;
}
}
Base.Components.Cache.SetValueExt<INTranExt.usrPercentOfWIP>(tran, percentOfWIP);
}
}
protected void INComponentTran_UsrPoundsUsed_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (INComponentTran)e.Row;
if (row == null) return;
CalculateTotalLBSProduced();
CalculatePercentOfWIP();
}
Thanks.
question from:
https://stackoverflow.com/questions/65901251/update-field-for-all-rows-on-grid 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…