project QadFinancials > class TConCheck > method CPaymentControlGLBalance

Description

AP payments with GLs of type Supplier Payment Account


Parameters


icFileinputcharacter
ocConCheckLineDetResultoutputcharacter
otConCheckLineDetStartDateoutputdate
oiConCheckLineDetStartTimeoutputinteger
oiConCheckLineDetDurationoutputinteger
iiCompanyIDinputinteger
iiPeriodIDinputinteger
iiSessionIDinputinteger
oiErrorNumbersoutputinteger
ocConCheckLineDetVersionoutputcharacter
icAppVersioninputcharacter
icSumFileinputcharacterSummary file
ihTranslationHandleinputhandle
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method TConCheck.MainEntry


program code (program1/tconcheck.p)

/* ================================================================================= */
/* This is the method for AP payments with GLs of type Supplier Payment Account      */
/* ================================================================================= */
assign oiReturnStatus = -98.

assign vdStartEtime               = etime(no)
       otConCheckLineDetStartDate = now
       oiConCheckLineDetStartTime = time
       oiErrorNumbers             = 0
       ocConCheckLineDetVersion   = icAppVersion + '.1'.

/* open logfile stream */
OUTPUT STREAM sTechOut TO value(icFile) APPEND.
OUTPUT STREAM sSumOut TO value(icSumFile) APPEND. 

PUT STREAM sTechOut unformatted ' ' skip.
PUT STREAM sTechOut unformatted '#####  AP payments with GL #####' skip.
PUT STREAM sTechOut unformatted 'Version: ' ocConCheckLineDetVersion skip.
PUT STREAM sTechOut unformatted 'Validation Start At: ' string(now, '99/99/9999 HH:MM:SS') skip. 
   
