project QadFinancials > class BBankEntry > method CalculateBankState

Description

Public method that is used by method Calcuate and that can be used by the UI. This method will fill all kind of information related to field BankState.GL


Parameters


iiCompanyIDinputintegerEntity ID
iiBankStateGLIDinputintegerBankStateGLID
icBankStateGLCodeinputcharacterBankStateGLCode; the parameter-value is not used if a value was provided for paramater BankStateGLID
oiGLCurrencyIDoutputintegerGLCurrencyID
ocGLCurrencyCodeoutputcharacterGLCurrencyCode
ocGLDescriptionoutputcharacterGLDescription
odGLBalanceLCoutputdecimalGLBalanceLC
odGLBalanceCCoutputdecimalGLBalanceCC
odGLBalanceGLCurroutputdecimalGLBalanceGLCurr
ocGLBankNumberoutputcharacterGLBankNumber
oiGLJournalIDoutputintegerGLJournalID
ocGLJournalCodeoutputcharacterGLJournalCode
ocGLJournalLayerCodeoutputcharacter
odGLUnallocStatBalanceoutputdecimalUnallocated Statement Balance for this GL
odBankStateOpeningBalanceoutputdecimal
odGLBalanceTCoutputdecimalBalance TC
oiGLBankNumberIdoutputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AddBankEntryHeader
method BBankEntry.CalculateAll


program code (program4/bbankentry.p)

