Preview:
[ExtensionOf(formStr(PurchReqAddVendor))]
final class PurchReqAddVendor_Qatar_Extension
{
    
    [FormControlEventHandler(formControlStr(PurchReqAddVendor, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormControl   callerStr = sender as FormControl;
        FormRun form = callerStr.formRun();

        FormDataSource ds = form.dataSource("PurchReqLine");
        //PurchReqLine PurchReqLine = ds.getFirst();//ds.cursor();
        PurchReqLine purchReqLineRecord;
        for (purchReqLineRecord = ds.getFirst();
            purchReqLineRecord;
            purchReqLineRecord = ds.getNext())
        {
            NW_PRToPo   prToPo;
            if(PurchReqTable::find(purchReqLineRecord.PurchReqTable).blanketPO)
            {
                ttsbegin;
                prToPo.PurchReqTable = purchReqLineRecord.PurchReqTable;
                prToPo.Vendor = purchReqLineRecord.VendAccount;
                prToPo.insert();
                ttscommit;
            }
        }
    }

}
//----------
[ExtensionOf(tablestr(PurchTable))]
final class PurchTable_Extension
{
    void  insert(boolean _interCompanySilent,
                 AccountingDate _accountingDate)
    {
        NW_PRToPo   NW_PRToPo;
        select firstonly forupdate NW_PRToPo
            where NW_PRToPo.Vendor == this.InvoiceAccount;
            //order by RecId desc;
        if(NW_PRToPo)
        {
            str newPoId = this.newNumSequ(NW_PRToPo.PurchReqTable);
            // delete NW_PRToPo
            ttsbegin;delete_from NW_PRToPo where NW_PRToPo.Vendor == this.InvoiceAccount;ttscommit;
            PurchId oldPoId = this.PurchId;
            // change po id
            this.PurchId = newPoId;
            // abort old po id 
            this.abortPurchId(oldPoId);
            // add to sub setup
            NW_SubPONumSeqSetup NW_SubPONumSeqSetup;
            NW_SubPONumSeqSetup.PONo = this.PurchId;
            ttsbegin;NW_SubPONumSeqSetup.insert();ttscommit;
        }
        next insert(_interCompanySilent, _accountingDate);
    }

    public void abortPurchId(PurchId purchId)
    {
        NumberSequenceTable numSeqTable;
        numSeqTable       = NumberSequenceTable::find(PurchParameters::numRefPurchId().NumberSequenceId);
        
        if (numSeqTable.Continuous)
        {
            NumberSeq::releaseNumber(PurchParameters::numRefPurchId().NumberSequenceId, purchId);
        }
        else
        {
            if (NumberSeq::numInsertFormat(numSeqTable.NextRec - 1, numSeqTable.Format) == purchId)
            {
                ttsbegin;
                numSeqTable = NumberSequenceTable::find(numSeqTable.NumberSequenceScope, true);
                numSeqTable.NextRec--;
                numSeqTable.doUpdate();
                ttscommit;
            }
        }
    }

    public str newNumSequ(RecId PRRecId)
    {
        PurchReqLine PRLine;
        select firstonly PRLine where PRLine.PurchReqTable == PRRecId;

        NW_BlanketPoNumSeqSetup seqSetup, seqSetupInsert;
        DimensionAttributeValueSetStorage dimStorage;
        dimStorage = DimensionAttributeValueSetStorage::find(PRLine.DefaultDimension);
        str dep = dimStorage.getDisplayValueByDimensionAttribute(DimensionAttribute::findByName('Department').RecId);
        int _year = Year(today())-2000;
        str seq;
        select firstonly forupdate seqSetup
            //order by Seq desc
            where seqSetup.DepId == dep
            && seqSetup.Year == _year;
        if(seqSetup)
        {
            ttsbegin;
            seqSetup.Seq +=1;
            seqSetup.update();
            ttscommit;
        }
        else
        {
            ttsbegin;
            seqSetup.Year = _year;
            seqSetup.DepId = dep;
            seqSetup.Seq = 1;
            seqSetup.insert();
            ttscommit;

        }
        //this.blanketPO
        if(seqSetup.Seq < 10) seq = strFmt('000%1', seqSetup.Seq); //0-9
        else if(seqSetup.Seq >= 10 && seqSetup.Seq < 100) seq = strFmt('00%1', seqSetup.Seq); // 10-99
        else if(seqSetup.Seq >= 100 && seqSetup.Seq < 1000) seq = strFmt('0%1', seqSetup.Seq); // 100-999
        else if(seqSetup.Seq >= 1000) seq = strFmt('%1', seqSetup.Seq); // 1000-9999

        return strFmt("COA%1-%2-%3", _year , dep, seq);
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter