project QadFinancials > class BBankEntry > method AdditionalUpdatesInvCIMovements

Description

create ci movements


Parameters


iiBJournalEntryIDinputintegerInstance of BJournalEntry
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdatesInv


program code (program4/bbankentry.p)

/* ======================================================= */
/* Set default return-status                               */
/* Empty temp-table that is passed to a BCI-method         */
/* ======================================================= */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

WHTBLOCK: DO:    
    /* ====================================================================== */
    /* Go through the already existing Invoices wherefor a movement is needed */
    /* Existing ones : no prepayments                                         */
    /* ====================================================================== */
    for each tBankStateAlloc where
             tBankStateAlloc.tc_ParentRowid = tBankStateLine.tc_Rowid and
             (tBankStateAlloc.tc_Status          = "N":U or
              tBankStateAlloc.tc_Status          = "C":U) and
             (tBankStateAlloc.CInvoice_ID        <> 0 and
              tBankStateAlloc.CInvoice_ID        <> ?)
             break by tBankStateAlloc.Company_ID :
        
        /* ========================================================================================= */     
        /* Reset the WHT-number for every company used as the WHT-number should be company-dependant */
        /* ========================================================================================= */
        if first-of (tBankStateAlloc.Company_ID)
        then assign viWHTNumber = 0.
        
        /* =========================================================== */
        /* Create the movement record for updating the control account */
        /* =========================================================== */             
        create tBankCIMovement.
        assign tBankCIMovement.tiCInvoiceId            = tBankStateAlloc.CInvoice_ID
               tBankCIMovement.tcGLAccountDivisionCode = "":U
               tBankCIMovement.tdAmountDebitTC         = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                         then tBankStateAlloc.BankStateAllocAmountTC - tBankStateAlloc.BankStateAllocWHTAmtTC
                                                         else 0
               tBankCIMovement.tdAmountCreditTC        = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                                         then tBankStateAlloc.BankStateAllocAmountTC - tBankStateAlloc.BankStateAllocWHTAmtTC
                                                         else 0
               tBankCIMovement.tdMovementDiscountTC    = if tBankStateAlloc.BankStateAllocType = {&INVOICETYPE-INVOICE} or 
                                                            tBankStateAlloc.BankStateAllocType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                                         then - tBankStateAlloc.BankStateAllocDiscountTC
                                                         else tBankStateAlloc.BankStateAllocDiscountTC
               tBankCIMovement.tcPostingRowId          = tBankStateLine.tcPostingRowId
               tBankCIMovement.tcPostingText           = tBankStateLine.BankStateLineDescription
               tBankCIMovement.tdCInvoiceHoldAmountTC  = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
               tBankCIMovement.tlMovementisForDraft    = false
			   tBankCIMovement.tlIsUndoPayment         = false.


        for each tBankStateStageAlloc where
                 tBankStateStageAlloc.tc_ParentRowid = tBankStateAlloc.tc_Rowid and
                (tBankStateStageAlloc.tc_Status      = "N":U or
                 tBankStateStageAlloc.tc_Status      = "C":U):
            create tCDocInvoiceXrefStageRef.
            assign tCDocInvoiceXrefStageRef.tc_Rowid                      = tBankStateStageAlloc.tc_Rowid
                   tCDocInvoiceXrefStageRef.CDocInvoiceXrefStage_ID       = tBankStateStageAlloc.CInvoiceStage_ID
                   tCDocInvoiceXrefStageRef.CDocInvoiceXrefStageAlloTC    = tBankStateStageAlloc.BankStateStageAllocAmntTC
                   tCDocInvoiceXrefStageRef.CDocInvoiceXrefStageDiscTC    = tBankStateStageAlloc.BankStateStageAllocDiscTC
                   tCDocInvoiceXrefStageRef.CDocInvoiceXrefStageWHTTC     = tBankStateStageAlloc.BankStateStageAllocWHTTC 
                   tCDocInvoiceXrefStageRef.CDocInvoiceXrefStagePaidAmtTC = tBankStateStageAlloc.BankStateStageAllocPaidTC
                   tCDocInvoiceXrefStageRef.tiCInvoice_ID                 = tBankStateAlloc.CInvoice_ID
                   tCDocInvoiceXrefStageRef.CInvoiceStage_ID              = tBankStateStageAlloc.CInvoiceStage_ID /* Fin-2138: tCDocInvoiceXrefStageRef.CInvoiceStage_ID needs to be assigned to make sure the stages on the invoice get updated */                   
                   tCDocInvoiceXrefStageRef.tc_Status                     = tBankStateStageAlloc.tc_Status.                   
        end. /* for each tBankStateStageAlloc where */
        
        /* ====================================================================== */
        /* Create two extra movements in case there is WHT on the bankstatealloc: */
        /* - One line with tlMovementIsAboutWHT set to false (to automatically    */
        /*   handle the posting on the control account in BCInvoice)              */
        /* - one line with tlMovementIsAboutWHT set to true (to automatically     */
        /*   handle WHT posting line in BCInvoice)                                */
        /* Do not pass in the LC or CC Rates so class BCInvoice will use the      */
        /* rates of the invoices to get the LC and CC amounts for the movement    */
        /* ====================================================================== */
        if tBankStateAlloc.BankStateAllocWHTAmtTC <> 0 and
           tBankStateAlloc.BankStateAllocWHTAmtTC <> ?
        then do:
            create tBankCIMovement.
            assign tBankCIMovement.tiCInvoiceId            = tBankStateAlloc.CInvoice_ID
                   tBankCIMovement.tcGLAccountDivisionCode = "":U
                   tBankCIMovement.tdAmountDebitTC         = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                             then tBankStateAlloc.BankStateAllocWHTAmtTC
                                                             else 0
                   tBankCIMovement.tdAmountCreditTC        = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                                             then tBankStateAlloc.BankStateAllocWHTAmtTC
                                                             else 0
                   tBankCIMovement.tcPostingRowId          = tBankStateLine.tcPostingRowId
                   tBankCIMovement.tcPostingText           = tBankStateLine.BankStateLineDescription
                   tBankCIMovement.tdCInvoiceHoldAmountTC  = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
                   tBankCIMovement.tlMovementisForDraft    = false
                   tBankCIMovement.tlIsUndoPayment         = false
                   tBankCIMovement.tlMovementIsAboutWHT    = false
                   tBankCIMovement.tdMovementDiscountTC    = 0
                   tBankCIMovement.tdRateTCLC              = 0
                   tBankCIMovement.tdScaleTCLC             = 0
                   tBankCIMovement.tdRateTCCC              = 0
                   tBankCIMovement.tdScaleTCCC             = 0
                   /* keep those in variables because they are needed later on to see if there are realized exchange gain/losses */
                   vdMovementDebitAmountLC               = if tBankCIMovement.tdAmountDebitTC <> 0
                                                           then <M-30 RoundAmount
                                                                   (input  tBankCIMovement.tdAmountDebitTC * tBankStateAlloc.tdDocExchangeRate * tBankStateAlloc.tdDocExchangeRateScale (idUnroundedAmount), 
                                                                    input  viCompanyLCId (iiCurrencyID), 
                                                                    input  vcCompanyLC (icCurrencyCode)) in BBankEntry>
                                                           else 0
                   vdMovementCreditAmountLC              = if tBankCIMovement.tdAmountCreditTC <> 0
                                                           then <M-73 RoundAmount
                                                                   (input  tBankCIMovement.tdAmountCreditTC * tBankStateAlloc.tdDocExchangeRate * tBankStateAlloc.tdDocExchangeRateScale (idUnroundedAmount), 
                                                                    input  viCompanyLCId (iiCurrencyID), 
                                                                    input  vcCompanyLC (icCurrencyCode)) in BBankEntry>
                                                           else 0
                   vdMovementDebitAmountCC               = if tBankCIMovement.tdAmountDebitTC <> 0
                                                           then <M-17 RoundAmount
                                                                   (input  tBankCIMovement.tdAmountDebitTC * tBankStateAlloc.tdDocCCExchangeRate * tBankStateAlloc.tdDocCCExchangeRateScale (idUnroundedAmount), 
                                                                    input  viCompanyCCId (iiCurrencyID), 
                                                                    input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                                                           else 0
                   vdMovementCreditAmountCC              = if tBankCIMovement.tdAmountCreditTC <> 0
                                                           then <M-9 RoundAmount
                                                                   (input  tBankCIMovement.tdAmountCreditTC * tBankStateAlloc.tdDocCCExchangeRate * tBankStateAlloc.tdDocCCExchangeRateScale (idUnroundedAmount), 
                                                                    input  viCompanyCCId (iiCurrencyID), 
                                                                    input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                                                           else 0.
                                                           
            /* Get the Company ID of the invoice because that is the company where the rate must be retrieved */
            /* so you need to pass it to getexchangerate */
            <Q-55 run CInvoiceByIDForRegistrationNr (all) (Read) (NoCache)
               (input tBankStateAlloc.CInvoice_ID, (CInvoiceID)
                input ?, (CompanyId)
                output dataset tqCInvoiceByIDForRegistrationNr) in BCInvoice>
            find first tqCInvoiceByIDForRegistrationNr where
                       tqCInvoiceByIDForRegistrationNr.tiCInvoice_ID = tBankStateAlloc.CInvoice_ID
                       no-lock no-error.
            if not available tqCInvoiceByIDForRegistrationNr
            then next.
            
            /* Get the exchange rates (TC>LC and TC>CC) for VAT at the date of the banking entry */
            assign vcExchangeRateType = {&EXCHANGERATETYPE-VAT}.
            <M-87 run GetExchangeRate
               (input  tqCInvoiceByIDForRegistrationNr.tiCompany_ID (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  tBankStateAlloc.tcDocCurrencyCode (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyID), 
                input  '':U (icToCurrencyCode), 
                input  0 (iiExchangeRateTypeID), 
                input  vcExchangeRateType (icExchangeRateTypeCode), 
                input  tBankStateLine.BankStateLineValueDate (itValidityDate), 
                output vdBankWHTLCRate (odExchangeRate), 
                output vdBankWHTLCRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper < 0 or viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave WHTBLOCK.
            
            /* PWI */
            assign tBankStateAlloc.BankStateAllocWHTLCRate  = vdBankWHTLCRate
                   tBankStateAlloc.BankStateAllocWHTLCScale = vdBankWHTLCRateScale.
                   
            assign vcExchangeRateType = {&EXCHANGERATETYPE-VAT}.
            <M-53 run GetExchangeRate
               (input  tqCInvoiceByIDForRegistrationNr.tiCompany_ID (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  tBankStateAlloc.tcDocCurrencyCode (icFromCurrencyCode), 
                input  viCompanyCCId (iiToCurrencyID), 
                input  '':U (icToCurrencyCode), 
                input  0 (iiExchangeRateTypeID), 
                input  vcExchangeRateType (icExchangeRateTypeCode), 
                input  tBankStateLine.BankStateLineValueDate (itValidityDate), 
                output vdBankWHTCCRate (odExchangeRate), 
                output vdBankWHTCCRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            if viFcReturnSuper < 0 or viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave WHTBLOCK.
            
            /* PWI */
            assign tBankStateAlloc.BankStateAllocWHTCCRate  = vdBankWHTCCRate
                   tBankStateAlloc.BankStateAllocWHTCCScale = vdBankWHTCCRateScale.
                                 
            /* Create the movement for WHT */                                                       
            create tBankCIMovement.
            assign tBankCIMovement.tiCInvoiceId              = tBankStateAlloc.CInvoice_ID
                   tBankCIMovement.tcGLAccountDivisionCode   = "":U
                   tBankCIMovement.tdAmountCreditTC          = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                               then tBankStateAlloc.BankStateAllocWHTAmtTC
                                                               else 0
                   tBankCIMovement.tdAmountDebitTC           = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                                               then tBankStateAlloc.BankStateAllocWHTAmtTC
                                                               else 0
                   tBankCIMovement.tcPostingRowId            = tBankStateLine.tcPostingRowId
                   tBankCIMovement.tcPostingText             = tBankStateLine.BankStateLineDescription
                   tBankCIMovement.tdCInvoiceHoldAmountTC    = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
                   tBankCIMovement.tlMovementisForDraft      = false
                   tBankCIMovement.tlIsUndoPayment           = false
                   tBankCIMovement.tlMovementIsAboutWHT      = true
                   /* don't forget to pass the payment date, it is needed for cross company postings to know on which date they need to retrieve the rate */
                   tBankCIMovement.ttPaymentDate             = tBankStateLine.BankStateLineValueDate
                   tBankCIMovement.tdMovementDiscountTC      = 0                   
                   tBankCIMovement.tdRateTCLC                = vdBankWHTLCRate
                   tBankCIMovement.tdScaleTCLC               = vdBankWHTLCRateScale
                   tBankCIMovement.tdRateTCCC                = vdBankWHTCCRate
                   tBankCIMovement.tdScaleTCCC               = vdBankWHTCCRateScale.
      
            /* ============================================================================================= */
            /* Get the WHT-number if we had no WHT-number yet for this bank state line + company combination */
            /* ============================================================================================= */
            if viWHTNumber = 0 
            then do :
                assign vhFcComponent = ?.
                <M-44 run GetNumber
                   (input  tBankStateAlloc.Company_ID (iiCompanyId), 
                    input  9999 (iiNumbrYear), 
                    input  'WHT':U (icNumbrType), 
                    output viWHTNumber (oiNumber), 
                    input  viFcCurrentInstanceId (iiInstanceId), 
                    input  vcFcComponentName (icClassName), 
                    output viFcReturnSuper (oiReturnStatus)) in BApplicationProperty>
                if viFcReturnSuper < 0 or viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave WHTBLOCK.                
                if vcReleaseWHTNumberListBE = "" or 
                   vcReleaseWHTNumberListBE = ?
                then assign vcReleaseWHTNumberListBE = string(viWHTNumber).
                else assign vcReleaseWHTNumberListBE = vcReleaseWHTNumberListBE + chr(4) + string(viWHTNumber).
            end. /* if viWHTNumber = 0  */
                                                                                                                          
            /* ============================================================================================ */
            /* create the WHT                                                                               */
            /* make sure the wht number is reset for each company. You only get a wht number if wht is used */
            /* and we have no number yet and it concerns a new document. the number is retrieved from the   */
            /* entity/domain where the invoice is created                                                   */
            /* ============================================================================================ */                    
            create tInvoiceWHTPaymentForBE.
            assign tInvoiceWHTPaymentForBE.tiCInvoiceID              = tBankStateAlloc.CInvoice_ID
                   tInvoiceWHTPaymentForBE.tdAllocatedAmountTC       = tBankStateAlloc.BankStateAllocAmountTC
                   tInvoiceWHTPaymentForBE.tdDiscountAmountTC        = tBankStateAlloc.BankStateAllocDiscountTC
                   tInvoiceWHTPaymentForBE.tdWHTTaxableFeeTC         = tBankStateAlloc.BankStateAllocWHTTxFeeTC
                   tInvoiceWHTPaymentForBE.tdWHTAmountTC             = tBankStateAlloc.BankStateAllocWHTAmtTC
                   tInvoiceWHTPaymentForBE.tlCreateWHT               = true                   
                   tInvoiceWHTPaymentForBE.tiWHTNumber               = viWHTNumber 
                   tInvoiceWHTPaymentForBE.tcPaymentReference        = substring(
                                                                       string(tBankStateLine.tiPostingPeriodYear, "9999") + "/" +
                                                                       trim(tBankStateLine.tcPostingJournalCode) + "/" +
                                                                       string(tBankStateLine.tiPostingVoucher, "999999999"), 1, 40, "CHARACTER")
                   tInvoiceWHTPaymentForBE.tcCurrencyCode            = tBankState.tcCurrencyCode
                   tInvoiceWHTPaymentForBE.tiCDocumentInvoiceXrefID  = ?
                   tInvoiceWHTPaymentForBE.tiBankStateAllocID        = tBankStateAlloc.BankStateAlloc_ID
                   tInvoiceWHTPaymentForBE.tiOIAjustLnID             = ?
                   tInvoiceWHTPaymentForBE.ttPostingDate             = tBankState.BankStatePostingDate
                   tInvoiceWHTPaymentForBE.tcWHTStatus               = {&WHTSTATUS-DUE}
                   tInvoiceWHTPaymentForBE.ttSettlementDate          = ?
                   tInvoiceWHTPaymentForBE.tlUpdateAllAmounts        = false
                   tInvoiceWHTPaymentForBE.tdPaymentWHTRateTCLC      = tBankStateAlloc.BankStateAllocWHTLCRate
                   tInvoiceWHTPaymentForBE.tdPaymentWHTScaleTCLC     = tBankStateAlloc.BankStateAllocWHTLCScale
                   tInvoiceWHTPaymentForBE.tdPaymentWHTRateTCCC      = tBankStateAlloc.BankStateAllocWHTCCRate
                   tInvoiceWHTPaymentForBE.tdPaymentWHTScaleTCCC     = tBankStateAlloc.BankStateAllocWHTCCScale
                   tInvoiceWHTPaymentForBE.tdPaymentAccRateTCLC      = tBankStateAlloc.BankStateAllocBankRate
                   tInvoiceWHTPaymentForBE.tdPaymentAccScaleTCLC     = tBankStateAlloc.BankStateAllocBankRteScale
                   tInvoiceWHTPaymentForBE.tdPaymentAccRateTCCC      = tBankStateAlloc.BankStateAllocCCRate
                   tInvoiceWHTPaymentForBE.tdPaymentAccScaleTCCC     = tBankStateAlloc.BankStateAllocCCScale
                   tInvoiceWHTPaymentForBE.tiPaymentAccountingYear   = tBankState.tiPeriodYear
                   tInvoiceWHTPaymentForBE.tiPaymentAccountingPeriod = tBankState.tiPeriodPeriod.

            /* =================================================================================================== */           
            /* keep the total of the wht amounts in lc and cc, to see if there are differences with the movements, */
            /* if so, then exchange gain/losses are made                                                           */
            /* only do that for fees because for expenses, no postings are made                                    */
            /* =================================================================================================== */
            assign vdTotWhtAmntTC      = 0
                   vdTotDebitAmountLC  = 0
                   vdTotCreditAmountLC = 0
                   vdTotDebitAmountCC  = 0
                   vdTotCreditAmountCC = 0
                   vdDebitAmountTC     = 0
                   vdCreditAmountTC    = 0.
                   
            <Q-79 run CInvoiceWHTByCInvoice (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input tBankStateAlloc.CInvoice_ID, (CInvoiceId)
                input ?, (UpdateTax)
                output dataset tqCInvoiceWHTByCInvoice) in BCInvoice>
            for each tqCInvoiceWHTByCInvoice where
                     tqCInvoiceWHTByCInvoice.tiCInvoice_ID = tBankStateAlloc.CInvoice_ID :
                  assign vdTotWhtAmntTC = vdTotWHTAmntTC + abs(tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtDebitTC - tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtCreditTC).
            end.
            
            for each tqCInvoiceWHTByCInvoice where
                     tqCInvoiceWHTByCInvoice.tiCInvoice_ID            = tBankstateAlloc.CInvoice_ID and
                     tqCInvoiceWHTByCInvoice.tdCInvoiceWHTFeeDebitTC  <> 0 or 
                     tqCInvoiceWHTByCInvoice.tdCInvoiceWHTFeeCreditTC <> 0
                     no-lock:
                     
                /* if allocated amount is credited, then WHT must be debited, and vice versa */
                assign vdDebitAmountTC     = if tBankstateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                             then (tBankStateAlloc.BankStateAllocWHTAmtTC * 
                                                   abs(tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtDebitTC - tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtCreditTC)/ vdTotWhtAmntTC)
                                             else 0
                       vdCreditAmountTC    = if tBankstateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                             then (tBankstateAlloc.BankstateAllocWHTAmtTC * 
                                                   abs(tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtDebitTC - tqCInvoiceWHTByCInvoice.tdCInvoiceWHTAmtCreditTC) / vdTotWhtAmntTC)
                                             else 0
                       vdDebitAmountTC     = if vdDebitAmountTC <> 0
                                             then <M-4 RoundAmount
                                                     (input  vdDebitAmountTC (idUnroundedAmount), 
                                                      input  ? (iiCurrencyID), 
                                                      input  tBankState.tcCurrencyCode (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdCreditAmountTC    = if vdCreditAmountTC <> 0
                                             then <M-45 RoundAmount
                                                     (input  vdCreditAmountTC (idUnroundedAmount), 
                                                      input  ? (iiCurrencyID), 
                                                      input  tBankState.tcCurrencyCode (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdDebitAmountLC     = if vdDebitAmountTC <> 0
                                             then <M-85 RoundAmount
                                                     (input  vdDebitAmountTC * tBankstateAlloc.BankstateAllocWHTLCRate * tBankstateAlloc.BankstateAllocWHTLCScale (idUnroundedAmount), 
                                                      input  viCompanyLCId (iiCurrencyID), 
                                                      input  vcCompanyLC (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdCreditAmountLC    = if vdCreditAmountTC <> 0
                                             then <M-29 RoundAmount
                                                     (input  vdCreditAmountTC * tBankstateAlloc.BankstateAllocWHTLCRate * tBankstateAlloc.BankstateAllocWHTLCScale (idUnroundedAmount), 
                                                      input  viCompanyLCId (iiCurrencyID), 
                                                      input  vcCompanyLC (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdDebitAmountCC     = if vdDebitAmountTC <> 0
                                             then <M-78 RoundAmount
                                                     (input  vdDebitAmountTC * tBankstateAlloc.BankstateAllocWHTCCRate * tBankstateAlloc.BankstateAllocWHTCCScale (idUnroundedAmount), 
                                                      input  viCompanyCCId (iiCurrencyID), 
                                                      input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdCreditAmountCC    = if vdCreditAmountTC <> 0
                                             then <M-62 RoundAmount
                                                     (input  vdCreditAmountTC * tBankstateAlloc.BankstateAllocWHTCCRate * tBankstateAlloc.BankstateAllocWHTCCScale (idUnroundedAmount), 
                                                      input  viCompanyCCId (iiCurrencyID), 
                                                      input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                                             else 0
                       vdTotDebitAmountLC  = vdTotDebitAmountLC + vdDebitAmountLC
                       vdTotCreditAmountLC = vdTotCreditAmountLC + vdCreditAmountLC
                       vdTotDebitAmountCC  = vdTotDebitAmountCC + vdDebitAmountCC
                       vdTotCreditAmountCC = vdTotCreditAmountCC + vdCreditAmountCC.
            end. /* end for each tqCInvoiceWHTByCInvoice */
        end. /* if tBankStateAlloc.BankStateAllocWHTAmtTC <> 0 and */   
        
        /* automatically post realized exchange gain/loss for LC and CC values */
        if (tBankStateAlloc.tdDocExchangeRate <> tBankStateAlloc.BankStateAllocWHTLCRate and
            ((vdTotCreditAmountLC                     <> 0 and
              vdMovementDebitAmountLC                 <> 0 and
              vdTotCreditAmountLC                     <> vdMovementDebitAmountLC) or
             (vdTotDebitAmountLC                      <> 0 and
              vdMovementCreditAmountLC                <> 0 and
              vdTotDebitAmountLC                      <> vdMovementCreditAmountLC)))    or
           (tBankStateAlloc.tdDocCCExchangeRate <> tBankStateAlloc.BankStateAllocWHTCCRate and
            ((vdTotCreditAmountCC                     <> 0 and
              vdMovementDebitAmountCC                 <> 0 and
              vdTotCreditAmountCC                     <> vdMovementDebitAmountCC) or
             (vdTotDebitAmountCC                      <> 0 and
              vdMovementCreditAmountCC                <> 0 and
             vdTotDebitAmountCC                      <> vdMovementCreditAmountCC)))
        then do:
            <M-26 run AddPostingLineExchRateGainLoss
               (input  tBankStateLine.tcPostingRowId (icPostingtcRowId), 
                input  {&EXCHANGERATETYPE_REALIZED} (icExchangeRateGainLoseType), 
                input  vdTotCreditAmountLC - vdMovementDebitAmountLC (idDeltaDebitLC), 
                input  vdTotDebitAmountLC - vdMovementCreditAmountLC (idDeltaCreditLC), 
                input  vdTotCreditAmountCC - vdMovementDebitAmountCC (idDeltaDebitCC), 
                input  vdTotDebitAmountCC - vdMovementCreditAmountCC (idDeltaCreditCC), 
                input  tBankStateAlloc.tdDocExchangeRate (idLCExchangeRate), 
                input  tBankStateAlloc.tdDocExchangeRateScale (idLCExchangeRateScale), 
                input  tBankStateAlloc.tdDocCCExchangeRate (idCCExchangeRate), 
                input  tBankStateAlloc.tdDocCCExchangeRateScale (idCCExchangeRateScale), 
                input  'WHT ':U (icPostingLineTextPre), 
                input  tBankStateLine.BankStateLineValueDate (itExchangeRateDate), 
                input  tBankStateAlloc.tcDocCurrencyCode (icCurrencyCode), 
                output viDummy (oiLCPostingLine_ID), 
                output viDummy (oiCCPostingLine_ID), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper < 0 or viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave WHTBLOCK.
        end. /* end tBankStateAlloc.tdDocCCExchangeRate  <> tBankStateAlloc.BankStateAllocWHTCCRate  .... */
    
    end. /* for each tBankStateAlloc where */
end. /* end block */

/* ======================================================= */
/* Set return-status = OK                                  */
/* ======================================================= */
assign oiReturnStatus = viLocalReturnStatus.