assign vdExchangeRate = 0
       vdExchangeRateScale = 0
       /*JBA BTS 4906 use the correct exchange rate type*/
       vcExchangeRateType = <M-18 GetExchangeRateType () in BBankEntry>.
 
    /* ======================================================== */
    /* Convert unknown values and check for mandatory stuff     */
    /* Convert icBankStateGLCode to iiBankStateGLID when needed */
    /* ======================================================== */
    if iiBankStateGLID = ? then assign iiBankStateGLID = 0.
    if icBankStateGLCode = ? then assign icBankStateGLCode = "":U.
    if iiCompanyID = 0 or iiCompanyID = ? then return.
    if iiBankStateGLID = 0 and icBankStateGLCode = "":U then return.

    if (iiBankStateGLID = 0  and icBankStateGLCode <> "":U) or
       (iiBankStateGLID <> 0 and icBankStateGLCode = "":U)
    then do :
        <Q-4 run GLPrim (all) (Read) (NoCache)
           (input iiCompanyID, (CompanyId)
            input if iiBankStateGLID = 0 then icBankStateGLCode else '':U, (GLCode)
            input iiBankStateGLID, (GLId)
            output dataset tqGLPrim) in BGL >
        find first tqGLPrim no-lock no-error.

        if not available tqGLPrim 
        then do:
            <M-23 run SetMessage
               (input  trim(#T-23'The bank account number specified is not defined in the system.':150(1244)T-23#) (icMessage), 
                input  '':U (icArguments), 
                input  'tBankState.tcGLCode':U (icFieldName), 
                input  icBankStateGLCode (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-6574':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>

            assign oiReturnStatus = -1.
            return.
        end.

        assign iiBankStateGLID   = tqGLPrim.tiGL_ID
               icBankStateGLCode = tqGLPrim.tcGLCode.
    end. /* if iiBankStateGLID    = 0 and */

    /* ========================================== */
    /* Resolve Currency, Journal                  */
    /* ========================================== */
    
    if  not available tBankState or tBankState.BankStateTransactionType = {&BANKSTATETRANSTYPE-BANK}
    then do:
        <Q-9 run GLByGLForBankInfo (all) (Read) (NoCache)
           (input iiCompanyID, (CompanyId)
            input iiBankStateGLID, (GLID)
            input ?, (GLCode)
            output dataset tqGLByGLForBankInfo) in BGL >
        find first tqGLByGLForBankInfo where
                   tqGLByGLForBankInfo.tiGL_ID = iiBankStateGLID
                   no-lock no-error.
        if not available tqGLByGLForBankInfo
        then do:
                assign vcMessage =  trim (#T-22'There is some information missing. Check if the banking daybook profile was set up correctly.':150(59492)T-22#).                                
                       oiReturnStatus = -1.
                 <M-20 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-9093':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                Return.
        end. /* if not available tqGLByGLForBankInfo */
    
        assign oiGLJournalID        = tqGLByGLForBankInfo.tiJournal_ID
               ocGLJournalCode      = tqGLByGLForBankInfo.tcJournalCode
               ocGLJournalLayerCode = tqGLByGLForBankInfo.tcLayerCode
               oiGLCurrencyID       = if  tqGLByGLForBankInfo.tiCurrency_ID = 0 or 
                                         (tqGLByGLForBankInfo.tiCurrency_ID = ? or
                                          tqGLByGLForBankInfo.tiCurrency_ID = 0)
                                      then viCompanyLCId
                                      else tqGLByGLForBankInfo.tiCurrency_ID
               ocGLCurrencyCode     = if tqGLByGLForBankInfo.tcCurrencyCode = "":U
                                      then vcCompanyLC
                                      else tqGLByGLForBankInfo.tcCurrencyCode
               ocGLDescription      = tqGLByGLForBankInfo.tcGLDescription.
    
        if tqGLByGLForBankInfo.tcAPDiscountGLCode = "":U or
           tqGLByGLForBankInfo.tcAPDiscountGLCode = ? 
        then do:
            assign vcMessage      = trim(substitute(#T-16'The AP discount account was not specified for bank account &1.':150(64241)T-16#, icBankStateGLCode))
                   oiReturnStatus = -1.
             <M-15 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-9092':U (icFcMsgNumber), 
                 input  '' (icFcExplanation), 
                 input  '' (icFcIdentification), 
                 input  '' (icFcContext), 
                 output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end.
    
        if tqGLByGLForBankInfo.tcARDiscountGLCode = "":U or
           tqGLByGLForBankInfo.tcARDiscountGLCode = ?
        then do:
            assign vcMessage      = trim(substitute(#T-25'The AR discount account was not specified for bank account &1.':150(64242)T-25#, icBankStateGLCode))
                   oiReturnStatus = -1.
             <M-24 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-9094':U (icFcMsgNumber), 
                 input  '' (icFcExplanation), 
                 input  '' (icFcIdentification), 
                 input  '' (icFcContext), 
                 output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end.
                          
    
        if tqGLByGLForBankInfo.tcGLTypeCode <> {&GLTYPECODE-CASH}
        then do:
            /* =========================== */
            /* Resolve the BankStateNumber */
            /* =========================== */
             <Q-17 run BankNumberByParentID (all) (Read) (NoCache)
                (input iiBankStateGLID, (ParentObjectId)
                 input ?, (BankNumberParentType)
                 input ?, (BankNumberIsDefault)
                 input iiCompanyID, (CompanyId)
                 output dataset tqBankNumberByParentID) in BBankNumber >
            /*JBA extra conditions added plus a new search 'mechanism' implemented
            first try to find a default bank account 
            if no default bank account exists take the fist one*/
            find first tqBankNumberByParentID where 
                       tqBankNumberByParentID.tiParentObject_ID = iiBankStateGLID and
                       tqBankNumberByParentID.tiCompany_ID = iiCompanyID  and 
                       tqBankNumberByParentID.tlBankNumberIsDefault = true no-error.
            if not available tqBankNumberByParentID
            then find first tqBankNumberByParentID where 
                       tqBankNumberByParentID.tiParentObject_ID = iiBankStateGLID and
                       tqBankNumberByParentID.tiCompany_ID = iiCompanyID no-error.
    
            if available tqBankNumberByParentID
            then assign ocGLBankNumber = if tqBankNumberByParentID.tcBankNumberFormatted <> ? and 
                                            tqBankNumberByParentID.tcBankNumberFormatted <> "":U 
                                         then tqBankNumberByParentID.tcBankNumberFormatted 
                                         else tqBankNumberByParentID.tcBankNumber.
        end.
        else assign ocGLBankNumber = "":U.
    end.
    /* if tBankState.BankStateTransactionType = {&BANKSTATETRANSTYPE-CASH} */
    else do:
        <Q-40 run GLByGLForCashInfo (all) (Read) (NoCache)
           (input iiCompanyID, (CompanyId)
            input iiBankStateGLID, (GLID)
            input ?, (GLCode)
            output dataset tqGLByGLForCashInfo) in BGL >
        find first tqGLByGLForCashInfo where
                   tqGLByGLForCashInfo.tiGL_ID = iiBankStateGLID
                   no-lock no-error.
        if not available tqGLByGLForCashInfo
        then do:
                assign vcMessage =  trim (#T-37'There is some Information missing. Please check if the Cash Journal Profile was setup correctly.':150(999890406)T-37#).                                
                       oiReturnStatus = -1.
                 <M-33 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-9081':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                Return.
        end. /* if not available tqGLByGLForCashInfo */
    
        assign oiGLJournalID        = tqGLByGLForCashInfo.tiCashInJournalId
               ocGLJournalCode      = tqGLByGLForCashInfo.tcCashInJournalCode
               ocGLJournalLayerCode = tqGLByGLForCashInfo.tcCashInLayerCode
               oiGLCurrencyID       = if  tqGLByGLForCashInfo.tiCurrency_ID = 0 or 
                                         (tqGLByGLForCashInfo.tiCurrency_ID = ? or
                                          tqGLByGLForCashInfo.tiCurrency_ID = 0)
                                      then viCompanyLCId
                                      else tqGLByGLForCashInfo.tiCurrency_ID
               ocGLCurrencyCode     = if tqGLByGLForCashInfo.tcCurrencyCode = "":U
                                      then vcCompanyLC
                                      else tqGLByGLForCashInfo.tcCurrencyCode
               ocGLDescription      = tqGLByGLForCashInfo.tcGLDescription.
    
        if tqGLByGLForCashInfo.tcAPDiscountGLCode = "":U or
           tqGLByGLForCashInfo.tcAPDiscountGLCode = ? 
        then do:
            assign vcMessage      = trim(substitute(#T-38'The AP discount account was not specified for cash account &1.':150(999890407)T-38#, icBankStateGLCode))
                   oiReturnStatus = -1.
             <M-34 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-9082':U (icFcMsgNumber), 
                 input  '' (icFcExplanation), 
                 input  '' (icFcIdentification), 
                 input  '' (icFcContext), 
                 output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end.
    
        if tqGLByGLForCashInfo.tcARDiscountGLCode = "":U or
           tqGLByGLForCashInfo.tcARDiscountGLCode = ?
        then do:
            assign vcMessage      = trim(substitute(#T-39'The AR discount account was not specified for cash account &1.':150(999890409)T-39#, icBankStateGLCode))
                   oiReturnStatus = -1.
             <M-35 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-9083':U (icFcMsgNumber), 
                 input  '' (icFcExplanation), 
                 input  '' (icFcIdentification), 
                 input  '' (icFcContext), 
                 output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end.
    
        if tqGLByGLForCashInfo.tcGLTypeCode <> {&GLTYPECODE-CASH}
        then do:
            /* =========================== */
            /* Resolve the BankStateNumber */
            /* =========================== */
             <Q-36 run BankNumberByParentID (all) (Read) (NoCache)
                (input iiBankStateGLID, (ParentObjectId)
                 input ?, (BankNumberParentType)
                 input ?, (BankNumberIsDefault)
                 input iiCompanyID, (CompanyId)
                 output dataset tqBankNumberByParentID) in BBankNumber >
            /*JBA extra conditions added plus a new search 'mechanism' implemented
            first try to find a default bank account 
            if no default bank account exists take the fist one*/
            find first tqBankNumberByParentID where 
                       tqBankNumberByParentID.tiParentObject_ID = iiBankStateGLID and
                       tqBankNumberByParentID.tiCompany_ID = iiCompanyID  and 
                       tqBankNumberByParentID.tlBankNumberIsDefault = true no-error.
            if not available tqBankNumberByParentID
            then find first tqBankNumberByParentID where 
                       tqBankNumberByParentID.tiParentObject_ID = iiBankStateGLID and
                       tqBankNumberByParentID.tiCompany_ID = iiCompanyID no-error.
    
            if available tqBankNumberByParentID
            then assign ocGLBankNumber   = if tqBankNumberByParentID.tcBankNumberFormatted <> ? and 
                                              tqBankNumberByParentID.tcBankNumberFormatted <> "":U 
                                              then tqBankNumberByParentID.tcBankNumberFormatted 
                                              else tqBankNumberByParentID.tcBankNumber
                        oiGLBankNumberId = tqBankNumberByParentID.tiBankNumber_ID.
        end.
        else assign ocGLBankNumber = "":U.
    end.
    /* ===================================================================== */
    /* Resolve the Balance after getting the current accounting year/period  */
    /* ===================================================================== */
    assign vhFcComponent = ?.
    <M-2 run ApiGetAccountBalance
       (input  9999 (iiAccYear), 
        input  99 (iiAccPeriod), 
        input  iiBankStateGLID (iiGL_ID), 
        input  if iibankstateGLID = 0 then icbankStateGLCode else '':U (icGLCode), 
        input  viCompanyId (iiCompanyID), 
        input  '':U (icCompanyCode), 
        output odGLBalanceLC (odBalanceLC), 
        output odGLBalanceCC (odBalanceCC), 
        output odGLBalanceTC (odBalanceTC), 
        output viFcReturnSuper (oiReturnStatus)) in BGL>
    if oiGLCurrencyID   = ? or 
       oiGLCurrencyID   = 0 or
       viFcReturnSuper <> 0
    then return.

    if oiGLCurrencyID= viCompanyLCId
    then assign odGLBalanceGLCurr = odGLBalanceLC.
    else if oiGLCurrencyID= viCompanyCCId
         then assign odGLBalanceGLCurr = odGLBalanceCC.
         else do :
            <M-30 run GetExchangeRate
               (input  iiCompanyID (iiCompanyID), 
                input  viCompanyLCId (iiFromCurrencyID), 
                input  ? (icFromCurrencyCode), 
                input  oiGLCurrencyID (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  vcExchangeRateType (icExchangeRateTypeCode), 
                input  today (itValidityDate), 
                output vdExchangeRate (odExchangeRate), 
                output vdExchangeRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper <> 0 
            then return.

            /* Defect 10249-0523 A validation need to be added to validate exchange rate between 
            account currency and base currency */
            if vdExchangeRate = 0 or
               vdExchangeRate = ?
            then do:
                 assign vcMessage      = trim(#T-31'The exchange rate between the bank currency and base currency is missing. Use Exchange Rate Create to create it.':200(69703)T-31#)
                        oiReturnStatus = -1.
                <M-29 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-8260':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                return.
            end. /* end of if vdEXchangeRate = 0 */

            assign odGLBalanceGLCurr = <M-19 RoundAmount
                                               (input  odGLBalanceLC * vdExchangeRate * vdExchangeRateScale (idUnroundedAmount), 
                                                input  oiGLCurrencyID (iiCurrencyID), 
                                                input  ocGLCurrencyCode (icCurrencyCode)) in business>.
         end. /* oiGLCurrencyID <> LC and <> CC */

    /* ======================================= */
    /* calculate unallocated statement balance */
    /* we do this in 2 query calls to have the best performance  */
    /* ======================================= */
    
    /* read PARTALLOCATED */
    assign odGLUnallocStatBalance = 0.
    empty temp-table tqBankStateByGLBankStateNbr.
    <Q-11 run BankStateByGLBankStateNbr (all) (Read) (NoCache)
       (input iiCompanyID, (CompanyId)
        input ?, (MinBankStateNumber)
        input iiBankStateGLID, (GLID)
        input {&BANKSTATELINESTATUS-UNALLOC}, (BankStateLineStatus)
        input if iiBankStateGLID = 0 then icBankStateGLCode else '':U, (GLCode)
        input {&BANKSTATESTATUS-PARTALLOC}, (BankStateStatus)
        output dataset tqBankStateByGLBankStateNbr) in BBankEntry>
    for each tqBankStateByGLBankStateNbr :
        assign odGLUnallocStatBalance = odGLUnallocStatBalance + tqBankStateByGLBankStateNbr.tdBankStateLineAmountTC.
    end.
    
    /* read UNALLOCATED ALLOCATED */    
    empty temp-table tqBankStateByGLBankStateNbr.
      <Q-52 run BankStateByGLBankStateNbr (all) (Read) (NoCache)
         (input iiCompanyID, (CompanyId)
          input ?, (MinBankStateNumber)
          input iiBankStateGLID, (GLID)
          input {&BANKSTATELINESTATUS-UNALLOC}, (BankStateLineStatus)
          input if iiBankStateGLID = 0 then icBankStateGLCode else '':U, (GLCode)
          input {&BANKSTATESTATUS-UNALLOC}, (BankStateStatus)
          output dataset tqBankStateByGLBankStateNbr) in BBankEntry>
    
    for each tqBankStateByGLBankStateNbr :
        assign odGLUnallocStatBalance = odGLUnallocStatBalance + tqBankStateByGLBankStateNbr.tdBankStateLineAmountTC.
    end.

    /* ========================= */
    /* calculate opening balance */
    /* ========================= */
    assign odBankStateOpeningBalance = odGLBalanceTC + odGLUnallocStatBalance.

    /* ================================================================================== */ 
    /* Fill in BankStateLine calculated values when still empty                           */
    /* ================================================================================== */
    for each tBankStateLine where tBankStateLine.BankState_ID = tBankState.BankState_ID:

        if tBankStateLine.tcPostingJournalCode = '' or tBankStateLine.tcPostingJournalCode = ?
        then do :
            if tBankStateLine.Posting_ID <> 0
            then do:
                <Q-26 run PostingPrim (all) (Read) (NoCache)
                   (input ?, (CompanyId)
                    input ?, (JournalCode)
                    input ?, (PeriodYear)
                    input ?, (PostingVoucher)
                    input tBankStateLine.Posting_ID, (PostingID)
                    output dataset tqPostingPrim) in BPosting>
                for each tqPostingPrim where tqPostingPrim.tiPosting_ID = tBankStateLine.Posting_ID :
                    assign
                        tBankStateLine.tiPostingVoucher = tqPostingPrim.tiPostingVoucher
                        tBankStateLine.tiPostingPeriodYear = tqPostingPrim.tiPostingYear
                        tBankStateLine.tcPostingJournalCode = tqPostingPrim.tcPostingJournalCode.
                end.
            end.
            else do:
                <Q-28 run GLByGLForCashInfo (all) (Read) (Cache)
                   (input ?, (CompanyId)
                    input tBankState.GL_ID, (GLID)
                    input ?, (GLCode)
                    output dataset tqGLByGLForCashInfo) in BGL>
                for each tqGLByGLForCashInfo where tqGLByGLForCashInfo.tiGL_ID = tBankState.GL_ID:
                    assign
                        tBankStateLine.tcPostingJournalCode = 
                            if(tBankStateLine.BankStateLineInOut = {&BANKSTATELINEINOUT-IN}) then
                                tqGLByGLForCashInfo.tcCashInJournalCode
                            else
                                tqGLByGLForCashInfo.tcCashOutJournalCode.
                end.
            end.
        end. /* tBankStateLine.tcPostingJournalCode */

        if tBankStateLine.tiPeriodPeriod = 0 or tBankStateLine.tiPeriodPeriod = ? or
           tBankStateLine.tiPeriodYear = 0 or tBankStateLine.tiPeriodYear = ?
        then do :
            <Q-27 run PeriodPrim (all) (Read) (Cache)
               (input ?, (CompanyId)
                input tBankStateLine.Period_ID, (PeriodId)
                input ?, (PeriodYear)
                input ?, (PeriodPeriod)
                output dataset tqPeriodPrim) in BPeriod>

            for each tqPeriodPrim where tqPeriodPrim.tiPeriod_ID = tBankStateLine.Period_ID:
                assign
                    tBankStateLine.tiPeriodPeriod = tqPeriodPrim.tiPeriodPeriod
                    tBankStateLine.tiPeriodYear = tqPeriodPrim.tiPeriodYear.
            end. 
        end. /* tBankStateLine.tiPeriodPeriod */
    end. /* for each tBankStateLine */