project QadFinancials > class BBankEntry > method AdditionalUpdatesInv

Description

method that will perform all actions related to the CI, DI, C-Doc, D-Doc and the linked variance postings. It also contains the optimistic-lcok checks for the involved instances


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdates


program code (program4/bbankentry.p)

/* ======================================================= */
/* Set default return-status                               */
/* Reset a data-item that is used in StopExternalInstances */
/* ======================================================= */
assign oiReturnStatus      = -98
       vcBankPostingLineId = "":U.

/* ============================================================= */
/* Call a submethod that will Start/Open BCI and BDI when needed */
/* ============================================================= */
 <M-85 run AdditionalUpdatesInvInstanceOpen
    (output vlBCInvoiceOpened (olBCInvoiceOpened), 
     output vlBDInvoiceOpened (olBDInvoiceOpened), 
     output vlBCDocumentOpened (olBCDocumentOpened), 
     output vlBDDocumentOpened (olBDDocumentOpened), 
     output vlBWithHoldingTaxOpened (olBWithHoldingTaxOpened), 
     output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0 then return.

/* =================================================== */
/* Check if we still have an instance of bjournalentry */
/*  Open the BJE-instance when not yet opened          */
/* =================================================== */
if viBJournalEntryBEID = 0 or viBJournalEntryBEID = ?
then do:
    assign vcMessage      = trim(#T-76'System error: cannot commit the transaction due to missing system information. No journal entry information exists.':255(17359)T-76#) + chr(10) +
                            trim(substitute(#T-77'Journal entry: &1.':255(316)T-77#,string(viBJournalEntryBEID)))
           oiReturnStatus = -1.
    <M-1 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-949':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
    return.
end. /* if viBJournalEntryBEID = 0 or viBJournalEntryBEID = ? */
if vhBJournalEntryBEInst               = ? or
   valid-handle(vhBJournalEntryBEInst) <> true
then do :
    <I-69 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
end. /* if vhBJournalEntryBEInst */

/* =========== */
/* Start block */
/* =========== */
BLOCKBANKSTATE: DO : 
    
    /* ================================= */
    /* Go through all allocated BE-Lines */
    /* ================================= */
    for each tBankState where
             tBankState.tc_Status <> "D":U and
             (tBankState.BankStateStatus = {&BANKSTATESTATUS-PARTALLOC} or
              tBankState.BankStateStatus = {&BANKSTATESTATUS-ALLOC}) no-lock,
        each tBankStateLine where
             tBankStateLine.tc_ParentRowid      = tBankState.tc_Rowid          and
             tBankStateLine.BankStateLineStatus = {&BANKSTATELINESTATUS-ALLOC} and
             (tBankStateLine.tc_status = "N":U or
              tBankStateLine.tc_status = "C":U) 
        by tBankStateLine.BankStateLineNumber :
        /*JBA BTS 4906 Exchange rate of cash type should be used in cash box, while it is the Accounting type now.*/
        assign vcExchangeRateType = <M-94 GetExchangeRateType () in BBankEntry>.
        /* ====================================================== */    
        /* Calculate the value of  BC                             */
        /* ====================================================== */
        if (tBankStateLine.tdBankStateLineAmountLC = 0 or tBankStateLine.tdBankStateLineAmountLC = ? )
            and tBankStateLine.BankStateLineAmountTC <> 0 then
        do:
            if tBankState.Currency_ID <> viPreviousBankBaseCurr  then
            do:
                assign viPreviousBankBaseCurr = tBankState.Currency_ID.
                if tBankState.Currency_ID = viCompanyLCId
                    then assign vdExchangeRate = 1
                                vdExchangeRateScale = 1.
                else do:
                    <M-82 run GetExchangeRate
                       (input  tBankState.Company_ID (iiCompanyID), 
                        input  tBankState.Currency_ID (iiFromCurrencyID), 
                        input  tBankState.tcCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyLCId (iiToCurrencyID), 
                        input  ? (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        input  tBankStateLine.BankStateLineValueDate (itValidityDate), 
                        output vdExchangeRate (odExchangeRate), 
                        output vdExchangeRateScale (odExchangeScaleFactor), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>                    
                    if viFcReturnSuper <> 0 
                    then assign oiReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0 then leave BLOCKBANKSTATE.  
               end.
           end.
                        
           assign tBankStateLine.tdBankStateLineAmountLC = <M-98 RoundAmount
                                                              (input  tBankStateLine.BankStateLineAmountTC * vdExchangeRate * vdExchangeRateScale (idUnroundedAmount), 
                                                               input  viCompanyLCId (iiCurrencyID), 
                                                               input  vcCompanyLC (icCurrencyCode)) in BBankEntry>.
                
        end.
        /* ====================================================== */    
        /* Get Exchange Rate between bank account currency and SC */
        /* ====================================================== */
        if tBankState.Currency_ID <> viPreviousBankStateCurr
        then do:
            assign viPreviousBankStateCurr = tBankState.Currency_ID.
            if tBankState.Currency_ID = viCompanyCCId
            then assign vdBankRateCC      = 1
                        vdBankRateScaleCC = 1.
            else do:
              if vlDomainIsStatutory = true
              then do:
                assign vcExchangeRateType = {&EXCHANGERATETYPE-STATUTORY}.
                <M-13 run GetExchangeRate
                   (input  tBankState.Company_ID (iiCompanyID), 
                    input  tBankState.Currency_ID (iiFromCurrencyID), 
                    input  ? (icFromCurrencyCode), 
                    input  viCompanyCCId (iiToCurrencyID), 
                    input  ? (icToCurrencyCode), 
                    input  ? (iiExchangeRateTypeID), 
                    input  vcExchangeRateType (icExchangeRateTypeCode), 
                    input  tBankStateLine.BankStateLineValueDate (itValidityDate), 
                    output vdBankRateCC (odExchangeRate), 
                    output vdBankRateScaleCC (odExchangeScaleFactor), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankEntry>     
                if viFcReturnSuper <> 0 
                then assign oiReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave BLOCKBANKSTATE. 
              end. /*  if vlDomainIsStatutory = true */                   
            end. /* else do: */ 
        end. /* if tBankState.tc_Rowid <> vcPreviousBankState */
                        
        /* ====================== */
        /* Reset local data-items */
        /* ====================== */
        assign vdAmountTC = 0
               vdAmountLC = 0
               vdAmountCC = 0
               vlUseDebit = if tBankStateLine.BankStateLineInOut = {&BANKSTATELINEINOUT-IN} 
                            then true 
                            else false.
        /* ===================================================================== */
        /* Go through all allocations of the BE-Line and sum the posting-amounts */
        /* Get the posting-amounts of the posting linked to the BE-Line          */
        /* Post the difference on the Bank-GL account                            */
        /* ===================================================================== */
        for each tBankStateAlloc where 
                 tBankStateAlloc.tc_ParentRowid = tBankStateLine.tc_Rowid:                                
            assign vdBankWHTLC = <M-22 RoundAmount
                                    (input  tBankStateAlloc.tdBankStateAllocBankWhtAmtTC * tBankStateAlloc.BankStateAllocBankRate * tBankStateAlloc.BankStateAllocBankRteScale (idUnroundedAmount), 
                                     input  viCompanyLCId (iiCurrencyID), 
                                     input  vcCompanyLC (icCurrencyCode)) in BBankEntry>
                   vdBankWHTCC = if viCompanyLCId = viCompanyCCId
                                 then vdBankWHTLC
                                 else <M-24 RoundAmount
                                         (input  tBankStateAlloc.tdBankStateAllocBankWhtAmtTC * vdBankRateCC * vdBankRateScaleCC (idUnroundedAmount), 
                                          input  viCompanyCCId (iiCurrencyID), 
                                          input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                   vdAmountTC  = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                 then vdAmountTC + tBankStateAlloc.BankStateAllocBankAmountTC - tBankStateAlloc.BankStateAllocBankDiscTC - tBankStateAlloc.tdBankStateAllocBankWHTAmtTC
                                 else vdAmountTC - (tBankStateAlloc.BankStateAllocBankAmountTC - tBankStateAlloc.BankStateAllocBankDiscTC - tBankStateAlloc.tdBankStateAllocBankWHTAmtTC)
                   vdAmountLC  = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                 then vdAmountLC + tBankStateAlloc.BankStateAllocBankAmountLC - tBankStateAlloc.BankStateAllocBankDiscLC - vdBankWHTLC
                                 else vdAmountLC - (tBankStateAlloc.BankStateAllocBankAmountLC - tBankStateAlloc.BankStateAllocBankDiscLC - vdBankWHTLC)
                   vdAmountCC  = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                 then vdAmountCC + tBankStateAlloc.tdBankStateAllocBankAmountCC - tBankStateAlloc.tdBankStateAllocBankDiscCC - vdBankWHTCC
                                 else vdAmountCC - (tBankStateAlloc.tdBankStateAllocBankAmountCC - tBankStateAlloc.tdBankStateAllocBankDiscCC - vdBankWHTCC).                                 
        end. /* for each tbankstatealloc where */

        <M-84 run GetJournalEntryBalance
           (input  tBankState.tcCurrencyCode (icBankCurrencyCode), 
            input  tBankStateLine.Posting_ID (iiPostingId), 
            input  tBankStateLine.BankStateLineValueDate (itBankStateLineValueDate), 
            input  false (ilCloseJournalEntryInstance), 
            input  tBankStateLine.BankStateLineAmountTC (idBankStateLineAmountTC), 
            input  tBankStateLine.tdBankStateLineAmountLC (idBankStateLineAmountLC), 
            input  vdAmountTC (idAllocatedAmountTC), 
            input  vdAmountLC (idAllocatedAmountLC), 
            output vdPostingAmountDebitTC (odPostingAmountDebitTC), 
            output vdPostingAmountCreditTC (odPostingAmountCreditTC), 
            output vdPostingAmountDebitLC (odPostingAmountDebitLC), 
            output vdPostingAmountCreditLC (odPostingAmountCreditLC), 
            output vdPostingAmountDebitCC (odPostingAmountDebitCC), 
            output vdPostingAmountCreditCC (odPostingAmountCreditCC), 
            output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE.                                  

        assign vdAmountTC = vdAmountTC + vdPostingAmountDebitTC - vdPostingAmountCreditTC
               vdAmountTC = IF vdAmountTC < 0 THEN (vdAmountTC * -1) ELSE vdAmountTC
               vdAmountLC = vdAmountLC + vdPostingAmountDebitLC - vdPostingAmountCreditLC
               vdAmountLC = IF vdAmountLC < 0 THEN (vdAmountLC * -1) ELSE vdAmountLC
               vdAmountCC = vdAmountCC + vdPostingAmountDebitCC - vdPostingAmountCreditCC
               vdAmountCC = If vdAmountCC < 0 then (vdAmountCC * -1) else vdAmountCC.

        assign vdAmountCCAlloc = vdAmountCC.

        /* posting on bank account */ 
        if vdAmountTC <> 0 or  
           vdAmountLC <> 0
        then do:
            /* make sure the LC amount gets filled by AddStandardPosting in case it's not filled yet */
            /* It will only fill it when it's unknown value, not when it's zero                      */
            if vdAmountTC <> 0 and vdAmountLC = 0
            then assign vdAmountLC = ?.
            
            empty temp-table tBankDefaultSafs.       
            
            <M-12 run AddStandardPosting
               (input  tBankStateLine.tcPostingRowId (icPostingtcRowid), 
                input  tBankState.tcGLCode (icGLCode), 
                input  '':U (icDivisionCode), 
                input  '':U (icCostCentreCode), 
                input  '':U (icCostCentreText), 
                input  '':U (icProjectCode), 
                input  '':U (icProjectText), 
                input  '':U (icIntercoBusinessRelationCode), 
                input  tBankState.tcCurrencyCode (icCurrencyCode), 
                input  if vlUseDebit then vdAmountTC else 0 (idDebitTC), 
                input  if vlUseDebit then vdAmountLC else 0 (idDebitLC), 
                input  if vlUseDebit then vdAmountCC else 0 (idDebitCC), 
                input  ? (idDebitPC), 
                input  if vlUseDebit then 0 else vdAmountTC (idCreditTC), 
                input  if vlUseDebit then 0 else vdAmountLC (idCreditLC), 
                input  if vlUseDebit then 0 else vdAmountCC (idCreditCC), 
                input  ? (idCreditPC), 
                input  0 (idQty), 
                input  tBankStateLine.BankStateLineDescription (icLineText), 
                input  '':U (icSafText), 
                input  tBankDefaultSafs (tDefaultSafs), 
                input  vcExchangeRateType (icExchangeRateType), 
                input  (vdAmountLC / vdAmountTC) (idExchangeRate), 
                input  ? (idExchangeRateScale), 
                input  ? (idPostingLineCCRate), 
                input  ? (idPostingLineCCScale), 
                output viBankPostingLineId (oiPostingLineId), 
                input  ? (iiSafStructureId), 
                input  ? (icSafStructureCode), 
                input  ? (icAllocationKey), 
                input  false (ilLinkedCrCyDaemonReqExists), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>    
            assign vcBankPostingLineId = if vcBankPostingLineId = "":U 
                                         then string(viBankPostingLineId) 
                                         else vcBankPostingLineId + ",":U + string(viBankPostingLineId).
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
            if viBlockError < 0 then Leave BLOCKBANKSTATE. 
            empty temp-table tBankDefaultSafs.
        end. /* if vdAmountTC <> 0 and */

        /* bank currency is equal to statutoy currenty, it maybe generate difference on SC value.
           since bank tc use accounting exchange rate, while invoice uses statutory exchange rate.
           we put the diffrence on gain/loss account and leave exchange rate is zero.
        */
        vdAmountCCDif = vdAmountCC - vdAmountCCAlloc.
        if vdAmountCCDif <> 0 then
        do:
             if (vlUseDebit and vdAmountCC > vdAmountCCAlloc) or
                ((not (vlUseDebit)) and vdAmountCC < vdAmountCCAlloc) then 
             do:  /*gain account*/
                  <Q-67 run GLByGLSystemType (all) (Read) (NoCache)
                      (input tBankStateLine.Company_ID, (CompanyId)
                       input {&GLSYSTEMTYPE-EXCHANGEREALPROFIT}, (GLSystemTypeCode)
                       input {&GLTYPECODE-SYST}, (GlTypeCode)
                       input ?, (GlIsDivisionAccount)
                       output dataset tqGLByGLSystemType) in BGL>
                  find first tqGLByGLSystemType where
                           tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-EXCHANGEREALPROFIT} and
                           tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
                           no-error.
                  if not available tqGLByGLSystemType
                  then do:
                    assign vcMessage           = trim(#T-7'The revaluation account (realized profit) is not defined.':150(304)T-7#)
                           viBlockError = -1.
                  end.
             end.
             else do: /*loss account */
                   <Q-46 run GLByGLSystemType (all) (Read) (NoCache)
                                 (input tBankStateLine.Company_ID, (CompanyId)
                                  input {&GLSYSTEMTYPE-EXCHANGEREALLOSS}, (GLSystemTypeCode)
                                  input {&GLTYPECODE-SYST}, (GlTypeCode)
                                  input ?, (GlIsDivisionAccount)
                                  output dataset tqGLByGLSystemType) in BGL>
                   find first tqGLByGLSystemType where
                           tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-EXCHANGEREALLOSS} and
                           tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
                           no-error.
                if not available tqGLByGLSystemType
                then do:
                    assign vcMessage           = trim(#T-27'The revaluation account (realized loss) is not defined.':150(305)T-27#)
                           viBlockError = -1.
                end.
             end.
             if viBlockError = -1 then
             do: 
                <M-4 run SetMessage
                    (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  ? (icRowid), 
                     input  'qadfin-936620':U (icFcMsgNumber), 
                     input  ? (icFcExplanation), 
                     input  ? (icFcIdentification), 
                     input  ? (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                Leave BLOCKBANKSTATE.
             end.
             if vlUseDebit and vdAmountCCDif >0 then assign vlGainLossDebit = false.
             else if vlUseDebit and vdAmountCCDif < 0 then assign vlGainLossDebit = true.
             else if (not vlUseDebit) and vdAmountCCDif > 0 then assign vlGainLossDebit = true.
             else if (not vlUseDebit) and vdAmountCCDif < 0 then assign vlGainLossDebit = false.

             empty temp-table tDefaultSafsBE.
             
             <M-96 run AddStandardPosting
                (input  tBankStateLine.tcPostingRowId (icPostingtcRowid), 
                 input  tqGLByGLSystemType.tcGLCode (icGLCode), 
                 input  ? (icDivisionCode), 
                 input  ? (icCostCentreCode), 
                 input  ? (icCostCentreText), 
                 input  ? (icProjectCode), 
                 input  ? (icProjectText), 
                 input  ? (icIntercoBusinessRelationCode), 
                 input  vcCompanyCC (icCurrencyCode), 
                 input  0 (idDebitTC), 
                 input  0 (idDebitLC), 
                 input  if vlGainLossDebit then abs(vdAmountCCDif)  else 0 (idDebitCC), 
                 input  0 (idDebitPC), 
                 input  0 (idCreditTC), 
                 input  0 (idCreditLC), 
                 input  if vlGainLossDebit then 0 else abs(vdAmountCCDif) (idCreditCC), 
                 input  0 (idCreditPC), 
                 input  0 (idQty), 
                 input  ? (icLineText), 
                 input  ? (icSafText), 
                 input  tDefaultSafsBE (tDefaultSafs), 
                 input  '':U (icExchangeRateType), 
                 input  ? (idExchangeRate), 
                 input  ? (idExchangeRateScale), 
                 input  ? (idPostingLineCCRate), 
                 input  ? (idPostingLineCCScale), 
                 output viDummy1 (oiPostingLineId), 
                 input  ? (iiSafStructureId), 
                 input  ? (icSafStructureCode), 
                 input  ? (icAllocationKey), 
                 input  false (ilLinkedCrCyDaemonReqExists), 
                 output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
             if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
             if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        end.
    
        /* ============================================================== */
        /* Clear temp-tables for additional updates in another components */
        /* ============================================================== */
        empty temp-table tBankCIMovement.
        empty temp-table tCDocInvoiceXrefStageRef.
        empty temp-table tInvoiceWHTPaymentForBE.
        empty temp-table tCInvoiceVatDelayDeltaRef.
        
        /* ======================================= */
        /* Create CI- and DI-Movements when needed */
        /* ======================================= */
        if vlBCInvoiceOpened
        then do:
            <M-56 run AdditionalUpdatesInvCIMovements
               (input  viBJournalEntryBEID (iiBJournalEntryID), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry> 
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
            if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        end. /* if vlBCInvoiceOpened */
        if vlBDInvoiceOpened
        then do:
            <M-57 run AdditionalUpdatesInvDIMovements
               (input  viBJournalEntryBEID (iiBJournalEntryId), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
            if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        end. /* if vlBDInvoiceOpened */
        
        /* ============================== */
        /* Create WHT records when needed */
        /* ============================== */
        if can-find(first tInvoiceWHTPaymentForBE) and vlBWithHoldingTaxOpened = true
        then do:
            <M-39 run AdditionalUpdatesInvWHT  (output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
            if viBlockError < 0 then Leave BLOCKBANKSTATE.
        end. /* if can-find(first tInvoiceWHTPaymentForBE) */
        
        /* ========================================================================== */
        /* Call a submethod that takes the required actions for Collections/Documents */
        /* ========================================================================== */
        <M-61 run AdditionalUpdatesInvDocuments
           (input  viBJournalEntryBEID (iiBJournalEntryId), 
            output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        
        /* ===================================================================================== */
        /* Call a submethod that will create a posting-line for the discount                     */
        /* (including the VAT-details when applicable)                                           */
        /* ===================================================================================== */
        <M-92 run AdditionalUpdatesInvDiscountCurrDiff  (output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        

        /* ===================================================================================== */
        /* Execute required methods in invoice components                                        */
        /* (including the VAT-details when applicable)                                           */
        /* ===================================================================================== */
        if vlBCInvoiceOpened
        then do:
            <M-33 run AdditionalUpdatesInvCInvoice  (output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
            if viBlockError < 0 then Leave BLOCKBANKSTATE. 
        end. /* if vlBCInvoiceOpened */
        
                
        /* ============================================================ */
        /* Get the posting info because it is used for the pre-payments */
        /* ============================================================ */
        <M-42 run GetPostingInfoByPostingId
           (input  tBankStateLine.Posting_ID (iiPostingId), 
            input  ? (icGLCode), 
            output viPostingPeriodYear (oiPostingPeriodYear), 
            output vcPostingJournalCode (ocPostingJournalCode), 
            output viPostingVoucher (oiPostingVoucher), 
            output viPostingPeriodPeriod (oiPostingPeriodPeriod), 
            output viDummyPostingLineId (oiPostingLineId), 
            output vdDummy (odPostingLineDebitTC), 
            output vdDummy (odPostingLineCreditTC), 
            output vdDummy (odPostingLineDebitLC), 
            output vdDummy (odPostingLineCreditLC), 
            output vdDummy (odPostingLineDebitCC), 
            output vdDummy (odPostingLineCreditCC), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE.
        
        /* ================================================================== */
        /* Call a submethod that will create the PrePayments for the BE-Line  */
        /* ================================================================== */
        <M-90 run AdditionalUpdatesInvPrePayments
           (input  viPostingPeriodYear (iiPostingPeriodYear), 
            input  viPostingPeriodPeriod (iiPostingPeriodPeriod), 
            input  vcPostingJournalCode (icPostingJournalCode), 
            input  viPostingVoucher (iiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE.
        
        /* ================================================================== */
        /* Call a submethod that will create the deductions for the BE-Line  */
        /* ================================================================== */
        <M-18 run AdditionalUpdatesInvDeductions
           (input  viPostingPeriodYear (iiPostingPeriodYear), 
            input  viPostingPeriodPeriod (iiPostingPeriodPeriod), 
            input  vcPostingJournalCode (icPostingJournalCode), 
            input  viPostingVoucher (iiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE.    
    end. /* for each tBankState, each tBankStateLine where */
    
    /* ====================================================================================== */
    /* After all DataLoads are done: Compare the data in the db with the initialy read data   */
    /* This done because there may be a time gap between the moment the Invoices were linked  */
    /* to the BE-Lines and the moment a DataLoad was performed on the Invoices.               */
    /* ====================================================================================== */
    <M-65 run AdditionalUpdatesInvOptimisticLock (output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
    if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
    if viBlockError < 0 then Leave BLOCKBANKSTATE.
    
    /* ======================================================== */
    /* Make sure the BankStateAllocType gets assigned correctly */
    /* ======================================================== */
    for each tBankState where
             tBankState.tc_Status <> "D":U and
             (tBankState.BankStateStatus = {&BANKSTATESTATUS-PARTALLOC} or
              tBankState.BankStateStatus = {&BANKSTATESTATUS-ALLOC}) no-lock,
        each tBankStateLine where
             tBankStateLine.tc_ParentRowid      = tBankState.tc_Rowid and
             (tBankStateLine.tc_status = "N":U or
              tBankStateLine.tc_status = "C":U) and
             tBankStateLine.BankStateLineStatus = {&BANKSTATELINESTATUS-ALLOCPOST},
        each tbankstatealloc where
             tbankstatealloc.bankstatealloctype = {&BANKSTATEALLOCTYPE-NCINV} or
             tbankstatealloc.bankstatealloctype = {&BANKSTATEALLOCTYPE-NDINV} or
             tbankstatealloc.bankstatealloctype = {&BANKSTATEALLOCTYPE-NEINV} :
        if tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NCINV} 
        then assign tbankstatealloc.bankstatealloctype = {&INVOICETYPE-INVOICE}.
        else if tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NDINV} 
             then assign tBankStateAlloc.BankStateAllocType = {&INVOICETYPE-INVOICE}.
             else if tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NEINV} 
                  then assign tbankstatealloc.bankstatealloctype = {&INVOICETYPE-INVOICE}.
    end. /* for each tBankState where, EACH tbankstatealloc */
  
    /* ================================ */     
    /* Validate the BCI/BDI-instances   */
    /* ================================ */     
    <M-88 run AdditionalUpdatesInvInstanceValidate
       (input  vlBCInvoiceOpened (ilBCInvoiceOpened), 
        input  vlBDInvoiceOpened (ilBDInvoiceOpened), 
        input  vlBCDocumentOpened (ilBCDocumentOpened), 
        input  vlBDDocumentOpened (ilBDDocumentOpened), 
        input  vlBWithHoldingTaxOpened (ilBWithHoldingTaxOpened), 
        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
    if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
    if viBlockError < 0 then Leave BLOCKBANKSTATE.
    
    /* ================================= */
    /* Go through all allocated BE-Lines */
    /* ================================= */
    for each tBankState where
             tBankState.tc_Status <> "D":U and
             (tBankState.BankStateStatus = {&BANKSTATESTATUS-PARTALLOC} or
              tBankState.BankStateStatus = {&BANKSTATESTATUS-ALLOC}) no-lock,
        each tBankStateLine where
             tBankStateLine.tc_ParentRowid      = tBankState.tc_Rowid          and
             tBankStateLine.BankStateLineStatus = {&BANKSTATELINESTATUS-ALLOC} and
             (tBankStateLine.tc_status = "N":U or
              tBankStateLine.tc_status = "C":U) 
        by tBankStateLine.BankStateLineNumber :
        /* ===================================================================================== */
        /* Call a submethod that will create a posting-line for the currency-differences         */
        /* ===================================================================================== */
        <M-50 run AdditionalUpdatesInvCurrDiff  (output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper <> 0 then assign viBlockError = viFcReturnSuper.
        if viBlockError < 0 then Leave BLOCKBANKSTATE. 

        /* ============================================================= */    
        /* Set the status on allocated and posted                        */
        /* ============================================================= */
        assign tBankStateLine.BankStateLineStatus = {&BANKSTATELINESTATUS-ALLOCPOST}.

    end. /* for each tBankState where */
END.  /* stop block BLOCKBANKSTATE */


/* ========================================================================================================= */
/* Normally we would here close BJournalEntry but we will not do it for performance                          */
/* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
/* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
/* appserver that is executed after each call to the appserver                                               */
/* ========================================================================================================= */

                                                                                                           
/* ===================================================================================================== */
/* Close the invoice/doc-instances -  This needs to be done before checking the value of viBlockError    */
/* ===================================================================================================== */
<M-86 run AdditionalUpdatesInvInstanceClose
   (input  vlBCInvoiceOpened (ilBCInvoiceOpened), 
    input  vlBDInvoiceOpened (ilBDInvoiceOpened), 
    input  vlBCDocumentOpened (ilBCDocumentOpened), 
    input  vlBDDocumentOpened (ilBDDocumentOpened), 
    input  vlBWithHoldingTaxOpened (ilBWithHoldingTaxOpened), 
    output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0 then return.

/* =============================================================================== */
/* Capture the errors produced in the BankStateBlock (after closing the instances) */
/* =============================================================================== */
if viBlockError <> 0 then assign oiReturnStatus = viBlockError.
if viBlockError < 0 then return.

/* ====================== */
/* Set return-status = OK */
/* ====================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.