project QadFinancials > class BProjectReport > method DebtorAgeingAnalysisByProj

report procedure


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrDebtorAgeingAnalysisByProjoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bprojectreport.p)

empty temp-table tqDebtorAgeingAnalysisByProj.

<M-1 run GetReportLabels
   (input  'DebtorAgeingAnalysisByProj':U (icReportName), 
    input  icLanguageCode (icLanguageCode), 
    input  tFilter (tFilter), 
    output tqHeader (tqHeader), 
    output tqFilter (tqFilter), 
    output tqText (tqText), 
    output oiReturnStatus (oiReturnStatus)) in BProjectReport>
                          
if oiReturnStatus <> 0
then return.       

<M-2 run SetDataItemsBasedOnFilterTT (output viFcReturnSuper (oiReturnStatus)) in BProjectReport>

/* We need to check if there are still some transactions pending in the historydaemon */
if vlPCheckHistoryFilter = ? or
   vlPCheckHistoryFilter
then do:
    <Q-23 assign vlFcQueryRecordsAvailable = QDInvoiceMovementAll (NoCache)
       (input viCompanyId, (CompanyId)
        input ?, (DaemonId)) in BQDInvoiceMovement >
    if vlFcQueryRecordsAvailable = true or
       vlFcQueryRecordsAvailable = ?
    then do:        
        <M-27 run SetMessage
          (input  #T-27'Transaction history is not up to date.':50(14722)t-27# (icMessage), 
           input  '':U (icArguments), 
           input  '':U (icFieldName), 
           input  '':U (icFieldValue), 
           input  'E':U (icType), 
           input  3 (iiSeverity), 
           input  '':U (icRowid), 
           input  'QADFIN-3735':U (icFcMsgNumber), 
           input  #T-28'There are still some transactions waiting to be processed by the History daemon, which causes inconsistencies.':150(14719)T-28# (icFcExplanation), 
           input  '':U (icFcIdentification), 
           input  '':U (icFcContext), 
           output oiReturnStatus (oiReturnStatus)) in BProjectReport>                      
        assign oiReturnStatus = -1.
        return.
    end.
end.

/*check mandatory filter fields*/
assign vcErrorMessage1 = "":U.        

if vtDateForAgeingCalcFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-17'You must enter the date for aging calculation.':50(1467)T-17# + chr(10).
    <M-11 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2575':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>    
end.
if vcAgeingTypeFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-18'You must enter the aging type.':50(1468)T-18# + chr(10).
    <M-12 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2576':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
end.
if viAgeingOffsetFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-19'You must enter the aging offset.':50(1469)T-19# + chr(10).
    <M-13 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2577':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
end.
else do:
    if vcAgeingTypeFilter2 = {&AGEINGTYPE-DAYS} and (viAgeingOffsetFilter2 < 1 or viAgeingOffsetFilter2 > 60)
    then do:
        assign vcErrorMessage1 = #T-20'Aging offset is out of range (1-60).':50(1470)T-20# + chr(10).
          <M-14 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2578':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
    end.                         
    if vcAgeingTypeFilter2 = {&AGEINGTYPE-MONTHS} and (viAgeingOffsetFilter2 < 1 or viAgeingOffsetFilter2 > 12)
    then do:
         assign vcErrorMessage1 = #T-21'Aging offset is out of range (1-12).':50(1471)T-21# + chr(10).
        <M-15 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2579':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
        end.
end.
if vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and vcCurrencyCodeFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-22'You must enter the currency code.':50(1460)T-22# + chr(10).
      <M-16 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2580':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
