project QadFinancials > class BBillReport > method GetBillReportPayment

Description

Return the payment information of current bill. Besides the invoice in the bill, it will also contains prepayment, deduction and adjustment.
All the payments info are divided into two groups, Bill payment and Non-bill payment, the payments which pay for the bill invoice and post in the bill period are displayed in the Bill payment group, and payments which pay for other invoice are displayed in Non-bill payment group. But for prepayment, deduction and adjustment, there’re some other logic.
-For prepayment, the initial prepayment always displays in Bill payment. The allocated for prepayment, which paying for the previous or current bill invoice, will not be displayed in any group. The allocated to the future bill invoice should be displayed into Non-bill payment. And for the allocated to non-bill invoice, it should be displayed in Non-bill payment , and at the same time, there should be a piece of opposite data displaying in Bill payment group.
-For deduction, it should be the same as normal invoice, but the description info should come from the deduction but not the invoice.
-For Adjustment, it is the same as normal invoice.


Parameters


iiCurBillIDinputinteger
icCurBillStatusinputcharacter
iiCurHighWaterMarkinputinteger
ilIsFirstBillinputlogical
itPreBillDateinputdate
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBillReport.BillReportDInvoice


program code (program3/bbillreport.p)

empty temp-table tdinvoiceIdForFirstBillTT.
assign viLengthOfQueryString = 0
       vcPostingIdList       = ''.

/* For the performance purpose, put some query at the top of this method and use append mode */
for each tDInvoiceMovementForBill where
    tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT} and
    tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-INVOICE}
    break by tDInvoiceMovementForBill.tiPosting_ID:

    if first-of(tDInvoiceMovementForBill.tiPosting_ID) then
    do:
        if vcPostingIdList = '' then
        assign vcPostingIdList = string(tDInvoiceMovementForBill.tiPosting_ID).
        else assign vcPostingIdList = vcPostingIdList + "," + string(tDInvoiceMovementForBill.tiPosting_ID).
        
        viLengthOfQueryString = viLengthOfQueryString + length(string(tDInvoiceMovementForBill.tiPosting_ID), "CHARACTER") + 50.
        if viLengthOfQueryString  > 8000 then
        do:
            /* Get and Append dinvoice movement id in same posting by posting_id*/
            <Q-56 run GetDInvoiceMovementByPostingID (all) (Append) (NoCache)
               (input ?, (CompanyId)
                input vcPostingIdList, (Posting_IDs)
                output dataset tqGetDInvoiceMovementByPostingID) in BBillReport>
            
            /* Get and Append DDocument Info by posting id */
            <Q-63 run GetDDocumentByPostingID (all) (Append) (NoCache)
               (input ?, (CompanyId)
                input vcPostingIdList, (Posting_IDs)
                output dataset tqGetDDocumentByPostingID) in BBillReport>
                
            assign vcPostingIdList        = ''
                   viLengthOfQueryString  = 0.
        end.
    end.
end.
if vcPostingIdList <> '' and vcPostingIdList <> ? then
do:
    /* Get and Append dinvoice movement id in same posting by posting_id*/    
    <Q-12 run GetDInvoiceMovementByPostingID (all) (Append) (NoCache)
       (input ?, (CompanyId)
        input vcPostingIdList, (Posting_IDs)
        output dataset tqGetDInvoiceMovementByPostingID) in BBillReport>
    
    /* Get and Append DDocument Info by posting id */
    <Q-18 run GetDDocumentByPostingID (all) (Append) (NoCache)
       (input ?, (CompanyId)
        input vcPostingIdList, (Posting_IDs)
        output dataset tqGetDDocumentByPostingID) in BBillReport>
end.

/* Create tdinvoiceIdForFirstBillTT, save invoice_id and posting date,
    this temp tabel will be used for prepayment */
for each tDInvoiceMovementForBill where
    tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} and
    tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-INVOICE}
    break by tDInvoiceMovementForBill.tiDInvoice_ID:

    /* Get initial invoice posting date */
    if first-of(tDInvoiceMovementForBill.tiDInvoice_ID) then
    do:
        Create tdinvoiceIdForFirstBillTT.
        assign tdinvoiceIdForFirstBillTT.tiDInvoiceId   = tDInvoiceMovementForBill.tiDInvoice_ID
               tdinvoiceIdForFirstBillTT.ttDInvoiceDate = tDInvoiceMovementForBill.ttPostingDate.
    end.
