project QadFinancials > class BOpenItemAdjustment > method OIAdjCommitSubCInvoice

Description

OIAdjCommitCInvoice; 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-CREDITOR})
    then Return.
    empty temp-table tOICMovement.
    empty temp-table tOICMovementStage.
    empty temp-table tOIOpenBalance.
    empty temp-table tDefaultSafsFromOIAdj.
    empty temp-table tInvoiceWHTPaymentForOIA.
    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0.
    
    /* ====================== */
    /* Start / Open BCInvoice */
    /* ====================== */
    if viBCInvoiceInOIAID = 0 or viBCInvoiceInOIAID = ?
    then do :
        <I-40 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BCInvoice"}>
        assign vlBCInvoiceIsStartedFromOIAdj = true.
    end. /* if viBCInvoiceInOIAID = 0 */
    else do :
        <I-41 {bFcOpenInstance
            &CLASS           = "BCInvoice"}>
    end. /* if viBCInvoiceInOIAID <> 0 */
    
    /* =========================== */
    /* Start Main processing block */
    /* =========================== */
    CIBLOCK : DO :
        
        /* ====================== */
        /* Actions for a new CI   */
        /* ====================== */
        find first tOIAdj where 
                   tOIAdj.tlIsNewOpenItem = true and 
                   tOIAdj.tcInvoiceOrigin = {&INVOICEORIGIN-CREDITOR}
                   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.tcPostingText         = trim(#T-27'Open Item Adjustment':30(2900)t-27#)
                   tOIOpenBalance.tcCreditorCode        = tOIAdj.tcCredDebEmplCode
                   tOIOpenBalance.ttPostingDate         = vtMainPostingDate
                   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":U) /*CA707545*/
                                                           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-54 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-16 run GetExchangeRateAndScale
                       (input  ? (iiFromCurrencyId), 
                        input  tOIOpenBalance.tcTCCurrencyCode (icFromCurrencyCode), 
                        input  viCompanyCCId (iiToCurrencyId), 
                        input  ? (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeId), 
                        input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                        input  false (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 CIBLOCK.
                    assign tOIOpenBalance.tdExchangeRateTCCC    = vdRate
                           tOIOpenBalance.tdCCExchangeRateScale = vdScale
                           tOIOpenBalance.tdInvoiceAmountCC     = <M-46 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-487 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 CIBLOCK.

                assign tOIOpenBalance.tcNormalPaymentConditionCode = vcPaymentConditionCode.
            end.

/*
            if tOIOpenBalance.tcInvoiceType = {&INVOICETYPE-PREPAYMENT} then
            do:
                <Q-19 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 Creditor; used for further defaulting */
            if tOIOpenBalance.tiCompanyId    <> 0    and tOIOpenBalance.tiCompanyId    <> ? and 
               tOIOpenBalance.tcCreditorCode <> "":U and tOIOpenBalance.tcCreditorCode <> ?
            then do :
                <Q-15 run CreditorByCreditor (first) (Read) (NoCache)
                   (input ?, (CreditorId)
                    input tOIOpenBalance.tiCompanyId, (CompanyId)
                    input tOIOpenBalance.tcCreditorCode, (CreditorCode)
                    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
                    output dataset tqCreditorByCreditor) in BCreditor >
                find first tqCreditorByCreditor where 
                           tqCreditorByCreditor.tcCreditorCode = tOIOpenBalance.tcCreditorCode
                           no-error.
                if available tqCreditorByCreditor 
                then do :
                    /* Get the Payment-conditions from the Creditor */
                    if tOIOpenBalance.tcNormalPaymentConditionCode = "":U or
                       tOIOpenBalance.tcNormalPaymentConditionCode = ?
                    then do:
                        assign tOIOpenBalance.tcNormalPaymentConditionCode = tqCreditorByCreditor.tcNormalPaymentConditionCode
                                tOIOpenBalance.ttInvoiceDiscountDueDate     = tOIAdj.ttInvoiceDiscountDueDate.
                        /* prepayment should use no stage, no discount payment condition */
                        if tOIOpenBalance.tcInvoiceType = {&INVOICETYPE-PREPAYMENT} and 
                            tqCreditorByCreditor.tcNormalPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} 
                        then do:
                            <Q-71 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 creditor uses electronic payment-means - ignore the returnstatus */
                    assign vhFcComponent = ?.
                    <M-35 run GetParentBankData
                       (input  tOIOpenBalance.tiCompanyId (iiCompanyID), 
                        input  tqCreditorByCreditor.tcCreditorCode (icCreditorCode), 
                        input  ? (icDebtorCode), 
                        input  {&PAYMENTMEANSCREDITOR-ELECTRONIC} (icPaymentInstrument), 
                        output viBankNumberID (oiBankNumberID), 
                        output vcBankNumber (ocBankNumber), 
                        output vcBankNumberExtension (ocBankNumberExtension), 
                        input  ? (icOwnBankNumber), 
                        input  ? (icGLCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BBankNumber>
                    if viBankNumberID <> 0
                    then assign tOIOpenBalance.tcBankNumber          = vcBankNumber
                                tOIOpenBalance.tcBankNumberExtension = vcBankNumberExtension
                                tOIOpenBalance.tiBankNumber_ID       = viBankNumberID.
                end. /* if available tqCreditorByCreditor */
            end. /* if tOIOpenBalance.tiCompanyId <> 0 and */
            /* Create the new invoice */
            <M-3 run CreateCInvoices
               (input-output tOIOpenBalance (tOpenBalanceCI), 
                input  viBJournalEntryID (iiBJournalEntryId), 
                input  viMainPostingID (iiPostingId), 
                input  ? (icPONbrInv), 
                output vcListNewInvoices (ocNewRecordInfo), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj     = trim(SUBSTITUTE(#T-28'An error (&1) occurred while saving the new supplier invoice for supplier &2. See other messages for further details.':255(69538)T-28#,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-822':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave CIBLOCK.
            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-43'Internal error. The system was unable to extract the invoice year, invoice daybook, and invoice voucher from the reference (&1).':255(70210)T-43#,tOIAdj.tcInvoiceNumberReference)).
                if error-status:num-messages > 0
                then assign vcMsgOIAdj = vcMsgOIAdj + chr(10) + 
                                         trim(substitute(#T-44'Detailed info: &1 (&2)':252(70208)T-44#,error-status:Get-Message(1),string(error-status:Get-Number(1)))).
                <M-42 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-8302':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave CIBLOCK.
            end. /* if error-status:error */
            find tOIAdjustLn where 
                 tOIAdjustLn.tc_Status                   <> "D":U                       and
                 tOIAdjustLn.OIAdjustLnType               = {&OIADJUSTLNTYPE-CINEW}     and 
                 tOIAdjustLn.OIAdjustLnInvYear            = viOIAdjustLnInvYear         and                   
                 tOIAdjustLn.OIAdjustLnInvJournalCode     = vcOIAdjustLnInvJournalCode  and 
                 tOIAdjustLn.OIAdjustLnInvVoucher         = viOIAdjustLnInvVoucher
                 no-error.
            if available tOIAdjustLn
            then assign tOIAdjustLn.CInvoice_ID = integer(entry(2,vcListNewInvoices,",":U)) no-error.
        end. /* if available tOIAdj */
        
        /* ====================================== */
        /* Actions for movements on existing CI's */
        /* ====================================== */
        /* Create temp-table records to pass to BCInvoice */
        for each tOIAdj where 
                 tOIAdj.tcInvoiceOrigin = {&INVOICEORIGIN-CREDITOR} and 
                 tOIAdj.tlIsNewOpenItem = false
                 no-lock 
                 break by tOIAdj.tiCompanyId:
            /* ========================================================================================= */     
            /* Reset the WHT-number for every company used as the WHT-number should be company-dependant */
            /* ========================================================================================= */
            if first-of (tOIAdj.tiCompanyId)
            then assign viWHTNumber = 0.
                             
            /* =========================================================== */
            /* Create the movement record for updating the control account */
            /* =========================================================== */
            create tOICMovement.
            assign tOICMovement.tiCInvoiceId               = tOIAdj.tiInvoiceID
                   tOICMovement.tcGLAccountDivisionCode    = "":U
                   tOICMovement.tcPostingRowId             = vcMainPostingTcRowid
                   tOICMovement.tcPostingText              = vcMainDescription
                   tOICMovement.tdCInvoiceHoldAmountTC     = tOIAdj.tdCInvoiceHoldAmountTC
                   tOICMovement.tlMovementisForDraft       = false
                   tOICMovement.tiCDocumentId              = 0
                   tOICMovement.tlIsDelTaxAtPartialPayment = false.
            
            if tOIAdj.tcAdjustmentCrDt = {&CREDITDEBITABBREVIATION-CREDIT}
            then assign tOICMovement.tdAmountCreditTC = tOIAdj.tdAdjustmentAmountTC - tOIAdj.tdAdjustmentWHTAmountTC
                        tOICMovement.tdAmountDebitTC  = 0.
            else assign tOICMovement.tdAmountCreditTC = 0
                        tOICMovement.tdAmountDebitTC  = tOIAdj.tdAdjustmentAmountTC - tOIAdj.tdAdjustmentWHTAmountTC.
            assign tOICMovement.tlIsUndoPayment      = (if tOIAdj.tdNewBalanceTC = tOIAdj.tdInvoiceOriginalBalanceTC
                                                        then true
                                                        else false)
                   tOICMovement.tlMovementIsAboutWHT = false.

            /* ASSIGN THE TC DISCOUNT AMOUNT WITH "-" TO CREDIT AND "+" TO DEBIT */
            if tOIAdj.tcNewBalanceCrDt = {&CREDITDEBITABBREVIATION-CREDIT} 
            then assign tOICMovement.tdMovementDiscountTC = - tOIAdj.tdAdjustmentDiscountAmountTC.
            else assign tOICMovement.tdMovementDiscountTC =  tOIAdj.tdAdjustmentDiscountAmountTC.
                   
            for each tOIAdjStage where 
                     tOIAdjStage.tc_ParentRowid = tOIAdj.tc_Rowid and
                     tOIAdjStage.tdAllocatedPaymentAmountTC <> 0  and
                     tOIAdjStage.tdAllocatedPaymentAmountTC <> ?:
                create tOICMovementStage.
                assign tOICMovementStage.tc_ParentRowid             = tOIAdj.tc_Rowid
                       tOICMovementStage.tc_Rowid                   = tOIAdjStage.tc_Rowid
                       tOICMovementStage.tc_Status                  = "N":U
                       tOICMovementStage.tiCInvoice_ID              = tOIAdjStage.tiInvoiceID
                       tOICMovementStage.CDocInvoiceXrefStage_ID    = tOIAdjStage.tiInvoiceStageID
                       tOICMovementStage.CInvoiceStage_ID           = tOIAdjStage.tiInvoiceStageID
                       tOICMovementStage.CDocInvoiceXrefStageAlloTC = tOIAdjStage.tdAllocatedPaymentAmountTC + 
                                                                      tOIAdjStage.tdAllocatedDiscounttAmountTC +
                                                                      tOIAdjStage.tdAllocatedWHTAmountTC
                       tOICMovementStage.CDocInvoiceXrefStageDiscTC = tOIAdjStage.tdAllocatedDiscounttAmountTC.
            end. /* for each tOIAdjStage where */
            
            if tOICMovement.tdMovementDiscountTC <> 0 and 
               tOICMovement.tdMovementDiscountTC <> ?
            then do :
                /* Call a submethod that will create the postings for the discount */
                <M-36 run OIAdjCommitSubCInvoiceDiscount  (output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                if viFcReturnSuper <> 0 
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 
                then Leave CIBLOCK.
            end. /* if tOICMovement.tdMovementDiscountTC <> 0 and */

            /* ================================================= */                
            /* Create the WHT Movements + WHT temp-table records */
            /* ================================================= */
            <M-91 run OIAdjCommitSubCInvoiceCreateWHT
               (input  viWHTNumber (iiWHTNumber), 
                output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
            if viFcReturnSuper < 0 or viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave CIBLOCK.                
        end. /* for each tOIAdj where */
        
        if can-find (first tOICMovement)
        then do :
            /* Call method in BCInvoice */
            <M-4 run CreateCInvoiceMovements
               (input-output tOICMovement (tMovement), 
                input  tOICMovementStage (tCInvoiceStageUpdates), 
                input-output viBJournalEntryID (biBJournalEntryId), 
                input  false (ilClearData), 
                input  ? (itPaymentTaxPointDate), 
                input  true (ilIsCallFromBankingEntryOrOIAdj), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj     = trim(substitute(#T-29'Error &1 occurred when saving the supplier invoice. See other messages for further details.':255(2906)T-29#,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-820':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave CIBLOCK.
            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 tOICMovement where 
                     tOICMovement.tiPostingLine_ID <> 0 and 
                     tOICMovement.tiPostingLine_ID <> ?, 
                each tOIAdj where 
                     tOIAdj.tiInvoiceID = tOICMovement.tiCInvoiceId :
                assign tOIAdj.tiInvMovementPostingLineID = tOICMovement.tiPostingLine_ID.
            end. /* for each tOICMovement where */
        end. /* if can-find first (create tOICMovement) */
        
        if can-find(first tInvoiceWHTPaymentForOIA)
        then do:
            /* ============================ */
            /* Start / Open BWithHoldingTax */
            /* ============================ */
            if viBWithholdingTaxInOIAID = 0 or viBWithholdingTaxInOIAID = ?
            then do :
                <I-70 {bFcStartAndOpenInstance
                     &ADD-TO-TRANSACTION   = "true"
                     &CLASS                = "BWithholdingTax"}>
                assign vlBWHTIsStartedFromOIAdj = true.
            end. /* if viBWithholdingTaxInOIAID = 0 */
            else do :
                <I-48 {bFcOpenInstance
                     &CLASS           = "BCInvoice"}>
            end. /* if viBWithholdingTaxInOIAID <> 0 */
            
            /* =================================================== */
            /* Call method to create WHT recods in BWithHoldingTax */
            /* =================================================== */
            empty temp-table tInvoiceWHTPaymentCIWHTForOIA.
            <M-72 run CreateAndOrUpdateWHT
               (input  tInvoiceWHTPaymentForOIA (tInvoiceWHTPayment), 
                input  tInvoiceWHTPaymentCIWHTForOIA (tInvoiceWHTPaymentCIWHT), 
                output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>
            if viFcReturnSuper < 0 or oiReturnStatus  = 0
            then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then return.
            
            /* ================================================= */    
            /* Performing ValidateBCAndAddUpd on BWithHoldingTax */
            /* ================================================= */
            if vlBWHTIsStartedFromOIAdj = true
            then do:
                <M-33 run ValidateBCAndAdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BWithholdingTax>                    
                if viFcReturnSuper <> 0 
                then assign oiReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 
                then Return.    
            end. /* if vlBWHTIsStartedFromOIAdj = true */
                        
            /* ===================== */
            /* Close BWithHoldingTax */
            /* ===================== */
            <I-581 {bFcCloseInstance
                 &CLASS           = "BWithholdingTax"}>            
        end. /* if can-find(first tInvoiceWHTPaymentForOIA) */
                                                   
        /* ===================== */
        /* Validations fo the CI */
        /* ===================== */
        if vlBCInvoiceIsStartedFromOIAdj = true
        then do :
            /* No check if mandatory on role needed when comming from other components. */
            /* Keep track by setting a variable in session.                             */        
            <I-21 {bFcOpenInstance
                 &CLASS           = "Session"}>            
            <M-22 run SetLogicalValue
               (input  'CInvoiceRoleIsMandatory':U (icName), 
                input  false (ilValue), 
                output viFcReturnSuper (oiReturnStatus)) in Session>     
            <I-26 {bFcCloseInstance
                 &CLASS           = "Session"}>
            <M-12 run ValidateBCAndAdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 
            then do :
                assign vcMsgOIAdj     = trim(substitute(#T-30'Error &1 occurred during the supplier invoice validation. See other messages for further details.':255(2907)T-30#,string(viFcReturnSuper))). 
                <M-11 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-821':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
                Leave CIBLOCK.
            end. /* if viFcReturnSuper < 0 */
            <I-25 {bFcOpenInstance
            &CLASS           = "Session"}>
            <M-23 run SetLogicalValue (input  'CInvoiceRoleIsMandatory':U (icName), 
                           input  true (ilValue), 
                           output viFcReturnSuper (oiReturnStatus)) in Session>                   
            <I-24 {bFcCloseInstance
            &CLASS           = "Session"}>
        end. /* if vlBCInvoiceIsStartedFromOIAdj = true */
        
    END. /* CIBLOCK */
    
    /* =============== */
    /* Close BCInvoice */
    /* =============== */
    <I-37 {bFcCloseInstance
         &CLASS           = "BCInvoice"}>
    
    /* ============================= */
    /* Check for errors in the block */
    /* ============================= */
    if viLocalReturnStatus < 0 
    then do :
        assign vcMsgOIAdj = trim(SUBSTITUTE(#T-39'Error &1 occurred when saving the supplier adjustments. See other messages for further details.':255(2908)T-39#,string(oiReturnStatus))). 
        <M-38 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-8159':U (icFcMsgNumber),
                 input  '' (icFcExplanation),
                 input  '' (icFcIdentification),
                 input  '' (icFcContext),
                 output viFcReturnSuper (oiReturnStatus)) in BOpenItemAdjustment>
    end. /* if oiReturnStatus < 0 */
    
    /* ==================================================== */
    /* Exception handling                                   */
    /* ==================================================== */
    assign oiReturnStatus = viLocalReturnStatus.