project QadFinancials > class BOpenItemAdjustment > method OIAdjCommitSubDInvoice

Description

OIAdjCommitDInvoice; submethod of IAdjCommit because it could not contain all the code


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BOpenItemAdjustment.OIAdjCommitSub


program code (program8/bopenitemadjustment.p)

    
    /* ==================================================== */
    /* Return in case no actions needed - clear temp-tables */
    /* Exception handling                                   */
    /* ==================================================== */
    if not can-find (first tOIAdj where tOIAdj.tcInvoiceOrigin = {&INVOICEORIGIN-DEBTOR})
    then Return.
    empty temp-table tOIDMovement.
    empty temp-table tOIDMovementStage.
    empty temp-table tOIOpenBalance.
    empty temp-table tDefaultSafsFromOIAdj.
    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0.
           
    /* ====================== */
    /* Start / Open BDInvoice */
    /* ====================== */
    if viBDInvoiceInOIAID = 0 or viBDInvoiceInOIAID = ?
    then do :
        <I-1 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BDInvoice"}>
        assign vlBDInvoiceIsStartedFromOIAdj = true.
    end. /* if viBDInvoiceInOIAID = 0 */
    else do :
        <I-2 {bFcOpenInstance
            &CLASS           = "BDInvoice"}>
    end. /* if viBDInvoiceInOIAID <> 0 */
    
    /* =========================== */
    /* Start Main processing block */
    /* =========================== */
    DIBLOCK : DO :
        
        /* ====================== */
        /* Actions for a new DI   */
        /* ====================== */
        find first tOIAdj where 
                   tOIAdj.tlIsNewOpenItem = true and 
                   tOIAdj.tcInvoiceOrigin = {&INVOICEORIGIN-DEBTOR}
                   no-lock no-error.
        if available tOIAdj
        then do : 
            create tOIOpenBalance.
            assign tOIOpenBalance.tiCompanyId           = tOIAdj.tiCompanyId
                   tOIOpenBalance.tlOnlyControlAccount  = true
                   tOIOpenBalance.tcAction              = {&DAEMONACTION-SAVE}
                   tOIOpenBalance.tcInvoiceType         = tOIAdj.tcInvoiceType
                   tOIOpenBalance.tiPeriodYear          = viMainPostingYear
                   tOIOpenBalance.tiPeriodPeriod        = viMainPostingPeriod
                   tOIOpenBalance.tcJournalCode         = vcMainPostingJournalCode
                   tOIOpenBalance.tiInvoiceVoucher      = viMainPostingVoucher
                   tOIOpenBalance.tcDebtorCode          = tOIAdj.tcCredDebEmplCode
                   tOIOpenBalance.ttPostingDate         = vtMainPostingDate
                   tOIOpenBalance.tcPostingText         = trim(#T-20'Open Item Adjustment':30(2900)t-20#)
                   tOIOpenBalance.tcPostingType         = (if tOIAdj.tcNewBalanceCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
                                                          then {&POSTINGTYPE-CREDIT}
                                                          else if tOIAdj.tcNewBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
                                                                then {&POSTINGTYPE-DEBIT}
                                                                else tOIAdj.tcNewBalanceCrDt)
                   tOIOpenBalance.tdInvoiceAmountTC     = tOIAdj.tdAdjustmentAmountTC
                   tOIOpenBalance.tdInvoiceAmountLC     = tOIAdj.tdAdjustmentAmountLC
                   tOIOpenBalance.tcTCCurrencyCode      = tOIAdj.tcInvoiceCurrencyCode
                   tOIOpenBalance.ttInvoiceDueDate      = tOIAdj.ttInvoiceNormalDueDate
                   tOIOpenBalance.ttInvoiceDate         = tOIAdj.ttInvoiceDate
                   tOIOpenBalance.ttInvoiceTaxPointDate = tOIAdj.ttInvoiceDate  
                   tOIOpenBalance.tdExchangeRateTCLC    = (if tOIAdj.tcInvoiceCurrencyCode = vcCompanyLC and 
                                                              (tOIAdj.tdAdjustmentExchangeRate = 0 or 
                                                               tOIAdj.tdAdjustmentExchangeRate = ?)
                                                           then 1
                                                           else tOIAdj.tdAdjustmentExchangeRate)
                   tOIOpenBalance.tdExchangeRateScale   = tOIAdj.tdAdjustmentExchangeRateScale
                   tOIOpenBalance.tcInvoiceReference    = (if tOIOpenBalance.tcInvoiceReference = ? or 
                                                              tOIOpenBalance.tcInvoiceReference = "":U
                                                           then STRING(viMainPostingYear) + '/' +
                                                                vcMainPostingJournalCode + '/' +
                                                                STRING(viMainPostingVoucher,"999999")
                                                           else tOIOpenBalance.tcInvoiceReference)
                   tOIOpenBalance.tcInvoiceDescription     = tOIAdj.tcInvoiceDescription
                   tOIOpenBalance.tcInvoiceDescription     = (if tOIOpenBalance.tcInvoiceDescription = "":U or tOIOpenBalance.tcInvoiceDescription = ? then tOIOpenBalance.tcInvoiceReference else tOIOpenBalance.tcInvoiceDescription)
                   tOIOpenBalance.tcDivisionCode           = tOIAdj.tcInvoiceDivisonCode
                   tOIOpenBalance.tcProjectCode            = tOIAdj.tcInvoiceProjectCode
                   tOIOpenBalance.tcCostCentreCode         = tOIAdj.tcInvoiceCostCentreCode
                   tOIOpenBalance.tcReasonCode             = tOIAdj.tcReasonCode
                   tOIOpenBalance.tcKey                    = "PROVIDEKEY":U.

            /* Get the CC amounts */
            if tOIOpenBalance.tdInvoiceAmountCC = 0 or 
               tOIOpenBalance.tdInvoiceAmountCC = ?
            then do:     
                <Q-59 run DomainPropertyByDomainCodeID (all) (Read) (NoCache)
                   (input vcDomainCode, (DomainCode)
                    input ?, (DomainID)
                    output dataset tqDomainPropertyByDomainCodeID) in BDomainProperty>
                
                find first tqDomainPropertyByDomainCodeID where
                           tqDomainPropertyByDomainCodeID.tcDomainCode = vcDomainCode
                           no-error.
                
                if available tqDomainPropertyByDomainCodeID and
                             tqDomainPropertyByDomainCodeID.tlDomainPropertyIsStatutory = true
                then do:
                    <M-65 run GetExchangeRateAndScale
                       (input  ? (iiFromCurrencyId), 
                        input  tOIOpenBalance.tcTCCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyCCId (iiToCurrencyId), 
                        input  ? (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeId), 
                        input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                        input  true (ilisCustomer), 
                        input  tOIOpenBalance.ttInvoiceDate (itInvoiceDate), 
                        input  tOIOpenBalance.ttPostingDate (itPostingDate), 
                        input  tOIAdj.tcInvoiceType (icInvoiceType), 
                        output vdRate (odExchangeRate), 
                        output vdScale (odExchangeRateScale), 
                        output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>

                    if viFcReturnSuper <> 0 
                    then assign viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0 
                    then leave DIBLOCK.
                    assign tOIOpenBalance.tdExchangeRateTCCC    = vdRate
                           tOIOpenBalance.tdCCExchangeRateScale = vdScale
                           tOIOpenBalance.tdInvoiceAmountCC     = <M-33 RoundAmount
                                                                     (input  tOIOpenBalance.tdInvoiceAmountTC * vdRate * vdScale (idUnroundedAmount), 
                                                                      input  viCompanyCCId (iiCurrencyID), 
                                                                      input  ? (icCurrencyCode)) in BOpenItemAdjustment>.
                end.
            end. /* if tOIOpenBalance.tdInvoiceAmountCC = 0 or  */


            /*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*/
            if tOIOpenBalance.tcInvoiceType = {&INVOICETYPE-PREPAYMENT} then
            do:
                assign vhFcComponent = ?
                       vcPaymentConditionCode = '':U.

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

                if viFcReturnSuper <> 0 
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 
                then leave DIBLOCK.

                assign tOIOpenBalance.tcNormalPaymentConditionCode = vcPaymentConditionCode.
            end.

            /*
            if tOIOpenBalance.tcInvoiceType = {&INVOICETYPE-PREPAYMENT} then
            do:
                <Q-51 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
                   assign tOIOpenBalance.tcNormalPaymentConditionCode = tqDomainCreditTermByDomain.tcPrePaymentConditionCode.
               else
                   assign tOIOpenBalance.tcNormalPaymentConditionCode = '':U.
            end.
            */

            
            
            /* Find the Debtor; used for further defaulting */
            if tOIOpenBalance.tiCompanyId    <> 0    and tOIOpenBalance.tiCompanyId  <> ? and 
               tOIOpenBalance.tcDebtorCode   <> "":U and tOIOpenBalance.tcDebtorCode <> ?
            then do :
                <Q-18 run DebtorByDebtor (first) (Read) (NoCache)
                   (input tOIOpenBalance.tiCompanyId, (CompanyId)
                    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
                    input ?, (DebtorId)
                    input tOIOpenBalance.tcDebtorCode, (DebtorCode)
                    output dataset tqDebtorByDebtor) in BDebtor >
                find first tqDebtorByDebtor where 
                           tqDebtorByDebtor.tcDebtorCode = tOIOpenBalance.tcDebtorCode
                           no-error.       
                /* Get the Payment-conditions from the Debtor */
                if available tqDebtorByDebtor                         and
                  (tOIOpenBalance.tcNormalPaymentConditionCode = "":U or
                   tOIOpenBalance.tcNormalPaymentConditionCode = ?)
                then do:
                        assign tOIOpenBalance.tcNormalPaymentConditionCode = (if tOIOpenBalance.tcNormalPaymentConditionCode   = "":U or tOIOpenBalance.tcNormalPaymentConditionCode   = ? then tqDebtorByDebtor.tcNormalPaymentConditionCode else tOIOpenBalance.tcNormalPaymentConditionCode)
                            tOIOpenBalance.ttInvoiceDiscountDueDate     = tOIAdj.ttInvoiceDiscountDueDate.
                        /* prepayment should use no stage, no discount payment condition */
                        if tOIOpenBalance.tcInvoiceType = {&INVOICETYPE-PREPAYMENT} and 
                            tqDebtorByDebtor.tcNormalPaymentType = {&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
                            do:
                                assign tOIOpenBalance.tcNormalPaymentConditionCode = tqPaymentConditionByProperties.tcPaymentConditionCode.
                            end.
                        end.
                end.
                /* Get the bank information if the debtor uses directdebit payment instrumenet */
                if available tqDebtorByDebtor
                then do:
                    assign vhFcComponent = ?.
                    <M-26 run GetParentBankData
                       (input  tOIOpenBalance.tiCompanyId (iiCompanyID), 
                        input  ? (icCreditorCode), 
                        input  tqDebtorByDebtor.tcDebtorCode (icDebtorCode), 
                        input  {&PAYFORMATPAYINSTRUMENT-DIRECTDEBIT} (icPaymentInstrument), 
                        output viBankNumberID (oiBankNumberID), 
                        output vcBankNumber (ocBankNumber), 
                        output vcBankNumberExtension (ocBankNumberExtension), 
                        input  ? (icOwnBankNumber), 
                        input  ? (icGLCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankNumber>
                    if viFcReturnSuper >= 0 and viBankNumberID <> ? and viBankNumberID <> 0
                    then assign tOIOpenBalance.tiBankNumber_ID       = viBankNumberID
                                tOIOpenBalance.tcBankNumber          = vcBankNumber
                                tOIOpenBalance.tcBankNumberExtension = vcBankNumberExtension.
                end. /* if available tqDebtorByDebtor */
            end. /* if tOIOpenBalance.tiCompanyId <> 0 and */                  
            /* Create the new invoice */            
            <M-7 run CreateDInvoices
               (input-output tOIOpenBalance (tDIOpenBalance), 
                input  viBJournalEntryID (iiBJournalEntryId), 
                input  viMainPostingID (iiPostingId), 
                output vcListNewInvoices (ocNewRecordInfo), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj = trim(SUBSTITUTE(#T-21'An error (&1) occurred when saving the new customer invoice for customer &2. See other messages for further details.':255(69539)T-21#,string(viFcReturnSuper),tOIAdj.tcCredDebEmplCode)). 
                <M-9 run SetMessage
                   (input  vcMsgOIAdj (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QADFIN-825':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave DIBLOCK.
            end. /* if viFcReturnSuper <> 0 */
            /* Find the class-table and store the Invoice_ID in it based on the output param of the call to the invoice-class */
            assign viOIAdjustLnInvYear        = integer(entry(1,tOIAdj.tcInvoiceNumberReference,"/":U))
                   vcOIAdjustLnInvJournalCode = entry(2,tOIAdj.tcInvoiceNumberReference,"/":U)
                   viOIAdjustLnInvVoucher     = integer(entry(3,tOIAdj.tcInvoiceNumberReference,"/":U))
                   no-error.
            if error-status:error
            then do :
                assign viLocalReturnStatus  = -3
                       vcMsgOIAdj           = trim(substitute(#T-30'Internal error. The system was unable to extract the invoice year, invoice daybook, and invoice voucher from the reference (&1).':255(70210)T-30#,tOIAdj.tcInvoiceNumberReference)).
                if error-status:num-messages > 0
                then assign vcMsgOIAdj = vcMsgOIAdj + chr(10) + 
                                         trim(substitute(#T-31'Detailed info: &1 (&2)':252(70208)T-31#,error-status:Get-Message(1),string(error-status:Get-Number(1)))).
                <M-29 run SetMessage
                   (input  vcMsgOIAdj (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-8301':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave DIBLOCK.
            end. /* if error-status:error */
            find tOIAdjustLn where 
                 tOIAdjustLn.tc_Status                   <> "D":U                       and
                 tOIAdjustLn.OIAdjustLnType               = {&OIADJUSTLNTYPE-DINEW}     and 
                 tOIAdjustLn.OIAdjustLnInvYear            = viOIAdjustLnInvYear         and                   
                 tOIAdjustLn.OIAdjustLnInvJournalCode     = vcOIAdjustLnInvJournalCode  and 
                 tOIAdjustLn.OIAdjustLnInvVoucher         = viOIAdjustLnInvVoucher
                 no-error.
            if available tOIAdjustLn
            then assign tOIAdjustLn.DInvoice_ID = integer(entry(2,vcListNewInvoices,",":U)) no-error.
        end. /* if available tOIAdj */
        
        /* ====================================== */
        /* Actions for movements on existing DI's */
        /* ====================================== */
        /* Create temp-table records to pass to BDInvoice */
        for each tOIAdj where 
                 tOIAdj.tcInvoiceOrigin = {&INVOICEORIGIN-DEBTOR} and 
                 tOIAdj.tlIsNewOpenItem = false
                 no-lock :      
            create tOIDMovement.
            assign tOIDMovement.tiDInvoiceId                   = tOIAdj.tiInvoiceID
                   tOIDMovement.tcGLAccountDivisionCode        = "":U
                   tOIDMovement.tcPostingRowId                 = vcMainPostingTcRowid
                   tOIDMovement.tlMovementIsForDraft           = false
                   tOIDMovement.tiDDocumentId                  = 0.
                   tOIDMovement.tlIsSuspTaxAtPartialPayment    = false.
            if tOIAdj.tcAdjustmentCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
            then assign tOIDMovement.tdAmountCreditTC = tOIAdj.tdAdjustmentAmountTC
                        tOIDMovement.tdAmountDebitTC  = 0.
            else assign tOIDMovement.tdAmountCreditTC = 0
                        tOIDMovement.tdAmountDebitTC  = tOIAdj.tdAdjustmentAmountTC.
            assign tOIDMovement.tlIsUndoPayment      = (if tOIAdj.tdNewBalanceTC = tOIAdj.tdInvoiceOriginalBalanceTC
                                                        then true
                                                        else false).
            /* ASSIGN THE TC DISCOUNT AMOUNT WITH "-" TO CREDIT AND "+" TO DEBIT */
            if tOIAdj.tcNewBalanceCrDt = {&CREDITDEBITABBREVIATION-DEBIT} 
            then 
                assign tOIDMovement.tdMovementDiscountTC =   tOIAdj.tdAdjustmentDiscountAmountTC.
            else 
                assign tOIDMovement.tdMovementDiscountTC = - tOIAdj.tdAdjustmentDiscountAmountTC.
                   
            for each tOIAdjStage where 
                     tOIAdjStage.tc_ParentRowid = tOIAdj.tc_Rowid and
                     tOIAdjStage.tdAllocatedPaymentAmountTC <> 0  and
                     tOIAdjStage.tdAllocatedPaymentAmountTC <> ?:
                create tOIDMovementStage.
                assign tOIDMovementStage.tc_ParentRowid             = tOIAdj.tc_Rowid
                       tOIDMovementStage.tc_Rowid                   = tOIAdjStage.tc_Rowid
                       tOIDMovementStage.tc_Status                  = "N":U
                       tOIDMovementStage.tiDInvoice_ID              = tOIAdjStage.tiInvoiceID
                       tOIDMovementStage.DDocInvoiceXrefStage_ID    = tOIAdjStage.tiInvoiceStageID
                       tOIDMovementStage.DInvoiceStage_ID           = tOIAdjStage.tiInvoiceStageID
                       tOIDMovementStage.DDocInvoiceXrefStageAlloTC = tOIAdjStage.tdAllocatedPaymentAmountTC
                       tOIDMovementStage.DDocInvoiceXrefStageDiscTC = tOIAdjStage.tdAllocatedDiscounttAmountTC.
            end. /* for each tOIAdjStage where */
            if tOIDMovement.tdMovementDiscountTC <> 0 and 
               tOIDMovement.tdMovementDiscountTC <> ?
            then do :
                /* Call a submethod that will create the postings for the discount */
                <M-28 run OIAdjCommitSubDInvoiceDiscount  (output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                if viFcReturnSuper <> 0 
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 
                then Leave DIBLOCK.
            end. /* if tOIDMovement.tdMovementDiscountTC <> 0 and */
        end. /* for each tOIAdj where */
        if can-find (first tOIDMovement) 
        then do :
            /* Call method in BDInvoice */
            <M-8 run CreateDInvoiceMovements
               (input-output tOIDMovement (tDIMovement), 
                input  tOIDMovementStage (tDInvoiceStageUpdates), 
                input-output viBJournalEntryID (biBJournalEntryId), 
                input  false (ilClearData), 
                input  vtMainPostingDate (itPaymentTaxPointDate), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj = trim(substitute(#T-22'An error (&1) occurred while saving the customer invoice activity. See other messages for further details.':255(69537)T-22#,string(viFcReturnSuper))). 
                <M-10 run SetMessage
                   (input  vcMsgOIAdj (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QADFIN-823':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
            end. /* if viFcReturnSuper < 0 */
            /* Go through the in-out temp-table and store the PostingLine_ID in tOIAdju as it will be used later on */
            for each tOIDMovement where 
                     tOIDMovement.tiPostingLineID <> 0 and 
                     tOIDMovement.tiPostingLineID <> ?, 
                each tOIAdj where 
                     tOIAdj.tiInvoiceID = tOIDMovement.tiDInvoiceId :
                assign tOIAdj.tiInvMovementPostingLineID = tOIDMovement.tiPostingLineID.
            end. /* for each tOIDMovement where */
        end. /* if can-find (first tOIDMovement) */
        
        /* ===================== */
        /* Validations fo the DI */
        /* ===================== */
        if vlBDInvoiceIsStartedFromOIAdj = true
        then do :
            <M-13 run ValidateBCAndAdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj = trim(substitute(#T-23'An error occurred during validation of the customer invoices: &1. See other messages for further details.':255(69536)T-23#,string(viFcReturnSuper))). 
                <M-12 run SetMessage
                   (input  vcMsgOIAdj (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QADFIN-824':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave DIBLOCK.
            end. /* if viFcReturnSuper < 0 */
        end. /* if vlBDInvoiceIsStartedFromOIAdj = true */
        
    END. /* DIBLOCK */
    
    /* =============== */
    /* Close BDInvoice */
    /* =============== */
    <I-5 {bFcCloseInstance
         &CLASS           = "BDInvoice"}>
    
    /* ============================= */
    /* Check for errors in the block */
    /* ============================= */
    if viLocalReturnStatus < 0 
    then do :
        assign vcMsgOIAdj = trim(SUBSTITUTE(#T-24'An error occurred while saving the customer adjustments: &1. See other messages for further details.':255(2904)T-24#,string(viLocalReturnStatus))). 
        <M-6 run SetMessage
           (input  vcMsgOIAdj (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QADFIN-759':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
    end. /* if oiReturnStatus < 0 */
    
    /* ==================================================== */
    /* Exception handling                                   */
    /* ==================================================== */
    assign oiReturnStatus = viLocalReturnStatus.