end.
if vlOnlyProjectTotalsFilter = true and vlShowDetailLinesFilterPR = true
then do:
    assign vcErrorMessage1 = #T-30'The Project Totals Only and the Show Detail Lines filter fields cannot both be set to Yes.':100(16252)t-30# + chr(10).
    <M-29 run SetMessage (input  vcErrorMessage1 (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  '':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-3916':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>        
end.


if vcErrorMessage1 <> "":U
then do:     
    assign oiReturnStatus = -1.
    return.
end.          


/*calculate Ageing periods*/
<M-10 run SetAgeingPeriods (output viFcReturnSuper (oiReturnStatus)) in BProjectReport>

if viAccYearFilter = 0 then viAccYearFilter = -999.
if viAccPeriodFilter = 0 then viAccPeriodFilter = -999.
assign viAccPeriodFilter = (viAccYearFilter * 100) + viAccPeriodFilter.             

if vcCurrencyTypeFilter = ? then assign vcCurrencyTypeFilter = {&CURRENCYTYPE-LC}.         

<Q-9 run ProfileLinkByGL (all) (Read) (NoCache)
          (input ?, (GlProfileId)
           input viCompanyId, (CompanyId)
           output dataset tqProfileLinkByGL) in BProfile >  
                          
<Q-4 run DebtorBalanceByFilter (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input ?, (DebtorId)
           input viCurrencyIdFilter2, (CurrencyId)
           input vcFromDebtorFilter, (FromDebtorCode)
           input vcToDebtorFilter, (ToDebtorCode)
           input vcCandoDebtorFilter, (CanDoDebtorCode)
           output dataset tqDebtorBalanceByFilter) in BProjectReport >    
                                
<Q-5 run BaseDebtorAAByProject (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input vcCandoDebtorFilter, (CanDoDebtorCode)
           input vcFromDebtorFilter, (FromDebtorCode)
           input vcToDebtorFilter, (ToDebtorCode)
           input vcFromBRFilter, (FromBRCode)
           input vcToBRFilter, (ToBRCode)
           input vcCandoBRFilter, (CanDoBRCode)
           input viAccPeriodFilter, (AccToPeriodYear)
           input vcFromDivisionFilter, (FromDivisionCode)
           input vcToDivisionFilter, (ToDivisionCode)
           input vcCandoDivisionFilter, (CanDoDivisionCode)
           input vcFromProjectFilter, (FromProjectCode)
           input vcToProjectFilter, (ToProjectCode)
           input vcCandoProjectFilter, (CanDoProjectCode)
           input vcCandoProjectManagerFilter, (CanDoProjectManager)
           input vcFromProjectManagerFilter, (FromProjectManager)
           input vcToProjectManagerFilter, (ToProjectManager)
           input vcCandoJournalFilter, (CanDoJournalCode)
           input vcToJournalFilter, (ToJournalCode)
           input vcFromJournalFilter, (FromJournalCode)
           input vcCurrencyCodeFilter2, (CurrencyCode)
           output dataset tqBaseDebtorAAByProject) in BProjectReport >                                
                                
<Q-6 run DInvoiceStages (Start) in BProjectReport >                               
for each tqBaseDebtorAAByProject break by tqBaseDebtorAAByProject.tiDInvoice_ID:
    if tqBaseDebtorAAByProject.tiProject_ID = 0 then next.
    /*prepayments - valid only if "Within terms = yes"*/
    if vlInvoiceWithinTermsFilter <> true and
        tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
        tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}
    then next.     
            
    /*GL info initialization*/
    assign vlIsGLValid       = false
           vcGLCodeINV       = "":U
           vcGLDescriptionINV= "":U
           vcGLCodeCN        = "":U
           vcGLDescriptionCN = "":U.
    /*get GLCode for InvControlGLProfileCode*/    
    find first tqProfileLinkByGL where 
               tqProfileLinkByGL.tiProfile_ID = tqBaseDebtorAAByProject.tiProfile_ID_INV no-error.
    /*check GLCode for InvControlGLProfileCode*/
    if available tqProfileLinkByGL and 
       ((vcFromGLFilter1 <= tqProfileLinkByGL.tcGLCode or
         vcFromGLFilter1 = "":U or
         vcFromGLFilter1 = "?":U or
         vcFromGLFilter1 = ?) and 
        (vcToGLFilter1 >= tqProfileLinkByGL.tcGLCode or
         vcToGLFilter1 = "":U or
         vcToGLFilter1 = "?":U or
         vcToGLFilter1 = ?) and 
        (can-do(vcCandoGLFilter, tqProfileLinkByGL.tcGLCode) or
         vcCandoGLFilter = "":U or
         vcCandoGLFilter = "?":U or
         vcCandoGLFilter = ?))
    then assign vlIsGLValid        = true
                vcGLCodeINV        = tqProfileLinkByGL.tcGLCode
                vcGLDescriptionINV = tqProfileLinkByGL.tcGLDescription.
    /*get GLCode for CnControlGLProfileCode*/
    find first tqProfileLinkByGL where 
               tqProfileLinkByGL.tiProfile_ID = tqBaseDebtorAAByProject.tiProfile_ID_CN no-error.
    /*check GLCode for CnControlGLProfileCode*/
    if available tqProfileLinkByGL and 
       ((vcFromGLFilter1 <= tqProfileLinkByGL.tcGLCode or
         vcFromGLFilter1 = "":U or
         vcFromGLFilter1 = "?":U or
         vcFromGLFilter1 = ?) and 
        (vcToGLFilter1 >= tqProfileLinkByGL.tcGLCode or
         vcToGLFilter1 = "":U or
         vcToGLFilter1 = "?":U or
         vcToGLFilter1 = ?) and 
        (can-do(vcCandoGLFilter, tqProfileLinkByGL.tcGLCode) or
         vcCandoGLFilter = "":U or
         vcCandoGLFilter = "?":U or
         vcCandoGLFilter = ?))
    then assign vlIsGLValid       = true
                vcGLCodeCN        = tqProfileLinkByGL.tcGLCode
                vcGLDescriptionCN = tqProfileLinkByGL.tcGLDescription.
    if not vlIsGLValid
    then next.
    
     if last-of(tqBaseDebtorAAByProject.tiDInvoice_ID)
              then assign vlIsGLValid = true.
              else assign vlIsGLValid = false.              
              
    if not vlIsGLValid
    then next.             
     
    /*record is valid only if    
    a) DInvoice of type "Invoice" (according to HanHor in this case type <> "CreditNote") and 
            vcInvControlGLCode exists and fits the filter criteria  or 
    b) DInvoice of type "CreditNote" and 
            vcCnControlGLCode exists and fits the filter criteria */
    if not ((tqBaseDebtorAAByProject.tcDInvoiceType <> {&INVOICETYPE-CREDITNOTE} and
       vcGLCodeINV <> "":U and 
       vcGLCodeINV <> "?":U and 
       vcGLCodeINV <> ?) or
      (tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE} and
       vcGLCodeCN <> "":U and 
       vcGLCodeCN <> "?":U and 
       vcGLCodeCN <> ?))
    then next.
    
    /*filter out records according to 
    ReportingCurrencyFilter, CurrencyCodeFilter, DebtorBalance info*/
    
    /*Calculates sums of CC,LC and TC
    It is obvious that sum of TC doesn't make sens if no currency is specified
    that is when vcCurrencyCodeFilter is blank and viCurrencyIdFilter is ? 
    but for TC it should return correct sum*/
    assign vdSumDebtorBalanceCC = ?
           vdSumDebtorBalanceLC = ?
           vdSumDebtorBalanceTC = ?.
    for each tqDebtorBalanceByFilter where tqDebtorBalanceByFilter.tiDebtor_ID = tqBaseDebtorAAByProject.tiDebtor_ID:
        /*eliminate ? values*/
        if vdSumDebtorBalanceCC = ?
        then assign vdSumDebtorBalanceCC = 0.
        if vdSumDebtorBalanceLC = ?
        then assign vdSumDebtorBalanceLC = 0.
        if vdSumDebtorBalanceTC = ?
        then assign vdSumDebtorBalanceTC = 0.
        
        if tqDebtorBalanceByFilter.tdDBalanceCC = ?
        then assign tqDebtorBalanceByFilter.tdDBalanceCC = 0.
        if tqDebtorBalanceByFilter.tdDBalanceLC = ?
        then assign tqDebtorBalanceByFilter.tdDBalanceLC = 0.
        if tqDebtorBalanceByFilter.tdDBalanceTC = ?
        then assign tqDebtorBalanceByFilter.tdDBalanceTC = 0.
        
        assign vdSumDebtorBalanceCC = vdSumDebtorBalanceCC + tqDebtorBalanceByFilter.tdDBalanceCC
               vdSumDebtorBalanceLC = vdSumDebtorBalanceLC + tqDebtorBalanceByFilter.tdDBalanceLC
               vdSumDebtorBalanceTC = vdSumDebtorBalanceTC + tqDebtorBalanceByFilter.tdDBalanceTC.
    end.
    /*eliminate Debtor if his balance doesn't fit the filter criteria*/
    /*UC says
    when currency selected  DebtorBalance in selected currency
    otherwise summ of Debtorbalances in LC
    not found in selected currency => not valid    
    >  0 when <selection> Debtor Balance = D
    <  0 when <selection> Debtor Balance = C
    <> 0 when <selection> Debtor Balance = B    
    otherwise  valid (A or empty)*/
    if vcDebtorBalanceFilter1 <> {&DEBTORBALANCE-ALL} and
       vcDebtorBalanceFilter1 <> ?
    then do:    
        if /*CC*/
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-CC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-CREDIT} and
            vdSumDebtorBalanceCC      > 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-CC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-DEBIT} and
            vdSumDebtorBalanceCC      < 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-CC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-BALANCE} and
            vdSumDebtorBalanceCC      = 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-CC} and
            vcDebtorBalanceFilter1     <> {&DEBTORBALANCE-ALL} and
            vdSumDebtorBalanceCC      = ?) or
           /*LC*/
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-LC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-CREDIT} and
            vdSumDebtorBalanceLC      > 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-LC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-DEBIT} and
            vdSumDebtorBalanceLC      < 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-LC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-BALANCE} and
            vdSumDebtorBalanceLC      = 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-LC} and
            vcDebtorBalanceFilter1     <> {&DEBTORBALANCE-ALL} and
            vdSumDebtorBalanceLC      = ?) or
           /*TC*/
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-CREDIT} and
            vdSumDebtorBalanceTC      > 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-DEBIT} and
            vdSumDebtorBalanceTC      < 0) or
           (vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and
            vcDebtorBalanceFilter1     = {&DEBTORBALANCE-BALANCE} and
            vdSumDebtorBalanceTC      = 0) or
            (vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and
            vcDebtorBalanceFilter1     <> {&DEBTORBALANCE-ALL} and
            vdSumDebtorBalanceTC      = ?)      
        then next.    
    end.
    /*if we are here, it means all invalid recods were filter out and we can create a report record.
      If filtered by project all lines are added, 
      if no filtering by project is done, then add only one line from invoice*/
   
    create tqDebtorAgeingAnalysisByProj.
    buffer-copy tqBaseDebtorAAByProject to tqDebtorAgeingAnalysisByProj.
    /*GL info assign*/
    assign  /*CnControlGL for DInvoice of type "CreditNote" otherwise InvControlGL*/
           tqDebtorAgeingAnalysisByProj.tcGLCode          = 
                if tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}
                then vcGLCodeCN
                else vcGLCodeINV                            
           tqDebtorAgeingAnalysisByProj.tcGLDescription    = 
                if tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}
                then vcGLDescriptionCN
                else vcGLDescriptionINV.
    /*AgeingPeriod1...5Balance = DInvoiceBalanceDebit - DInvoiceBalanceCredit*/
    assign /*CC*/
           tqDebtorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = 0
           tqDebtorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = 0
           tqDebtorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = 0
           tqDebtorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = 0
           tqDebtorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = 0           
           /*balances*/
           vdBalanceCC = tqBaseDebtorAAByProject.tdDInvoiceBalanceDebitCC - 
                         tqBaseDebtorAAByProject.tdDInvoiceBalanceCreditCC
           vdBalanceLC = tqBaseDebtorAAByProject.tdDInvoiceBalanceDebitLC - 
                         tqBaseDebtorAAByProject.tdDInvoiceBalanceCreditLC
           vdBalanceTC = tqBaseDebtorAAByProject.tdDInvoiceBalanceDebitTC - 
                         tqBaseDebtorAAByProject.tdDInvoiceBalanceCreditTC
                         
            vdOriginal2CC = tqBaseDebtorAAByProject.tdDInvoiceBalanceDebitCC - 
                        tqBaseDebtorAAByProject.tdDInvoiceOriginalCreditCC
            vdOriginal2LC = tqBaseDebtorAAByProject.tdDInvoiceOriginalDebitLC - 
                        tqBaseDebtorAAByProject.tdDInvoiceOriginalCreditLC
            vdOriginal2TC = tqBaseDebtorAAByProject.tdDInvoiceOriginalDebitTC - 
                        tqBaseDebtorAAByProject.tdDInvoiceOriginalCreditTC.                              
           
    if vcCurrencyTypeFilter = ? 
    then assign vcCurrencyTypeFilter = {&CURRENCYTYPE-LC}.
    
    case vcCurrencyTypeFilter:
        when {&CURRENCYTYPE-CC} 
            then assign vdBalanceXC = vdBalanceCC
                vdOriginal2XC = vdOriginal2CC.                                                                               
        when {&CURRENCYTYPE-LC} 
            then assign vdBalanceXC = vdBalanceLC
                vdOriginal2XC = vdOriginal2LC.
        when {&CURRENCYTYPE-TC} 
            then assign vdBalanceXC = vdBalanceTC
                vdOriginal2XC = vdOriginal2TC.
    end.  
    
    <Q-8 run DInvoiceStages (all) (Read) (NoCache)
          (input tqBaseDebtorAAByProject.tiDInvoice_ID, (DInvoiceID)
           output dataset tqDInvoiceStages) in BProjectReport >
    
    /* assign open values that shows within most of combnations of CC/LC/TC */
    assign tqDebtorAgeingAnalysisByProj.tdBalanceXC = 
               if vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} 
               then vdBalanceLC 
               else vdBalanceXC
           tqDebtorAgeingAnalysisByProj.tdBalanceTC = vdBalanceTC.
    /*prepayments*/
    if vlInvoiceWithinTermsFilter = true and
       tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-PREPAYMENT} or
       tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-ADJUSTMENT}
    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = vdBalanceXC.   
    
    /*staged payment conditions*/                                                                                                                                                  
    find first tqDInvoiceStages no-error.
    if available tqDInvoiceStages 
    then do:          
        assign vdBalanceXC = vdOriginal2XC - vdBalanceXC.  
        if vdBalanceXC < 0 then assign vdBalanceXC = vdBalanceXC * -1.  
        for each tqDInvoiceStages:
             case vcCurrencyTypeFilter:
                when {&CURRENCYTYPE-CC} then assign vdStageAmount2XC = tqDInvoiceStages.tdDInvoiceStageAmountCC.
                when {&CURRENCYTYPE-LC} then assign vdStageAmount2XC = tqDInvoiceStages.tdDInvoiceStageAmountLC.
                when {&CURRENCYTYPE-TC} then assign vdStageAmount2XC = tqDInvoiceStages.tdDInvoiceStageAmountTC.
            end.
        
             if vdBalanceXC > 0 
            then assign vdBalanceXC = vdBalanceXC - vdStageAmount2XC.
            else assign vdBalanceXC = vdStageAmount2XC * -1.  
            if vdBalanceXC < 0 
            then vdXC2Value = vdBalanceXC * -1.
            else vdXC2Value = vdBalanceXC.    
            
            /*invoices*/       
            if tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-INVOICE} or
               tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}
            then do:
                if tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE} then assign vdXC2Value = vdXC2Value * -1.
                find first tAgeingPeriod2 where
                           tAgeingPeriod2.ttAgeingPeriodFromDate <= tqDInvoiceStages.ttDInvoiceStageDueDate and
                           tAgeingPeriod2.ttAgeingPeriodToDate   >= tqDInvoiceStages.ttDInvoiceStageDueDate no-error.
                if available tAgeingPeriod2 and vdBalanceXC < 0            
                then do:
                    case tAgeingPeriod2.tiAgeingPeriodSeq:
                        when 1
                        then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = tqDebtorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC + vdXC2Value.
                        when 2
                        then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = tqDebtorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC + vdXC2Value.
                        when 3
                        then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = tqDebtorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC + vdXC2Value.
                        when 4
                        then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = tqDebtorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC + vdXC2Value.
                        when 5
                        then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = tqDebtorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC + vdXC2Value.
                    end case.
                end.
            end.
        end.
    end.    /*payment condition without stages*/
    else do:
        if tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-INVOICE} or
        tqBaseDebtorAAByProject.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE}
        then do:
            find first tAgeingPeriod2 where                                                                                  
                       tAgeingPeriod2.ttAgeingPeriodFromDate <= tqDebtorAgeingAnalysisByProj.ttDInvoiceDueDate and
                       tAgeingPeriod2.ttAgeingPeriodToDate   >= tqDebtorAgeingAnalysisByProj.ttDInvoiceDueDate no-error.
            if available tAgeingPeriod2
            then do:
                case tAgeingPeriod2.tiAgeingPeriodSeq:
                    when 1
                    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = vdBalanceXC.       
                    when 2
                    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = vdBalanceXC.
                    when 3
                    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = vdBalanceXC.
                    when 4
                    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = vdBalanceXC.
                    when 5
                    then assign tqDebtorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = vdBalanceXC.
                end case.
            end.
        end.   
    end.                              
end.
<Q-7 run DInvoiceStages (Stop) in BProjectReport >