project QadFinancials > class BPosting > method ValidateComponentPreBalancePosting

Description

ValidateComponentPreBalancePosting: submethod of ValidateComponentPre that adds postinglines to make the posting balanced in LC and CC.
Validations that need to be done before ANCESTOR CODE of ValidateComponent.


Parameters


idBalanceTCinputdecimalBalance of the Posting in TC
idBalanceLCinputdecimalBalance of the Posting in LC
idBalanceCCinputdecimalBalance of the Posting in CC
iiNumberOfPostingLinesInPostinginputintegerNumberOfPostingLinesInPosting; The number of postng-lines that are involved in the posting
iiNumberOfCurrenciesInPostinginputintegerNumberOfCurrenciesInPosting; The number of currencies that are involved in the posting
ilPostingLineWithTCZeroForLCinputlogicalPostingLineWithTCZeroExists: true in case the posting holds a line where all TC-amoutns are zero
ilPostingLineWithTCZeroForCCinputlogical
ilDifferentLCExchRatesInPostinginputlogicalDifferentLCExchRatesInPosting: true in case the posting has lines with different LC-Exchange-rates
ilDifferentCCExchRatesInPostinginputlogicalDifferentCCExchRatesInPosting: true in case the posting has lines with different CC-Exchange-rates
biLCCCRoundingGLIDinput-outputintegerGL-ID of the account used for the rounding
bcLCCCRoundingGLCodeinput-outputcharacterGL-Code of the account used for the rounding
bdLCRoundingMethodUnitinput-outputdecimalLCRoundingMethodUnit: Field RoundingMethod.RoundingMethodUnit of the currency that is LC
bdCCRoundingMethodUnitinput-outputdecimalCCRoundingMethodUnit: Field RoundingMethod.RoundingMethodUnit of the currency that is CC
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPosting.ValidateComponentPre


program code (program4/bposting.p)

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

