project QadFinancials > class BDebtorReport > method DebtorAgingHistAcroDomain

report procedure


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrDebtorAgingHistAcroDomainoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bdebtorreport.p)

empty temp-table tqDebtorAgingHistAcroDomain.
empty temp-table tExchangeRateInfo.

<M-20 run GetReportLabels
   (input  'DebtorAgingHistAcroDomain':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-36 run DebtorCurrencyByEntity (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    output dataset tqDebtorCurrencyByEntity) in BDebtorReport>
find first tqDebtorCurrencyByEntity no-error.
if available tqDebtorCurrencyByEntity then
assign viUserCurrencyId = if vcReportingCurrencyFilter = {&CURRENCYTYPE-LC} 
                          then tqDebtorCurrencyByEntity.tiCurrency_ID
                          else tqDebtorCurrencyByEntity.tiStatutoryCurrency_ID.
       vcUserCurrencyCode = if vcReportingCurrencyFilter = {&CURRENCYTYPE-LC} 
                          then tqDebtorCurrencyByEntity.tcCurrencyCode
                          else tqDebtorCurrencyByEntity.tcCurrencyCode1.

<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.

    /* Get Current Company Period */
    <M-9 run GetAppropriatePeriodByEndOfDate
       (input  viDRCurrentCompany_ID (iiCompanyId), 
        input  vtAgingDate (itCurrentPeriodDate), 
        input  viToYearPeriodFilter (iiCurrentPeriod), 
        output viTargetPeriod (oiTargetPeriod), 
        output vtTargetPeriodDate (otTargetPeriodDate), 
        output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>

    if viFcReturnSuper < 0 or
       oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.
    
    if oiReturnStatus < 0
    then return.

    /* Generate tExchangeRateInfo */
    <M-85 run GetExchangeRateForEntity
       (input  viCompanyId (iiTargetEntityId), 
        input  viDRCurrentCompany_ID (iiFromEntityId), 
        input  viUserCurrencyId (iiTargetCurrencyId), 
        input  vtTargetPeriodDate (itExchangeRateDate), 
        input  vcUserCurrencyCode (icTargetCurrencyCode), 
        output vdCurrExchangeRate (odOutExchangeRate), 
        output vdCurrExchangeRateSF (odOutExchangeRateSF), 
        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 vtTargetPeriodDate, (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 viTargetPeriod, (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:

        /* Check Date Type */
        if vcDebtorAgingHistDateTypeFilter = {&DEBTORAGINGDATETYPE-INVOICEDATE}
        then assign vtCalculateDate = tqDebtorAgeingAnalysisBackMR.ttDInvoiceDate.
        else assign vtCalculateDate = tqDebtorAgeingAnalysisBackMR.ttDInvoiceDueDate.

        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 vtCalculateDate >= 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.
        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 tqDebtorAgingHistAcroDomain.
                buffer-copy tqDebtorAgeingAnalysisBackMR to tqDebtorAgingHistAcroDomain.
                assign tqDebtorAgingHistAcroDomain.tcControlGLCode          = tqDebtorAgeingAnalysisBackMR.tcGLCode
                       tqDebtorAgingHistAcroDomain.tcControlGLDescription   = tqDebtorAgeingAnalysisBackMR.tcGLDescription
                       tqDebtorAgingHistAcroDomain.tdBalanceXC              = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF
                       tqDebtorAgingHistAcroDomain.tdBalanceTC              = vdBalanceTC
                       tqDebtorAgingHistAcroDomain.tcDiscountType           = "Y":U
                       tqDebtorAgingHistAcroDomain.tdAgeingPeriod1BalanceXC = 0
                       tqDebtorAgingHistAcroDomain.tdAgeingPeriod2BalanceXC = 0
                       tqDebtorAgingHistAcroDomain.tdAgeingPeriod3BalanceXC = 0
                       tqDebtorAgingHistAcroDomain.tdAgeingPeriod4BalanceXC = 0
                       tqDebtorAgingHistAcroDomain.tdAgeingPeriod5BalanceXC = 0
                       tqDebtorAgingHistAcroDomain.tcCompanyCode            = tqDebtorAgeingAnalysisBackMR.tcDInvoiceCompanyCode.

                /* Determine the Ageing Column. For Prepayments/Adjustments there is an option to put them in first column */
                if ((tqDebtorAgingHistAcroDomain.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
                     tqDebtorAgingHistAcroDomain.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}) and
                    (not vlIsAgePrepayAdjFilter or vlIsAgePrepayAdjFilter = ?)                   and
                     vlIsAABGroup <> true)
                then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod1BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                else do:
                    find first tAgeingPeriod where
                               tAgeingPeriod.ttAgeingPeriodFromDate <= vtCalculateDate and
                               tAgeingPeriod.ttAgeingPeriodToDate   >= vtCalculateDate
                               no-error.
                    if available tAgeingPeriod
                    then case tAgeingPeriod.tiAgeingPeriodSeq:
                        when 1
                        then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod1BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                        when 2
                        then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod2BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                        when 3
                        then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod3BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                        when 4
                        then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod4BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                        when 5
                        then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod5BalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF.
                    end case.
                end. /*else do*/  
                if tqDebtorAgingHistAcroDomain.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} then
                do:
                    <M-59 run CalDeductionStatus
                       (input  tqDebtorAgingHistAcroDomain.tiDInvoice_ID (iiDInvoiceID), 
                        output tqDebtorAgingHistAcroDomain.tcDInvoiceDeductionStatus (ocDeductionStatus), 
                        input-output tqDebtorAgingHistAcroDomain.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 tqDebtorAgingHistAcroDomain.
                        buffer-copy tqDebtorAgeingAnalysisBackMR to tqDebtorAgingHistAcroDomain.
                        assign tqDebtorAgingHistAcroDomain.tcControlGLCode          = tqDebtorAgeingAnalysisBackMR.tcGLCode
                               tqDebtorAgingHistAcroDomain.tcControlGLDescription   = tqDebtorAgeingAnalysisBackMR.tcGLDescription
                               tqDebtorAgingHistAcroDomain.tcDiscountType           = "Y":U
                               tqDebtorAgingHistAcroDomain.ttDInvoiceDueDate        = tqDInvoiceStageByInvoice.ttDInvoiceStageDueDate
                               tqDebtorAgingHistAcroDomain.tdAgeingPeriod1BalanceXC = 0
                               tqDebtorAgingHistAcroDomain.tdAgeingPeriod2BalanceXC = 0
                               tqDebtorAgingHistAcroDomain.tdAgeingPeriod3BalanceXC = 0
                               tqDebtorAgingHistAcroDomain.tdAgeingPeriod4BalanceXC = 0
                               tqDebtorAgingHistAcroDomain.tdAgeingPeriod5BalanceXC = 0
                               tqDebtorAgingHistAcroDomain.tcCompanyCode            = tqDebtorAgeingAnalysisBackMR.tcDInvoiceCompanyCode.
                        if (vdBalanceXC - vdStageAmountXC) * vdOriginal1XC > 0
                        then do:
                            assign vdBalanceXC = vdBalanceXC - vdStageAmountXC
                                   vdBalanceTC = vdBalanceTC - vdStageAmountTC
                                   tqDebtorAgingHistAcroDomain.tdBalanceXC = vdStageAmountXC * vdCurrExchangeRate * vdCurrExchangeRateSF
                                   tqDebtorAgingHistAcroDomain.tdBalanceTC = vdStageAmountTC.
                        end.
                        else do:
                            assign tqDebtorAgingHistAcroDomain.tdBalanceXC = vdBalanceXC * vdCurrExchangeRate * vdCurrExchangeRateSF
                                   tqDebtorAgingHistAcroDomain.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 tqDebtorAgingHistAcroDomain.tdAgeingPeriod1BalanceXC = tqDebtorAgingHistAcroDomain.tdBalanceXC.
                            when 2
                            then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod2BalanceXC = tqDebtorAgingHistAcroDomain.tdBalanceXC.
                            when 3
                            then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod3BalanceXC = tqDebtorAgingHistAcroDomain.tdBalanceXC.
                            when 4
                            then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod4BalanceXC = tqDebtorAgingHistAcroDomain.tdBalanceXC.
                            when 5
                            then assign tqDebtorAgingHistAcroDomain.tdAgeingPeriod5BalanceXC = tqDebtorAgingHistAcroDomain.tdBalanceXC.
                        end case.
                    end.
                end. /* for each tqDInvoiceStageByInvoice  */
            end. /* STAGED */
        end. /* if last-of(tqDebtorAgeingAnalysisBackMR.tiDInvoice_ID) */
    end. /* for each tqDebtorAgeingAnalysisBackMR */

    /* ================================================== */
    /* Get the Drafts                                     */
    /* ================================================== */
    if vlIncludeDraftsFilter = TRUE
    then do:
        empty temp-table tqGetDraftInvoicesforDAAB.
    
        /* Get the Open Draft Payments */
        <Q-46 run GetDraftInvoicesforDAAB (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 vcCurrencyCodeFilter, (CurrencyCode)
            input vtTargetPeriodDate, (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 DebtorAgingHistAcroDomainDrafts
           (input  vdCurrExchangeRate (idCurrExchangeRate), 
            input  vdCurrExchangeRateSF (idCurrExchangeRateSF), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then return.

    end. /* if vlIncludeDraftsFilter = TRUE */
end. /*do viDRCompanyIterator = 1 to viDRCompanyEntries*/

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

/* ===================================================== */
/* 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 */

define buffer bDebtorAgingHistAcroDomain for tqDebtorAgingHistAcroDomain.
for each tqDebtorAgingHistAcroDomain
         break by tqDebtorAgingHistAcroDomain.tcDebtorCode:

    if first-of (tqDebtorAgingHistAcroDomain.tcDebtorCode)
    then assign vdBalanceXC = 0.

    assign vdBalanceXC = vdBalanceXC + tqDebtorAgingHistAcroDomain.tdBalanceXC
           tqDebtorAgingHistAcroDomain.tcSortByFilter2 = vcSortByNameFilter.

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

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

                if viFcReturnSuper < 0 or oiReturnStatus  = 0
                then assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0 then return.
            end.
            else assign vdCreditOnTurnOver = ?.

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

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

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

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