MAIN_BLOCK: 
do on error undo, throw:  
    empty temp-table tqReport.
    empty temp-table tqGLCurrency.
    empty temp-table tqPayment.
    define buffer bCDocPostLine for CDocumentPostingLine.
    define buffer bPostLine for PostingLine.
    define buffer bbank for banknumber.                                             
    define buffer bgl for gl.  
  
    /* Get CompanyCode by iiCompanyId */
    find first Company where 
               Company.Company_id = iiCompanyID 
               no-lock no-error.     
    if iiCompanyID = ? or iiCompanyID = 0 or not available Company
    then do:
       assign ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED}.
       PUT STREAM sTechOut unformatted "**Error** Input parameter CompanyID is null or Company not found." skip. 
       LEAVE MAIN_BLOCK.
    end.
    assign vcCompanyCodeLocal = Company.Companycode. 

    /* Get YearPeriod by iiPeriodID */
    find first Period of Company where 
               Period_id = iiPeriodID 
               no-lock no-error.    
    if iiPeriodID = ? or iiPeriodID = 0 or not available Period
    then do:
       assign ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED}.
       PUT STREAM sTechOut unformatted "**Error** Input parameter PeriodID is null or Period not found." skip. 
       LEAVE MAIN_BLOCK.
    end.
    assign viSelectedYearPeriod = Period.PeriodYearPeriod. 
    
    find first DomainProperty where DomainProperty.Domain_ID = Company.Domain_ID no-lock no-error.
    if available DomainProperty then
    assign viCompanyLC = DomainProperty.Currency_ID
           viCompanyCC = DomainProperty.StatutoryCurrency_ID.

    /* now we look for postinglines for payments that have happened in the checked period only */ 
    for each postingline where postingline.company_ID = company.company_ID and
    	                        postingline.postingyearperiod = viSelectedYearPeriod no-lock,
    	each CDocumentPostingLine of postingline no-lock:
    	find CDocument of CDocumentPostingLine no-lock no-error.
    	if not available CDocument 
    	then do:
           <M-73 run GetPostingLineKey
              (input  postingline.posting_id (iiPostingId), 
               input-output viErrorsFound (biErrorNumbers), 
               output vcJournalCodeTmp (ocJournalCode), 
               output vcPostingVoucherTmp (ocPostingVoucher), 
               input  postingline.postingline_id (iiPostingLineId), 
               output vlBothFound (olBothFound), 
               output viFcReturnSuper (oiReturnStatus)) in TConCheck>
           if vlBothFound = yes
           then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText = "Referential integrity error: CDocumentPostingLine without parent CDocument: " + vcKey + " CDocumentPostingLine_ID: " + string(CDocumentPostingLine_ID)
    		    tqReport.tiErrorSeverity = 1
    		    viErrorsFound = viErrorsFound + 1.
    	    next.
    	end.
    	find tqPayment where tqPayment.tiPaymentID = CDocument.CDocument_ID no-error.
    	if available tqPayment then next. /* we already checked this payment */
    	create tqPayment.
    	assign tqPayment.tiPaymentID = CDocument.CDocument_ID.

    	if cdocument.cdocumentstatus = "INITIAL" then next.  	
    	/* now check the Allocations */
    	if cdocument.cdocumentstatus <> "BOUNCED" and
    	   cdocument.cdocumentstatus <> "PAID" and
    	   cdocument.cdocumentstatus <> "VOID" then do:
           assign vdCurrDebitTC = 0.
    	    for each CDocumentInvoiceXref of CDocument no-lock:
    	        find cinvoice where cinvoice.cinvoice_ID = CDocumentInvoiceXref.cinvoice_ID no-lock no-error.
    	        if not available CInvoice 
    	        then do:
                   <M-72 run GetPostingLineKey
                      (input  postingline.posting_id (iiPostingId), 
                       input-output viErrorsFound (biErrorNumbers), 
                       output vcJournalCodeTmp (ocJournalCode), 
                       output vcPostingVoucherTmp (ocPostingVoucher), 
                       input  postingline.postingline_id (iiPostingLineId), 
                       output vlBothFound (olBothFound), 
                       output viFcReturnSuper (oiReturnStatus)) in TConCheck>
    	            if vlBothFound = yes
    	            then assign vcKey = " " + vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	            else next.
    	            create tqReport.
    	            assign tqReport.tcReportText = "Referential integrity error: CDocumentInvoiceXref with invalid CInvoice_ID: " + string(CDocumentInvoiceXref.cinvoice_ID) + vcKey + " CDocumentPostingLine_ID: " + string(CDocumentPostingLine_ID)
    		            tqReport.tiErrorSeverity = 1
    		            viErrorsFound = viErrorsFound + 1.
     	            next.   		     
    	        end.
    	        find journal of cinvoice no-lock no-error.
    	        if not available Journal
    	        then do: 
                   <M-14 run GetPostingLineKey
                      (input  postingline.posting_id (iiPostingId), 
                       input-output viErrorsFound (biErrorNumbers), 
                       output vcJournalCodeTmp (ocJournalCode), 
                       output vcPostingVoucherTmp (ocPostingVoucher), 
                       input  postingline.postingline_id (iiPostingLineId), 
                       output vlBothFound (olBothFound), 
                       output viFcReturnSuper (oiReturnStatus)) in TConCheck>    	            
    	            if vlBothFound = yes
    	            then assign vcKey = " " + vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	            else next.
    	            create tqReport.
    	            assign tqReport.tcReportText = "Referential integrity error: CInvoice with invalid Journal_ID: " + string(CInvoice.Journal_ID) + vcKey + " CDocumentPostingLine_ID: " + string(CDocumentPostingLine_ID)
    	                   tqReport.tiErrorSeverity = 1
    		            viErrorsFound = viErrorsFound + 1.
     	            next.  
    	        end. 
    	        if journal.journaltypecode = "CREDITORPAYMENT" 
    	        then do:
    	            assign viPAYPostingID = 0
    	                   viSIPostingID = 0.
    	            for each bCDocPostLine of CDocument no-lock,
    	                each bPostline of bCDocPostLine no-lock, 
    	                each posting of bPostLine no-lock by posting.postingdate by posting.lastmodifiedtime:
    	                assign viPAYPostingID = posting.posting_ID.
    	                leave.
    	            end.

    	            find posting where posting.company_ID = cinvoice.company_ID and
                                      posting.postingyear = cinvoice.cinvoicepostingyear and
    					    posting.journal_id = cinvoice.journal_ID and
    					    posting.postingvoucher = cinvoice.cinvoicevoucher no-lock no-error.
    	            if available posting
    	            then do:
    	                assign viSIPostingID = posting.posting_ID.
    	            end.
    	        end.
    	        
    	        /* Note that CDocumentInvoiceXref accounts for staged payments */
    	        assign vdAllocatedStageTC = 0
    	               vdDiscountStageTC  = 0.
    	        for each CDocInvoiceXrefStage of CDocumentInvoiceXref no-lock:
    	            assign vdAllocatedStageTC = vdAllocatedStageTC + CDocInvoiceXrefStage.CDocInvoiceXrefStageAlloTC
    	                   vdDiscountStageTC = vdDiscountStageTC + CDocInvoiceXrefStage.CDocInvoiceXrefStageDiscTC.   	                  
    	        end. /* for each CDocInvoiceXrefStage */
    	        
    	        if(vdAllocatedStageTC <> 0 and 
    	            abs(vdAllocatedStageTC) - abs(vdDiscountStageTC) <> abs(CDocumentInvoiceXref.CDocumentInvoiceXrefAlloTC) - abs(CDocumentInvoiceXref.CDocumentInvoiceXrefDiscTC)) 
    	        then do:
    	            <M-68 run GetPostingLineKey
                    (input  postingline.posting_id (iiPostingId), 
                     input-output viErrorsFound (biErrorNumbers), 
                     output vcJournalCodeTmp (ocJournalCode), 
                     output vcPostingVoucherTmp (ocPostingVoucher), 
                     input  postingline.postingline_id (iiPostingLineId), 
                     output vlBothFound (olBothFound), 
                     output viFcReturnSuper (oiReturnStatus)) in TConCheck>
    	            if vlBothFound = yes
    	            then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	            else next.
    	            create tqReport. 
    	            assign tqReport.tcReportText = "Detail inconsistency error: Unbalanced staged invoice. Payment: " + Cdocument.CdocumentType + "/" + 
    	                                           string(cdocument.cdocumentyear) + "/" + string(cdocument.cdocumentnumber) + " Journal Entry: " + vcKey + 
    	                                           " Calculated Stages Total in TC: " + string(abs(vdAllocatedStageTC) - abs(vdDiscountStageTC)) +
    	                                           " Invoice Total in TC: " + string(abs(CDocumentInvoiceXref.CDocumentInvoiceXrefAlloTC) - abs(CDocumentInvoiceXref.CDocumentInvoiceXrefDiscTC))
    	                   tqReport.tiErrorSeverity = 1
                          viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	        end.
    	        if CDocumentInvoiceXrefPaidAmt <> 0
    	        then do:
    	            if journal.journaltypecode = "CREDITORINVOICE" or
    	               journal.journaltypecode = "CREDITORINVOICECORRECT" or
    	               journal.journaltypecode = "CREDITORADJUSTMENT" or
    	               (journal.journaltypecode = "CREDITORPAYMENT" and viPAYPostingID = viSIPostingID) 
    	            then assign vdCurrDebitTC = vdCurrDebitTC - CDocumentInvoiceXrefPaidAmt.
    	            /* else is the credit note allocations */
    	            else assign vdCurrDebitTC = vdCurrDebitTC + CDocumentInvoiceXrefPaidAmt.
    	        end.
    	        else do:
    	            /* there is the weird case of prepayments created in payment create. those have PaidAmt zero, while these count for allocated */
    	            assign vdCurrDebitTC = vdCurrDebitTC - CDocumentInvoiceXrefAlloTC.
    	        end.
    	    end.
    	    if vdCurrDebitTC <> (CDocument.CDocumentOriginalDebitTC - CDocument.CDocumentOriginalCreditTC - CDocument.CDocumentBankChargeAmountTC) 
    	    then do:

    	        <M-7 run GetPostingLineKey
                (input  postingline.posting_id (iiPostingId), 
                 input-output viErrorsFound (biErrorNumbers), 
                 output vcJournalCodeTmp (ocJournalCode), 
                 output vcPostingVoucherTmp (ocPostingVoucher), 
                 input  postingline.postingline_id (iiPostingLineId), 
                 output vlBothFound (olBothFound), 
                 output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
    	        if vlBothFound = yes
    	        then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	        else next.
    	        create tqReport. 
    	        assign tqReport.tcReportText = "Detail inconsistency error: Payment Allocation details do not match total payment. Payment: " + Cdocument.CdocumentType + "/" + 
    	                                       string(cdocument.cdocumentyear) + "/" + string(cdocument.cdocumentnumber) + " Journal Entry: " + vcKey + 
    	                                       " Calculated Allocations in TC: " + string(vdCurrDebitTC) +
    	                                       " Payment Total in TC: " + string(CDocument.CDocumentOriginalDebitTC - CDocument.CDocumentOriginalCreditTC - CDocument.CDocumentBankChargeAmountTC )
    	               tqReport.tiErrorSeverity = 1
    	               viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	    end.
    	end. /* cdocument.cdocumentstatus <> "BOUNCED" */
 	
    	/* now check the Payment balance and status */
    	assign 
    	    vdCurrDebitLC = 0
    	    vdCurrCreditLC = 0
    	    vdCurrDebitTC = 0
    	    vdCurrCreditTC = 0
    	    vdCurrDebitCC = 0
    	    vdCurrCreditCC = 0
    	    vtLastPostingDate = 01/01/1900.
     	for each bCDocPostLine of CDocument no-lock:
     	    find bPostLine where bPostLine.PostingLine_ID = bCDocPostLine.PostingLine_ID no-lock no-error.
     	    if not available bPostLine 
     	    then do:
     	        create tqReport.
     	        assign tqReport.tcReportText = "Referential integrity error. Postingline not available for CDocumentPostingLine " + vcCompanyCodeLocal + " " + "Payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
    								"CDocumentPostingLine_ID" + string(bCDocPostLine.CDocumentPostingLine_ID) + " PostingLine_ID: " + string(bCDocPostLine.PostingLine_ID)
    			 tqReport.tiErrorSeverity = 1
    			 viErrorsFound = viErrorsFound + 1.
     	        next.
     	    end.
     	    /* let's also save the latest postingdate for this item to check the closingdate */
     	    if bPostLine.PostingDate > vtLastPostingDate 
     	    then do:
     	        assign vtLastPostingDate = bPostLine.PostingDate.
     	    end.
     	    /* accumulate the amounts */
     	    assign 
     	        vdCurrDebitLC = vdCurrDebitLC + bPostLine.PostingLineDebitLC
     	        vdCurrCreditLC = vdCurrCreditLC + bPostLine.PostingLineCreditLC
     	        vdCurrDebitTC = vdCurrDebitTC + bPostLine.PostingLineDebitTC
     	        vdCurrCreditTC = vdCurrCreditTC + bPostLine.PostingLineCreditTC
     	        vdCurrDebitCC = vdCurrDebitCC + bPostLine.PostingLineDebitCC
     	        vdCurrCreditCC = vdCurrCreditCC + bPostLine.PostingLineCreditCC.    			
    	end.    	
   	
    	/* net the calculated balances, because that is also done in the payment postings */
    	if vdCurrDebitLC > vdCurrCreditLC 
    	then do:
    	    assign vdCurrDebitLC = vdCurrDebitLC - vdCurrCreditLC
    	           vdCurrCreditLC = 0.
    	end.
    	else do:
    	    assign vdCurrCreditLC = vdCurrCreditLC - vdCurrDebitLC
    	           vdCurrDebitLC = 0.
    	end.
    	if vdCurrDebitTC > vdCurrCreditTC 
    	then do:
    	    assign vdCurrDebitTC = vdCurrDebitTC - vdCurrCreditTC
    	           vdCurrCreditTC = 0.
    	end.
    	else do:
    	    assign vdCurrCreditTC = vdCurrCreditTC - vdCurrDebitTC
    	           vdCurrDebitTC = 0.
    	end.
    	if vdCurrDebitCC > vdCurrCreditCC 
    	then do:
    	    assign vdCurrDebitCC = vdCurrDebitCC - vdCurrCreditCC
    	           vdCurrCreditCC = 0.
    	end.
    	else do:
    	    assign vdCurrCreditCC = vdCurrCreditCC - vdCurrDebitCC
    	           vdCurrDebitCC = 0.
    	end.
     	
    	/* we have now the complete balance of the item and we can check it */
    	if CDocumentIsOpen = true and
    	(vdCurrDebitLC - vdCurrCreditLC) <> (CDocument.CDocumentOriginalDebitLC - CDocument.CDocumentOriginalCreditLC  - CDocument.CDocumentBankChargeAmountLC) 
    	then do:
    	    <M-49 run GetPostingLineKey
            (input  bPostLine.posting_id (iiPostingId), 
             input-output viErrorsFound (biErrorNumbers), 
             output vcJournalCodeTmp (ocJournalCode), 
             output vcPostingVoucherTmp (ocPostingVoucher), 
             input  bPostLine.postingline_id (iiPostingLineId), 
             output vlBothFound (olBothFound), 
             output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
    	    if vlBothFound = yes
    	    then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText =	"Detail inconsistency error. Payment is open. BC postings total should be equal to orginal BC.  " + "Payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
    					" Calculated: " + string(vdCurrDebitLC - vdCurrCreditLC) +
    					" In CDocument record: " + string(CDocument.CDocumentOriginalDebitLC - CDocument.CDocumentOriginalCreditLC - CDocument.CDocumentBankChargeAmountLC)
            	    tqReport.tiErrorSeverity = 1
            	    viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	end.
 
    	if CDocumentIsOpen = true and
    	(vdCurrDebitTC - vdCurrCreditTC) <> (CDocument.CDocumentOriginalDebitTC - CDocument.CDocumentOriginalCreditTC - CDocument.CDocumentBankChargeAmountTC) 
    	then do:
    	    <M-96 run GetPostingLineKey
            (input  bPostLine.posting_id (iiPostingId), 
             input-output viErrorsFound (biErrorNumbers), 
             output vcJournalCodeTmp (ocJournalCode), 
             output vcPostingVoucherTmp (ocPostingVoucher), 
             input  bPostLine.postingline_id (iiPostingLineId), 
             output vlBothFound (olBothFound), 
             output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
   	    if vlBothFound = yes
    	    then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText =	"Detail inconsistency error. Payment is open. TC postings total should be equal to orginal TC.  " + "Payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
    					" Calculated: " + string(vdCurrDebitTC - vdCurrCreditTC) +
    					" In CDocument record: " + string(CDocument.CDocumentOriginalDebitTC - CDocument.CDocumentOriginalCreditTC - CDocument.CDocumentBankChargeAmountTC)
            	    tqReport.tiErrorSeverity = 1
            	    viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	end.
 
    	if CDocumentIsOpen = true and
    	(vdCurrDebitCC - vdCurrCreditCC) <> (CDocument.CDocumentOriginalDebitCC - CDocument.CDocumentOriginalCreditCC - CDocument.CDocumentBankChargeAmountCC) 
    	then do:
    	    <M-8 run GetPostingLineKey
            (input  bPostLine.posting_id (iiPostingId), 
             input-output viErrorsFound (biErrorNumbers), 
             output vcJournalCodeTmp (ocJournalCode), 
             output vcPostingVoucherTmp (ocPostingVoucher), 
             input  bPostLine.postingline_id (iiPostingLineId), 
             output vlBothFound (olBothFound), 
             output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
   	    if vlBothFound = yes
    	    then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText =	"Detail inconsistency error. Payment is open. SC postings total should be equal to orginal SC.  " + "Payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
    					" Calculated: " + string(vdCurrDebitCC - vdCurrCreditCC) +
    					" In CDocument record: " + string(CDocument.CDocumentOriginalDebitCC - CDocument.CDocumentOriginalCreditCC - CDocument.CDocumentBankChargeAmountCC)   					
            	    tqReport.tiErrorSeverity = 1
            	    viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	end.

    	if CDocument.CDocumentIsOpen = True and     	
    	   CDocument.CDocumentClosingDate <> 12/31/9999 
    	then do:
    	    <M-31 run GetPostingLineKey
            (input  bPostLine.posting_id (iiPostingId), 
             input-output viErrorsFound (biErrorNumbers), 
             output vcJournalCodeTmp (ocJournalCode), 
             output vcPostingVoucherTmp (ocPostingVoucher), 
             input  bPostLine.postingline_id (iiPostingLineId), 
             output vlBothFound (olBothFound), 
             output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
   	    if vlBothFound = yes
    	    then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText =	"Detail inconsistency error in Closing Date. Expected 12/31/9999 for OPEN status. " + "Payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
                        			" CDocumentStatus: " + CDocument.CdocumentStatus +
                        			" CDocumentIsOpen: " + string(CDocument.CDocumentIsOpen) +
                        			" CDocumentClosingDate:" + string(CDocument.CDocumentClosingDate)
            	    tqReport.tiErrorSeverity = 1
            	    viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	end.

    	if CDocument.CDocumentIsOpen = False and CDocument.CDocumentClosingDate <> vtLastPostingDate 
    	then do:
    	    <M-54 run GetPostingLineKey
            (input  bPostLine.posting_id (iiPostingId), 
             input-output viErrorsFound (biErrorNumbers), 
             output vcJournalCodeTmp (ocJournalCode), 
             output vcPostingVoucherTmp (ocPostingVoucher), 
             input  bPostLine.postingline_id (iiPostingLineId), 
             output vlBothFound (olBothFound), 
             output viFcReturnSuper (oiReturnStatus)) in TConCheck>
                    
   	    if vlBothFound = yes
    	    then assign vcKey = vcCompanyCodeLocal + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999") + "/" + string(PostingLine.PostingLineSequence). 
    	    else next.
    	    create tqReport.
    	    assign tqReport.tcReportText =	"Detail inconsistency error in Closing Date. Expected " + string(vtLastPostingDate) +  " for closed payment: " + CDocument.CDocumentType + "/" + 
    						string(CDocument.CDocumentYear) + "/" + string(CDocument.CDocumentNumber) +
    			                    " CDocumentIsOpen: " + string(CDocument.CDocumentIsOpen) +
                        			" CDocumentClosingDate:" + string(CDocument.CDocumentClosingDate)
            	    tqReport.tiErrorSeverity = 1
            	    viDetailInconsistencyError = viDetailInconsistencyError + 1.
    	end.

    end. /* for each postingline */ 
  
    /* now check the GL Balance vs. Open Payments */
    for each CDocument where CDocumentClosingDate > period.periodenddate and
             CDocument.Company_ID = Company.Company_ID no-lock:
        assign vlExistedAtPeriodEnd = no
               vcStatusAtPeriodEnd = "".
        for each CDocumentPostingLine of CDocument no-lock,
            each postingline of CDocumentPostingLine no-lock by postingline.postingdate:
            if postingline.postingdate <= period.periodenddate 
            then do:
                assign vlExistedAtPeriodEnd = yes
                       vcStatusAtPeriodEnd = CDocumentPostingLine.CDocumentStatus.
            end.
            else leave.
        end. 
        
        if vlExistedAtPeriodEnd = no or 
           vcStatusAtPeriodEnd = "INITIAL" or
           vcStatusAtPeriodEnd = "BOUNCED" or
           vcStatusAtPeriodEnd = "PAID" or
           vcStatusAtPeriodEnd = "VOID" or
           vcStatusAtPeriodEnd = "" then next.
        find banknumber of CDocument no-lock no-error.                                       
        if not available BankNumber 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in CDocument.BankNumber! Missing banknumber: " + string(CDocument.BankNumber_ID) + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod)
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
            next.
        end. 
        find bankpayformat where bankpayformat.bankpayformat_ID = banknumber.bankpayformat_ID no-lock no-error.                                            
        if not available bankpayformat 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in BankNumber.Bankpayformat! Missing bankpayformat: " + string(BankNumber.bankpayformat_ID) + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod)
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
            next.
        end. 
        find bbank where bbank.banknumber_ID = bankpayformat.banknumber_ID no-lock no-error.     
        if not available bBank 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in BankPayFormat.BankNumber! Missing banknumber 2: " + string(bankpayformat.banknumber_ID) + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod)
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
            next.
        end. 
        find gl where gl.gl_ID = bbank.parentobject_ID no-lock no-error.
        if not available gl 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in BankNumber.GL! GL: " + string(bbank.parentobject_ID) + " Parent type: " + bbank.bankNumberParentType + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) 
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
            next.
        end. 
        find CDocumentstatus where CDocumentstatus.company_ID = CDocument.company_ID and 
             CDocumentstatus.bankgl_ID = bbank.parentobject_ID and                       
             CDocumentstatus.CDocumentStatusStatus = vcStatusAtPeriodEnd and           
             CDocumentstatus.CDocumentstatusdoctype = CDocument.CDocumenttype no-lock no-error.
        if not available CDocumentstatus 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in CDocumentStatus! Missing status: " + vcStatusAtPeriodEnd + " Document type: " + CDocument.CDocumenttype + " Bank Account: " + GL.GLCode + " Entity: " + Company.CompanyCode + " Period: " + string(Period.PeriodYearPeriod) + " GL Account: " + GL.GLCode 
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
            next.
        end. 
        
        find gl where gl.gl_ID = CDocumentstatus.gl_ID no-lock no-error.
        if not available gl 
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in CDocumentstatus.GL! GL: " + string(CDocumentstatus.gl_ID) + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) 
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
        end. 

        find currency where currency.currency_ID = CDocument.currency_ID no-lock no-error.
        if not available currency
        then do:
            create tqReport.
            assign tqReport.tcReportText =	"Referential integrity error in CDocument.Currency! Currency: " + string(CDocument.currency_ID) + " Year: " + string(CDocument.CDocumentYear) + " Document type: " + CDocument.CDocumenttype + " Number: " + string(Cdocument.CdocumentNumber) + " Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) 
                   tqReport.tiErrorSeverity = 1
                   viErrorsFound = viErrorsFound + 1.
        end. 
        
        find tqGLCurrency where tqGLCurrency.tiGLID = CDocumentstatus.gl_ID and tqGLCurrency.tiCurrencyID = CDocument.currency_ID no-error.
        if not available tqGLCurrency 
        then do:
            create tqGLCurrency.
            assign tqGLCurrency.tiGLID = CDocumentstatus.gl_ID
                   tqGLCurrency.tiCurrencyID = CDocument.currency_ID.
            for first bgl where bgl.gl_id = tqGLCurrency.tiGLID no-lock:
                assign tqGLCurrency.tcGLCode = bgl.glcode
                       tqGLCurrency.tcGLDescription = bgl.gldescription.
            end.
            for first currency where currency.currency_id = tqGLCurrency.tiCurrencyID no-lock:
                assign tqGLCurrency.tcCurrencyCode = currency.currencycode.
            end. 
        end. 
        
        assign tqGLCurrency.tdSubBalanceLC = tqGLCurrency.tdSubBalanceLC + CDocument.CDocumentOriginalDebitLC - CDocument.CDocumentOriginalCreditLC
               tqGLCurrency.tdSubBalanceTC = tqGLCurrency.tdSubBalanceTC + CDocument.CDocumentOriginalDebitTC - CDocument.CDocumentOriginalCreditTC 
               tqGLCurrency.tdSubBalanceCC = tqGLCurrency.tdSubBalanceCC + CDocument.CDocumentOriginalDebitCC - CDocument.CDocumentOriginalCreditCC.
    end. 
    
    for each tqGLCurrency,
        each postinghist where postinghist.company_ID = Company.Company_ID and
             postingHist.GL_ID = tqGLCurrency.tiGLID and
             postinghist.Currency_ID = tqGLCurrency.tiCurrencyID and
             postinghistyearperiodfrom <= period.periodyearperiod and 
             postinghistyearperiodtill >= period.periodyearperiod no-lock:
        assign tqGLCurrency.tdBalanceGLLC = tqGLCurrency.tdBalanceGLLC + PostingHist.PostingHistBalanceDebitLC - PostingHist.PostingHistBalanceCreditLC
               tqGLCurrency.tdBalanceGLTC = tqGLCurrency.tdBalanceGLTC + PostingHist.PostingHistBalanceDebitTC - PostingHist.PostingHistBalanceCreditTC
               tqGLCurrency.tdBalanceGLCC = tqGLCurrency.tdBalanceGLCC + PostingHist.PostingHistBalanceDebitCC - PostingHist.PostingHistBalanceCreditCC.
    end. 
  
    for each tqGLCurrency:
        find currency where currency.currency_ID = tqGLCurrency.tiCurrencyID no-lock no-error.
        find gl where gl.gl_ID = tqGLCurrency.tiGLID no-lock no-error.
        if available currency then assign vcCurrencyCode = currency.currencycode.
        else assign vcCurrencyCode = "Missing":U.
        if available GL then assign vcGLCode = GL.GLCode.
        else assign vcGLCode = "Missing":U.
               
        
        /* now we can compare the balances */
        if tqGLCurrency.tdSubBalanceLC <> tqGLCurrency.tdBalanceGLLC 
        then do:
            find first QPostingLine where QPostingLine.GL_ID = GL.GL_ID and QPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and QPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
            if available QPostingLine 
            then do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Unprocessed Hitory Daemon queue causes inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode +
                                                " GL balance in BC: " + string(tqGLCurrency.tdBalanceGLLC) +
                                                " payment sub-ledger in BC: " + string(tqGLCurrency.tdSubBalanceLC).                              
                assign tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
            else do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode  +
                                                " GL balance in BC: " + string(tqGLCurrency.tdBalanceGLLC) +
                                                " payment sub-ledger in BC: " + string(tqGLCurrency.tdSubBalanceLC)
                       tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
        end.
        if tqGLCurrency.tdSubBalanceTC <> tqGLCurrency.tdBalanceGLTC 
        then do:
            find first QPostingLine where QPostingLine.GL_ID = GL.GL_ID and QPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and QPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
            if available QPostingLine 
            then do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Unprocessed Hitory Daemon queue causes inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode +
                                                " GL balance in TC: " + string(tqGLCurrency.tdBalanceGLTC) +
                                                " payment sub-ledger in TC: " + string(tqGLCurrency.tdSubBalanceTC)
                       tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
            else do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode +
                                                " GL balance in TC: " + string(tqGLCurrency.tdBalanceGLTC) +
                                                " payment sub-ledger in TC: " + string(tqGLCurrency.tdSubBalanceTC)
                       tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
        end.
        if tqGLCurrency.tdSubBalanceCC <> tqGLCurrency.tdBalanceGLCC 
        then do:
            find first QPostingLine where QPostingLine.GL_ID = GL.GL_ID and QPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and QPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
            if available QPostingLine 
            then do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Unprocessed Hitory Daemon queue causes inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode +
                                                " GL balance in SC: " + string(tqGLCurrency.tdBalanceGLCC) +
                                                " payment sub-ledger in SC: " + string(tqGLCurrency.tdSubBalanceCC)
                       tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
            else do:
                create tqReport.
                assign tqReport.tcReportText =	"Balance inconsistency error: Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + vcGLCode + " Currency: " + vcCurrencyCode +
                                                " GL balance in SC: " + string(tqGLCurrency.tdBalanceGLCC) +
                                                " payment sub-ledger in SC: " + string(tqGLCurrency.tdSubBalanceCC)
                       tqReport.tiErrorSeverity = 0
                       viBalanceInconsistencyError = viBalanceInconsistencyError + 1.
            end. 
        end.            
    end. 
end. /* main_block */

if can-find(first tqReport) 
then assign ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED}.
else assign ocConCheckLineDetResult = {&CONCHECKRESULT-PASSED}.

/* print output */
for each tqReport use-index ErrorSeverityIndex:
    put stream sTechOut unformatted tqReport.tcReportText + "," + string(tqReport.tiErrorSeverity) skip.   
    assign oiErrorNumbers = oiErrorNumbers + 1.
end.

assign oiConCheckLineDetDuration = etime - vdStartEtime.

/* to format the total duration time */
assign viMilliSecs = oiConCheckLineDetDuration.
assign viSecs = TRUNCATE (oiConCheckLineDetDuration / 1000, 0).
assign viMilliSecs = viMilliSecs mod 1000.
assign vcTotalTime = string(viSecs, 'HH:MM:SS')  +  '.'  +  string(viMilliSecs).

put stream sTechOut unformatted 'Execution Duration: ' vcTotalTime  skip. 
put stream sTechOut unformatted 'Execution Result: ' ocConCheckLineDetResult skip.
put stream sTechOut unformatted '<Error Count:> ' oiErrorNumbers skip.
put stream sTechOut unformatted '**********************************************************************' skip.

output stream sTechOut close.


put stream sSumOut unformatted ' ' skip.
put stream sSumOut unformatted trim({&CONCHECKLINEDETTYPE-APGL-TR}) skip.
put stream sSumOut unformatted '-------------------' skip.
put stream sSumOut unformatted trim(#T-10'Execution Result':255(510391963)T-10#) + ': ' + ocConCheckLineDetResult skip.

if ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED} 
then do: 
    if viErrorsFound > 0 
    then put stream sSumOut unformatted 
                        trim(#T-81'Total number of referential integrity errors':150(916604167)T-81#)
                        ': '
                         viErrorsFound  skip. 
    if viBalanceInconsistencyError > 0 
    then put stream sSumOut unformatted 
                        trim(#T-74'Total number of balance inconsistency errors':100(282714224)T-74#)
                        ': ' viBalanceInconsistencyError skip. 
    if viDetailInconsistencyError > 0 
    then put stream sSumOut unformatted 
                        trim(#T-15'Total number of detail inconsistency errors':150(665310851)T-15#)
                        ': ' viDetailInconsistencyError skip.
        
    put stream sSumOut unformatted 
                        '(' 
                        trim(#T-39'Please see details in the file':150(806040074)T-39#) 
                        ' ' 
                        icFile 
                        ')' 
                        skip.    

end.

if can-find (first tqGLCurrency)
then do:
    for each tqGLCurrency break by tqGLCurrency.tiGLID
                                  by tqGLCurrency.tiCurrencyID:                        
        if first-of(tqGLCurrency.tiGLID)
        then do:
            assign vdGLBalanceLCPerGL = 0
                   vdGLBalanceCCPerGL = 0
                   vdSubBalanceCCPerGL = 0
                   vdSubBalanceLCPerGL = 0
                   vlDetailPrinted = no. 
            put stream sSumOut unformatted ' ' skip.
            put stream sSumOut unformatted trim(#T-52'GL':20(199)T-52#)
                                            ': ':U 
                                           tqGLCurrency.tcGLCode
                                           '    ':U
                                           tqGLCurrency.tcGLDescription
                                           '    ':U skip.
        end.
        assign vdGLBalanceLCPerGL = vdGLBalanceLCPerGL + tqGLCurrency.tdBalanceGLLC
               vdGLBalanceCCPerGL = vdGLBalanceCCPerGL + tqGLCurrency.tdBalanceGLCC
               vdSubBalanceLCPerGL = vdSubBalanceLCPerGL + tqGLCurrency.tdSubBalanceLC
               vdSubBalanceCCPerGL = vdSubBalanceCCPerGL + tqGLCurrency.tdSubBalanceCC.
        if tqGLCurrency.tdBalanceGLTC <> tqGLCurrency.tdSubBalanceTC or
           tqGLCurrency.tdBalanceGLLC <> tqGLCurrency.tdSubBalanceLC or
           tqGLCurrency.tdBalanceGLCC <> tqGLCurrency.tdSubBalanceCC
        then do:
            assign vlDetailPrinted = yes.
            put stream sSumOut unformatted trim(#T-95'Currency':20(220)T-95#) + 
                                            ': ':U + tqGLCurrency.tcCurrencyCode at 1
                                           trim(#T-60'GL Balance':20(1017)T-60#) + 
                                           '':U to 30
                                           trim(#T-58'Sub-ledger Balance':20(254104290)T-58#) to 60
                                           trim(#T-38'Difference':20(4114)T-38#) to 90 skip.
            <M-55 run RoundAmount
               (input-output tqGLCurrency.tdBalanceGLTC (bdUnroundedAmount), 
                input  tqGLCurrency.tiCurrencyID (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcGLBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
            <M-66 run RoundAmount
               (input-output tqGLCurrency.tdSubBalanceTC (bdUnroundedAmount), 
                input  tqGLCurrency.tiCurrencyID (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcSubBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck> 
            assign vdDiff = tqGLCurrency.tdBalanceGLTC - tqGLCurrency.tdSubBalanceTC.
            <M-86 run RoundAmount
               (input-output vdDiff (bdUnroundedAmount), 
                input  tqGLCurrency.tiCurrencyID (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcDiffFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                                    
            put stream sSumOut unformatted trim(#T-9'TC':3(4029)T-9#) at 1
                                           vcGLBalanceFormat to 30
                                           vcSubBalanceFormat to 60
                                           vcDiffFormat to 90 skip.
            <M-77 run RoundAmount
               (input-output tqGLCurrency.tdBalanceGLLC (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcGLBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
            <M-78 run RoundAmount
               (input-output tqGLCurrency.tdSubBalanceLC (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcSubBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>
            assign vdDiff = tqGLCurrency.tdBalanceGLLC - tqGLCurrency.tdSubBalanceLC.     
            <M-29 run RoundAmount
               (input-output vdDiff (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcDiffFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                         
            put stream sSumOut unformatted trim(#T-32'BC':3(4030)T-32#) at 1
                                           vcGLBalanceFormat to 30
                                           vcSubBalanceFormat to 60
                                           vcDiffFormat to 90 skip. 
            <M-61 run RoundAmount
               (input-output tqGLCurrency.tdBalanceGLCC (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcGLBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
            <M-70 run RoundAmount
               (input-output tqGLCurrency.tdSubBalanceCC (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcSubBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>
            assign vdDiff = tqGLCurrency.tdBalanceGLCC - tqGLCurrency.tdSubBalanceCC. 
            <M-13 run RoundAmount
               (input-output vdDiff (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcDiffFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                          
            put stream sSumOut unformatted trim(#T-756'SC':12(3936)T-756#) at 1
                                           vcGLBalanceFormat to 30
                                           vcSubBalanceFormat to 60
                                           vcDiffFormat to 90 skip.
        end.                                
        if last-of(tqGLCurrency.tiGLID)
        then do:                                
            assign vdSumGLBalanceCC = vdSumGLBalanceCC + vdGLBalanceCCPerGL
                   vdSumGLBalanceLC = vdSumGLbalanceLC + vdGLBalanceLCPerGL
                   vdSumSubBalanceCC = vdSumSubBalanceCC + vdSubBalanceCCPerGL
                   vdSumSubBalanceLC = vdSumSubBalanceLC + vdSubBalanceLCPerGL. 
            if vlDetailPrinted then put stream sSumOut unformatted ' ':U skip. 
            put stream sSumOut unformatted trim(#T-5'All Currencies':50(837972044)T-5#) at 1
                                           trim(#T-36'GL Balance':20(1017)T-36#) to 30
                                           trim(#T-40'Sub-ledger Balance':20(254104290)T-40#) to 60
                                           trim(#T-4'Difference':20(4114)T-4#) to 90 skip. 
            <M-87 run RoundAmount
               (input-output vdGLBalanceLCPerGL (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcGLBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
            <M-20 run RoundAmount
               (input-output vdSubBalanceLCPerGL (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcSubBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>
            assign vdDiff = vdGLBalanceLCPerGL - vdSubBalanceLCPerGL.     
            <M-26 run RoundAmount
               (input-output vdDiff (bdUnroundedAmount), 
                input  viCompanyLC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcDiffFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                                                                              
            put stream sSumOut unformatted trim(#T-42'BC':3(4030)T-42#) at 1
                                           vcGLBalanceFormat to 30
                                           vcSubBalanceFormat to 60
                                           vcDiffFormat to 90 skip. 
            <M-75 run RoundAmount
               (input-output vdGLBalanceCCPerGL (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcGLBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
            <M-51 run RoundAmount
               (input-output vdSubBalanceCCPerGL (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcSubBalanceFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck> 
            assign vdDiff = vdGLBalanceCCPerGL - vdSubBalanceCCPerGL. 
            <M-22 run RoundAmount
               (input-output vdDiff (bdUnroundedAmount), 
                input  viCompanyCC (iiCurrencyId), 
                input  ? (icCurrencyCode), 
                output vcDiffFormat (ocFormatedAmount), 
                output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                                    
            put stream sSumOut unformatted trim(#T-98'SC':12(3936)T-98#) at 1
                                           vcGLBalanceFormat to 30
                                           vcSubBalanceFormat to 60
                                           vcDiffFormat to 90 skip.   

        end.        
    
    end. /* for each tBalanceByGLAP */
            
    put stream sSumOut unformatted ' ' skip.
    <M-6 run RoundAmount
       (input-output vdSumGLBalanceLC (bdUnroundedAmount), 
        input  viCompanyLC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcGLBalanceFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
    <M-89 run RoundAmount
       (input-output vdSumSubBalanceLC (bdUnroundedAmount), 
        input  viCompanyLC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcSubBalanceFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck> 
    assign vdDiff = vdSumGLBalanceLC - vdSumSubBalanceLC.    
    <M-11 run RoundAmount
       (input-output vdDiff (bdUnroundedAmount), 
        input  viCompanyLC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcDiffFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck>    
    put stream sSumOut unformatted trim(#T-65'Total BC':50(330832880)T-65#) at 1
                                   vcGLBalanceFormat to 30
                                   vcSubBalanceFormat to 60
                                   vcDiffFormat to 90 skip. 
    <M-28 run RoundAmount
       (input-output vdSumGLBalanceCC (bdUnroundedAmount), 
        input  viCompanyCC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcGLBalanceFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck>   
    <M-47 run RoundAmount
       (input-output vdSumSubBalanceCC (bdUnroundedAmount), 
        input  viCompanyCC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcSubBalanceFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck>
    assign vdDiff = vdSumGLBalanceCC - vdSumSubBalanceCC.     
    <M-64 run RoundAmount
       (input-output vdDiff (bdUnroundedAmount), 
        input  viCompanyCC (iiCurrencyId), 
        input  ? (icCurrencyCode), 
        output vcDiffFormat (ocFormatedAmount), 
        output viFcReturnSuper (oiReturnStatus)) in TConCheck>                                                
    put stream sSumOut unformatted trim(#T-71'Total SC':50(601335079)T-71#) at 1
                                   vcGLBalanceFormat to 30
                                   vcSubBalanceFormat to 60
                                   vcDiffFormat to 90 skip.   

end.


put stream sSumOut unformatted "____________________________________________________________" skip.

output stream sSumOut close.

if oiReturnStatus = -98
then assign oiReturnStatus = 0.