Prevent over receipt for mass receipt

I’m following this post : Prevent Over Receipts for a PO Release - #5 by ckrusen
and created 2 BPMs to stop the user from over receiving a PO.

The BPM is working as expected when the user add the receipt line by line.

However, the user will hit the exception “The table ds.RcvDtl has more than one record” when they are doing mass receipt and trying to change the receiving qty to more or less than the PO qty.
image

How do I resolve this ?

If you have multiple records then in my experience you have to use a custom code block to iterate over each record and apply your business logic to each.

foreach (var RcvDtlRecord in ds.RcvDtl)
{
   // Apply business logic to check for over receipt here
   // if (inputOurQty > RcvDtlRecord.OurQty)
   //     throw new ArgumentOutOfRangeException(RcvDtlRecord.PackSlip + " " + RcvDtlRecord.PackLine + " had qty of " + RcvDtlRecord.OurQty + " which user entry exceeded");
}

Though as mentioned in the other post - it’s probably better to leave this as a warning to prevent issues further downstream.

1 Like

Thanks for responding. Writing custom C# code is a challenge for me as I’m not very proficient in it. :sweat:

Another question, from which method directive should this custom code block be triggered?

Welcome !

What does your trace show?

I’ve placed my custom code at the following methods’ Pre-Processing
Erp.BO.Receipt.CommitRcvDtl
Erp.BO.Receipt.OnChangeDtlReceived
Erp.BO.Receipt.OnChangeHdrReceived
Erp.BO.Receipt.UpdateMaster

The code is working as expected when the user is receiving the packline 1 by 1.
But it’s not working when the user entered a batch of receiving lines and received all. :thinking:

// Initialize a variable to store the previous receipts quantity.
decimal prevrcpts = 0;

// Iterate over each receiving detail that meets PO conditions.
foreach (var rdtl in (from rdtlrow in ds.RcvDtl
                     where (string.Equals(rdtlrow.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase)
                            || string.Equals(rdtlrow.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase))
                            && rdtlrow.PONum != 0
                            && rdtlrow.POLine != 0
                     select rdtlrow))
{
    // Retrieve the purchase order release related to the current receiving detail.
    var prel = Db.PORel.FirstOrDefault(p => p.Company == rdtl.Company && p.PONum == rdtl.PONum && p.POLine == rdtl.POLine && p.PORelNum == rdtl.PORelNum);

    if (prel != null)
    {
        // Determine the previous receipts quantity based on the RowMod of the receiving detail.
        if (rdtl.RowMod == "A")
            prevrcpts = prel.ReceivedQty;
        if (rdtl.RowMod == "U")
            prevrcpts = prel.ReceivedQty - rdtl.OurQty;

        // Check if the sum of the current and previous receipts quantity exceeds the PO release quantity.
        if ((rdtl.InputOurQty + prevrcpts) > (prel.XRelQty * 1))
        {
            // Throw an exception if over receiving is detected.
            if (prevrcpts > 0)
                throw new Ice.BLException("Over receiving detected at Line [" + rdtl.PackLine + "] ! Entered quantity of " + rdtl.InputOurQty + " plus previous receipts of " + Math.Round(prevrcpts, 2) + " is more than the PO release quantity of " + Math.Round(prel.XRelQty, 2) + "  [BPM]");
            else
                throw new Ice.BLException("Over receiving detected at Line [" + rdtl.PackLine + "] ! Entered quantity of " + rdtl.InputOurQty + " is more than the PO release quantity of " + Math.Round(prel.XRelQty, 2) + "  [BPM]");
        }
    }
}