end.

/* Get and Append deduction movement info */
for each tDInvoiceMovementForBill where
    tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-DEDUCTION}:

    /* Get deduction ddocument */
    <Q-66 run GetDDocInvXrefByDinvoiceId (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input tDInvoiceMovementForBill.tiDInvoice_ID, (DInvoiceId)
        output dataset tqGetDDocInvXrefByDinvoiceId) in BBillReport>

    find first tqGetDDocInvXrefByDinvoiceId no-lock no-error.
    if available tqGetDDocInvXrefByDinvoiceId then
    do:
        <Q-98 run GetDInvoiceMovementByDocId (all) (Append) (NoCache)
           (input ?, (CompanyId)
            input tqGetDDocInvXrefByDinvoiceId.tiDDocument_ID, (DDocumentId)
            output dataset tqGetDInvoiceMovementByDocID) in BBillReport>
    end.
end.

/* This loop will handle some special invoice type, such as prepayment and deduction. */
for each tDInvoiceMovementForBill break by tDInvoiceMovementForBill.tiDInvoiceMovement_ID:

    /*Initialize parameters */
    assign vlIsAlloToPrepay           = false
           vlIsAlloToDeduction        = false
           vlIsMovementInFutureBill   = false
           vlIsPrepInPreviousBill     = false
           vcPrepDDocumentRef         = '':U
           vcPrepGLTransaction        = '':U
           vcPrepPaymentType          = '':U
           vcPrepPostingTxt           = '':U
           vcdedcDDocumentRef         = '':U
           vcdedcGLTransaction        = '':U
           vcdedcPaymentType          = '':U
           vcdedcPostingTxt           = '':U
           vdprepTC                   = 0
           vdalloTC                   = 0
           viTempPostingID            = 0.
    /* Empty temp table */
    empty temp-table tBillReportPaymentTT.
   
    /* If current Movement ID is bigger than High water mark of current bill, 
       then it should be recorded into next bill period. */
    if icCurBillStatus <> {&BILLSTATUS-INITIAL} and
       iiCurHighWaterMark <> 0                  and
       tDInvoiceMovementForBill.tiDInvoiceMovement_ID > iiCurHighWaterMark then next.

    /* -If current Movement belongs to the invoice of future bill, 
        then set it as 'non-bill' and leave it to the future bill print.*/
    if tDInvoiceMovementForBill.tiBill_ID <> 0 then
        if tDInvoiceMovementForBill.tiBill_ID > iiCurBillID then
            assign vlIsMovementInFutureBill = true.

    if tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT} and
       tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-INVOICE} then
    do:
        /* Check if current Movement type is invoice and it posting with a prepayment */
        /* The cases as below */
        /* --------------------------------------------------------------------------------------------------------- */
        /*  *Inv For Bill      **In Cur Bill    ***Alo to Prep    Bill: N/A;   Non-Bill: N/A                         */
        /*                                      ***Not Alo        Bill: Plus;  Non-Bill: N/A                         */
        /*                     **In Pre Bill    ***Alo to Prep    Bill: N/A;   Non-Bill: N/A                         */
        /*                                      ***Not Alo        Bill: Plus;  Non-Bill: N/A                         */
        /*                     **In next Bill   ***Alo to Prep    Bill: Minus; Non-Bill: Plus                        */
        /*                                      ***Not Alo        Bill: N/A;   Non-Bill: Plus                        */
        /* --------------------------------------------------------------------------------------------------------- */
        /* *Inv For Non-Bill   **Alo to Prep                      Bill: Minus; Non-Bill: Plus                        */
        /*                     **Not Alo                          Bill: N/A;   Non-Bill: Plus                        */
        /* --------------------------------------------------------------------------------------------------------- */
        if can-find (first tqGetDInvoiceMovementByPostingID where
            tqGetDInvoiceMovementByPostingID.tiPosting_ID   = tDInvoiceMovementForBill.tiPosting_ID and
           (tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
            tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}) no-lock) then 
        do:
            /* Check if current posting is allocate prep to dinvoice */
            for each tqGetDInvoiceMovementByPostingID where
                tqGetDInvoiceMovementByPostingID.tiPosting_ID   = tDInvoiceMovementForBill.tiPosting_ID no-lock:

                if tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
                   tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT} then 
                assign vdprepTC = vdprepTC + tqGetDInvoiceMovementByPostingID.tdPostingLineCreditTC - tqGetDInvoiceMovementByPostingID.tdPostingLineDebitTC.
                else assign vdalloTC = vdalloTC + tqGetDInvoiceMovementByPostingID.tdPostingLineCreditTC - tqGetDInvoiceMovementByPostingID.tdPostingLineDebitTC.
            end.

            if (vdprepTC + vdalloTC = 0) then 
            assign vlIsAlloToPrepay = true.

            /* -If this movement belongs to invoice of current bill period, 
                skip and not show this movement in Payment section.
               -If this movement belongs to invoice of previous bill period,
                we need to check if the prepayment happend in previous bill, 
                then this movement need to be displayed cause in previous bill it had been rolled back,
                else if prepayment happend in current bill, then skip. 
               -If this movement belongs to invoice of future bill period,
                show this movement in non-bill payment group and a opposite one in bill payment group. */
            if tDInvoiceMovementForBill.tiBill_ID <> 0 and vlIsMovementInFutureBill = false then
            do:
                if vlIsAlloToPrepay = true then 
                do:
                    /* Check if prepayment belongs to previous bill */
                    find first tqGetDInvoiceMovementByPostingID where 
                        tqGetDInvoiceMovementByPostingID.tiPosting_ID   = tDInvoiceMovementForBill.tiPosting_ID and
                        tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT}             and         
                        tqGetDInvoiceMovementByPostingID.ttPostingDate  < itPreBillDate no-lock no-error.
                        
                    if available tqGetDInvoiceMovementByPostingID and ilIsFirstBill = false then
                    do:                        
                        assign vlIsAlloToPrepay = false.
                        /* Get the initial posting info of this Prepayment */
                        <M-46 run GetDInvoicePostingInfo
                           (input  tqGetDInvoiceMovementByPostingID.tiDInvoice_ID (iiDInvoice_ID), 
                            output vcPrepGLTransaction (ocGLTransaction), 
                            output vcPrepPostingTxt (ocPostingTxt), 
                            output vcPrepPaymentType (ocPaymentType), 
                            output viTempPostingID (oiPosting_ID), 
                            output viFcReturnSuper (oiReturnStatus)) in BBillReport>
                                
                        /* Get initial posting Docuemnt info of Prepayment */
                        find first tqGetDDocumentByPostingID where
                            tqGetDDocumentByPostingID.tiPosting_ID = viTempPostingID no-lock no-error.
                        if available tqGetDDocumentByPostingID then
                            assign vcprepddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
                        else do:
                            /* Get DDocument Info */
                            <Q-96 run GetDDocumentByPostingID (all) (Append) (NoCache)
                               (input ?, (CompanyId)
                                input viTempPostingID, (Posting_ID)
                                output dataset tqGetDDocumentByPostingID) in BBillReport>
        
                            find first tqGetDDocumentByPostingID where
                                tqGetDDocumentByPostingID.tiPosting_ID = viTempPostingID no-lock no-error.
                            if available tqGetDDocumentByPostingID then
                            assign vcprepddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
                        end. /* if available tqGetDDocumentByPostingID */
                    end.
                    else next.
                end. /* if vlIsAlloToPrepay = true */
            end. /* if tDInvoiceMovementForBill.tiBill_ID <> 0 and vlIsMovementInFutureBill = false */
            /* -If this movement doesn't belong to any bill, then it will be showed in 'Non-bill',
                and also in 'Bill' section with the Amount as mineus, 
                so that this amount will be addded back to sales as a correct item. 
               -The same as movement in future bill. */
            else do:
                if vlIsAlloToPrepay = true then
                do:
                    /* For first bill, the prepayment for non-bill has a little different:
                       If this invoice was created before the beginning date of bill, then it should be 
                        considered as containing in the bill, so current movement should be skipped,
                       Else the movement should be handled as general. */
                    if ilIsFirstBill = true and vlIsMovementInFutureBill = false and
                       not can-find (first tdinvoiceIdForFirstBillTT where 
                                     tdinvoiceIdForFirstBillTT.tiDInvoiceId = tDInvoiceMovementForBill.tiDInvoice_ID no-lock)
                    then next.

                    /* The invoice movement should be displayed as the same posting information 
                        as the prepayment which they had been allocated to. */
                    find first tqGetDInvoiceMovementByPostingID where 
                        tqGetDInvoiceMovementByPostingID.tiPosting_ID = tDInvoiceMovementForBill.tiPosting_ID and
                       (tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
                        tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}) no-lock no-error.

                    if available tqGetDInvoiceMovementByPostingID then 
                    do:
                        /* Get the initial posting info of this Prepayment */
                        <M-7 run GetDInvoicePostingInfo
                           (input  tqGetDInvoiceMovementByPostingID.tiDInvoice_ID (iiDInvoice_ID), 
                            output vcPrepGLTransaction (ocGLTransaction), 
                            output vcPrepPostingTxt (ocPostingTxt), 
                            output vcPrepPaymentType (ocPaymentType), 
                            output viTempPostingID (oiPosting_ID), 
                            output viFcReturnSuper (oiReturnStatus)) in BBillReport>

                        /* Get initial posting Docuemnt info of Prepayment */
                        find first tqGetDDocumentByPostingID where
                            tqGetDDocumentByPostingID.tiPosting_ID = viTempPostingID no-lock no-error.
                        if available tqGetDDocumentByPostingID then
                        assign vcprepddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
                        else do:
                            /* Get DDocument Info */
                            <Q-43 run GetDDocumentByPostingID (all) (Append) (NoCache)
                               (input ?, (CompanyId)
                                input viTempPostingID, (Posting_ID)
                                output dataset tqGetDDocumentByPostingID) in BBillReport>

                            find first tqGetDDocumentByPostingID where
                                tqGetDDocumentByPostingID.tiPosting_ID = viTempPostingID no-lock no-error.
                            if available tqGetDDocumentByPostingID then
                            assign vcprepddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
                        end.
                    end. /* if available tqGetDInvoiceMovementByPostingID */
                end. /* if vlIsAlloToPrepay = true */
            end. /* else do */
        end. /* if can-find (first tqGetDInvoiceMovementByPostingID */

        /* Check if current Movement type is invoice and it allocated to a deduction */
        if can-find (first tqGetDInvoiceMovementByDocId where
            tqGetDInvoiceMovementByDocId.tiDInvoiceMovement_ID = tDInvoiceMovementForBill.tiDInvoiceMovement_ID no-lock) then
        do:
            assign vlIsAlloToDeduction = true.
            /* Get DDocument ID of this deduction */
            find first tqGetDInvoiceMovementByDocId where
                tqGetDInvoiceMovementByDocId.tiDInvoiceMovement_ID = tDInvoiceMovementForBill.tiDInvoiceMovement_ID no-lock no-error.
            assign viDedcDocumentID = tqGetDInvoiceMovementByDocID.tiDDocument_ID.

            /* Get the deduction id in the same ddocument */
            find first tqGetDInvoiceMovementByDocId where
                tqGetDInvoiceMovementByDocID.tiDDocument_ID = viDedcDocumentID         and
                tqGetDInvoiceMovementByDocID.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} no-lock no-error.
            if available tqGetDInvoiceMovementByDocID then
            assign vcDedcGLTransaction = string(tqGetDInvoiceMovementByDocID.tiPostingYear)                    + "/":U +
                                         tqGetDInvoiceMovementByDocID.tcJournalCode                            + "/":U +
                                         string(tqGetDInvoiceMovementByDocID.tiPostingVoucher, "999999999":U)
                   vcDedcPostingTxt    = tqGetDInvoiceMovementByDocID.tcPostingText
                   vcDedcPaymentType   = {&INVOICETYPE-DEDUCTION-TR}.

            /* Get initial posting Docuemnt info of Deduction */
            find first tqGetDDocumentByPostingID where
                tqGetDDocumentByPostingID.tiPosting_ID = tqGetDInvoiceMovementByDocID.tiPosting_ID no-lock no-error.
            if available tqGetDDocumentByPostingID then
            assign vcDedcddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
            else do:
                /* Get DDocument Info */
                <Q-22 run GetDDocumentByPostingID (all) (Append) (NoCache)
                   (input ?, (CompanyId)
                    input tqGetDInvoiceMovementByDocID.tiPosting_ID, (Posting_ID)
                    output dataset tqGetDDocumentByPostingID) in BBillReport>

                find first tqGetDDocumentByPostingID where
                    tqGetDDocumentByPostingID.tiPosting_ID = tqGetDInvoiceMovementByDocID.tiPosting_ID no-lock no-error.
                if available tqGetDDocumentByPostingID then
                assign vcDedcddocumentRef = tqGetDDocumentByPostingID.tcDDocumentReference.
            end.
        end. /* if can-find (first tqGetDInvoiceMovementByDocId */
    end. /*if tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT} */

    /* Create tBillReportPayment for normal invoice/movement and Prepayment.Adjstment.Deduction/initial */
    if (tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT}      and
        (tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-INVOICE}               or 
         tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}            or
         tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION}     or
         tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}  or
         tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-FINANCECHARGE}))       or 
       (tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-INITIAL}       and 
        (tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-PREPAYMENT}    or
         tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-ADJUSTMENT} )) then
    do:
        /* Skip DInvoiceMovement which has already been added */
        if can-find (first tBillReportPayment where 
            tBillReportPayment.tiDInvoiceMovement_ID = tDInvoiceMovementForBill.tiDInvoiceMovement_ID and
            tBillReportPayment.tiBillReport_ID       = viBillReport_ID no-lock)
        then next.

        create tBillReportPayment.
        assign tBillReportPayment.ttPostingDate             = tDInvoiceMovementForBill.ttPostingDate
                tBillReportPayment.tiBillReport_ID          = viBillReport_ID
                tBillReportPayment.tiDInvoiceMovement_ID    = tDInvoiceMovementForBill.tiDInvoiceMovement_ID
                tBillReportPayment.tcGLTransaction          = if vcPrepGLTransaction <> '' then vcPrepGLTransaction
                                                              else if vlIsAlloToDeduction then vcDedcGLTransaction
                                                              else (string(tDInvoiceMovementForBill.tiPostingYear)                    + "/":U +
                                                                    tDInvoiceMovementForBill.tcJournalCode                            + "/":U +
                                                                    string(tDInvoiceMovementForBill.tiPostingVoucher, "999999999":U))
                tBillReportPayment.tiBill_ID                = tDInvoiceMovementForBill.tiBill_ID
                tBillReportPayment.tcBillCurrency           = tDInvoiceMovementForBill.tcCurrencyCode
                tBillReportPayment.ttPaymentDate            = tDInvoiceMovementForBill.ttDInvoiceDueDate   
                tBillReportPayment.tcPostingTxt             = if vcpreppostingtxt <> '' then vcpreppostingtxt
                                                              else if vcdedcpostingtxt <> '' then vcdedcpostingtxt
                                                              else tDInvoiceMovementForBill.tcPostingText
                tBillReportPayment.tiPosting_ID             = tDInvoiceMovementForBill.tiPosting_ID
                tBillReportPayment.tdPaymentAmt             = (tDInvoiceMovementForBill.tdPostingLineCreditTC - tDInvoiceMovementForBill.tdPostingLineDebitTC).
        
        /* Get DDocument Info */
        find first tqGetDDocumentByPostingID where
            tqGetDDocumentByPostingID.tiPosting_ID = tBillReportPayment.tiPosting_ID no-lock no-error.

        if available tqGetDDocumentByPostingID then
            assign tBillReportPayment.tiDDOcument_ID           = tqGetDDocumentByPostingID.tiDDocument_ID
                   tBillReportPayment.tcDDocumentReference     = if vcprepddocumentref <> '' then vcprepddocumentref
                                                                 else if vcdedcddocumentref <> '' then vcdedcddocumentref
                                                                 else tqGetDDocumentByPostingID.tcDDocumentReference
                   tBillReportPayment.tcPaymentInstrument      = if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-CHEQUE}
                                                                 then {&DOCUMENTTYPE-CHEQUE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-DIRECTDEBIT}
                                                                 then {&DOCUMENTTYPE-DIRECTDEBIT-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-DRAFT}
                                                                 then {&DOCUMENTTYPE-DRAFT-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-ELECTRONIC}
                                                                 then {&DOCUMENTTYPE-ELECTRONIC-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-PROMNOTE}
                                                                 then {&DOCUMENTTYPE-PROMNOTE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-SUMSTATE}
                                                                 then {&DOCUMENTTYPE-SUMSTATE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-TRANSFER}
                                                                 then {&DOCUMENTTYPE-TRANSFER-TR}
                                                                 else "":U.

        if (tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-INVOICE}               or 
            tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}            or
            tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION}     or
            tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}  or
            tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-FINANCECHARGE})  then
        do:
            /* assign payment type */
            assign tBillReportPayment.tcPaymentType = if vcPrepPaymentType <> '' then vcPrepPaymentType
                                                      else if vcDedcPaymentType <> '' then vcDedcPaymentType
                                                      else "Payment".
            /* These are Non-Bill payments */
            if tDInvoiceMovementForBill.tiBill_ID = 0 or vlIsMovementInFutureBill = true then 
            do:
                assign tBillReportPayment.tcPaymentTarget   = "Non-Bill"
                       vdNonBillPay                         = vdNonBillPay + tBillReportPayment.tdPaymentAmt.

                /* If current payment is allocated to non-bill from prepayment, or a future bill, 
                   then after be shown in non-bill payment list with positive amount, 
                   it also should add its amount as a negative amount in payment group. */
                if vlIsAlloToPrepay = true then
                do:
                    create tBillReportPaymentTT.
                    buffer-copy tBillReportPayment to tBillReportPaymentTT.
                    assign tBillReportPaymentTT.tcPaymentTarget  = "Bill"
                           tBillReportPaymentTT.tdPaymentAmt     = tBillReportPaymentTT.tdPaymentAmt * -1
                           vdBillPay                             = vdBillPay + tBillReportPaymentTT.tdPaymentAmt.

                    create tBillReportPayment.
                    buffer-copy tBillReportPaymentTT to tBillReportPayment.
                end.
            end.
            /* Bill payments */
            else 
            do:
                assign tBillReportPayment.tcPaymentTarget   = "Bill"
                       vdBillPay                            = vdBillPay + tBillReportPayment.tdPaymentAmt.
            end. /* end else */
        end. /* if (tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-INVOICE} or */
        else assign tBillReportPayment.tcPaymentTarget   = "Bill"
                    vdBillPay                            = vdBillPay + tBillReportPayment.tdPaymentAmt
                    tBillReportPayment.tcPaymentType     = if tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT}
                                                           then {&INVOICETYPE-PREPAYMENT-TR}
                                                           else if tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}
                                                           then {&INVOICETYPE-ADJUSTMENT-TR}
                                                           else if tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-DEDUCTION}
                                                           then {&INVOICETYPE-DEDUCTION-TR}
                                                           else '':U.

    end. /* if (tDInvoiceMovementForBill.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT}*/

    /* --------------------------------------------------------- */
    /* Create tBillReportPayment for bounced Prepayment/movement */
    /* --------------------------------------------------------- */
    if tDInvoiceMovementForBill.tcDInvoiceMovementType = {&MOVEMENTTYPE-MOVEMENT}  and
       tDInvoiceMovementForBill.tcDInvoiceType         = {&INVOICETYPE-PREPAYMENT} then
    do:
        assign vdprepTC = 0
               vdalloTC = 0.
        /* Check if this movement for prepayment is allocate or bounce */
        for each tqGetDInvoiceMovementByPostingID where
            tqGetDInvoiceMovementByPostingID.tiPosting_ID   = tDInvoiceMovementForBill.tiPosting_ID no-lock:

            if tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
               tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT} or
               tqGetDInvoiceMovementByPostingID.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} then 
            assign vdprepTC = vdprepTC + tqGetDInvoiceMovementByPostingID.tdPostingLineCreditTC - tqGetDInvoiceMovementByPostingID.tdPostingLineDebitTC.
            else assign vdalloTC = vdalloTC + tqGetDInvoiceMovementByPostingID.tdPostingLineCreditTC - tqGetDInvoiceMovementByPostingID.tdPostingLineDebitTC.
        end.
        if (vdprepTC + vdalloTC = 0) then next.

        /* Skip DInvoiceMovement which has already been added */
        if can-find (first tBillReportPayment where 
            tBillReportPayment.tiDInvoiceMovement_ID = tDInvoiceMovementForBill.tiDInvoiceMovement_ID)
        then next.
    
        create tBillReportPayment.
        assign tBillReportPayment.ttPostingDate             = tDInvoiceMovementForBill.ttPostingDate
                tBillReportPayment.tiBillReport_ID          = viBillReport_ID
                tBillReportPayment.tiDInvoiceMovement_ID    = tDInvoiceMovementForBill.tiDInvoiceMovement_ID
                tBillReportPayment.tcGLTransaction          = string(tDInvoiceMovementForBill.tiPostingYear)                    + "/":U +
                                                              tDInvoiceMovementForBill.tcJournalCode                            + "/":U +
                                                              string(tDInvoiceMovementForBill.tiPostingVoucher, "999999999":U)
                tBillReportPayment.tiBill_ID                = tDInvoiceMovementForBill.tiBill_ID
                tBillReportPayment.tcBillCurrency           = tDInvoiceMovementForBill.tcCurrencyCode
                tBillReportPayment.ttPaymentDate            = tDInvoiceMovementForBill.ttDInvoiceDueDate   
                tBillReportPayment.tcPostingTxt             = tDInvoiceMovementForBill.tcPostingText
                tBillReportPayment.tiPosting_ID             = tDInvoiceMovementForBill.tiPosting_ID
                tBillReportPayment.tdPaymentAmt             = (tDInvoiceMovementForBill.tdPostingLineCreditTC - tDInvoiceMovementForBill.tdPostingLineDebitTC)
                tBillReportPayment.tcPaymentTarget          = "Bill"
                vdBillPay                                   = vdBillPay + tBillReportPayment.tdPaymentAmt
                tBillReportPayment.tcPaymentType            = {&INVOICETYPE-PREPAYMENT-TR}.
        
        /* Get DDocument Info */
        find first tqGetDDocumentByPostingID where
            tqGetDDocumentByPostingID.tiPosting_ID = tBillReportPayment.tiPosting_ID no-lock no-error.

        if available tqGetDDocumentByPostingID then
            assign tBillReportPayment.tiDDOcument_ID           = tqGetDDocumentByPostingID.tiDDocument_ID
                   tBillReportPayment.tcDDocumentReference     = tqGetDDocumentByPostingID.tcDDocumentReference
                   tBillReportPayment.tcPaymentInstrument      = if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-CHEQUE}
                                                                 then {&DOCUMENTTYPE-CHEQUE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-DIRECTDEBIT}
                                                                 then {&DOCUMENTTYPE-DIRECTDEBIT-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-DRAFT}
                                                                 then {&DOCUMENTTYPE-DRAFT-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-ELECTRONIC}
                                                                 then {&DOCUMENTTYPE-ELECTRONIC-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-PROMNOTE}
                                                                 then {&DOCUMENTTYPE-PROMNOTE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-SUMSTATE}
                                                                 then {&DOCUMENTTYPE-SUMSTATE-TR}
                                                                 else if tqGetDDocumentByPostingID.tcDDocumentType = {&DOCUMENTTYPE-TRANSFER}
                                                                 then {&DOCUMENTTYPE-TRANSFER-TR}
                                                                 else "":U.
    end.
end. /* for each tqDInvoiceMovementForBill */

empty temp-table tqGetDDocumentByPostingID.
empty temp-table tqGetDInvoiceMovementByPostingID.
empty temp-table tqGetDInvoiceMovementByDocID.