Wednesday 24 September 2014

Attachements code

class NotesPrintMatrix
{
    Notes   notes;
}
private LineNum getLastLineNum()
{
    PrintMatrixDocuRefTmp   printMatrixDocuRefTmp;
    select firstOnly LineNum from printMatrixDocuRefTmp
        order by LineNum desc;

    return printMatrixDocuRefTmp.LineNum + 1;

}
public void insertPrintMatrixDocuRef(DocuRef                   docuRef,
                                     DocumentNum               _documentNum,
                                     RefRecId                  _documentRecId,
                                     RefTableId                _refTableId,
                                     DocumentNoteType          _docuNote = DocumentNotetype::None,
                                     LineNum                   _documentLine = 0
                                     )
{
    PrintMatrixDocuRefTmp   printMatrixDocuRefTmp;

    if(docuRef.RefTableId == tableNum(VendTable))
    {
       _docuNote = DocumentNotetype::VendorNotes;
    }
    else if(docuRef.RefTableId == tableNum(CustTable))
    {
       _docuNote = DocumentNotetype::CustomerNotes;
    }

    printMatrixDocuRefTmp.initValue();
    printMatrixDocuRefTmp.LineNum           = this.getLastLineNum();
    printMatrixDocuRefTmp.Name              = docuRef.Name;
    printMatrixDocuRefTmp.Notes             = docuRef.Notes;
    printMatrixDocuRefTmp.DocumentNum       = _documentNum;
    printMatrixDocuRefTmp.DocumentLineNum   = _documentLine;
    printMatrixDocuRefTmp.DocumentRecId     = _documentRecId;
    printMatrixDocuRefTmp.RefTableId        = _refTableId;
    printMatrixDocuRefTmp.DocumentNoteType  = _docuNote;
    printMatrixDocuRefTmp.insert();

    notes += (notes==''?'':'\n') + docuRef.Notes;

//if(curUserId() == '' info(strFmt("write (%1) %2 %4    >%5", tableId2Name(docuRef.RefTableId),  tableId2Name(_refTableId), _documentRecId, _documentLine, docuRef.Notes));
}

notes notes()
{
    return notes;
}

static Notes concatNotes(RefRecId   refRecId,
                         RefTableId refTableId,
                         LineNum    docuLineNum = 0
                         )
{
    PrintMatrixDocuRefTmp   printMatrixDocuRefTmp;
    Notes   result;
    Counter cnt;
    #xppTexts

    while select Notes from printMatrixDocuRefTmp
        where printMatrixDocuRefTmp.RefTableId      == refTableId
           && printMatrixDocuRefTmp.DocumentRecId        == refRecId
           && printMatrixDocuRefTmp.DocumentLineNum == docuLineNum
    {
        if (printMatrixDocuRefTmp)
        {
            result += printMatrixDocuRefTmp.Notes + #newline;
            cnt++;
//if(curUserId() == '') info(strFmt("read %1 %2 %3.%4 %5", cnt, refTableId, tableId2Name(refTableId), refRecId, docuLineNum));
        }
    }
    return result;
}

