Restrict Job based PO order qty as per Job Required qty

Dear Team,
I want to restrict Job based order qty on the basis of Job required qty. I have written code on data directive which works well for all added and update condition but not working for delete commend.

At the time of delete command, system calculation double the order qty and checking the validation condition. Please check and help.

Erp.Tables.PORel PORel;
Erp.Tables.JobMtl JobMtl;
string Company = "";
string JobNum_c = "";
int AssemblySeq_c = 0;
int JobSeq_c = 0;
string EquipID = "";
DateTime? ReqDueDate = null;
DateTime? WarrantyExpDate = null;
decimal POQty = 0.0m;
decimal OldPOQty = 0.0m;
decimal JobQtty = 0.0m;
decimal TTL = 0.0m;
decimal Remaining = 0.0m;
string Part = "";
decimal Total = 0.0m;
int PONum = 0;

foreach (var PODetail_ITR in
    from PODetail_Row in ttPODetail
    where string.Equals(PODetail_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase) ||
          string.Equals(PODetail_Row.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase)
    select PODetail_Row)
{
    var PODetail = PODetail_ITR;
    Company = PODetail.Company;
    JobNum_c = PODetail.JobNum_c;
    AssemblySeq_c = PODetail.AssmSeq_c;
    JobSeq_c = PODetail.JobSeq_c;
    POQty = PODetail.OrderQty;
    Part = PODetail.PartNum;
    PONum = PODetail.PONUM;

    var PODetail_I = from PODetail_Row in Db.PODetail
                     where string.Compare(PODetail_Row.Company, Company, true) == 0 &&
                           PODetail_Row.JobNum_c == JobNum_c &&
                           PODetail_Row.JobSeq_c == JobSeq_c &&
                           PODetail_Row.AssmSeq_c == AssemblySeq_c
                     group PODetail_Row by new { PODetail_Row.Company, PONum } into g
                     select new
                     {
                         Company = g.Key.Company,
                         Total = g.Sum(e => e.OrderQty)
                     };

    foreach (var v in PODetail_I)
    {
        Total = v.Total;
        this.PublishInfoMessage(
            "V.Total" + Total,
            Ice.Common.BusinessObjectMessageType.Information,
            Ice.Bpm.InfoMessageDisplayMode.Individual,
            "",
            ""
        );
    }

    JobMtl = (from JobMtl_Row in Db.JobMtl
              where string.Compare(JobMtl_Row.Company, Company, true) == 0 &&
                    JobMtl_Row.JobNum == JobNum_c &&
                    JobMtl_Row.MtlSeq == JobSeq_c &&
                    JobMtl_Row.AssemblySeq == AssemblySeq_c
              select JobMtl_Row).FirstOrDefault();

    if (JobMtl != null)
    {
        JobQtty = JobMtl.RequiredQty;
        this.PublishInfoMessage(
            "Message Text2",
            Ice.Common.BusinessObjectMessageType.Information,
            Ice.Bpm.InfoMessageDisplayMode.Individual,
            "",
            ""
        );
    }

    TTL = OldPOQty + POQty;
    Remaining = JobQtty - OldPOQty;

    if (Total > JobQtty)
    {
        CallContext.Current.ExceptionManager.AddBLException(
            "You can not use PO Qty more than Job Qty. " +
            "(Remaining Quantity " + Remaining.ToString("0.00") + ") " +
            "For Part Num = " + Part
        );
    }
}

Code Syntax Highlighting in Posts

Fixed your post to be more readable. It should be easier for someone to help now.

1 Like