project QadFinancials > class TConCheck > method GLOpenItemControl

Description

GL open item control for consistency check


Parameters


icFileinputcharacter
ocConCheckLineDetResultoutputcharacter
otConCheckLineDetStartDateoutputdate
oiConCheckLineDetStartTimeoutputinteger
oiConCheckLineDetDurationoutputinteger
iiCompanyIDinputinteger
iiPeriodIDinputinteger
iiSessionIDinputinteger
oiErrorNumbersoutputinteger
ocConCheckLineDetVersionoutputcharacter
icAppVersioninputcharacter
icSumFileinputcharacter
ihTranslationHandleinputhandleTranslation Handle.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method TConCheck.MainEntry


program code (program1/tconcheck.p)

/* ================================================================================= */
/* This is the method for GL Open Items with GL                                      */
/* ================================================================================= */

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 '##### GL Open Items 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:   
    /* 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.

    /* init variables */
    assign ocConCheckLineDetResult = {&CONCHECKRESULT-PASSED}.    
 
    for each companysharedset of company no-lock, each sharedset of companysharedset where sharedset.sharedsettypecode = "GL" no-lock:
        assign viGLSharedSetID = sharedset.sharedset_ID.
    end. 

    def buffer bMove for GLOpenItemMovement.
    def buffer bPostLine for PostingLine.
    def buffer bComp for Company.
    def buffer bPer for Period.
    def buffer bQPostingLine for QPostingLine.

    empty temp-table tqOpenItem.
    empty temp-table tqReport.
    empty temp-table tqGLCurrency.
    
    /* loop through all GL accounts of type 'Open Item' */
    for each GL where GL.SharedSet_ID = viGLSharedSetID and
    	GL.GLTypeCode = "OPENITEM" no-lock:  
 
     /* Below code is commentted by PO decision. When 'allocate later' is resolved, this code will be reopen to use. */   	
 /*   	for each GLOpenItem of GL where GLOpenItem.Company_ID = Company.Company_ID and 
					     GLOpenItem.GLOpenItemClosingDate > period.periodenddate no-lock:
		assign vlExistedAtPeriodEnd = no.
		for each GLOpenItemMovement of GLOpenItem no-lock, each postingline of GLOpenItemMovement no-lock by postingline.postingdate:
			if postingline.postingdate <= period.periodenddate and 
			postingline.postingyearperiod <= period.periodyearperiod then do: /* also the period must be checked because of the year closing 13 that ends on the same day as period 12 */
				assign vlExistedAtPeriodEnd = yes.
				leave.
			end.
		end.

		if vlExistedAtPeriodEnd = no then next.

		find first tqGLCurrency where tqGLCurrency.tiGLID = GL.gl_ID and
			    tqGLCurrency.tiCurrencyID = GLOpenItem.currency_ID no-error.
		if not available tqGLCurrency then do:
			create tqGLCurrency.
			assign tqGLCurrency.tiGLID = GL.gl_ID
			       tqGLCurrency.tcGLCode = GL.GLCode
			       tqGLCurrency.tcGLDescription = GL.GLDescription
				tqGLCurrency.tiCurrencyID = GLOpenItem.currency_ID.
		       find first Currency where Currency.Currency_id = GLOpenItem.currency_ID no-lock no-error.
		       if available Currency then assign tqGLCurrency.tcCurrencyCode = Currency.CurrencyCode.
		end.
		
		assign tqGLCurrency.tdSubBalanceTC = tqGLCurrency.tdSubBalanceTC + GLOpenItem.GLOpenItemBalanceDebitTC - GLOpenItem.GLOpenItemBalanceCreditTC
			tqGLCurrency.tdSubBalanceLC = tqGLCurrency.tdSubBalanceLC + GLOpenItem.GLOpenItemBalanceDebitLC - GLOpenItem.GLOpenItemBalanceCreditLC
			tqGLCurrency.tdSubBalanceCC = tqGLCurrency.tdSubBalanceCC + GLOpenItem.GLOpenItemBalanceDebitCC - GLOpenItem.GLOpenItemBalanceCreditCC.
		/* now subtract the movements after the period end */
		for each bMove of GLOpenItem where glopenitemmovementtype = "MOVEMENT" no-lock, 
		    each bPostLine of bMove where bPostLine.postingdate > period.periodenddate no-lock:
			assign tqGLCurrency.tdSubBalanceTC = tqGLCurrency.tdSubBalanceTC - bPostLine.PostingLineDebitTC + bPostLine.PostingLineCreditTC
				tqGLCurrency.tdSubBalanceLC = tqGLCurrency.tdSubBalanceLC - bPostLine.PostingLineDebitLC + bPostLine.PostingLineCreditLC
				tqGLCurrency.tdSubBalanceCC = tqGLCurrency.tdSubBalanceCC - bPostLine.PostingLineDebitCC + bPostLine.PostingLineCreditCC.		
		end.	
	end.  	/* for each GLOpenItem */
