project QadFinancials > class BBankEntry > method AdditionalUpdatesInvPrePayments

Description

AdditionalUpdatesInvPrePayments: method that takes creates the prepayments


Parameters


iiPostingPeriodYearinputintegerPosting year
iiPostingPeriodPeriodinputintegerPosting Period
icPostingJournalCodeinputcharacterPosting Journal Code
iiPostingVoucherinputintegerPosting voucher
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdatesInv


program code (program4/bbankentry.p)

/* =================================================== */
    /* This method is a submethod of AddtionaUpdatesInv as */
    /* that method could not contain all the program-code  */
    /* =================================================== */
    
    /* ========================= */
    /* Set default return-status */
    /* ========================= */
    assign oiReturnStatus = -98.
    
    /* ================================================= */
    /* Empty the table that is the base for a prepayment */
    /* ================================================= */
    empty temp-table tBankOpenBalance.
    
    /* ============================================================================ */
    /* Go though all allocations of the current BE-Line that refer to a pre-payment */
    /* ============================================================================ */
    for each tBankStateAlloc where
             tBankStateAlloc.tc_ParentRowid   = tBankStateLine.tc_Rowid     and
             (tBankStateAlloc.tc_Status       = "N":U or
              tBankStateAlloc.tc_Status       = "C":U)                      and
             (tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NCINV} or
              tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NDINV} or
              tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NEINV}) :
        
        /* ================================================= */
        /* Create a record in the base-table for prepayments */
        /* ================================================= */
        create tBankOpenBalance.
        assign  tBankOpenBalance.tiCompanyId             = tBankStateAlloc.Company_ID
                tBankOpenBalance.tcAction                = {&DAEMONACTION-SAVE}
                tBankOpenBalance.tcInvoiceType           = {&INVOICETYPE-PREPAYMENT}
                tBankOpenBalance.tcTransferAccountGLCode = tBankState.tcGLCode
                tBankOpenBalance.tiPeriodYear            = iiPostingPeriodYear
                tBankOpenBalance.tiPeriodPeriod          = iiPostingPeriodPeriod
                tBankOpenBalance.tcJournalCode           = icPostingJournalCode
                tBankOpenBalance.tiInvoiceVoucher        = iiPostingVoucher
                tBankOpenBalance.ttInvoiceDate           = tBankStateAlloc.ttDocInvDate
                tBankOpenBalance.ttInvoiceTaxPointDate   = tBankStateAlloc.ttDocInvDate
                tBankOpenBalance.tcCreditorCode          = if tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NCINV} then tBankStateAlloc.tcDocCredDebEmplCode else "":U
                tBankOpenBalance.tcDebtorCode            = if tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NDINV} then tBankStateAlloc.tcDocCredDebEmplCode else "":U
                tBankOpenBalance.tcInvoiceDescription    = tBankStateAlloc.tcDocDescription
                tBankOpenBalance.tcDivisionCode          = tBankStateAlloc.tcDocDivisionCode
                tBankOpenBalance.tcProjectCode           = tBankStateAlloc.tcDocProjectCode
                tBankOpenBalance.tcCostCentreCode        = tBankStateAlloc.tcDocCostCentreCode
                tBankOpenBalance.ttPostingDate           = tBankStateLine.BankStateLinePostingDate
                tBankOpenBalance.tcPostingType           = if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT} then {&POSTINGTYPE-DEBIT}
                                                           else if tBankStateAlloc.tcAllocAmountCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                                                then {&POSTINGTYPE-CREDIT} 
                                                                else "":U
                tBankOpenBalance.tdInvoiceAmountTC       = tBankStateAlloc.BankStateAllocAmountTC
                tBankOpenBalance.tdInvoiceAmountLC       = tBankStateAlloc.BankStateAllocAmountLC
                tBankOpenBalance.tdExchangeRateTCLC      = (if tBankStateAlloc.tcDocCurrencyCode = vcCompanyLC and
                                                               (tBankStateAlloc.tdDocExchangeRate  = 0 or
                                                                tBankStateAlloc.tdDocExchangeRate  = ?)
                                                            then 1
                                                            else tBankStateAlloc.tdDocExchangeRate)
                tBankOpenBalance.tdExchangeRateScale     = (if tBankStateAlloc.tcDocCurrencyCode = vcCompanyLC and
                                                               (tBankStateAlloc.tdDocExchangeRateScale   = 0 or
                                                                tBankStateAlloc.tdDocExchangeRateScale = ?)
                                                            then 1
                                                            else tBankStateAlloc.tdDocExchangeRateScale)
                tBankOpenBalance.tiInvoiceVoucher        = tBankStateLine.tiPostingVoucher
                tBankOpenBalance.tcPostingText           = tBankStateLine.BankStateLineDescription
                tBankOpenBalance.tlOnlyControlAccount    = true
                vlBankOpenBalanceCInvoice                = tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NCINV}
                vlBankOpenBalanceDInvoice                = tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NDINV}
                tBankOpenBalance.tcTCCurrencyCode        = tBankStateAlloc.tcDocCurrencyCode
                tBankOpenBalance.tcKey                   = string(rowid(tBankOpenBalance))
                vcPostingOriginAddressCode               = tBankStateAlloc.tcDocCredDebEmplCode.

        
        /* =============================== */
        /* Create CC Record                */
        /* =============================== */
        if vlDomainIsStatutory <> true
        then assign tBankOpenBalance.tdInvoiceAmountCC     = tBankOpenBalance.tdInvoiceAmountLC
                    tBankOpenBalance.tdExchangeRateTCCC    = tBankOpenBalance.tdExchangeRateTCLC
                    tBankOpenBalance.tdCCExchangeRateScale = tBankOpenBalance.tdExchangeRateScale.
        else do:
            if tBankStateAlloc.tcDocCurrencyCode = vcCompanyCC
            then assign tBankOpenBalance.tdInvoiceAmountCC     = tBankOpenBalance.tdInvoiceAmountTC
                        tBankOpenBalance.tdExchangeRateTCCC    = (if tBankStateAlloc.BankStateAllocCCRate = 0 or
                                                                     tBankStateAlloc.BankStateAllocCCRate = ?
                                                                  then 1
                                                                  else tBankStateAlloc.BankStateAllocCCRate)
                        tBankOpenBalance.tdCCExchangeRateScale = (if tBankStateAlloc.BankStateAllocCCScale = 0 or
                                                                     tBankStateAlloc.BankStateAllocCCScale = ?
                                                                  then 1
                                                                  else tBankStateAlloc.BankStateAllocCCScale)
                        tBankOpenBalance.tcCCCurrencyCode      = tBankOpenBalance.tcTCCurrencyCode.
            else do:
                if vcCompanyLC = vcCompanyCC
                then assign tBankOpenBalance.tdInvoiceAmountCC     = tBankOpenBalance.tdInvoiceAmountLC
                            tBankOpenBalance.tdExchangeRateTCCC    = tBankOpenBalance.tdExchangeRateTCLC
                            tBankOpenBalance.tdCCExchangeRateScale = tBankOpenBalance.tdExchangeRateScale.
                else do:
                    
                    <M-42 run GetExchangeRate
                       (input  tBankStateLine.Company_ID (iiCompanyID), 
                        input  ? (iiFromCurrencyID), 
                        input  tBankStateAlloc.tcDocCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyCCId (iiToCurrencyID), 
                        input  ? (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                        input  tBankStateAlloc.ttDocInvDate (itValidityDate), 
                        output vdCCRate (odExchangeRate), 
                        output vdCCRateScale (odExchangeScaleFactor), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                    if viFcReturnSuper < 0 or
                       oiReturnStatus  = 0
                    then assign oiReturnStatus = viFcReturnSuper.
        
                    if viFcReturnSuper < 0
                    then return.
                    
                    assign tBankOpenBalance.tdInvoiceAmountCC     = <M-41 RoundAmount
                                                                       (input  tBankOpenBalance.tdInvoiceAmountTC * vdCCRate * vdCCRateScale (idUnroundedAmount), 
                                                                        input  viCompanyCCId (iiCurrencyID), 
                                                                        input  vcCompanyCC (icCurrencyCode)) in BBankEntry>
                           tBankOpenBalance.tdExchangeRateTCCC    = vdCCRate
                           tBankOpenBalance.tdCCExchangeRateScale = vdCCRateScale.
                end. /* else do: */
            end. /* else do: */
        end. /* IF  vcCompanyCC = "":U or vcCompanyCC = ? */  

        /* =============================== */
        /* Extended assignments for the CI */
        /* =============================== */
        if tBankStateAlloc.BankStateAllocType    = {&BANKSTATEALLOCTYPE-NCINV}  and
           tBankStateAlloc.tcDocCredDebEmplCode <> "":U
        then do:

            /*Set up credit terms for prepayments per domain(PrepayPaymentCondition_ID), so that when prepayments are created these credit terms 
            are used instead of the customer's default credit terms*/

            assign vhFcComponent = ?
                   vcPaymentConditionCode = '':U.

            <M-6 run GetDeductPrepayCreditTerm
               (input  tBankOpenBalance.tcInvoiceType (icInvoiceType), 
                output vcPaymentConditionCode (ocPaymentConditionCode), 
                output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>

            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0) then
                assign oiReturnStatus = viFcReturnSuper.
       
            if viFcReturnSuper < 0
                then return.

            assign tBankOpenBalance.tcNormalPaymentConditionCode = vcPaymentConditionCode.
            if vcPaymentConditionCode <> '':U and vcPaymentConditionCode <> ? then
                tBankOpenBalance.ttInvoiceDueDate = tBankStateAlloc.ttDocDueDate.



            <Q-1 run CreditorByCreditor (first) (Read) (NoCache)
               (input ?, (CreditorId)
                input tBankStateAlloc.Company_ID, (CompanyId)
                input tBankStateAlloc.tcDocCredDebEmplCode, (CreditorCode)
                input ?, (AddressType)
                output dataset tqCreditorByCreditor) in BCreditor >
            find first tqCreditorByCreditor no-error.
            if available tqCreditorByCreditor
            then do:
                if (tBankOpenBalance.tcNormalPaymentConditionCode = "":U or
                   tBankOpenBalance.tcNormalPaymentConditionCode = ?) then
                   do:
                       assign tBankOpenBalance.tcNormalPaymentConditionCode   = tqCreditorByCreditor.tcNormalPaymentConditionCode
                           tBankOpenBalance.ttInvoiceDueDate               = tBankStateAlloc.ttDocDueDate
                           tBankOpenBalance.ttInvoiceDiscountDueDate       = ?.
                        /* prepayment should use no stage, no discount payment condition */
                        if tqCreditorByCreditor.tcNormalPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} then
                        do:
                            <Q-52 run PaymentConditionByProperties (all) (Read) (NoCache)
                               (input ?, (PaymentCondition_ID)
                                input ?, (PaymentConditionCode)
                                input true, (PaymentConditionIsActive)
                                input {&PAYMENTCONDITIONPAYMENTTYPE-NORMAL}, (PaymentConditionPaymentType)
                                input ?, (PaymentConditionPdDiscType)
                                input 0, (PaymentConditionPercentage)
                                output dataset tqPaymentConditionByProperties) in BPaymentCondition>
                            find first tqPaymentConditionByProperties no-error.
                            if available tqPaymentConditionByProperties then
                                assign tBankOpenBalance.tcNormalPaymentConditionCode = tqPaymentConditionByProperties.tcPaymentConditionCode.
                        end.    
                   end.

                assign tBankOpenBalance.tcInvoiceReference             = string(tBankState.tiPeriodYear, "9999":U) + "/":U + tBankStateLine.tcPostingJournalCode + "/":U + tBankState.BankStateNumber
                       tBankOpenBalance.tcInvoiceReference             = substring(tBankOpenBalance.tcInvoiceReference,1,22,"CHARACTER").

                if tBankOpenBalance.ttInvoiceDueDate              = ? and
                   tBankOpenBalance.tcNormalPaymentConditionCode <> "":U
                then do:
                    <Q-2 run PaymentConditionByIdCode (all) (Read) (NoCache)
                       (input ?, (PaymentConditionId)
                        input tBankOpenBalance.tcNormalPaymentConditionCode, (PaymentConditionCode)
                        output dataset tqPaymentConditionByIdCode) in BPaymentCondition>
                    find first tqPaymentConditionByIdCode no-error.
                    if available tqPaymentConditionByIdCode
                    then do:
                        assign vhFcComponent = ?.
                        assign vtInvoiceDueDate         = ?
                               vtInvoiceDiscountDueDate = ?.
                            <M-20 run CalculateDueDiscountDate
                               (input  tqPaymentConditionByIdCode.tiPaymentCondition_ID (iiPaymentConditionID), 
                                input  tqPaymentConditionByIdCode.tcPaymentConditionCode (icPaymentConditionCode), 
                                input  tBankStateAlloc.ttDocInvDate (itInvoiceDate), 
                                output vtInvoiceDueDate (otDueDate), 
                                output vtInvoiceDiscountDueDate (otDiscountDate), 
                                output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>
                        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
                        if viFcReturnSuper < 0 
                        then return.

                        if tBankOpenBalance.ttInvoiceDueDate = ?
                        then assign tBankOpenBalance.ttInvoiceDueDate = vtInvoiceDueDate.
                    end. /* if available tqPaymentConditionByIdCode */
                end.
                /* if tBankOpenBalance.tcDiscountPaymentConditionCode <> "":U */
            end. /* if available tqCreditorByCreditor */
        end. /* if tBankStateAlloc.BankStateAllocType    = {&BANKSTATEALLOCTYPE-NCINV}  and */
        
        /* =============================== */
        /* Extended assignments for the DI */
        /* =============================== */
        if tBankStateAlloc.BankStateAllocType    = {&BANKSTATEALLOCTYPE-NDINV} and
           tBankStateAlloc.tcDocCredDebEmplCode <> "":U
        then do:
        
            /*Set up credit terms for prepayments per domain(PrepayPaymentCondition_ID), so that when prepayments are created these credit terms 
            are used instead of the customer's default credit terms*/
            assign vhFcComponent = ?
                   vcPaymentConditionCode = '':U.

            <M-44 run GetDeductPrepayCreditTerm
               (input  tBankOpenBalance.tcInvoiceType (icInvoiceType), 
                output vcPaymentConditionCode (ocPaymentConditionCode), 
                output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>

            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0) then
                assign oiReturnStatus = viFcReturnSuper.
       
            if viFcReturnSuper < 0
                then return.

            assign tBankOpenBalance.tcNormalPaymentConditionCode = vcPaymentConditionCode.
            if vcPaymentConditionCode <> '':U and vcPaymentConditionCode <> ? then
                tBankOpenBalance.ttInvoiceDueDate = tBankStateAlloc.ttDocDueDate.

/*
            <Q-94 run DomainCreditTermByDomain (all) (Read) (NoCache)
                 (input viDomainID, (Domain_ID)
                  output dataset tqDomainCreditTermByDomain) in BDomainProperty>
           
            find first tqDomainCreditTermByDomain no-error.
            if available tqDomainCreditTermByDomain and  tqDomainCreditTermByDomain.tcPrePaymentConditionCode <> ? and tqDomainCreditTermByDomain.tcPrePaymentConditionCode <> '':U then
            do:
                assign tBankOpenBalance.tcNormalPaymentConditionCode = tqDomainCreditTermByDomain.tcPrePaymentConditionCode
                       tBankOpenBalance.ttInvoiceDueDate = tBankStateAlloc.ttDocDueDate.
            end.
            else
                assign tBankOpenBalance.tcNormalPaymentConditionCode = '':U.
*/
        
            <Q-4 run DebtorByDebtor (all) (Read) (NoCache)
               (input tBankStateAlloc.Company_ID, (CompanyId)
                input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
                input ?, (DebtorId)
                input tBankStateAlloc.tcDocCredDebEmplCode, (DebtorCode)
                output dataset tqDebtorByDebtor) in BDebtor>

            find first tqDebtorByDebtor no-error.
            if available tqDebtorByDebtor
            then do:

                if (tBankOpenBalance.tcNormalPaymentConditionCode = "":U or
                   tBankOpenBalance.tcNormalPaymentConditionCode = ?) then
                do:
                   assign tBankOpenBalance.tcNormalPaymentConditionCode = tqDebtorByDebtor.tcNormalPaymentConditionCode
                          tBankOpenBalance.ttInvoiceDueDate             = tBankStateAlloc.ttDocDueDate
                          tBankOpenBalance.ttInvoiceDiscountDueDate     = ?.

                    /* prepayment should use no stage, no discount payment condition */
                    if tqDebtorByDebtor.tcNormalPaymentType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} then
                    do:
                        <Q-75 run PaymentConditionByProperties (all) (Read) (NoCache)
                           (input ?, (PaymentCondition_ID)
                            input ?, (PaymentConditionCode)
                            input true, (PaymentConditionIsActive)
                            input {&PAYMENTCONDITIONPAYMENTTYPE-NORMAL}, (PaymentConditionPaymentType)
                            input ?, (PaymentConditionPdDiscType)
                            input 0, (PaymentConditionPercentage)
                            output dataset tqPaymentConditionByProperties) in BPaymentCondition>
                        find first tqPaymentConditionByProperties no-error.
                        if available tqPaymentConditionByProperties then
                            assign tBankOpenBalance.tcNormalPaymentConditionCode = tqPaymentConditionByProperties.tcPaymentConditionCode.
                    end.

                   if tBankOpenBalance.ttInvoiceDueDate         = ? and
                       tBankOpenBalance.tcNormalPaymentConditionCode <> "":U
                   then do:
                      assign vhFcComponent            = ?
                           vtInvoiceDueDate         = ?
                           vtInvoiceDiscountDueDate = ?.

                       <M-21 run CalculateDueDiscountDate
                          (input  ? (iiPaymentConditionID), 
                           input  tBankOpenBalance.tcNormalPaymentConditionCode (icPaymentConditionCode), 
                           input  tBankStateAlloc.ttDocInvDate (itInvoiceDate), 
                           output vtInvoiceDueDate (otDueDate), 
                           output vtInvoiceDiscountDueDate (otDiscountDate), 
                           output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>

                        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
                        if viFcReturnSuper < 0 
                        then return.

                        if tBankOpenBalance.ttInvoiceDueDate = ?
                        then assign tBankOpenBalance.ttInvoiceDueDate = vtInvoiceDueDate.

                    end. /* if tBankOpenBalance.tcDiscountPaymentConditionCode <> "":U */
                end. /* if available tqDebtorByDebtor*/
            end. /* if available tqDebtorByDebtor */
        end. /* if tBankStateAlloc.BankStateAllocType    = {&BANKSTATEALLOCTYPE-NDINV} and */
          
    end. /* for each tBankStateAlloc where*/
    
    /* ====================================== */
    /* Create the CI-pre-payments when needed */
    /* ====================================== */
    if can-find(first tBankOpenBalance) and vlBankOpenbalanceDInvoice = TRUE
    then do:
        <M-7 run CreateDInvoices
           (input-output tBankOpenBalance (tDIOpenBalance), 
            input  viBJournalEntryBEID (iiBJournalEntryId), 
            input  tBankStateLine.Posting_ID (iiPostingId), 
            output vcNewRecordInfo (ocNewRecordInfo), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then do:
            assign vcMessage      = trim(substitute(#T-11'Error &1 occurred when creating the new customer invoice for bank statement line &2. See other messages for more information.':150(317)T-11#, string(viFcReturnSuper),string(tBankStateLine.BankStateLineNumber))).
            <M-8 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-4288':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end. /* if viFcReturnSuper < 0  */
        find first tBankOpenBalance where
                   tBankOpenBalance.tcKey = string(to-rowid(entry(1,vcNewRecordInfo)))
                   no-error.
        if available tBankOpenBalance
        then do:
            find first tBankStateAlloc where
                       tBankStateAlloc.tc_ParentRowid     =  tBankStateLine.tc_Rowid     and
                       tBankStateAlloc.BankStateAllocType =  {&BANKSTATEALLOCTYPE-NDINV} and
                       (tBankStateAlloc.tc_Status      = "N":U or
                        tBankStateAlloc.tc_Status      = "C":U)  no-error.
            if available tBankStateAlloc
            then assign tBankStateAlloc.DInvoice_ID = integer(entry(2,vcNewRecordInfo)) no-error.
            if error-status:error
            then do :
                assign oiReturnStatus = -3.
                       vcMessage = trim(substitute(#T-13'An internal error occurred while linking the allocation to the correct supplier prepayment.':255(17368)T-13#)).
                if ERROR-STATUS:NUM-MESSAGES > 0
                then assign vcMessage = vcMessage + chr(10) + 
                                        trim(substitute(#T-14'Detailed info: &1 (&2).':255(466)T-14#,ERROR-STATUS:GET-MESSAGE(1),string(ERROR-STATUS:GET-NUMBER(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-4291':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                Return.
            end. /* if error-status:error */
        end. /* if available tBankOpenBalance */
    end. /* if can-find(first tBankOpenBalance) and vlBankOpenbalanceDInvoice = TRUE */
    
    /* ====================================== */
    /* Create the DI-pre-payments when needed */
    /* ====================================== */
    if can-find(first tBankOpenBalance) and vlBankOpenbalanceCInvoice = TRUE
    then do:
        <M-9 run CreateCInvoices
           (input-output tBankOpenBalance (tOpenBalanceCI), 
            input  viBJournalEntryBEID (iiBJournalEntryId), 
            input  tBankStateLine.Posting_ID (iiPostingId), 
            input  ? (icPONbrInv), 
            output vcNewRecordInfo (ocNewRecordInfo), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then do:
            assign vcMessage      = trim(substitute(#T-12'Error &1 occurred when creating the new supplier invoice for bank statement line &2. See other messages for more information.':150(318)T-12#, string(viFcReturnSuper), string(tBankStateLine.BankStateLineNumber))).
            <M-10 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-4289':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
            Return.
        end. /* if viFcReturnSuper < 0  */ 
        find first tBankOpenBalance where
                   tBankOpenBalance.tcKey = string(to-rowid(entry(1,vcNewRecordInfo)))
                   no-error.
        if available tBankOpenBalance
        then do:
            find first tBankStateAlloc where
                   tBankStateAlloc.tc_ParentRowid     = tBankStateLine.tc_Rowid     and
                   tBankStateAlloc.BankStateAllocType = {&BANKSTATEALLOCTYPE-NCINV} and
                   (tBankStateAlloc.tc_Status      = "N":U or
                    tBankStateAlloc.tc_Status      = "C":U) no-error.
            if available tBankStateAlloc
            then assign tBankStateAlloc.CInvoice_ID = integer(entry(2,vcNewRecordInfo)) no-error.
            if error-status:error
            then do :
                assign oiReturnStatus = -3.
                       vcMessage = trim(substitute(#T-18'An internal error occurred while linking the allocation to the correct customer prepayment.':255(17369)T-18#)).
                if ERROR-STATUS:NUM-MESSAGES > 0
                then assign vcMessage = vcMessage + chr(10) + 
                                        trim(substitute(#T-19'Detailed info: &1 (&2).':255(466)T-19#,ERROR-STATUS:GET-MESSAGE(1),string(ERROR-STATUS:GET-NUMBER(1)))).
                <M-17 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-4292':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
                Return.
            end. /* if error-status:error */
        end. /* if available tBankOpenBalance */
    end. /* if can-find(first tBankOpenBalance) and vlBankOpenbalanceCInvoice = TRUE */
    
    /* update posting origin Address Code */
    <M-22 run UpdatePostingOriginAddressCode
       (input  tBankStateLine.tcPostingRowId (icPostingRowId), 
        input  vcPostingOriginAddresscode (icPostingOriginAddressCode), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 
    then do:
        assign vcMessage      = trim(substitute(#T-24'Error &1 occurred when updating the posting header for bank state line (&2). See other messages for more information.':150(59315)T-24#, string(viFcReturnSuper), string(tBankStateLine.BankStateLineNumber))).
        <M-23 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-5967':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BBankEntry>
        Return.
    end. /* if viFcReturnSuper < 0  */ 
    
    /* ========================= */
    /* Set return-status = OK    */
    /* ========================= */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.