/* ======================= */
/* Start Block             */
/* ======================= */
BALANCEPOSTINGBLOCK: DO :
    
    /* ================================================================================================================== */
    /* Prerequisites / Notes;                                                                                             */
    /* This method has to be located in the same segment as the calling method to make sure we have t_sPosting available  */
    /* ================================================================================================================== */
    if not available t_sPosting
    then do:                              
        assign vcMessage = trim(Substitute(#T-39'Internal error in &1: No &2 is available':255(733758247)T-39#,"ValidateComponentPreBalancePosting":U,"t_sPosting":U))
               viLocalReturnStatus = -3.
        <M-37 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-9656':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        Leave BALANCEPOSTINGBLOCK.
    end. /* if not available t_sPosting */
    
    if t_sPosting.tc_Status = "D":U
    then Leave BALANCEPOSTINGBLOCK. /* No actions required for deleted postings */
    
    /* ================================================ */
    /* Actions when posting is not balanced in LC or CC */
    /* ================================================ */
    if idBalanceLC <> 0 or idBalanceCC <> 0
    then do: 

        /* ============================================ */
        /* Find the rounding account when not yet known */
        /* ============================================ */
        if biLCCCRoundingGLID   = 0     or 
           biLCCCRoundingGLID   = ?     or 
           bcLCCCRoundingGLCode = "":U  or 
           bcLCCCRoundingGLCode = ?
        then do:
            <Q-31 run GLByGLSystemType (all) (Read) (Cache)
               (input t_sPosting.Company_ID, (CompanyId)
                input {&GLSYSTEMTYPE-ROUND}, (GLSystemTypeCode)
                input {&GLTYPECODE-SYST}, (GlTypeCode)
                input ?, (GlIsDivisionAccount)
                output dataset tqGLByGLSystemType) in BGL>
                
            find first tqGLByGLSystemType where
                       tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-ROUND} and
                       tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
                       no-error.
                       
            if not available tqGLByGLSystemType
            then do :
                assign vcMsgExplanation = <M-24 GetErrorExplanation
                                             (input  t_sPosting.tc_Rowid (icPostingRowId), 
                                              input  ? (icPostingLineRowId), 
                                              output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                              
                assign viLocalReturnStatus = -1
                       vcMessage           = trim(#T-44'The system account for rounding differences has not been defined.':255(733756123)T-44#).
                <M-26 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-9655':U (icFcMsgNumber), 
                    input  vcMsgExplanation (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                Leave BALANCEPOSTINGBLOCK.
            end. /* if not available tqGLByGLSystemType */
            
            assign biLCCCRoundingGLID   = tqGLByGLSystemType.tiGL_ID
                   bcLCCCRoundingGLCode = tqGLByGLSystemType.tcGLCode.
                   
        end. /*  if viLCCCRoundingGLID = 0 */
        
        /* ================================================ */
        /* Find the unreallised exchange gain-loss accounts */
        /* ================================================ */
        if ilDifferentCCExchRatesInPosting = TRUE or
           ilDifferentLCExchRatesInPosting = TRUE
        then do:
        
            /* ==================== */
            /* Get the LOSS account */
            /* ==================== */                    
            <Q-21 run GLByGLSystemType (all) (Read) (Cache)
               (input t_sPosting.Company_ID, (CompanyId)
                input {&GLSYSTEMTYPE-EXCHANGEUNREALLOSS}, (GLSystemTypeCode)
                input {&GLTYPECODE-SYST}, (GlTypeCode)
                input ?, (GlIsDivisionAccount)
                output dataset tqGLByGLSystemType) in BGL>
                
            find first tqGLByGLSystemType where
                       tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-EXCHANGEUNREALLOSS} and
                       tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
                       no-error.
                       
            if not available tqGLByGLSystemType
            then do :
                assign vcMsgExplanation = <M-66 GetErrorExplanation
                                             (input  t_sPosting.tc_Rowid (icPostingRowId), 
                                              input  ? (icPostingLineRowId), 
                                              output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                              
                assign viLocalReturnStatus = -1
                       vcMessage           = trim(#T-56'The system account for exchange rate differences has not been defined.':255(429370378)T-56#).
                <M-70 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-932994':U (icFcMsgNumber), 
                    input  vcMsgExplanation (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                Leave BALANCEPOSTINGBLOCK.
            end. /* if not available tqGLByGLSystemType */
            
            assign viExchangeRateLossGL_ID     = tqGLByGLSystemType.tiGL_ID
                   vcExchangeRateLossGLCode    = tqGLByGLSystemType.tcGLCode.
                   
            /* ====================== */            
            /* Get the PROFIT Account */       
            /* ====================== */                                           
            <Q-60 run GLByGLSystemType (all) (Read) (Cache)
               (input t_sPosting.Company_ID, (CompanyId)
                input {&GLSYSTEMTYPE-EXCHANGEUNREALPROFIT}, (GLSystemTypeCode)
                input {&GLTYPECODE-SYST}, (GlTypeCode)
                input ?, (GlIsDivisionAccount)
                output dataset tqGLByGLSystemType) in BGL>
                
            find first tqGLByGLSystemType where
                       tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-EXCHANGEUNREALPROFIT} and
                       tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
                       no-error.
            if not available tqGLByGLSystemType
            then do :
                assign vcMsgExplanation = <M-76 GetErrorExplanation
                                             (input  t_sPosting.tc_Rowid (icPostingRowId), 
                                              input  ? (icPostingLineRowId), 
                                              output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                              
                assign viLocalReturnStatus = -1
                       vcMessage           = trim(#T-68'The system account for exchange rate differences has not been defined.':255(429370378)T-68#).
                <M-92 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-694083':U (icFcMsgNumber), 
                    input  vcMsgExplanation (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                Leave BALANCEPOSTINGBLOCK.
                
            end. /* if not available tqGLByGLSystemType */
            
            assign viExchangeRateGainGL_ID     = tqGLByGLSystemType.tiGL_ID
                   vcExchangeRateGainGLCode    = tqGLByGLSystemType.tcGLCode.
                   
        end. /* if ilDifferentCCExchRatesInPosting = TRUE or */
        
        /* ========================================================================= */
        /* Lookup the rouding-method-unit when not yet known for LC                  */
        /* ========================================================================= */
        if iiNumberOfCurrenciesInPosting = 1 and
           (bdLCRoundingMethodUnit = 0 or bdLCRoundingMethodUnit = ?)
        then do :
            <Q-43 run CurrencyByCurrRoundingMethod (all) (Read) (NoCache)
               (input viCompanyLCId, (CurrencyID)
                input ?, (CurrencyCode)
                input ?, (RoundingMethodCode)
                output dataset tqCurrencyByCurrRoundingMethod) in BCurrency>
                
            Find tqCurrencyByCurrRoundingMethod where 
                 tqCurrencyByCurrRoundingMethod.tiCurrency_ID = viCompanyLCId
                 no-lock no-error.
                 
            if not available tqCurrencyByCurrRoundingMethod
            then do:
                assign vcMsgExplanation = <M-16 GetErrorExplanation
                                             (input  t_sPosting.tc_Rowid (icPostingRowId), 
                                              input  ? (icPostingLineRowId), 
                                              output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                              
                assign viLocalReturnStatus = -1
                       vcMessage           = trim(substitute(#T-45'Setup-error: unable to find the rounding-method for the base-currency (&1 / &2)':255(73476216)T-45#,vcCompanyLC,viCompanyLCId)).
                <M-41 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-9659':U (icFcMsgNumber), 
                    input  vcMsgExplanation (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                Leave BALANCEPOSTINGBLOCK.
                
            end. /* if not available tqCurrencyByCurrRoundingMethod */
            
            assign bdLCRoundingMethodUnit = tqCurrencyByCurrRoundingMethod.tdRoundingMethodUnit.
            
            if viCompanyCCId = viCompanyLCId and 
               (bdCCRoundingMethodUnit = 0 or bdCCRoundingMethodUnit = ?)
            then assign bdCCRoundingMethodUnit = bdLCRoundingMethodUnit.
            
        end. /*  if (bdLCRoundingMethodUnit = 0 or */
        
        /* ========================================================================= */
        /* Lookup the rouding-method-unit when not yet known for CC                  */
        /* ========================================================================= */
        if iiNumberOfCurrenciesInPosting = 1  and 
           (bdCCRoundingMethodUnit = 0 or bdCCRoundingMethodUnit = ?)
        then do :
            <Q-46 run CurrencyByCurrRoundingMethod (all) (Read) (NoCache)
               (input viCompanyCCId, (CurrencyID)
                input ?, (CurrencyCode)
                input ?, (RoundingMethodCode)
                output dataset tqCurrencyByCurrRoundingMethod) in BCurrency>
                
            Find tqCurrencyByCurrRoundingMethod where 
                 tqCurrencyByCurrRoundingMethod.tiCurrency_ID = viCompanyCCId
                 no-lock no-error.
                 
            if not available tqCurrencyByCurrRoundingMethod
            then do:
                assign vcMsgExplanation = <M-34 GetErrorExplanation
                                             (input  t_sPosting.tc_Rowid (icPostingRowId), 
                                              input  ? (icPostingLineRowId), 
                                              output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                              
                assign viLocalReturnStatus = -1
                       vcMessage           = trim(substitute(#T-48'Setup-error: unable to find the rounding-method for the statutory-currency (&1 / &2)':255(413968626)T-48#,vcCompanyCC,viCompanyCCId)).
                <M-47 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-9660':U (icFcMsgNumber), 
                    input  vcMsgExplanation (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                Leave BALANCEPOSTINGBLOCK.
            end. /* if not available tqCurrencyByCurrRoundingMethod */
            
            assign bdCCRoundingMethodUnit = tqCurrencyByCurrRoundingMethod.tdRoundingMethodUnit.
            
        end. /*  if (bdCCRoundingMethodUnit = 0 or */
                    
        /* =============================================================================================================================== */
        /* Check if the daybook type is 'consolidation' as in that case there are some specific things later on in the code of this method */
        /* =============================================================================================================================== */
        <Q-74 run JournalByJournalLayerType (all) (Read) (Cache)
           (input 0, (JournalId)
            input t_sPosting.tcJournalCode, (JournalCode)
            input t_sPosting.Company_ID, (CompanyId)
            input '':U, (LayerTypeCode)
            output dataset tqJournalByJournalLayerType) in BJournal>
            
        find first tqJournalByJournalLayerType where
                   tqJournalByJournalLayerType.tcJournalCode     = t_sPosting.tcJournalCode and 
                   tqJournalByJournalLayerType.tcJournalTypeCode = {&JOURNALTYPE-CONSOLIDATION}
                   no-error.
        
        
        /* I19 requirement for Brazil ==================================================================== *
         * When the posting is created for any operation with legal document, like Supplier invoice, we    *
         * keep reference of the legal document in all posting lines                                       *
         * =============================================================================================== */
        assign vcLegalDocNbr = "":U.
        for first t_sPostingLine where
                  t_sPostingLine.tc_ParentRowid          =  t_sPosting.tc_Rowid and
                  t_sPostingLine.tc_Status              <> "D":U                and
                  t_sPostingLine.PostingLineLegalDocNbr <> "":U                 and
                  t_sPostingLine.PostingLineLegalDocNbr <> ?:
            assign vcLegalDocNbr = t_sPostingLine.PostingLineLegalDocNbr.
        end.
        
        /* ========================================================================================= */
        /* 1- Balance the posting in LC by adding a posting-line where only the LC-fields are filled */
        /* ========================================================================================= */
        if (t_sPosting.PostingParentText     <> {&POSTINGPARENTTEXT-APMATCHING}  and  /* Non-Matching posting */
            idBalanceLC                      <> 0                                and  /* Local Currency is not balanced    */
            iiNumberOfCurrenciesInPosting     = 1                                and  /* Single Currency Transaction       */ 
            idBalanceTC                       = 0                                and  /* Transaction Currency is balanced  */           
            ilPostingLineWithTCZeroForLC      = false                            and  /* No lines with TC Db & Cr = 0 and LC filled */
            ilDifferentLCExchRatesInPosting   = false)                                /* all exchange rates the same       */ 
           OR
           (t_sPosting.PostingParentText      = {&POSTINGPARENTTEXT-APMATCHING}  and  /* Matching posting    */
            idBalanceLC                      <> 0                                and  /* Local Currency is not balanced    */
            iiNumberOfCurrenciesInPosting     = 1                                and  /* Single Currency Transaction       */ 
            idBalanceTC                       = 0)                                     /* Transaction Currency is balanced  */           
           OR
           (available tqJournalByJournalLayerType                                and /* Consolidation */
            idBalanceLC                      <> 0                                and
            iiNumberOfCurrenciesInPosting     = 1                                and
            idBalanceTC                       = 0                                and
            ilPostingLineWithTCZeroForLC = false)  
            
        then do:
        
            /* ===================================================================================================================================== */        
            /* Leave in case the difference is not within the acceptable limits - and is thus not just a rounding issue                              */
            /* in this case an error will be raised later on in the flow in the Auto-Balancing/Fall-back mechanism in AdditionalUpdatesPostFallBack  */
            /* ===================================================================================================================================== */                                
            if absolute(idBalanceLC) > absolute(bdLCRoundingMethodUnit * (iiNumberOfPostingLinesInPosting - 1))
            then leave BALANCEPOSTINGBLOCK.
            
            /* Actions in case of consolidation */
            if available tqJournalByJournalLayerType 
            then do:
                <Q-36 run ConsolidCycleByCompanyID (all) (Read) (NoCache)
                   (input viCompanyId, (CompanyId)
                    output dataset tqConsolidCycleByCompanyID) in BConsolidCycle>
                find first tqConsolidCycleByCompanyID where 
                           tqConsolidCycleByCompanyID.tiCompany_ID = viCompanyID 
                           no-error.
            end. /* if available tqJournalByJournalLayerType */  
            
            /* Create the additional postingline */          
            <M-27 run AddDetailLine
               (input  'PostingLine':U (icTable), 
                input  t_sPosting.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
                
            if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave BALANCEPOSTINGBLOCK.
            
            create t_sPostingLine.
            buffer-copy tPostingLine to t_sPostingLine.
            assign t_sPostingLine.PostingLine_ID          = tPostingLine.PostingLine_ID
                   t_sPostingLine.tc_Status               = tPostingLine.tc_Status
                   t_sPostingLine.tc_Rowid                = tPostingLine.tc_Rowid
                   t_sPostingLine.tc_ParentRowid          = tPostingLine.tc_ParentRowid
                   t_sPostingLine.PostingLineDebitLC      = if idBalanceLC > 0 then 0 else - idBalanceLC
                   t_sPostingLine.PostingLineCreditLC     = if idBalanceLC > 0 then idBalanceLC else 0
                   t_sPostingLine.PostingLineExchangeRate = 1
                   t_sPostingLine.PostingLineRateScale    = 1
                   t_sPostingLine.PostingLineCCRate       = 1
                   t_sPostingLine.PostingLineCCScale      = 1
                   t_sPostingLine.Period_ID               = t_sPosting.Period_ID            
                   t_sPostingLine.Company_ID              = t_sPosting.Company_ID
                   t_sPostingLine.PostingYearPeriod       = t_sPosting.PostingYearPeriod    
                   t_sPostingLine.PostingDate             = t_sPosting.PostingDate
                   t_sPostingLine.Currency_ID             = viCompanyLCId
                   t_sPostingLine.tcCurrencyCode          = vcCompanyLC
                   t_sPostingLine.GL_ID                   = if available tqConsolidCycleByCompanyID 
                                                            then tqConsolidCycleByCompanyID.tiGL_ID 
                                                            else if ilDifferentLCExchRatesInPosting 
                                                                 then (if idBalanceLC > 0 
                                                                       then viExchangeRateGainGL_ID
                                                                       else viExchangeRateLossGL_ID)
                                                            else biLCCCRoundingGLID
                   t_sPostingLine.tcGLCode                = if available tqConsolidCycleByCompanyID 
                                                            then tqConsolidCycleByCompanyID.tcGLCode 
                                                            else if ilDifferentLCExchRatesInPosting 
                                                                 then (if idBalanceLC > 0 
                                                                       then vcExchangeRateGainGLCode
                                                                       else vcExchangeRateLossGLCode)
                                                            else bcLCCCRoundingGLCode
                   t_sPostingLine.PostingLineLegalDocNbr  = vcLegalDocNbr
                   t_sPostingLine.tlLinkedCrCyDaemonReqExists  = false
                   vcAutoBalancePostingRowIds                = (if vcAutoBalancePostingRowIds = ? or vcAutoBalancePostingRowIds = "":U
                                                                then tPostingLine.tc_Rowid
                                                                else vcAutoBalancePostingRowIds + ",":U + tPostingLine.tc_Rowid).
            <Q-33 run GLForProfileLink (all) (Read) (Cache)
               (input t_sPosting.Company_ID, (CompanyId)
                input 0, (GLId)
                input '':U, (SharedSetCode)
                input t_sPostingLine.tcGLCode, (GLCode)
                output dataset tqGLForProfileLink) in BGL>
                
            find tqGLForProfileLink where
                 tqGLForProfileLink.tcGLCode = t_sPostingLine.tcGLCode
                 no-error.
                 
            if available tqGLForProfileLink
            then assign t_sPostingLine.tcGLDescription        = tqGLForProfileLink.tcGLDescription
                        t_sPostingLine.tlGLIsAutomaticAccount = tqGLForProfileLink.tlGLIsAutomaticAccount
                        t_sPostingLine.tlGLIsDebitAccount     = tqGLForProfileLink.tlGLIsDebitAccount
                        t_sPostingLine.tcGLTypeCode           = tqGLForProfileLink.tcGLTypeCode
                        t_sPostingLine.tcUnitCode             = tqGLForProfileLink.tcUnitCode.   
                                   
        end. /* 1- Balance the posting in LC */
                                    
        /* ========================================================================================= */
        /* 2- Balance the posting in CC by adding a posting-line where only the CC-fields are filled */
        /* ========================================================================================= */
        if (t_sPosting.PostingParentText     <> {&POSTINGPARENTTEXT-APMATCHING}  and  /* Non-Matching posting */
            idBalanceCC                      <> 0                                and  /* Local Currency is not balanced    */
            iiNumberOfCurrenciesInPosting     = 1                                and  /* Single Currency Transaction       */ 
            idBalanceTC                       = 0                                and  /* Transaction Currency is balanced  */           
            ilPostingLineWithTCZeroForCC      = false                            and  /* No lines with TC Db & Cr = 0 and CC filled */
            ilDifferentCCExchRatesInPosting   = false)                                /* all exchange rates the same       */ 
           OR
           (t_sPosting.PostingParentText      = {&POSTINGPARENTTEXT-APMATCHING}  and  /* Matching posting    */
            idBalanceCC                      <> 0                                and  /* Local Currency is not balanced    */
            iiNumberOfCurrenciesInPosting     = 1                                and  /* Single Currency Transaction       */ 
            idBalanceTC                       = 0)                                    /* Transaction Currency is balanced  */           
           OR
           (available tqJournalByJournalLayerType                                and /* Consolidation */
            idBalanceCC                      <> 0                                and
            iiNumberOfCurrenciesInPosting     = 1                                and
            idBalanceTC                       = 0                                and
            ilPostingLineWithTCZeroForCC     = false)
           OR
           (t_sPosting.PostingOriginIsExternal = true                            and /* For OTP transactions */ 
            iiNumberOfCurrenciesInPosting      = 1                               and
            idBalanceTC = 0 and
            idBalanceCC <> 0)
            
        then do :

            /* ===================================================================================================================================== */        
            /* Leave in case the difference is not within the acceptable limits - and is thus not just a rounding issue                              */
            /* in this case an error will be raised later on in the flow in the Auto-Balancing/Fall-back mechanism in AdditionalUpdatesPostFallBack  */
            /* ===================================================================================================================================== */                    
            if absolute(idBalanceCC) > absolute(bdCCRoundingMethodUnit * (iiNumberOfPostingLinesInPosting - 1))
            then leave BALANCEPOSTINGBLOCK.
            
            /* Actions in case of consolidation */
            if available tqJournalByJournalLayerType
            then do:
                <Q-79 run ConsolidCycleByCompanyID (all) (Read) (NoCache)
                   (input viCompanyId, (CompanyId)
                    output dataset tqConsolidCycleByCompanyID) in BConsolidCycle >
                find first tqConsolidCycleByCompanyID where 
                           tqConsolidCycleByCompanyID.tiCompany_ID = viCompanyID 
                           no-error.    
            end. /* if available tqJournalByJournalLayerType */
            
            /* Create the additional postingline */
            <M-28 run AddDetailLine
               (input  'PostingLine':U (icTable), 
                input  t_sPosting.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
                
            if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave BALANCEPOSTINGBLOCK.
            
            create t_sPostingLine.
            buffer-copy tPostingLine to t_sPostingLine. 
            assign t_sPostingLine.PostingLine_ID            = tPostingLine.PostingLine_ID
                   t_sPostingLine.tc_Status                 = tPostingLine.tc_Status
                   t_sPostingLine.tc_Rowid                  = tPostingLine.tc_Rowid
                   t_sPostingLine.tc_ParentRowid            = tPostingLine.tc_ParentRowid
                   t_sPostingLine.PostingLineDebitCC        = if idBalanceCC > 0 then 0 else - idBalanceCC
                   t_sPostingLine.PostingLineCreditCC       = if idBalanceCC > 0 then idBalanceCC else 0
                   t_sPostingLine.PostingLineExchangeRate   = 1  
                   t_sPostingLine.PostingLineRateScale      = 1
                   t_sPostingLine.PostingLineCCRate         = 1  
                   t_sPostingLine.PostingLineCCScale        = 1
                   t_sPostingLine.Period_ID                 = t_sPosting.Period_ID
                   t_sPostingLine.Company_ID                = t_sPosting.Company_ID
                   t_sPostingLine.PostingYearPeriod         = t_sPosting.PostingYearPeriod
                   t_sPostingLine.PostingDate               = t_sPosting.PostingDate
                   t_sPostingLine.Currency_ID               = viCompanyCCID
                   t_sPostingLine.tcCurrencyCode            = vcCompanyCC
                   t_sPostingLine.GL_ID                     = if available tqConsolidCycleByCompanyID 
                                                              then tqConsolidCycleByCompanyID.tiGL_ID 
                                                              else if ilDifferentCCExchRatesInPosting 
                                                                   then (if idBalanceLC > 0 
                                                                         then viExchangeRateGainGL_ID
                                                                         else viExchangeRateLossGL_ID)
                                                              else biLCCCRoundingGLID
                   t_sPostingLine.tcGLCode                  = if available tqConsolidCycleByCompanyID 
                                                              then tqConsolidCycleByCompanyID.tcGLCode 
                                                              else if ilDifferentCCExchRatesInPosting 
                                                                   then (if idBalanceLC > 0 
                                                                         then vcExchangeRateGainGLCode
                                                                         else vcExchangeRateLossGLCode) 
                                                              else bcLCCCRoundingGLCode
                   t_sPostingLine.PostingLineLegalDocNbr    = vcLegalDocNbr
                   t_sPostingLine.tlLinkedCrCyDaemonReqExists  = false
                   vcAutoBalancePostingRowIds                = (if vcAutoBalancePostingRowIds = ? or vcAutoBalancePostingRowIds = "":U
                                                                then tPostingLine.tc_Rowid
                                                                else  vcAutoBalancePostingRowIds + ",":U + tPostingLine.tc_Rowid).
            if not can-find (tqGLForProfileLink where
                             tqGLForProfileLink.tcGLCode = t_sPostingLine.tcGLCode)
            then do :
                <Q-35 run GLForProfileLink (all) (Read) (Cache)
                   (input t_sPosting.Company_ID, (CompanyId)
                    input 0, (GLId)
                    input '':U, (SharedSetCode)
                    input t_sPostingLine.tcGLCode, (GLCode)
                    output dataset tqGLForProfileLink) in BGL>
            end. /* if not can-find (tqGLForProfileLink where */
            
            find tqGLForProfileLink where
                 tqGLForProfileLink.tcGLCode = t_sPostingLine.tcGLCode
                 no-error.   
                 
            if available tqGLForProfileLink
            then assign t_sPostingLine.tcGLDescription        = tqGLForProfileLink.tcGLDescription
                        t_sPostingLine.tlGLIsAutomaticAccount = tqGLForProfileLink.tlGLIsAutomaticAccount
                        t_sPostingLine.tlGLIsDebitAccount     = tqGLForProfileLink.tlGLIsDebitAccount
                        t_sPostingLine.tcGLTypeCode           = tqGLForProfileLink.tcGLTypeCode
                        t_sPostingLine.tcUnitCode             = tqGLForProfileLink.tcUnitCode.                     
        end. /* if .... Balance in CC */ 
        
        else do : 
            
            /* ==================================================================== */
            /* Specifc actions for postings related to BCInvoice:                   */
            /*   Create rounding posting for SC updates to tax lines of the invoice */
            /*   (if the difference is within the allowed tollerance)               */
            /* ==================================================================== */                
            if vlTaxPostingSCExRateModified and /* Flag that optionally gets set by BCInvoice */
               idBalanceCC <> 0
            then do: 
                /* Create CC rounding posting */
                <M-38 run AddDetailLine
                   (input  'PostingLine':U (icTable), 
                    input  t_sPosting.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>
                    
                if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave BALANCEPOSTINGBLOCK.   
                                    
                create t_sPostingLine.
                buffer-copy tPostingLine to t_sPostingLine.
                    assign t_sPostingLine.PostingLineDebitCC        = if idBalanceCC > 0 then 0 else - idBalanceCC
                           t_sPostingLine.PostingLineCreditCC       = if idBalanceCC > 0 then idBalanceCC else 0
                           t_sPostingLine.PostingLineExchangeRate   = 1  
                           t_sPostingLine.PostingLineRateScale      = 1
                           t_sPostingLine.Period_ID                 = t_sPosting.Period_ID
                           t_sPostingLine.Company_ID                = t_sPosting.Company_ID
                           t_sPostingLine.PostingDate               = t_sPosting.PostingDate
                           t_sPostingLine.PostingYearPeriod         = t_sPosting.PostingYearPeriod
                           t_sPostingLine.Currency_ID               = viCompanyCCID
                           t_sPostingLine.tcCurrencyCode            = vcCompanyCC
                           t_sPostingLine.GL_ID                     = biLCCCRoundingGLID
                           t_sPostingLine.tcGLCode                  = bcLCCCRoundingGLCode
                           t_sPostingLine.PostingLineLegalDocNbr    = vcLegalDocNbr.
                           
                /* Show warning if CC difference is greater than the currency rounding threshold */                   
                if absolute(idBalanceCC) >= iiNumberOfPostingLinesInPosting * bdCCRoundingMethodUnit  
                then do:                                      
                    /* Create a message containing content of the posting */
                    assign vcPostingMessage = #T-62'Posting details:  &1/&2/&3/&4':255(43446359)T-62#
                           vcPostingMessage = substitute(vcPostingMessage, t_sPosting.PostingYear, t_sPosting.PostingYearPeriod, t_sPosting.tcJournalCode, t_sPosting.PostingVoucher)
                           vcPostingMessage = vcPostingMessage + chr(10)
                           vcPostingMessage = vcPostingMessage + substitute(#T-54'Balance in statutory currency: &1':255(298364963)T-54#, idBalanceCC) + chr(10)                       
                           vcPostingMessage = vcPostingMessage + chr(10)
                           vcPostingMessage = vcPostingMessage + #T-2'Posting line details:':255(39827807)T-2#
                           vcPostingMessage = vcPostingMessage + chr(10).  
                                                    
                    for each t_sPostingLine where
                             t_sPostingLine.tc_ParentRowid = t_sPosting.tc_Rowid and 
                             t_sPostingLine.tc_Status <> "D":U :
                             
                        if length(vcPostingMessage, "CHARACTER") < 30000
                        then assign vcPostingMessage = vcPostingMessage + chr(10) + 
                                                       trim(substitute(#T-18'Amount for account &1 (&2):  Statutory Currency: &3 &4':255(148377461)T-18#,
                                                                       t_sPostingLine.tcGLCode,
                                                                       t_sPostingLine.tcGLDescription,                                                                   
                                                                       t_sPostingLine.PostingLineDebitCC - t_sPostingLine.PostingLineCreditCC,
                                                                       vcCompanyCC)).  
                        else do:
                            assign vcPostingMessage = vcPostingMessage + chr(10) +
                                                      trim(#T-5'Note: Not all postinglines are included in the message.':255(999890618)T-5#).
                            leave.
                        end. /* Not if length */
                        
                    end. /* for each t_sPostingLine where */
                    
                    /* Now, create a warning message to alert/block user about the situation */                                         
                    assign vcMessage = substitute(#T-7'The journal entry balance in Statutory Currency is not zero and is outside the tolerance for posting &1\&2\&3.':255(997921301)T-7# , t_sPosting.PostingYear, t_sPosting.tcJournalCode, t_sPosting.PostingVoucher) + chr(10) +  
                                       trim(#T-61'Click Yes to post the balance to the rounding account.':255(765483126)T-61#) + chr(10) + chr(10) + vcPostingMessage.  
                    <M-59 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'W':U (icType), 
                        input  3 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'qadfin-754510':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BPosting>       
                                                                                         
                    assign  viLocalReturnStatus = 1.  
                                              
                end.  /* if absolute(idBalanceCC) */ 
                
            end. /* if vlTaxPostingSCExRateModified */
            
        end. /* 2- Balance the posting in CC */
                                 
    end. /* if idBalanceLC <> 0 or idBalanceCC <> 0 */
    
END. /* BALANCEPOSTINGBLOCK */

/* ================== */       
/* Exception handling */
/* ================== */       
assign oiReturnStatus = viLocalReturnStatus.