*/
    	/* now we look for postinglines that have happened on this account in the checked period only */ 
    	for each postingline where postingline.company_ID = company.company_ID and postingline.gl_ID = GL.GL_ID and
    	postingline.postingyearperiod = viSelectedYearPeriod no-lock use-index CompanyGLYearPd:
    		find GLOpenItemMovement where GLOpenItemMovement.postingline_id = postingline.postingline_ID no-lock no-error.
    		if not available GLOpenItemMovement 
    		then do:
    		    /* this is not an error. It happens with Allocate later cases */    			    			
                  <M-61 run GetPostingLineKey
                     (input  postingline.posting_id (iiPostingId), 
                      input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + 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 = "Information: Allocate later status for posting (" + string(postingline.posting_id) + "): " + vcKey
    			       tqReport.tiErrorSeverity = 3.
    			assign viAllocateLater = viAllocateLater + 1.
    			next.
    		end.
    		find GLOpenItem of GLOpenItemMovement no-lock no-error.
    		if not available GLOpenItem then do:
    			/* run iprGetPostingLineKey. */
    			<M-28 run GetPostingLineKey
          (input  postingline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
        	     else next.
    			create tqReport.
    			assign tqReport.tcReportText = "Integrity error: GLOpenItemMovement without parent GLOpenItem: " + vcKey + " glopenitemmovement_ID: " + string(glopenitemmovement_ID)
    			       tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    			next.
    		end.
    		
  
    		
    		find tqOpenItem where tqOpenItem.tiOpenItemID = GLOpenItem.GLOpenItem_ID no-lock no-error.
    		if available tqOpenItem then next. /* we already checked this item */
    		create tqOpenItem.
    		assign tqOpenItem.tiOpenItemID = GLOpenItem.GLOpenItem_ID
    			   viItemsChecked = viItemsChecked + 1.
    		
    		/* now check the open item balance and status */
    		assign 
    			vdCurrDebitLC = 0
    			vdCurrCreditLC = 0
    			vdCurrDebitTC = 0
    			vdCurrCreditTC = 0
    			vdCurrDebitCC = 0
    			vdCurrCreditCC = 0
    			vtLastPostingDate = 01/01/1900
    			viInitPostID = 0.
    			
    		for each bMove of GLOpenItem no-lock:
    			find bPostLine where bPostLine.PostingLine_ID = bMove.PostingLine_ID no-lock no-error.
    			if not available bPostLine then do:
    				create tqReport.
    				assign tqReport.tcReportText = "Integrity Error. Postingline not available for GLOpenItemMovement " + vcCompanyCodeLocal + " " + " GL: " + trim(GL.GLCode) + " Key: " + GLOpenItem.GLOpenItemKey + 
    									" GLOpenItemMovement_ID: " + string(bMove.GLOpenItemMovement_ID) + " PostingLine_ID: " + string(bMove.PostingLine_ID)
    			              tqReport.tiErrorSeverity = 1
    				       oiErrorNumbers = oiErrorNumbers + 1.
    				next.
    			end.
    			if bMove.Currency_ID <> GLOpenItem.currency_id then do:
    				/* run iprGetbPostLineKey. */
    			    <M-27 run GetPostingLineKey
              (input  bpostline.posting_id (iiPostingId), 
               input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    				create tqReport.
    				assign tqReport.tcReportText = "Inconsistency in Currency. " + vcKey + " GLOpenItemMovement.Currency_ID: " + string(bMove.Currency_ID) + " GLOpenItem.Currency_ID: " + string(GLOpenItem.Currency_ID)
    					tqReport.tiErrorSeverity = 1
        				oiErrorNumbers = oiErrorNumbers + 1.
        		end.
    			if bMove.GLOpenItemMovementPostDate <> bPostLine.PostingDate then do:
    				/* run iprGetbPostLineKey. */
    				<M-10 run GetPostingLineKey
           (input  bpostline.posting_id (iiPostingId), 
            input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    				create tqReport.
    				assign tqReport.tcReportText = "Inconsistency in PostingDate. " + vcKey + " GLOpenItemMovementPostDate: " + string(bMove.GLOpenItemMovementPostDate) +
    									" PostingLine.PostingDate: " + string(bPostLine.PostingDate)
        			       tqReport.tiErrorSeverity = 1
        				oiErrorNumbers = oiErrorNumbers + 1.
        		end.
    			if bMove.Currency_ID <> bPostLine.currency_id then do:
    				/* run iprGetbPostLineKey. */
    				<M-47 run GetPostingLineKey
           (input  bpostline.posting_id (iiPostingId), 
            input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    				create tqReport.
    				assign tqReport.tcReportText = "Inconsistency in Currency. " + vcKey + " GLOpenItemMovement.Currency_ID: " + string(bMove.Currency_ID) +
    									" PostingLine.Currency_ID: " + string(bPostLine.currency_id)
    			              tqReport.tiErrorSeverity = 1
        				oiErrorNumbers = oiErrorNumbers + 1.
        		end.
    			if bMove.GLOpenItemMovementType <> "INITIAL" and
    				bMove.GLOpenItemMovementType <> "MOVEMENT" then do:
    				/* run iprGetbPostLineKey. */
    				<M-40 run GetPostingLineKey
           (input  bpostline.posting_id (iiPostingId), 
            input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    				create tqReport.
    				assign tqReport.tcReportText = "Inconsistency in movement with wrong type." + vcKey + " GLOpenItemMovementType: " + trim(bMove.GLOpenItemMovementType)
    			              tqReport.tiErrorSeverity = 1
        				oiErrorNumbers = oiErrorNumbers + 1.
        			next.
    			end.
    			if bMove.GLOpenItemMovementType = "INITIAL" then do:
    				assign viInitPostID = bPostLine.PostingLine_ID.
    				/* now compare the postingline amounts with the original GLOpenItem amounts */
    				if bPostLine.PostingLineDebitLC <> GLOpenItem.GLOpenItemOriginalDebitLC then do:
    					/* run iprGetbPostLineKey. */
    					<M-41 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original BC Debit Amount." + vcKey + " PostingLineDebitLC " + string(bPostLine.PostingLineDebitLC) +
    																				" GLOpenItemOriginalDebitLC: " + string(GLOpenItem.GLOpenItemOriginalDebitLC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    				if bPostLine.PostingLineCreditLC <> GLOpenItem.GLOpenItemOriginalCreditLC then do:
    					/* run iprGetbPostLineKey. */
    					<M-56 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original BC Credit Amount." + vcKey + " PostingLineCreditLC " + string(bPostLine.PostingLineCreditLC) +
    																				" GLOpenItemOriginalCreditLC: " + string(GLOpenItem.GLOpenItemOriginalCreditLC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    				if bPostLine.PostingLineDebitTC <> GLOpenItem.GLOpenItemOriginalDebitTC then do:
    					/* run iprGetbPostLineKey. */
    					<M-96 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original TC Debit Amount." + vcKey + " PostingLineDebitTC " + string(bPostLine.PostingLineDebitTC) +
    																				" GLOpenItemOriginalDebitTC: " + string(GLOpenItem.GLOpenItemOriginalDebitTC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    				if bPostLine.PostingLineCreditTC <> GLOpenItem.GLOpenItemOriginalCreditTC then do:
    					/* run iprGetbPostLineKey. */
    					<M-2 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original TC Credit Amount." + vcKey + " PostingLineCreditTC " + string(bPostLine.PostingLineCreditTC) +
    																				" GLOpenItemOriginalCreditTC: " + string(GLOpenItem.GLOpenItemOriginalCreditTC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    				if bPostLine.PostingLineDebitCC <> GLOpenItem.GLOpenItemOriginalDebitCC then do:
    					/* run iprGetbPostLineKey. */
    					<M-15 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original SC Debit Amount." + vcKey + " PostingLineDebitCC " + string(bPostLine.PostingLineDebitCC) +
    																				" GLOpenItemOriginalDebitCC: " + string(GLOpenItem.GLOpenItemOriginalDebitCC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    				if bPostLine.PostingLineCreditCC <> GLOpenItem.GLOpenItemOriginalCreditCC then do:
    					/* run iprGetbPostLineKey. */
    					<M-71 run GetPostingLineKey
            (input  bpostline.posting_id (iiPostingId), 
             input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    					create tqReport.
    					assign tqReport.tcReportText = "Inconsistency in Original SC Credit Amount." + vcKey + " PostingLineCreditCC " + string(bPostLine.PostingLineCreditCC) +
    																				" GLOpenItemOriginalCreditCC: " + string(GLOpenItem.GLOpenItemOriginalCreditCC)
    						tqReport.tiErrorSeverity = 1
    					       oiErrorNumbers = oiErrorNumbers + 1.
    				end.
    			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.
    				
    			/* let's also save the latest postingdate for this item to check the closingdate */
    			if bMove.GLOpenItemMovementPostDate > vtLastPostingDate then do:
    				assign vtLastPostingDate = bMove.GLOpenItemMovementPostDate.
    			end.
    		end.
    		/* net the calculated balances, because that is also done in the OI 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.
    		/* one of the movements must have been the initial */
    		if viInitPostID = 0 then do:
    			create tqReport.
    			assign tqReport.tcReportText = "Integrity error. Open Item without intial type movement." + vcCompanyCodeLocal + " " + "GL:" + trim(GL.GLCode) + " Key: " + GLOpenItem.GLOpenItemKey + " GLOpenItem_ID: " + string(GLOpenItem.GLOpenItem_ID)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    			next.
    		end.
    		find bPostLine where bPostLine.PostingLine_ID = viInitPostID no-lock no-error.
    		
    		/* we have now the complete balance of the item and we can check it */
    		if vdCurrDebitLC <> GLOpenItem.GLOpenItemBalanceDebitLC then do:
    			/* run iprGetbPostLineKey. */
    			<M-60 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance BC Debit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrDebitLC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceDebitLC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if vdCurrCreditLC <> GLOpenItem.GLOpenItemBalanceCreditLC then do:
    			/* run iprGetbPostLineKey. */
    			<M-89 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance BC Credit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrCreditLC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceCreditLC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if vdCurrDebitTC <> GLOpenItem.GLOpenItemBalanceDebitTC then do:
    			/* run iprGetbPostLineKey. */
    			<M-59 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance TC Debit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrDebitTC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceDebitTC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if vdCurrCreditTC <> GLOpenItem.GLOpenItemBalanceCreditTC then do:
    			/* run iprGetbPostLineKey. */
    			<M-1 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance TC Credit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrCreditTC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceCreditTC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if vdCurrDebitCC <> GLOpenItem.GLOpenItemBalanceDebitCC then do:
    			/* run iprGetbPostLineKey. */
    			<M-44 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance SC Debit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrDebitCC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceDebitCC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if vdCurrCreditCC <> GLOpenItem.GLOpenItemBalanceCreditCC then do:
    			/* run iprGetbPostLineKey. */
    			<M-42 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance SC Credit Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    						" Calculated: " + string(vdCurrCreditCC) +
    						" In GLOpenItem record: " + string(GLOpenItem.GLOpenItemBalanceCreditCC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		
    		/* also check the net balance amount in the open item */
    		if GLOpenItem.GLOpenItemBalanceLC <> GLOpenItem.GLOpenItemBalanceDebitLC - GLOpenItem.GLOpenItemBalanceCreditLC then do:
    			/* run iprGetbPostLineKey. */
    			<M-82 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance BC Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    							" GLOpenItemBalanceLC: " + string(GLOpenItem.GLOpenItemBalanceLC) +
    							" GLOpenItemBalanceDebitLC: " + string(GLOpenItem.GLOpenItemBalanceDebitLC) +
    							" GLOpenItemBalanceCreditLC: " + string(GLOpenItem.GLOpenItemBalanceCreditLC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemBalanceTC <> GLOpenItem.GLOpenItemBalanceDebitTC - GLOpenItem.GLOpenItemBalanceCreditTC then do:
    			/* run iprGetbPostLineKey. */
    			<M-57 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance TC Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    							" GLOpenItemBalanceTC: " + string(GLOpenItem.GLOpenItemBalanceTC) +
    							" GLOpenItemBalanceDebitTC: " + string(GLOpenItem.GLOpenItemBalanceDebitTC) +
    							" GLOpenItemBalanceCreditTC: " + string(GLOpenItem.GLOpenItemBalanceCreditTC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemBalanceCC <> GLOpenItem.GLOpenItemBalanceDebitCC - GLOpenItem.GLOpenItemBalanceCreditCC then do:
    			/* run iprGetbPostLineKey. */
    			<M-81 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Balance SC Amount. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    							" GLOpenItemBalanceCC: " + string(GLOpenItem.GLOpenItemBalanceCC) +
    							" GLOpenItemBalanceDebitCC: " + string(GLOpenItem.GLOpenItemBalanceDebitCC) +
    							" GLOpenItemBalanceCreditCC: " + string(GLOpenItem.GLOpenItemBalanceCreditCC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		
    		/* now we check the status */
    		if GLOpenItem.GLOpenItemIsOpen = False and
    		   GLOpenItem.GLOpenItemBalanceLC <> 0 then do:
    			/* run iprGetbPostLineKey. */
    			<M-70 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Open Item Status. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    				" GLOpenItemIsOpen: " + string(GLOpenItem.GLOpenItemIsOpen) +
    				" GLOpenItemBalanceLC: " + string(GLOpenItem.GLOpenItemBalanceLC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemIsOpen = False and
    		   GLOpenItem.GLOpenItemBalanceCC <> 0 then do:
    			/* run iprGetbPostLineKey. */
    			<M-23 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Open Item Status. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    				" GLOpenItemIsOpen: " + string(GLOpenItem.GLOpenItemIsOpen) +
    				" GLOpenItemBalanceCC: " + string(GLOpenItem.GLOpenItemBalanceCC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemIsOpen = False and
    			GLOpenItem.GLOpenItemClosingDate <> vtLastPostingDate then do:
    			/* run iprGetbPostLineKey. */
    			<M-43 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Closing Date. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    				" Expected date: " + string(vtLastPostingDate) +
    				" GLOpenItemClosingDate:" + string(GLOpenItem.GLOpenItemClosingDate)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemIsOpen = True and
    		   GLOpenItem.GLOpenItemBalanceLC = 0 and
    		   GLOpenItem.GLOpenItemBalanceCC = 0 then
    		   do:
    			/* run iprGetbPostLineKey. */
    			<M-90 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Open Item Status. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    				" GLOpenItemIsOpen: " + string(GLOpenItem.GLOpenItemIsOpen) +
    				" GLOpenItemBalanceLC: " + string(GLOpenItem.GLOpenItemBalanceLC) +
    				" GLOpenItemBalanceTC: " + string(GLOpenItem.GLOpenItemBalanceTC) +
    				" GLOpenItemBalanceCC: " + string(GLOpenItem.GLOpenItemBalanceCC)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if GLOpenItem.GLOpenItemIsOpen = True and
    			GLOpenItem.GLOpenItemClosingDate <> 12/31/9999 then do:
    			/* run iprGetbPostLineKey. */
    			<M-24 run GetPostingLineKey
          (input  bpostline.posting_id (iiPostingId), 
           input-output oiErrorNumbers (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 + " " + trim(GL.GLCode) + " " + substring(string(viSelectedYearPeriod,"999999"),1,4,"CHARACTER") + "/" + substring(string(viSelectedYearPeriod,"999999"),5,2,"CHARACTER") + "/" + trim(vcJournalCodeTmp) + "/" + string(vcPostingVoucherTmp,"999999999"). 
                            else next.
    			create tqReport.
    			assign tqReport.tcReportText =	"Inconsistency in Closing Date. Expected 12/31/9999 for OPEN status. " + vcKey + " Key: " + GLOpenItem.GLOpenItemKey + 
    				" GLOpenItemIsOpen: " + string(GLOpenItem.GLOpenItemIsOpen) +
    				" GLOpenItemClosingDate:" + string(GLOpenItem.GLOpenItemClosingDate)
    			       tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    	end. /* for each postingline */ 
    end.  /* for each GL */
    
    for each GLOpenItemMovement where GLOpenItemMovementPostDate >= period.periodstartdate and
    								  GLOpenItemMovementPostDate <= period.periodenddate no-lock:
    		find GLOpenItem of GLOpenItemMovement no-lock no-error.
    		if not available GLOpenItem then do:
    			create tqReport.
    			assign tqReport.tcReportText =	"Integrity Error. Open Item Movement with invalid parent Open Item ID. GLOpenItemMovement_ID: " + string(GLOpenItemMovement.GLOpenItemMovement_ID) +
    					" GLOpenItem_ID: " + string(GLOpenItemMovement.GLOpenItem_ID)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    			next.
    		end.
    		if GLOpenItem.Company_ID <> Company.Company_ID then next.
    		
    		if not can-find(Currency of GLOpenItemMovement) then do:
    			create tqReport.
    			assign tqReport.tcReportText =	"Integrity Error. Open Item Movement with invalid Currency ID. GLOpenItemMovement_ID: " + string(GLOpenItemMovement.GLOpenItemMovement_ID) + 
    					" Currency_ID: " + string(GLOpenItemMovement.Currency_ID)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    		end.
    		if not can-find(PostingLine of GLOpenItemMovement) then do:
    			create tqReport.
    			assign tqReport.tcReportText =	"Integrity Error. Open Item Movement with invalid parent PostingLine ID. GLOpenItemMovement_ID: " + string(GLOpenItemMovement.GLOpenItemMovement_ID) +
    					" PostingLine_ID: " + string(GLOpenItemMovement.PostingLine_ID)
    				tqReport.tiErrorSeverity = 1
    			       oiErrorNumbers = oiErrorNumbers + 1.
    			next.
    		end.
    end. /* for each GLOpenItemMovement */ 

     /* Below code is commentted by PO decision. When 'allocate later' is resolved, this code will be reopen to use. */     
/*
    for each tqGLCurrency:
    	for 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.
    	if tqGLCurrency.tdSubBalanceTC <> tqGLCurrency.tdBalanceGLTC then do:
		find first bQPostingLine where bQPostingLine.GL_ID = GL.GL_ID and bQPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and bQPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
		if available bQPostingLine then do:
			create tqReport.
			assign tqReport.tcReportText =	"Unprocessed Hitory Daemon queue causes inconsistency between GL and sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in TC: " + string(tqGLCurrency.tdBalanceGLTC) +
						" payment sub-ledger in TC: " + string(tqGLCurrency.tdSubBalanceTC)
						tqReport.tiErrorSeverity = 0.
			assign oiErrorNumbers = oiErrorNumbers + 1.
		end.
		else do:
			create tqReport.
			assign tqReport.tcReportText =	"Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in TC: " + string(tqGLCurrency.tdBalanceGLTC) +
						" payment sub-ledger in TC: " + string(tqGLCurrency.tdSubBalanceTC)
						tqReport.tiErrorSeverity = 0.
			assign viInconsistencyWarning = viInconsistencyWarning + 1.
		end.
	end.
   	if tqGLCurrency.tdSubBalanceLC <> tqGLCurrency.tdBalanceGLLC then do:
		find first bQPostingLine where bQPostingLine.GL_ID = GL.GL_ID and bQPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and bQPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
		if available bQPostingLine then do:
			create tqReport.
			assign tqReport.tcReportText =	"Unprocessed Hitory Daemon queue causes inconsistency between GL and sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in BC: " + string(tqGLCurrency.tdBalanceGLLC) +
						" payment sub-ledger in BC: " + string(tqGLCurrency.tdSubBalanceLC)
						tqReport.tiErrorSeverity = 1.
			assign oiErrorNumbers = oiErrorNumbers + 1.
		end.
		else do:
			create tqReport.
			assign tqReport.tcReportText =	"Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in BC: " + string(tqGLCurrency.tdBalanceGLLC) +
						" payment sub-ledger in BC: " + string(tqGLCurrency.tdSubBalanceLC)
						tqReport.tiErrorSeverity = 0.
			assign viInconsistencyWarning = viInconsistencyWarning + 1.
		end. 
	end.
	if tqGLCurrency.tdSubBalanceCC <> tqGLCurrency.tdBalanceGLCC then do:
		find first bQPostingLine where bQPostingLine.GL_ID = GL.GL_ID and bQPostingLine.PeriodYearPeriod <= Period.PeriodYearPeriod and bQPostingLine.Currency_ID = currency.currency_ID use-index GL no-lock no-error.
		if available bQPostingLine then do:
			create tqReport.
			assign tqReport.tcReportText =	"Unprocessed Hitory Daemon queue causes inconsistency between GL and sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in SC: " + string(tqGLCurrency.tdBalanceGLCC) +
						" payment sub-ledger in SC: " + string(tqGLCurrency.tdSubBalanceCC)
						tqReport.tiErrorSeverity = 1.
			assign oiErrorNumbers = oiErrorNumbers + 1.
		end.
		else do:
			create tqReport.
			assign tqReport.tcReportText =	"Inconsistency between GL and payments sub-ledger! Entity: " + Company.CompanyCode + " Period " + string(Period.PeriodYearPeriod) + " GL Account: " + tqGLCurrency.tcGLCode + " Currency: " + tqGLCurrency.tcCurrencyCode +
						" GL balance in SC: " + string(tqGLCurrency.tdBalanceGLCC) +
						" payment sub-ledger in SC: " + string(tqGLCurrency.tdSubBalanceCC)
						tqReport.tiErrorSeverity = 0.
			assign viInconsistencyWarning = viInconsistencyWarning + 1.
		end.
	end.
    end. /* for each tqGLCurrency */
    */
end. /* main_block */

if can-find(first tqReport where tqReport.tiErrorSeverity = 1) 
then do:
    assign ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED}
           oiErrorNumbers          = 0.
end.
/* print output */
for each tqReport use-index ErrorSeverityIndex:
    put stream sTechOut unformatted tqReport.tcReportText + ", Severity " + string(tqReport.tiErrorSeverity) skip.
    if tqReport.tiErrorSeverity = 1 then
        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-OPENITEMGL-TR}) skip.
put stream sSumOut unformatted '---------------------' skip.
put stream sSumOut unformatted trim(#T-84'Execution Result':255(510391963)T-84#) + ': ' + ocConCheckLineDetResult skip. 

if ocConCheckLineDetResult = {&CONCHECKRESULT-FAILED} 
then do: 
     put stream sSumOut unformatted 
                        trim(#T-46'Total number of errors':100(376712702)T-46#)
                        + ': ' + string(oiErrorNumbers) skip.
     put stream sSumOut unformatted 
                        '(' 
                        trim(#T-76'Please see details in the file':150(806040074)T-76#) ' ' icFile ')' skip.    
end. 
put stream sSumOut unformatted 
                        trim(substitute(#T-29'Information: &1 journal entries with 'Allocate Later' status in this period.':100(312558691)T-29#, string(viAllocateLater))) 
                        skip.
put stream sSumOut unformatted "____________________________________________________________" skip.

output stream sSumOut close.

if oiReturnStatus = -98
then assign oiReturnStatus = 0.