project QadFinancials > class BProjectReport > method CreditorAgeingAnalysisByProj

report procedure


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrCreditorAgeingAnalysisByProjoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bprojectreport.p)

empty temp-table tqCreditorAgeingAnalysisByProj.

<M-1 run GetReportLabels
   (input  'CreditorAgeingAnalysisByProj':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-30 assign vlFcQueryRecordsAvailable = QCInvoiceMovementAll (NoCache)
       (input viCompanyId, (CompanyId)
        input ?, (DaemonId)) in BQCInvoiceMovement >
    if vlFcQueryRecordsAvailable = true or
       vlFcQueryRecordsAvailable = ?
    then do:
        <M-31 run SetMessage
          (input  #T-32'Transaction history is not up to date.':50(14722)t-32# (icMessage), 
           input  '':U (icArguments), 
           input  '':U (icFieldName), 
           input  '':U (icFieldValue), 
           input  'E':U (icType), 
           input  3 (iiSeverity), 
           input  '':U (icRowid), 
           input  'QADFIN-3736':U (icFcMsgNumber), 
           input  #T-34'There are still some transactions waiting to be processed by the History daemon, which causes inconsistencies.':150(14719)T-34# (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-20'You must enter the date for aging calculation.':50(1467)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-2569':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>    
end.
if vcAgeingTypeFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-21'You must enter the aging type.':50(1468)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-2570':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BProjectReport>
end.
if viAgeingOffsetFilter2 = ?
then do:
    assign vcErrorMessage1 = #T-22'You must enter the aging offset.':50(1469)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-2571':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-23'Aging offset is out of range (1-60).':50(1470)T-23# + chr(10).
          <M-17 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-2572':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-24'Aging offset is out of range (1-12).':50(1471)T-24# + chr(10).
        <M-18 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-2573':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-25'You must enter the currency code.':50(1460)T-25# + chr(10).
      <M-19 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-2574':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-35'The Project Totals Only and the Show Detail Lines filter fields cannot both be set to Yes.':100(16252)t-35# + chr(10).
    <M-36 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-3915':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-13 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-4 run BaseCreditorAAByProject (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input viAccPeriodFilter, (AccPeriod)
           input vcCandoBRFilter, (CandoBR)
           input vcFromBRFilter, (FromBR)
           input vcToBRFilter, (ToBR)
           input vcCandoCreditorFilter, (CandoCreditor)
           input vcFromCreditorFilter, (FromCreditor)
           input vcToCreditorFilter, (ToCreditor)
           input vcCandoDivisionFilter, (CandoDivision)
           input vcFromDivisionFilter, (FromDivision)
           input vcToDivisionFilter, (ToDivision)
           input vcCandoProjectFilter, (CandoProject)
           input vcFromProjectFilter, (FromProject)
           input vcToProjectFilter, (ToProject)
           input vcCandoJournalFilter, (CandoJournal)
           input vcFromJournalFilter, (FromJournal)
           input vcToJournalFilter, (ToJournal)
           input vcCurrencyCodeFilter2, (CurrencyCode)
           input vcCandoProjectManagerFilter, (CandoProjectManager)
           input vcFromProjectManagerFilter, (FromProjectManager)
           input vcToProjectManagerFilter, (ToProjectManager)
           output dataset tqBaseCreditorAAByProject) in BProjectReport >
              
<Q-5 run ProfileLinkByGL (all) (Read) (NoCache)
          (input ?, (GlProfileId)
           input viCompanyId, (CompanyId)
           output dataset tqProfileLinkByGL) in BProfile >                             
                              
<Q-6 run CreditorBalanceByFilter (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input ?, (CreditorId)
           input viCurrencyIdFilter2, (CurrencyId)
           input vcCandoCreditorFilter, (CandoCreditor)
           input vcFromCreditorFilter, (FromCreditor)
           input vcToCreditorFilter, (ToCreditor)
           output dataset tqCreditorBalanceByFilter) in BProjectReport >
                                  
<Q-10 run CInvoiceStages (Start) in BProjectReport >
for each tqBaseCreditorAAByProject break by tqBaseCreditorAAByProject.tiCInvoice_ID:
    if tqBaseCreditorAAByProject.tiProject_ID = 0 then next.
    
    /*prepayments - valid only if "Within terms = yes"*/
    if vlInvoiceWithinTermsFilter <> true and
        tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-PREPAYMENT} or
        tqBaseCreditorAAByProject.tcCInvoiceType = {&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 = tqBaseCreditorAAByProject.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 = tqBaseCreditorAAByProject.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(tqBaseCreditorAAByProject.tiCInvoice_ID)
          then assign vlIsGLValid = true.
          else assign vlIsGLValid = false.   
              
    if not vlIsGLValid
    then next.                  
    
    /*filter out records according to 
    ReportingCurrencyFilter, CurrencyCodeFilter, CreditorBalance 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 vdSumCreditorBalanceCC = ?
           vdSumCreditorBalanceLC = ?
           vdSumCreditorBalanceTC = ?.
    for each tqCreditorBalanceByFilter:
        /*eliminate ? values*/
        if vdSumCreditorBalanceCC = ?
        then assign vdSumCreditorBalanceCC = 0.
        if vdSumCreditorBalanceLC = ?
        then assign vdSumCreditorBalanceLC = 0.
        if vdSumCreditorBalanceTC = ?
        then assign vdSumCreditorBalanceTC = 0.
        
        if tqCreditorBalanceByFilter.tdCBalanceCC = ?
        then assign tqCreditorBalanceByFilter.tdCBalanceCC = 0.
        if tqCreditorBalanceByFilter.tdCBalanceLC = ?
        then assign tqCreditorBalanceByFilter.tdCBalanceLC = 0.
        if tqCreditorBalanceByFilter.tdCBalanceTC = ?
        then assign tqCreditorBalanceByFilter.tdCBalanceTC = 0.
        
        assign vdSumCreditorBalanceCC = vdSumCreditorBalanceCC + tqCreditorBalanceByFilter.tdCBalanceCC
               vdSumCreditorBalanceLC = vdSumCreditorBalanceLC + tqCreditorBalanceByFilter.tdCBalanceLC
               vdSumCreditorBalanceTC = vdSumCreditorBalanceTC + tqCreditorBalanceByFilter.tdCBalanceTC.
    end.
    /*eliminate Creditor if his balance doesn't fit the filter criteria*/
    /*UC says
    when currency selected  CreditorBalance in selected currency
    otherwise summ of Creditorbalances in LC
    not found in selected currency => not valid    
    >  0 when <selection> Creditor Balance = D
    <  0 when <selection> Creditor Balance = C
    <> 0 when <selection> Creditor Balance = B    
    otherwise  valid (A or empty)*/
    if /*CC*/   
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-CC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-CREDIT} and
        vdSumCreditorBalanceCC      > 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-CC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-DEBIT} and
        vdSumCreditorBalanceCC      < 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-CC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-BALANCE} and
        vdSumCreditorBalanceCC      = 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-CC} and
        vcCreditorBalanceFilter    <> {&CREDITORBALANCE-ALL} and
        vdSumCreditorBalanceCC      = ?) or
       /*LC*/
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-LC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-CREDIT} and
        vdSumCreditorBalanceLC      > 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-LC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-DEBIT} and
        vdSumCreditorBalanceLC      < 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-LC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-BALANCE} and
        vdSumCreditorBalanceLC      = 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-LC} and
        vcCreditorBalanceFilter    <> {&CREDITORBALANCE-ALL} and
        vdSumCreditorBalanceLC      = ?) or
       /*TC*/
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-TC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-CREDIT} and
        vdSumCreditorBalanceTC      > 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-TC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-DEBIT} and
        vdSumCreditorBalanceTC      < 0) or
       (vcCurrencyTypeFilter  = {&CURRENCYTYPE-TC} and
        vcCreditorBalanceFilter    = {&CREDITORBALANCE-BALANCE} and
        vdSumCreditorBalanceTC      = 0) or
        (vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} and
        vcCreditorBalanceFilter    <> {&CREDITORBALANCE-ALL} and
        vdSumCreditorBalanceTC      = ?)
    then next.       
    
    /*if we are here, it means all invalid recods were filter out and we can create a report record*/
    create tqCreditorAgeingAnalysisByProj.
    buffer-copy tqBaseCreditorAAByProject to tqCreditorAgeingAnalysisByProj.
    
    /*All gl values in one field for grouping*/
    assign tqCreditorAgeingAnalysisByProj.tcGLCode           = 
                if tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}
                then vcGLCodeCN
                else vcGLCodeINV                            
           tqCreditorAgeingAnalysisByProj.tcGLDescription    = 
                if tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}
                then vcGLDescriptionCN
                else vcGLDescriptionINV.
    
    /*AgeingPeriod1...5Balance = CInvoiceBalanceDebit - CInvoiceBalanceCredit*/
    assign /*CC*/
           tqCreditorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = 0
           tqCreditorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = 0
           tqCreditorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = 0
           tqCreditorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = 0
           tqCreditorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = 0           
           /*balances*/
           vdBalanceCC = tqBaseCreditorAAByProject.tdCInvoiceBalanceDebitCC - 
                         tqBaseCreditorAAByProject.tdCInvoiceBalanceCreditCC
           vdBalanceLC = tqBaseCreditorAAByProject.tdCInvoiceBalanceDebitLC - 
                         tqBaseCreditorAAByProject.tdCInvoiceBalanceCreditLC
           vdBalanceTC = tqBaseCreditorAAByProject.tdCInvoiceBalanceDebitTC - 
                         tqBaseCreditorAAByProject.tdCInvoiceBalanceCreditTC
                         
            vdOriginal2CC = tqBaseCreditorAAByProject.tdCInvoiceBalanceDebitCC - 
                        tqBaseCreditorAAByProject.tdCInvoiceOriginalCreditCC
            vdOriginal2LC = tqBaseCreditorAAByProject.tdCInvoiceOriginalDebitLC - 
                        tqBaseCreditorAAByProject.tdCInvoiceOriginalCreditLC
            vdOriginal2TC = tqBaseCreditorAAByProject.tdCInvoiceOriginalDebitTC - 
                        tqBaseCreditorAAByProject.tdCInvoiceOriginalCreditTC.                                                                                
                                          
    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.   
    
    assign tqCreditorAgeingAnalysisByProj.tdBalanceXC = 
        if vcCurrencyTypeFilter = {&CURRENCYTYPE-TC} 
        then vdBalanceLC 
        else vdBalanceXC
        tqCreditorAgeingAnalysisByProj.tdBalanceTC = vdBalanceTC.   
        
    if vlInvoiceWithinTermsFilter = true and
        tqCreditorAgeingAnalysisByProj.tcCInvoiceType = {&INVOICETYPE-PREPAYMENT} or
        tqCreditorAgeingAnalysisByProj.tcCInvoiceType = {&INVOICETYPE-ADJUSTMENT}
    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = vdBalanceXC.  
    
     <Q-11 run CInvoiceStages (all) (Read) (NoCache)
          (input tqBaseCreditorAAByProject.tiCInvoice_ID, (CInvoiceId)
           output dataset tqCInvoiceStages) in BProjectReport >
    /*stage payment condition or not*/    
    find first tqCInvoiceStages no-error.                                                                                                      
    if available tqCInvoiceStages 
    then do:    
        assign vdBalanceXC = vdOriginal2XC - vdBalanceXC.  
        if vdBalanceXC < 0 then assign vdBalanceXC = vdBalanceXC * -1.  
        
        for each tqCInvoiceStages:
            case vcCurrencyTypeFilter:
                when {&CURRENCYTYPE-CC} then assign vdStageAmount2XC = tqCInvoiceStages.tdCInvoiceStageAmountCC.                                                                             
                when {&CURRENCYTYPE-LC} then assign vdStageAmount2XC = tqCInvoiceStages.tdCInvoiceStageAmountLC.
                when {&CURRENCYTYPE-TC} then assign vdStageAmount2XC = tqCInvoiceStages.tdCInvoiceStageAmountTC.
            end.   
         
            if vdBalanceXC > 0 
            then assign vdBalanceXC = vdBalanceXC - vdStageAmount2XC.
            else assign vdBalanceXC = vdStageAmount2XC * -1.  
            if vdBalanceXC < 0 
            then vdXC2Value = vdBalanceXC * -1.
            else vdXC2Value = vdBalanceXC.
        
            if tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-INVOICE} or
               tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}
            then do:
                if tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} then assign vdXC2Value = vdXC2Value * -1. 
                find first tAgeingPeriod2 where
                           tAgeingPeriod2.ttAgeingPeriodFromDate <= tqCInvoiceStages.ttCInvoiceStageDueDate and
                           tAgeingPeriod2.ttAgeingPeriodToDate   >= tqCInvoiceStages.ttCInvoiceStageDueDate no-error.
                if available tAgeingPeriod2 and vdBalanceXC < 0 
                then do:
                    case tAgeingPeriod2.tiAgeingPeriodSeq:
                        when 1
                        then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = tqCreditorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC + vdXC2Value.
                        when 2
                        then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = tqCreditorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC + vdXC2Value.       
                        when 3
                        then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = tqCreditorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC + vdXC2Value.       
                        when 4
                        then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = tqCreditorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC + vdXC2Value.       
                        when 5
                        then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = tqCreditorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC + vdXC2Value.       
                    end case.                              
                end.
            end.
        end. 
    end.                 
    else do:           /*without stages*/         
        if tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-INVOICE} or
        tqBaseCreditorAAByProject.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}                        
        then do:              
            find first tAgeingPeriod2 where
                tAgeingPeriod2.ttAgeingPeriodFromDate <= tqCreditorAgeingAnalysisByProj.ttCInvoiceDueDate and
                tAgeingPeriod2.ttAgeingPeriodToDate   >= tqCreditorAgeingAnalysisByProj.ttCInvoiceDueDate no-error.
            if available tAgeingPeriod2
            then do:
                case tAgeingPeriod2.tiAgeingPeriodSeq:
                    when 1
                    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod1Balance1XC = vdBalanceXC.       
                    when 2
                    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod2Balance1XC = vdBalanceXC.
                    when 3
                    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod3Balance1XC = vdBalanceXC.
                    when 4
                    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod4Balance1XC = vdBalanceXC.
                    when 5
                    then assign tqCreditorAgeingAnalysisByProj.tdAgeingPeriod5Balance1XC = vdBalanceXC.
                end case.
            end.
        end.
    end.                 
end.    
<Q-12 run CInvoiceStages (Stop) in BProjectReport >