project QadFinancials > class BDebtorReport > method DebtorAgeingAnalysisBackwards

report procedure

Description

DebtorAgeingAnalysisBackwards


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrDebtorAgeingAnalysisBackwardsoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtorReport.DebtorAABGroup


program code (program7/bdebtorreport.p)

empty temp-table tqDebtorAgeingAnalysisBackwards.

<M-20 run GetReportLabels
   (input  'DebtorAgeingAnalysisBackwards':U (icReportName), 
    input  icLanguageCode (icLanguageCode), 
    input  tFilter (tFilter), 
    output tqHeader (tqHeader), 
    output tqFilter (tqFilter), 
    output tqText (tqText), 
    output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0 then return.

/* Get SetDataItemsBasedOnFilterTT. */
<M-1 run SetDataItemsBasedOnFilterTT  (output  viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0 then return.

create tqFilter.
assign 
    tqFilter.ti_Sequence = 0
    tqFilter.tcBusinessFieldLabel = "icActivityCode":U
    tqFilter.tcBusinessFieldName = "SummaryByInfo":U
    tqFilter.tcParameterValue = vcSummaryByFilter.

/* Run some validations on the filter values */       
<M-80 run ValidateDateFilter
   (output vtAgingDate (otAgingDate), 
    output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.
    
<M-28 run ValidateAgeingFilters  (output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0 then return.

/* CA783181 - You can not include drafts and make a selection on SubAcc CC or Prj or Daybooks */
if vlIncludeDraftsFilter = TRUE and
   ((vcFromCostCentreCodeFilter1 <> ? and vcFromCostCentreCodeFilter1 <> '')   or 
    (vcToCostCentreCodeFilter1 <> ?   and vcToCostCentreCodeFilter1 <> '')     or 
    (vcCanDoCostCentreCodeFilter1 <> ? and vcCanDoCostCentreCodeFilter1 <> '') or
    (vcFromProjectCodeFilter <> ?     and vcFromProjectCodeFilter <> '')       or 
    (vcToProjectCodeFilter <> ?       and vcToProjectCodeFilter <> '')         or 
    (vcCandoProjectCodeFilter <> ?    and vcCandoProjectCodeFilter <> '')      or
    (vcFromDivisionCodeFilter <> ?    and vcFromDivisionCodeFilter <> '')      or
    (vcToDivisionCodeFilter <> ?      and vcToDivisionCodeFilter <> '')        or 
    (vcCandoDivisionCodeFilter <> ?   and vcCandoDivisionCodeFilter <> '')     or
    (vcFromJournalCodeFilter <> ?     and vcFromJournalCodeFilter <> '')       or 
    (vcToJournalCodeFilter <> ?       and vcToJournalCodeFilter <> '')         or 
    (vcCandoJournalCodeFilter <> ?    and vcCandoJournalCodeFilter <> ''))
then do :
    assign vcMessage = trim(#T-94'When drafts are included, no selection can be made on sub-accounts, costcentres, projects or daybooks.':255(589442409)T-94#).
    <M-96 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-277215':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
    assign oiReturnStatus = -1.
    return.
end.    

/* Calculate Ageing periods */
<M-3 run SetAgeingPeriod  (output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0 then return.

/* If no currency selected, set default currency to LC */
if vcReportingCurrencyFilter = ?
then assign vcReportingCurrencyFilter = {&CURRENCYTYPE-LC}.

<Q-41 run DInvoiceStageByInvoice (Start) in BDebtorReport >
/* ============================================================== */
/* Get all the Invoices open at end of viToYearPeriodFilter       */
/* Note the aging date is not relevant here                       */
/* Query : Debtor --> DInvoice                                    */
/* ============================================================== */
do viDRCompanyIterator = 1 to viDRCompanyEntries:
    /* Get Info about Company (this method fills viDRCurrentCompany_ID) */
    <M-62 run CrossCompanyValues
       (input  viDRCompanyIterator (iiEntryNumber), 
        output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
    
    if viFcReturnSuper < 0 or
       oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.
    
    if oiReturnStatus < 0
    then return.
    
    empty temp-table tqDebtorAgeingAnalysisBackMR.
    
    <Q-31 run DebtorAgeingAnalysisBackMR (all) (Read) (NoCache)
       (input viDRCurrentCompany_ID, (CompanyId)
        input vcFromDebtorCodeFilter, (FromDebtorCode)
        input vcToDebtorCodeFilter, (ToDebtorCode)
        input vcCandoDebtorCodeFilter, (CandoDebtorCode)
        input vcFromBRCodeFilter, (FromBRCode)
        input vcToBRCodeFilter, (ToBRCode)
        input vcCandoBRCodeFilter, (CandoBRCode)
        input vcFromDivisionCodeFilter, (FromDivisionCode)
        input vcToDivisionCodeFilter, (ToDivisionCode)
        input vcCandoDivisionCodeFilter, (CandoDivisionCode)
        input vcFromJournalCodeFilter, (FromJournalCode)
        input vcToJournalCodeFilter, (ToJournalCode)
        input vcCandoJournalCodeFilter, (CandoJournalCode)
        input vcCurrencyCodeFilter, (CurrencyCode)
        input vtAgingDate, (AgeingDate)
        input vcCandoDContrGLCodeFilter, (CanDoGLCode)
        input vcFromDContrGLCodeFilter, (FromGLCode)
        input vcToDContrGLCodeFilter, (ToGLCode)
        input vcCanDoCostCentreCodeFilter1, (CanDoCCCode)
        input vcFromCostCentreCodeFilter1, (FromCCCode)
        input vcToCostCentreCodeFilter1, (ToCCCode)
        input vcCandoProjectCodeFilter, (CanDoProjectCode)
        input vcFromProjectCodeFilter, (FromProjectCode)
        input vcToProjectCodeFilter, (ToProjectCode)
        input vcDebtorTypeFilter, (DebtorTypeCode)
        input viToYearPeriodFilter, (EndOfYearPeriod)
        input vcSalespersonFilter, (Salesperson)
        input vcPaymentGroupFilter, (PaymentGroup)
        output dataset tqDebtorAgeingAnalysisBackMR) in BDebtorReport> 
    
    /* Main Loop: Process each Invoice */
    for each tqDebtorAgeingAnalysisBackMR
             break by tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID:
                   
        if first-of(tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID)
        then do:

            assign vlSkip      = false
                   vdBalanceCC = 0
                   vdBalanceLC = 0
                   vdBalanceTC = 0.

            /* If "Within terms = no", skip all invoices that aren't due yet */
            if vlInvoicesWithinTermsFilter = false
            then do :
                /* Prepayments and Adjustments: valid only if "Within terms = yes" */
                if (tqDebtorAgeingAnalysisBackMR.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
                    tqDebtorAgeingAnalysisBackMR.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT})
                then assign vlSkip = true.
                else
                if tqDebtorAgeingAnalysisBackMR.ttDInvoiceDueDate >= vtDateForAgeingCalcFilter
                then assign vlSkip = true.
                else 
                if tqDebtorAgeingAnalysisBackMR.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
                then do:
                    <Q-89 run DInvoiceStageByInvoice (all) (Read) (NoCache)
                       (input tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID, (DInvoiceId)
                        output dataset tqDInvoiceStageByInvoice) in BDebtorReport>
                    assign vlSkip = not can-find (first tqDInvoiceStageByInvoice where
                                                        tqDInvoiceStageByInvoice.tiDInvoice_ID = tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID and
                                                        tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate < vtDateForAgeingCalcFilter).
                end. /* &PAYMENTCONDITIONPAYMENTTYPE-STAGED} */
            end. /* if vlInvoicesWithinTermsFilter = false */
            else
            if tqDebtorAgeingAnalysisBackMR.tcPaymentConditionPaymentTyp = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
            then do :   
                <Q-66 run DInvoiceStageByInvoice (all) (Read) (NoCache)
                   (input tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID, (DInvoiceId)
                    output dataset tqDInvoiceStageByInvoice) in BDebtorReport>
            end.
            assign vcDInvoiceOpInfoCode = "":U.
            if tqDebtorAgeingAnalysisBackMR.tcDInvoiceShipperList <> "":U 
                and tqDebtorAgeingAnalysisBackMR.tcDInvoiceShipperList <> ? 
            then assign vcDInvoiceOpInfoCode = tqDebtorAgeingAnalysisBackMR.tcDInvoiceShipperList.
            if tqDebtorAgeingAnalysisBackMR.tcDInvoiceGoldenTaxInvoiceList <> "":U 
                and tqDebtorAgeingAnalysisBackMR.tcDInvoiceGoldenTaxInvoiceList <> ? 
            then do:
                if vcDInvoiceOpInfoCode <> "":U and vcDInvoiceOpInfoCode <> ? 
                then vcDInvoiceOpInfoCode = vcDInvoiceOpInfoCode + ",":U.
                assign vcDInvoiceOpInfoCode = vcDInvoiceOpInfoCode + tqDebtorAgeingAnalysisBackMR.tcDInvoiceShipperList. 
            end.
        
        end. /* if first-of(tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID) */

        if vlSkip then next.

        assign vdBalanceTC = vdBalanceTC + tqDebtorAgeingAnalysisBackMR.tdPostingLineDebitTC - tqDebtorAgeingAnalysisBackMR.tdPostingLineCreditTC
               vdBalanceLC = vdBalanceLC + tqDebtorAgeingAnalysisBackMR.tdPostingLineDebitLC - tqDebtorAgeingAnalysisBackMR.tdPostingLineCreditLC
               vdBalanceCC = vdBalanceCC + tqDebtorAgeingAnalysisBackMR.tdPostingLineDebitCC - tqDebtorAgeingAnalysisBackMR.tdPostingLineCreditCC.

        if last-of(tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID)
        then do:
            assign vdOriginal1CC = tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalDebitCC - tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalCreditCC
                   vdOriginal1LC = tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalDebitLC - tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalCreditLC
                   vdOriginal1TC = tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalDebitTC - tqDebtorAgeingAnalysisBackMR.tdDInvoiceOriginalCreditTC.

            case vcReportingCurrencyFilter:
                when {&CURRENCYTYPE-CC}
                then assign vdBalanceXC   = vdBalanceCC
                            vdOriginal1XC = vdOriginal1CC.
                when {&CURRENCYTYPE-LC}
                then assign vdBalanceXC   = vdBalanceLC
                            vdOriginal1XC = vdOriginal1LC.
                when {&CURRENCYTYPE-TC}
                then assign vdBalanceXC   = vdBalanceTC
                            vdOriginal1XC = vdOriginal1TC.
            end.

            if tqDebtorAgeingAnalysisBackMR.tcPaymentConditionPaymentTyp <> {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
            then do:
                /* tcrDebtorAgeingAnalysisBackwards */
                create tqDebtorAgeingAnalysisBackwards.
                buffer-copy tqDebtorAgeingAnalysisBackMR to tqDebtorAgeingAnalysisBackwards.  
                assign tqDebtorAgeingAnalysisBackwards.tcControlGLCode          = tqDebtorAgeingAnalysisBackMR.tcGLCode
                       tqDebtorAgeingAnalysisBackwards.tcControlGLDescription   = tqDebtorAgeingAnalysisBackMR.tcGLDescription
                       tqDebtorAgeingAnalysisBackwards.tdBalanceXC              = vdBalanceXC
                       tqDebtorAgeingAnalysisBackwards.tdBalanceTC              = vdBalanceTC
                       tqDebtorAgeingAnalysisBackwards.tcDiscountType           = "Y":U
                       tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod1BalanceXC = 0
                       tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod2BalanceXC = 0
                       tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod3BalanceXC = 0
                       tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod4BalanceXC = 0
                       tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod5BalanceXC = 0
                       tqDebtorAgeingAnalysisBackwards.tcDInvoiceOpInfoCode     = vcDInvoiceOpInfoCode.

                /* Determine the Ageing Column. For Prepayments/Adjustments there is an option to put them in first column */
                if ((tqDebtorAgeingAnalysisBackwards.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
                     tqDebtorAgeingAnalysisBackwards.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}) and
                    (not vlIsAgePrepayAdjFilter or vlIsAgePrepayAdjFilter = ?)                   and
                     vlIsAABGroup <> true)
                then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod1BalanceXC = vdBalanceXC.
                else do:
                    find first tAgeingPeriod where
                               tAgeingPeriod.ttAgeingPeriodFromDate <= tqDebtorAgeingAnalysisBackwards.ttDInvoiceDueDate and
                               tAgeingPeriod.ttAgeingPeriodToDate   >= tqDebtorAgeingAnalysisBackwards.ttDInvoiceDueDate
                               no-error.
                    if available tAgeingPeriod
                    then case tAgeingPeriod.tiAgeingPeriodSeq:
                        when 1
                        then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod1BalanceXC = vdBalanceXC.
                        when 2
                        then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod2BalanceXC = vdBalanceXC.
                        when 3
                        then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod3BalanceXC = vdBalanceXC.
                        when 4
                        then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod4BalanceXC = vdBalanceXC.
                        when 5
                        then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod5BalanceXC = vdBalanceXC.
                    end case.
                end. /*else do*/  
                if tqDebtorAgeingAnalysisBackwards.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} then
                do:
                    <M-59 run CalDeductionStatus
                       (input  tqDebtorAgeingAnalysisBackwards.tiDInvoice_ID (iiDInvoiceID), 
                        output tqDebtorAgeingAnalysisBackwards.tcDInvoiceDeductionStatus (ocDeductionStatus), 
                        input-output tqDebtorAgeingAnalysisBackwards.tcDInvoiceType (bcDInvoiceType), 
                        output oiReturnStatus (oiReturnStatus)) in BDebtorReport>
                end.  
            end. /* NOT STAGED */
            else do :
                for each tqDInvoiceStageByInvoice where
                         tqDInvoiceStageByInvoice.tiDInvoice_ID = tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID 
                    by tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate desc:
                
                    case vcReportingCurrencyFilter:
                        when {&CURRENCYTYPE-CC}
                        then assign vdStageAmountXC = if vdOriginal1XC * tqDInvoiceStageByInvoice.tdDInvoiceStageAmountCC > 0 
                                                      then tqDInvoiceStageByInvoice.tdDInvoiceStageAmountCC
                                                      else - tqDInvoiceStageByInvoice.tdDInvoiceStageAmountCC.
                        when {&CURRENCYTYPE-LC}
                        then assign vdStageAmountXC = if vdOriginal1XC * tqDInvoiceStageByInvoice.tdDInvoiceStageAmountLC > 0 
                                                      then tqDInvoiceStageByInvoice.tdDInvoiceStageAmountLC
                                                      else - tqDInvoiceStageByInvoice.tdDInvoiceStageAmountLC.
                        when {&CURRENCYTYPE-TC}
                        then assign vdStageAmountXC = if vdOriginal1XC * tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC > 0 
                                                      then tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC
                                                      else - tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC.
                    end. /* case */

                    assign vdStageAmountTC = if vdOriginal1TC * tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC > 0 
                                             then tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC
                                             else - tqDInvoiceStageByInvoice.tdDInvoiceStageAmountTC.
                    if vdBalanceXC * vdOriginal1XC > 0
                    then do:
                        /* tcrDebtorAgeingAnalysisBackwards */
                        create tqDebtorAgeingAnalysisBackwards.
                        buffer-copy tqDebtorAgeingAnalysisBackMR to tqDebtorAgeingAnalysisBackwards.
                        assign tqDebtorAgeingAnalysisBackwards.tcControlGLCode          = tqDebtorAgeingAnalysisBackMR.tcGLCode
                               tqDebtorAgeingAnalysisBackwards.tcControlGLDescription   = tqDebtorAgeingAnalysisBackMR.tcGLDescription
                               tqDebtorAgeingAnalysisBackwards.tcDiscountType           = "Y":U
                               tqDebtorAgeingAnalysisBackwards.ttDInvoiceDueDate = tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate
                               tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod1BalanceXC = 0
                               tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod2BalanceXC = 0
                               tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod3BalanceXC = 0
                               tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod4BalanceXC = 0
                               tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod5BalanceXC = 0
                               tqDebtorAgeingAnalysisBackwards.tcDInvoiceOpInfoCode    = vcDInvoiceOpInfoCode.
                        if (vdBalanceXC - vdStageAmountXC) * vdOriginal1XC > 0
                        then do:
                            assign vdBalanceXC = vdBalanceXC - vdStageAmountXC
                                   vdBalanceTC = vdBalanceTC - vdStageAmountTC
                                   tqDebtorAgeingAnalysisBackwards.tdBalanceXC = vdStageAmountXC
                                   tqDebtorAgeingAnalysisBackwards.tdBalanceTC = vdStageAmountTC.
                        end.
                        else do:
                            assign tqDebtorAgeingAnalysisBackwards.tdBalanceXC = vdBalanceXC
                                   tqDebtorAgeingAnalysisBackwards.tdBalanceTC = vdBalanceTC.
                            assign vdBalanceXC = vdBalanceXC - vdStageAmountXC
                                   vdBalanceTC = vdBalanceTC - vdStageAmountTC.
                        end.
                        find first tAgeingPeriod where
                               tAgeingPeriod.ttAgeingPeriodFromDate <= tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate and
                               tAgeingPeriod.ttAgeingPeriodToDate   >= tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate no-error.
                        if available tAgeingPeriod
                        then case tAgeingPeriod.tiAgeingPeriodSeq:
                            when 1
                            then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod1BalanceXC = tqDebtorAgeingAnalysisBackwards.tdBalanceXC.
                            when 2
                            then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod2BalanceXC = tqDebtorAgeingAnalysisBackwards.tdBalanceXC.
                            when 3
                            then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod3BalanceXC = tqDebtorAgeingAnalysisBackwards.tdBalanceXC.
                            when 4
                            then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod4BalanceXC = tqDebtorAgeingAnalysisBackwards.tdBalanceXC.
                            when 5
                            then assign tqDebtorAgeingAnalysisBackwards.tdAgeingPeriod5BalanceXC = tqDebtorAgeingAnalysisBackwards.tdBalanceXC.
                        end case.
                    end.
                end. /* for each tqDInvoiceStageByInvoice  */
            end. /* STAGED */
        end. /* if last-of(tqDebtorAgeingAnalysisBackwards.tiDInvoice_ID) */

    end. /* for each tqDebtorAgeingAnalysisBackwards */
end. /*do viDRCompanyIterator = 1 to viDRCompanyEntries*/

<Q-43 run DInvoiceStageByInvoice (Stop) in BDebtorReport >

/* ================================================== */
/* Get the Drafts                                     */
/* ================================================== */
if vlIncludeDraftsFilter = TRUE
then do:
    /* Get the Open Draft Payments */
    <Q-46 run GetDraftInvoicesforDAAB (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input vcFromDebtorCodeFilter, (FromDebtorCode)
        input vcToDebtorCodeFilter, (ToDebtorCode)
        input vcCandoDebtorCodeFilter, (CandoDebtorCode)
        input vcFromBRCodeFilter, (FromBRCode)
        input vcToBRCodeFilter, (ToBRCode)
        input vcCandoBRCodeFilter, (CandoBRCode)
        input vcCurrencyCodeFilter, (CurrencyCode)
        input vtAgingDate, (AgeingDate)
        input vcCandoDContrGLCodeFilter, (CanDoGLCode)
        input vcFromDContrGLCodeFilter, (FromGLCode)
        input vcToDContrGLCodeFilter, (ToGLCode)
        input {&DOCUMENTTYPE-DRAFT}, (DDocumentDraftType)
        input vcDebtorTypeFilter, (DebtorTypeCode)
        input vcPaymentGroupFilter, (PaymentGroup)
        output dataset tqGetDraftInvoicesforDAAB) in BDebtorReport> 

<M-95 run DebtorAgeingAnalysisBackwardsDrafts  (output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then return.

end. /* if vlIncludeDraftsFilter = TRUE */

/* ===================================================== */
/* Get the Customer Address Contact Data             */
/* ===================================================== */
if vlCalcCreditTurnOverFilter = TRUE
then do :
    <I-58 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION   = "false"
         &CLASS                = "BDebtor"}>   
end.

empty temp-table tqDebtorAddressContactInfo.

<Q-39 run AddressTypeByCode (all) (Read) (NoCache)
   (input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
    output dataset tqAddressTypeByCode) in BAddressType>

find first tqAddressTypeByCode where
           tqAddressTypeByCode.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
           no-error.
if available tqAddressTypeByCode
then do:
    <Q-12 run DebtorAddressContactInfo (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
        input vcCandoBRCodeFilter, (CandoBRCode)
        input vcCandoDebtorCodeFilter, (CandoDebtorCode)
        input vcFromBRCodeFilter, (FromBRCode)
        input vcFromDebtorCodeFilter, (FromDebtorCode)
        input vcToBRCodeFilter, (ToBRCode)
        input vcToDebtorCodeFilter, (ToDebtorCode)
        input tqAddressTypeByCode.tiAddressType_ID, (AddressType_ID)
        output dataset tqDebtorAddressContactInfo) in BDebtorReport>

end. /* if available tqAddressTypeByCode */

for each tqDebtorAgeingAnalysisBackwards
         break by tqDebtorAgeingAnalysisBackwards.tcDebtorCode: 
         
    if first-of (tqDebtorAgeingAnalysisBackwards.tcDebtorCode)
    then assign vdBalanceXC = 0.

    assign vdBalanceXC = vdBalanceXC + tqDebtorAgeingAnalysisBackwards.tdBalanceXC
           tqDebtorAgeingAnalysisBackwards.tcSortByFilter2 = vcSortByNameFilter
           tqDebtorAgeingAnalysisBackwards.tcAgeingType    = vcAgeingTypeFilter.

    if last-of(tqDebtorAgeingAnalysisBackwards.tcDebtorCode)
    then do:
        if vcDebtorBalanceFilter = {&DEBTORBALANCE-CREDIT} and vdBalanceXC > 0 or
           vcDebtorBalanceFilter = {&DEBTORBALANCE-DEBIT}  and vdBalanceXC < 0
        then do:
            for each bDebtorAgeingAnalysisBackwards where
                     bDebtorAgeingAnalysisBackwards.tcDebtorCode = tqDebtorAgeingAnalysisBackwards.tcDebtorCode and
                     rowid(bDebtorAgeingAnalysisBackwards) <> rowid(tqDebtorAgeingAnalysisBackwards):
                delete bDebtorAgeingAnalysisBackwards.
            end.
            delete tqDebtorAgeingAnalysisBackwards.
        end.
        else do:

            if vlCalcCreditTurnOverFilter = TRUE
            then do :            
                assign vdCreditOnTurnOver = 0.
                <M-69 run CalculateCreditOnTurnover
                   (input  0 (iiDebtorId), 
                    input  tqDebtorAgeingAnalysisBackwards.tcDebtorCode (icDebtorCode), 
                    input  vcCurrencyCodeFilter (icCurrencyCode), 
                    input  tqDebtorAgeingAnalysisBackwards.tdDebtorPercTurnOverCredLim (idPercentageOfTurnover), 
                    output vdCreditOnTurnOver (odCreditOnTurnover), 
                    output oiReturnStatus (oiReturnStatus)) in BDebtor>
            end.
            else assign vdCreditOnTurnOver = ?.

            /* First look for an address and a contact */
            find first tqDebtorAddressContactInfo where
                       tqDebtorAddressContactInfo.tcDebtorCode = tqDebtorAgeingAnalysisBackwards.tcDebtorCode AND
                       tqDebtorAddressContactInfo.tcContactName > ''
                       no-error.
            if not available tqDebtorAddressContactInfo
            then do :
                /* First look for an address only */
                find first tqDebtorAddressContactInfo where
                           tqDebtorAddressContactInfo.tcDebtorCode = tqDebtorAgeingAnalysisBackwards.TCDEBTORCODE
                           no-error.
            end.
        
            if available tqDebtorAddressContactInfo
            then do :
                
                for each bDebtorAgeingAnalysisBackwards where
                         bDebtorAgeingAnalysisBackwards.tcDebtorCode = tqDebtorAgeingAnalysisBackwards.tcDebtorCode:

                    assign bDebtorAgeingAnalysisBackwards.tcAddressCity        = tqDebtorAddressContactInfo.tcAddressCity
                           bDebtorAgeingAnalysisBackwards.tcAddressFax         = tqDebtorAddressContactInfo.tcAddressFax
                           bDebtorAgeingAnalysisBackwards.tcAddressStreet1     = tqDebtorAddressContactInfo.tcAddressStreet1
                           bDebtorAgeingAnalysisBackwards.tcAddressStreet2     = tqDebtorAddressContactInfo.tcAddressStreet2
                           bDebtorAgeingAnalysisBackwards.tcAddressStreet3     = tqDebtorAddressContactInfo.tcAddressStreet3
                           bDebtorAgeingAnalysisBackwards.tcAddressTelephone   = tqDebtorAddressContactInfo.tcAddressTelephone
                           bDebtorAgeingAnalysisBackwards.tcAddressZip         = tqDebtorAddressContactInfo.tcAddressZip
                           bDebtorAgeingAnalysisBackwards.tcAddressState       = tqDebtorAddressContactInfo.tcAddressState
                           bDebtorAgeingAnalysisBackwards.tiAddressFormat      = tqDebtorAddressContactInfo.tiAddressFormat
                           bDebtorAgeingAnalysisBackwards.tcCountryDescription = tqDebtorAddressContactInfo.tcCountryDescription
                           bDebtorAgeingAnalysisBackwards.tcContactEmail       = tqDebtorAddressContactInfo.tcContactEmail
                           bDebtorAgeingAnalysisBackwards.tcContactName        = tqDebtorAddressContactInfo.tcContactName
                           bDebtorAgeingAnalysisBackwards.tcContactMobile      = tqDebtorAddressContactInfo.tcContactMobilePhone
                           bDebtorAgeingAnalysisBackwards.tcContactPhone       = tqDebtorAddressContactInfo.tcContactTelephone

                           bDebtorAgeingAnalysisBackwards.tdDebtorFixedCredLimTC      = tqDebtorAgeingAnalysisBackwards.tdDebtorFixedCredLimTC
                           bDebtorAgeingAnalysisBackwards.tdDebtorPercTurnOverCredLim = tqDebtorAgeingAnalysisBackwards.tdDebtorPercTurnOverCredLim
                           bDebtorAgeingAnalysisBackwards.tdDebtorCreditOnTurnOver    = vdCreditOnTurnOver. 
                           
                end. /* for each bDebtorAgeingAnalysisBackwards */
            end. /* if available tqDebtorAddressContactInfo */
        end.
    end. /* if last-of(tqDebtorAgeingAnalysisBackwards.tcDebtorCode) */
end. /* for each tqDebtorAgeingAnalysisBackwards */

if vlCalcCreditTurnOverFilter = TRUE
then do :
    <I-90 {bFcAddToTransaction
         &CLASS           = "BDebtor"}>
end.