static Notes getNotesFromHeader(RefTableId               refTableId,
                               RefRecId                 refRecId,
                               DisplayNoteReportType    displayReport,
                               NoYes                    directDeliveryPO = NoYes::No)
{
    SalesTable              salesTable;
    PurchTable              purchTable;
    SalesLine               salesLine;
    PurchLine               purchLine;
    SalesAgreementHeader    salesAgreementHeader;
    AgreementLine           agreementLine;
    PurchAgreementHeader    purchAgreementHeader;
    NotesPrintmatrix        notesPrintmatrix = new NotesPrintmatrix();
    DocuRef                 docuRef;
    DocuRef                 docuRefInvent;
    InventTable             inventTable;
    CustTable               custTable;
    VendTable               vendTable;
    InventTransferTable     inventTransferTable;
    InventTransferLine      inventTransferLine;
    ProdTable               prodTable;
    SalesQuotationLine      salesQuotationLine;
    ProdBOM                 prodBom;
    docuRef.clear();

    switch(refTableId)
    {
        case tableNum(InventTable):
            inventTable = inventTable::findRecId(refRecId);
            if(inventTable)
            {
                while select Notes, Name, refTableId, refRecId from docuRefInvent
                    where
                          docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && (displayReport == DisplayNoteReportType::ReportProdPickList && docuRefInvent.ProductionPickList)
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent, salesQuotationLine.QuotationId, salesQuotationLine.RecId, tableNum(salesQuotationLine), salesQuotationLine.LineNum);
                    }
                }
            }
            break;
        case tableNum(SalesQuotationLine):
            salesQuotationLine = SalesQuotationLine::findRecId(refRecId);
            if (salesQuotationLine)
            {
                docuRef.clear();
                while select docuRef
                    where  (docuRef.RefTableId   == tableNum(SalesQuotationLine))
                       && (docuRef.RefRecId     == salesQuotationLine.RecId)
                       && (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRef.SalesQuotation)
                {
                    if (docuRef.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, salesQuotationLine.QuotationId, salesQuotationLine.RecId, tableNum(salesQuotationLine), salesQuotationLine.LineNum);
                    }
                }
                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == salesQuotationLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRefInvent.SalesQuotation)
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent, salesQuotationLine.QuotationId, salesQuotationLine.RecId, tableNum(salesQuotationLine), salesQuotationLine.LineNum);
                    }
                }
            }
            break;
        case tableNum(SalesTable):
            salesTable = SalesTable::findRecId(refRecId);
            if(!CustFormletterParameters::find().CustNotesFollowRecord)
            {   // only pull customer notes when Cust Notes copying is Off
                custTable = custTable::find(salesTable.CustAccount);
            }
            /* - ALWAYS print Agreement header notes on the line ->
            if(salesTable.MatchingAgreement)
            {
                salesAgreementHeader = SalesAgreementHeader::find(salesTable.MatchingAgreement);
            }  */
            //- ALWAYS print Agreement header notes on the line <-

            while select Notes, Name, refTableId, refRecId from docuRef
                order by RefTableId desc//DOC 58 -- MEE -- 10 Feb 2014
                 where ((docuRef.RefTableId    == refTableId && docuRef.RefRecId      == refRecId )
                    || (docuRef.RefTableId  == tableNum(CustTable) &&  docuRef.RefRecId    == custTable.RecId)
                    || (docuRef.RefTableId  == salesAgreementHeader.tableid  &&  docuRef.RefRecId == salesAgreementHeader.RecId))
                    && ((displayReport == DisplayNoteReportType::ReportSalesConfirm && docuRef.SalesOrderConfirm)
                    || (displayReport == DisplayNoteReportType::ReportSalesInvoice && docuRef.SalesOrderInvoice)
                    || (displayReport == DisplayNoteReportType::ReportSalesPackingSlip && docuRef.SalesOrderPackingSlip)
                    || (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRef.SalesQuotation)
                    || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease)
                    || (displayReport == DisplayNoteReportType::ReportSalesPickingList && docuRef.SalesOrderPickingList)
                    || (displayReport == DisplayNoteReportType::ReportSalesBillOfLading && docuRef.SalesBillOfLading))
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, salesTable.SalesId, salesTable.RecId, tableNum(SalesTable), DocumentNotetype::SalesNotes);
            }
            docuRef.clear();
            while select ItemId, RecId, SalesId, LineNum, MatchingAgreementLine from salesLine
                //outer join  Notes, Name from docuRef
                where salesLine.salesId     == salesTable.SalesId
                  // && (docuRef.RefTableId   == tableNum(SalesLine))
                   //&& (docuRef.RefRecId     == salesLine.RecId)
            {
                if(salesLine.MatchingAgreementLine)
                {
                    agreementLine = AgreementLine::find(salesLine.MatchingAgreementLine);
                    //Agreement tagged using update line - Agreement recid is 0 should execute other than sales release_->
                    if(
                        // removed -- ALWAYS print Agreement header notes on the line ->
                        //!salesTable.MatchingAgreement &&
                        // - ALWAYS print Agreement header notes on the line <-
                            displayReport != DisplayNoteReportType::ReportSalesRelease)
                    {
                        salesAgreementHeader = salesAgreementHeader::find(agreementLine.Agreement);
                        while select Notes, Name, refTableId, refRecId from docuRef
                            where ((docuRef.RefTableId  == salesAgreementHeader.tableid  &&  docuRef.RefRecId == salesAgreementHeader.RecId))
                                && ((displayReport == DisplayNoteReportType::ReportSalesConfirm && docuRef.SalesOrderConfirm)
                                || (displayReport == DisplayNoteReportType::ReportSalesInvoice && docuRef.SalesOrderInvoice)
                                || (displayReport == DisplayNoteReportType::ReportSalesPackingSlip && docuRef.SalesOrderPackingSlip)
                                || (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRef.SalesQuotation)
                                || (displayReport == DisplayNoteReportType::ReportSalesPickingList && docuRef.SalesOrderPickingList))
                        {
                            notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                        salesLine.SalesId, salesLine.RecId, tableNum(salesLine),DocumentNotetype::SalesNotes, salesLine.LineNum);
                        }
                    }
                    //Agreement tagged using update line - Agreement recid is 0 should execute other than sales release<-
                }
                docuRef.clear();
                while select docuRef
                    where  ((docuRef.RefTableId   == tableNum(SalesLine) && docuRef.RefRecId     == salesLine.RecId)
                    || (docuRef.RefTableId  == agreementLine.tableid  &&  docuRef.RefRecId == agreementLine.RecId))
                       && ((displayReport == DisplayNoteReportType::ReportSalesConfirm && docuRef.SalesOrderConfirm)
                        || (displayReport == DisplayNoteReportType::ReportSalesInvoice && docuRef.SalesOrderInvoice)
                        || (displayReport == DisplayNoteReportType::ReportSalesPackingSlip && docuRef.SalesOrderPackingSlip)
                        || (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRef.SalesQuotation)
                        || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease)
                        || (displayReport == DisplayNoteReportType::ReportSalesPickingList && docuRef.SalesOrderPickingList))
                {
                    if (docuRef.Notes)
                    {
                        if(docuRef.RefTableId ==agreementLine.tableid)
                        {
                            notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, salesLine.SalesId, salesLine.RecId, tableNum(SalesLine), DocumentNotetype::SalesAgreement, salesLine.LineNum);
                        }
                        else
                        {
                            notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, salesLine.SalesId, salesLine.RecId, tableNum(SalesLine), DocumentNotetype::SalesNotes, salesLine.LineNum);
                        }
                    }
                }

                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == salesLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportSalesConfirm && docuRefInvent.SalesOrderConfirm)
                        || (displayReport == DisplayNoteReportType::ReportSalesInvoice && docuRefInvent.SalesOrderInvoice)
                        || (displayReport == DisplayNoteReportType::ReportSalesPackingSlip && docuRefInvent.SalesOrderPackingSlip)
                        || (displayReport == DisplayNoteReportType::ReportSalesQuotation && docuRefInvent.SalesQuotation)
                        || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRefInvent.SalesOrderRelease)
                        || (displayReport == DisplayNoteReportType::ReportSalesPickingList && docuRefInvent.SalesOrderPickingList))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent, salesLine.SalesId, salesLine.RecId, tableNum(SalesLine), DocumentNotetype::ReleaseProductNotes, salesLine.LineNum);
                    }
                }
            }
            break;
        case tableNum(PurchTable):
            purchTable = PurchTable::findRecId(refRecId);
            vendTable = VendTable::find(purchTable.OrderAccount);
            /* - ALWAYS print Agreement header notes on the line ->
            if(purchTable.MatchingAgreement && directDeliveryPO == NoYes::No)
            {
                purchAgreementHeader = PurchAgreementHeader::find(purchTable.MatchingAgreement);
            }
            removed - rik - 5/30, per CJO - ALWAYS print Agreement header notes on the line ->*/
            while select Notes, Name, refTableId, refRecId from docuRef
                where ( (docuRef.RefTableId    == refTableId && docuRef.RefRecId      == refRecId )
                      || (docuRef.RefTableId  == tableNum(VendTable) &&  docuRef.RefRecId    == vendTable.RecId)
                      || (docuRef.RefTableId  == purchAgreementHeader.tableid  &&  docuRef.RefRecId == purchAgreementHeader.RecId))
                      && ((displayReport == DisplayNoteReportType::ReportPurchorder && docuRef.PurchaseOrderConfirm)
                        || (displayReport == DisplayNoteReportType::ReportPurchInvoice && docuRef.PurchaseOrderInvoice)
                        || (displayReport == DisplayNoteReportType::ReportPurchPackingSlip && docuRef.PurchaseOrderProductReceipt)
                        || (displayReport == DisplayNoteReportType::ReportPurchReceiptList && docuRef.PurchaseOrderReceiptList)
                        || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease))
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, purchTable.purchId, purchTable.RecId, tableNum(PurchTable),DocumentNotetype::PurchaseNotes);
            }
            while select ItemId, RecId, PurchId, LineNumber, MatchingAgreementLine from purchLine
                //outer join  Notes, Name from docuRef
                where purchLine.PurchId     == purchTable.PurchId
                   //&& (docuRef.RefTableId   == tableNum(purchLine))
                   //&& (docuRef.RefRecId     == purchLine.RecId)
                   //&& docuRef.dataAreaId    == purchLine.dataAreaId
            {
                
                 agreementLine.clear();
               
                if(purchLine.MatchingAgreementLine)
                {
                    agreementLine = AgreementLine::find(purchLine.MatchingAgreementLine);
                    //Agreement tagged using update line - Agreement recid is 0 should execute other than sales release->
                    if(
                         ALWAYS print Agreement header notes on the line ->
                        !purchTable.MatchingAgreement &&
                        removed -  ALWAYS print Agreement header notes on the line ->*/
                        displayReport != DisplayNoteReportType::ReportSalesRelease)
                    {
                        purchAgreementHeader = PurchAgreementHeader::find(agreementLine.Agreement);
                        while select Notes, Name, refTableId, refRecId from docuRef
                            where ((docuRef.RefTableId  == purchAgreementHeader.tableid  &&  docuRef.RefRecId == purchAgreementHeader.RecId))
                              && ((displayReport == DisplayNoteReportType::ReportPurchorder && docuRef.PurchaseOrderConfirm)
                                || (displayReport == DisplayNoteReportType::ReportPurchInvoice && docuRef.PurchaseOrderInvoice)
                                || (displayReport == DisplayNoteReportType::ReportPurchPackingSlip && docuRef.PurchaseOrderProductReceipt)
                                || (displayReport == DisplayNoteReportType::ReportPurchReceiptList && docuRef.PurchaseOrderReceiptList))
                        {

                            notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                        purchLine.PurchId, purchLine.RecId, tableNum(purchLine), DocumentNotetype::PurchaseNotes, purchLine.LineNumber);

                        }
                    }
                    //Agreement tagged using update line - Agreement recid is 0 should execute other than sales release<-
                }
                docuRef.clear();


                while select docuRef
                    where  ((docuRef.RefTableId   == tableNum(purchLine) && docuRef.RefRecId     == purchLine.RecId)
                         || (docuRef.RefTableId  == agreementLine.tableid  &&  docuRef.RefRecId == agreementLine.RecId))
                        && ((displayReport == DisplayNoteReportType::ReportPurchorder && docuRef.PurchaseOrderConfirm)
                        || (displayReport == DisplayNoteReportType::ReportPurchInvoice && docuRef.PurchaseOrderInvoice)
                        || (displayReport == DisplayNoteReportType::ReportPurchPackingSlip && docuRef.PurchaseOrderProductReceipt)
                        || (displayReport == DisplayNoteReportType::ReportPurchReceiptList && docuRef.PurchaseOrderReceiptList)
                        || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease))
                {
                    if (docuRef.Notes)
                    {
                        if(docuRef.RefTableId  == agreementLine.tableid)
                        {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                        purchLine.PurchId, purchLine.RecId, tableNum(purchLine), DocumentNotetype::PurchAgreement, purchLine.LineNumber);
                        }
                        else
                        {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                       purchLine.PurchId, purchLine.RecId, tableNum(purchLine), DocumentNotetype::PurchaseNotes, purchLine.LineNumber);
                        }
                    }
                }

                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == purchLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportPurchorder && docuRefInvent.PurchaseOrderConfirm)
                        || (displayReport == DisplayNoteReportType::ReportPurchInvoice && docuRefInvent.PurchaseOrderInvoice)
                        || (displayReport == DisplayNoteReportType::ReportPurchPackingSlip && docuRefInvent.PurchaseOrderProductReceipt)
                        || (displayReport == DisplayNoteReportType::ReportPurchReceiptList && docuRefInvent.PurchaseOrderReceiptList)
                        || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRefInvent.SalesOrderRelease && directDeliveryPO == NoYes::No))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        purchLine.PurchId, purchLine.RecId, tableNum(purchLine),DocumentNotetype::PurchaseNotes, purchLine.LineNumber);
                    }
                }
            }
            break;
        case tableNum(SalesAgreementHeader):
            salesAgreementHeader = SalesAgreementHeader::find(refRecId);
            custTable = custTable::find(salesAgreementHeader.CustAccount);

            while select Notes, Name, refTableId, refRecId from docuRef
                order by RefTableId desc//DOC 62 -- MEE -- 07 Feb 2014
                where (docuRef.RefTableId    == refTableId && docuRef.RefRecId      == refRecId )
                      || (docuRef.RefTableId  == tableNum(custTable) &&  docuRef.RefRecId    == custTable.RecId
                                 && displayReport != DisplayNoteReportType::ReportSalesRelease)
                      && ((displayReport == DisplayNoteReportType::ReportSalesAgreement && docuRef.SalesAgreement)
                             || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease))
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, salesAgreementHeader.SalesNumberSequence,
                                   salesAgreementHeader.RecId, tableNum(SalesAgreementHeader),DocumentNotetype::SalesNotes);
            }
            docuRef.clear();
            while select ItemId, RecId, LineNumber from agreementLine
                //outer join  Notes, Name from docuRef
                where agreementLine.Agreement == salesAgreementHeader.RecId
                   //&& (docuRef.RefTableId   == tableNum(AgreementLine))
                   //&& (docuRef.RefRecId     == agreementLine.RecId)
            {
                docuRef.clear();
                while select docuRef
                    where  (docuRef.RefTableId   == tableNum(AgreementLine))
                       && (docuRef.RefRecId     == agreementLine.RecId)
                       && ((displayReport == DisplayNoteReportType::ReportSalesAgreement && docuRef.SalesAgreement))
                {
                    if (docuRef.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                    salesAgreementHeader.SalesNumberSequence, agreementLine.RecId, tableNum(agreementLine), DocumentNotetype::SalesNotes, agreementLine.LineNumber);
                    }
                }
                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == agreementLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportSalesAgreement && docuRefInvent.SalesAgreement))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        salesAgreementHeader.SalesNumberSequence, agreementLine.RecId, tableNum(agreementLine), DocumentNotetype::SalesNotes, agreementLine.LineNumber);
                    }
                }
            }
            break;

        case tableNum(PurchAgreementHeader):
            purchAgreementHeader = PurchAgreementHeader::find(refRecId);
            vendTable = VendTable::find(PurchAgreementHeader.VendAccount);
            while select Notes, Name, refTableId, refRecId from docuRef
                order by RefTableId desc//DOC 62 -- MEE -- 07 Feb 2014
                where (docuRef.RefTableId    == refTableId && docuRef.RefRecId      == refRecId )
                      || (docuRef.RefTableId  == tableNum(VendTable) &&  docuRef.RefRecId    == vendTable.RecId
                                 && displayReport != DisplayNoteReportType::ReportSalesRelease) // no vendor notes on SalesRelease                       )
                      && ((displayReport == DisplayNoteReportType::ReportPurchAgreement && docuRef.PurchaseAgreement)
                             || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease)) // PA notes for sales release
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, purchAgreementHeader.PurchNumberSequence,
                                    purchAgreementHeader.RecId, tableNum(PurchAgreementHeader),DocumentNotetype::PurchaseNotes);
            }
            docuRef.clear();
            while select ItemId, RecId, LineNumber from agreementLine
                //outer join  Notes, Name from docuRef
                where agreementLine.Agreement == purchAgreementHeader.RecId
                   //&& (docuRef.RefTableId   == tableNum(AgreementLine))
                   //&& (docuRef.RefRecId     == agreementLine.RecId)
            {
                docuRef.clear();
                while select docuRef
                    where  (docuRef.RefTableId   == tableNum(AgreementLine))
                       && (docuRef.RefRecId     == agreementLine.RecId)
                       && ((displayReport == DisplayNoteReportType::ReportPurchAgreement && docuRef.PurchaseAgreement)
                             || (displayReport == DisplayNoteReportType::ReportSalesRelease && docuRef.SalesOrderRelease)) // PA notes for sales release 
                {
                    if (docuRef.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                    purchAgreementHeader.PurchNumberSequence, agreementLine.RecId, tableNum(agreementLine), DocumentNotetype::PurchAgreement, agreementLine.LineNumber);
                    }
                }
                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == agreementLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportPurchAgreement && docuRefInvent.PurchaseAgreement))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        purchAgreementHeader.PurchNumberSequence, agreementLine.RecId, tableNum(agreementLine),DocumentNotetype::PurchaseNotes, agreementLine.LineNumber);
                    }
                }
            }
            break;
        case tableNum(InventTransferTable):
            select TransferId, RecId from inventTransferTable
                where inventTransferTable.RecId == refRecId;
            while select Notes, Name, refTableId, refRecId from docuRef
                where (docuRef.RefTableId    == refTableId && docuRef.RefRecId == refRecId)
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, inventTransferTable.TransferId,
                                    inventTransferTable.RecId, tableNum(InventTransferTable));
            }
            while select inventTransferLine
                where inventTransferLine.TransferId == inventTransferTable.TransferId
            {
                docuRef.clear();
                while select docuRef
                    where  (docuRef.RefTableId   == tableNum(inventTransferLine))
                       && (docuRef.RefRecId     == inventTransferLine.RecId)
                       && ((displayReport == DisplayNoteReportType::ReportTransPicklist && docuRef.TransferPickList)
                        || (displayReport == DisplayNoteReportType::ReportTransReceiptList && docuRef.TransferReceiptList))
                {
                    if (docuRef.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRef,
                                    inventTransferLine.TransferId, inventTransferLine.RecId, tableNum(inventTransferLine), inventTransferLine.LineNum);
                    }
                }
                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId from docuRefInvent
                    where inventTable.ItemId        == inventTransferLine.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportTransPicklist && docuRefInvent.TransferPickList)
                        || (displayReport == DisplayNoteReportType::ReportTransReceiptList && docuRefInvent.TransferReceiptList))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        inventTransferLine.TransferId, inventTransferLine.RecId, tableNum(inventTransferLine), inventTransferLine.LineNum);
                    }
                }
            }
            break;
        case tableNum(ProdTable):
            select RecId, ProdId, inventRefId from prodTable
                where prodTable.RecId == refRecId;
            while select Notes, Name, refTableId, refRecId, ProductionPickList from docuRef
                where (docuRef.RefTableId    == refTableId && docuRef.RefRecId == refRecId)
            {
                notesPrintmatrix.insertPrintMatrixDocuRef(docuRef, prodTable.ProdId,
                                    prodTable.RecId, tableNum(ProdTable),DocumentNoteType::ReleaseProductNotes);
            }
            if (prodTable)
            {
                while select salesTable
                    where salesTable.salesId == prodTable.InventRefId
                    join  Notes, Name, refTableId, refRecId, ProductionPickList from docuRefInvent
                    where docuRefInvent.RefTableId  == tableNum(salesTable)
                       && docuRefInvent.RefRecId    == salesTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportProdPickList && docuRefInvent.ProductionPickList))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        prodTable.ProdId, prodTable.RecId, tableNum(prodTable),DocumentNoteType::ReleaseProductNotes);
                    }
                }
                while select ItemId, RecId from inventTable
                    join  Notes, Name, refTableId, refRecId, ProductionPickList from docuRefInvent
                    where inventTable.ItemId        == prodTable.ItemId
                       && docuRefInvent.RefTableId  == tableNum(InventTable)
                       && docuRefInvent.RefRecId    == inventTable.RecId
                       && ((displayReport == DisplayNoteReportType::ReportProdPickList && docuRefInvent.ProductionPickList))
                {
                    //if multiple line in docuref for an item what will be the result
                    if (docuRefInvent.Notes)
                    {
                        notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                        prodTable.ProdId, prodTable.RecId, tableNum(prodTable),DocumentNoteType::ReleaseProductNotes);
                    }
                }
                if(!directDeliveryPO)
                    while select prodBom
                        where prodBom.ProdId == prodTable.ProdId
                {
                    while select ItemId, RecId from inventTable
                        join  Notes, Name, refTableId, refRecId, ProductionPickList from docuRefInvent
                        where inventTable.ItemId        == prodBom.ItemId
                           && docuRefInvent.RefTableId  == tableNum(InventTable)
                           && docuRefInvent.RefRecId    == inventTable.RecId
                           && ((displayReport == DisplayNoteReportType::ReportProdPickList && docuRefInvent.ProductionPickList))
                    {
                        //if multiple line in docuref for an item what will be the result
                        if (docuRefInvent.Notes)
                        {
                            notesPrintmatrix.insertPrintMatrixDocuRef(docuRefInvent,
                                            prodBom.ProdId, prodBom.RecId, tableNum(prodBom), prodBom.LineNum);
                        }
                    }
                }
            }
            break;
    }
    return notesPrintmatrix.notes();
}