project QadFinancials > class BAPMatching > method AdditionalUpdatesAllExtCInvoice

Description

AdditionalUpdatesAllExtCInvoice; sub-method of AdditionalUpdatesAll that handles all actions regarding this external class


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.AdditionalUpdatesAllExt


program code (program6/bapmatching.p)

/* ============================================================================================= */
/* Call method in BCInvoice to do the update and run ValidateBC&AdditionalUpdates                */
/* tExtAllocateCInvoiceAPM-recs were already created in method AdditionalUpdatesAllCreatePLCirec */
/* Return in case there are no such records                                                      */
/* ============================================================================================= */
if not can-find (first tExtAllocateCInvoiceAPM)
then Return.

/* ======================== */
/* Set default ReturnStatus */
/* ======================== */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

MAINEXTCIBLOCK: DO : 
    
    /* ==================================================================================================================== */
    /* For those invoices that are marked as initial ones, we have to pass all tax information to the invoice so it can be  */
    /* updated with the tax-information that we have here. This was not done during the loop on tAPMatchingLnTax in method  */
    /* AdditionalUpdatesAll because we there only go thourhg these records in case there are changed compared with the pvo  */
    /* ==================================================================================================================== */
    empty temp-table tExtCInvoiceVat.
    empty temp-table tExtCInvoiceWHT.    
    for each tExtAllocateCInvoiceAPM no-lock,
        each tAPMatching where 
             tAPMatching.CInvoice_ID                = tExtAllocateCInvoiceAPM.tiCInvoiceId and 
             tAPMatching.tlCInvoiceIsInitialStatus  = true                                 and 
             tAPMatching.tc_Status                 <> "D":U no-lock :
        
        assign viAPMatchingLnTaxSequence = 0.              
        
        for each tAPMatchingLn where
                 tAPMatchingLn.tc_ParentRowid  = tAPMatching.tc_Rowid and 
                 tAPMatchingLn.tc_Status      <> "D":U no-lock:
            /* Reset the counter for the taxes within a single line and then process all taxes of the line */
             
            for each tAPMatchingLnTax where
                     tAPMatchingLnTax.tc_ParentRowid  = tAPMatchingLn.tc_Rowid and 
                     tAPMatchingLnTax.tc_Status      <> "D":U
                     no-lock :
                /* Create the tExtCInvoiceVat records: combination of VatCode-TaxClass-TaxUsage-TaxEnv should be unique */
                if not can-find (tExtCInvoiceVat where
                                 tExtCInvoiceVat.CInvoice_ID = tAPMatching.CInvoice_ID            and 
                                 tExtCInvoiceVat.tcVatCode   = tAPMatchingLnTax.tcVatCode         and 
                                 tExtCInvoiceVat.TxuTaxUsage = tAPMatchingLn.APMatchingLnTaxUsage and 
                                 tExtCInvoiceVat.TxenvTaxEnv = tAPMatchingLn.APMatchingLnTaxEnv   and
                                 tExtCInvoiceVat.TxclTaxCls  = tAPMatchingLn.APMatchingLnTaxClass)
                then do:
                    Create tExtCInvoiceVat.
                    assign viAPMatchingLnTaxSequence                      = viAPMatchingLnTaxSequence + 1
                           viUpdateCInvoiceVatCounter                     = viUpdateCInvoiceVatCounter + 1
                           tExtCInvoiceVat.tcVatCode                      = tAPMatchingLnTax.tcVatCode
                           tExtCInvoiceVat.TxuTaxUsage                    = tAPMatchingLn.APMatchingLnTaxUsage
                           tExtCInvoiceVat.TxenvTaxEnv                    = tAPMatchingLn.APMatchingLnTaxEnv
                           tExtCInvoiceVat.TxclTaxCls                     = tAPMatchingLn.APMatchingLnTaxClass
                           tExtCInvoiceVat.CInvoice_ID                    = tAPMatching.CInvoice_ID
                           tExtCInvoiceVat.Vat_ID                         = tAPMatchingLnTax.Vat_ID
                           tExtCInvoiceVat.NormalTaxGL_ID                 = tAPMatchingLnTax.NormalTaxGL_ID
                           tExtCInvoiceVat.NormalTaxDivision_ID           = tAPMatchingLnTax.NormalTaxDivision_ID
                           tExtCInvoiceVat.AbsRetTaxGL_ID                 = tAPMatchingLnTax.AbsRetTaxGL_ID
                           tExtCInvoiceVat.AbsRetTaxDivision_ID           = tAPMatchingLnTax.AbsRetTaxDivision_ID
                           tExtCInvoiceVat.CInvoiceVatVatBaseDebitCC      = ?
                           tExtCInvoiceVat.CInvoiceVatVatBaseCreditCC     = ?
                           tExtCInvoiceVat.CInvoiceVatVatDebitCC          = ?
                           tExtCInvoiceVat.CInvoiceVatVatCreditCC         = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatBaseDebitVC    = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatBaseCreditVC   = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatDebitVC        = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatCreditVC       = ?
                           tExtCInvoiceVat.tlIsRecalculateTax             = false
                           tExtCInvoiceVat.tcDomainCode                   = tAPMatchingLnTax.tcDomainCode
                           tExtCInvoiceVat.tcVatInOut                     = tAPMatchingLnTax.tcVatInOut
                           tExtCInvoiceVat.tcNormalTaxGLCode              = tAPMatchingLnTax.tcNormalTaxGLCode
                           tExtCInvoiceVat.tcNormalTaxDivisionCode        = tAPMatchingLnTax.tcNormalTaxDivisionCode
                           tExtCInvoiceVat.tcAbsRetTaxGLCode              = tAPMatchingLnTax.tcAbsRetTaxGLCode
                           tExtCInvoiceVat.tcAbsRetTaxDivisionCode        = tAPMatchingLnTax.tcAbsRetTaxDivisionCode
                           tExtCInvoiceVat.tc_Rowid                       = string(viUpdateCInvoiceVatCounter)
                           tExtCInvoiceVat.CInvoiceVatIsTaxable           = tAPMatchingLn.APMatchingLnIsTaxable
                           tExtCInvoiceVat.CInvoiceVatIsUpdAllow          = tAPMatchingLnTax.APMatchingLnTaxIsUpdAllow
                           tExtCInvoiceVat.CInvoiceVatIsAbsRet            = tAPMatchingLnTax.APMatchingLnTaxIsAbsRet
                           tExtCInvoiceVat.CInvoiceVatIsAccrRcpUs         = tAPMatchingLnTax.APMatchingLnTaxIsAccrRcp
                           tExtCInvoiceVat.CInvoiceVatTaxTrType           = tAPMatchingLnTax.APMatchingLnTaxTaxTrType
                           tExtCInvoiceVat.CInvoiceVatIsRevCharge         = tAPMatchingLnTax.APMatchingLnTaxIsRevCharge
                           tExtCInvoiceVat.CInvoiceVatIsSuspDel           = tAPMatchingLnTax.APMatchingLnTaxIsSuspDel
                           tExtCInvoiceVat.SuspDelTaxGL_ID                = tAPMatchingLnTax.SuspDelTaxGL_ID
                           tExtCInvoiceVat.SuspDelTaxDivision_ID          = tAPMatchingLnTax.SuspDelTaxDivision_ID
                           tExtCInvoiceVat.tcSuspDelTaxGLCode             = tAPMatchingLnTax.tcSuspDelTaxGLCode
                           tExtCInvoiceVat.tcSuspDelTaxDivisionCode       = tAPMatchingLnTax.tcSuspDelTaxDivisionCode
                           tExtCInvoiceVat.TxtyTaxType                    = tAPMatchingLnTax.APMatchingLnTaxTaxType
                           tExtCInvoiceVat.CInvoiceVatUILinkedRowID       = "1":U /* should just be the same for all taxes of a certain matching / invoice */
                           tExtCInvoiceVat.CInvoiceVatSequence            = viAPMatchingLnTaxSequence.
                end. /* if not available tExtCInvoiceVat  */
                Find tExtCInvoiceVat where 
                     tExtCInvoiceVat.CInvoice_ID = tAPMatching.CInvoice_ID            and 
                     tExtCInvoiceVat.tcVatCode   = tAPMatchingLnTax.tcVatCode         and 
                     tExtCInvoiceVat.TxuTaxUsage = tAPMatchingLn.APMatchingLnTaxUsage and 
                     tExtCInvoiceVat.TxenvTaxEnv = tAPMatchingLn.APMatchingLnTaxEnv   and
                     tExtCInvoiceVat.TxclTaxCls  = tAPMatchingLn.APMatchingLnTaxClass
                     no-error. /* no need to have a no-error as this is covered by the can-find and create above */
                assign vlTaxIsDebit =   (tAPMatchingLnTax.APMatchingLnTaxTaxTC <> 0 and 
                                         tAPMatchingLn.APMatchingLnPvodOpenAmtTC >= 0 and 
                                         tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE})           or 
                                        (tAPMatchingLnTax.APMatchingLnTaxTaxTC <> 0 and 
                                         tAPMatchingLn.APMatchingLnPvodOpenAmtTC <= 0 and 
                                         tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}) or 
                                        (tAPMatchingLnTax.APMatchingLnTaxTaxTC <> 0 and 
                                         tAPMatchingLn.APMatchingLnPvodOpenAmtTC <= 0 and 
                                         tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE})           or 
                                        (tAPMatchingLnTax.APMatchingLnTaxTaxTC <> 0 and 
                                         tAPMatchingLn.APMatchingLnPvodOpenAmtTC >= 0 and 
                                         tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}).
                                         
                assign tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC + 
                                                                           (if vlTaxIsDebit = true                                                                           
                                                                            then tAPMatchingLnTax.APMatchingLnTaxBaseTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then tAPMatchingLnTax.APMatchingLnTaxBaseTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then <M-55 GetAmountPreferencingExpected
                                                                                    (input  tAPMatchingLnTax.APMatchingLnTaxBaseLC (idExpectedAmount), 
                                                                                     input  tAPMatchingLnTax.APMatchingLnTaxBaseTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then <M-79 GetAmountPreferencingExpected
                                                                                    (input  tAPMatchingLnTax.APMatchingLnTaxBaseLC (idExpectedAmount), 
                                                                                     input  tAPMatchingLnTax.APMatchingLnTaxBaseTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatDebitTC          = tExtCInvoiceVat.CInvoiceVatVatDebitTC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then tAPMatchingLnTax.APMatchingLnTaxTaxTC 
                                                                            else 0)                                        
                       tExtCInvoiceVat.CInvoiceVatVatCreditTC         = tExtCInvoiceVat.CInvoiceVatVatCreditTC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then tAPMatchingLnTax.APMatchingLnTaxTaxTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatDebitLC          = tExtCInvoiceVat.CInvoiceVatVatDebitLC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then <M-39 GetAmountPreferencingExpected
                                                                                    (input  tAPMatchingLnTax.APMatchingLnTaxTaxLC (idExpectedAmount), 
                                                                                     input  tAPMatchingLnTax.APMatchingLnTaxTaxTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatCreditLC         = tExtCInvoiceVat.CInvoiceVatVatCreditLC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then <M-44 GetAmountPreferencingExpected
                                                                                    (input  tAPMatchingLnTax.APMatchingLnTaxTaxLC (idExpectedAmount), 
                                                                                     input  tAPMatchingLnTax.APMatchingLnTaxTaxTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC      = tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC + tAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC + tAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC
                       tExtCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC     = if tExtCInvoiceVat.CInvoiceVatIsSuspDel then abs(tExtCInvoiceVat.CInvoiceVatVatDebitTC - tExtCInvoiceVat.CInvoiceVatVatCreditTC) else 0.

                /* In case of Absorbed or Retained: set the CreditTaxTX equal to the DebitTaxTX and vice-versa */
                if tExtCInvoiceVat.CInvoiceVatIsAbsRet = true
                then if tExtCInvoiceVat.CInvoiceVatVatDebitTC <> 0 and 
                        vlTaxIsDebit = yes
                     then assign tExtCInvoiceVat.CInvoiceVatVatCreditTC = tExtCInvoiceVat.CInvoiceVatVatDebitTC
                                 tExtCInvoiceVat.CInvoiceVatVatCreditLC = tExtCInvoiceVat.CInvoiceVatVatDebitLC.
                     else assign tExtCInvoiceVat.CInvoiceVatVatDebitTC  = tExtCInvoiceVat.CInvoiceVatVatCreditTC 
                                 tExtCInvoiceVat.CInvoiceVatVatDebitLC  = tExtCInvoiceVat.CInvoiceVatVatCreditLC.
            end. /* for each tAPMatchingLnTax where */
            /* Go through all WHT records of the APMLine */
            for each tAPMatchingLnWHT where
                     tAPMatchingLnWHT.tc_ParentRowid  = tAPMatchingLn.tc_Rowid and 
                     tAPMatchingLnWHT.tc_Status      <> "D":U
                     no-lock :
                /* Create the tExtCInvoiceWHT records: combination of CInvoice_ID+CInvoiceVat_ID+UpdRKBFVat_ID should be unique */
                if not can-find (tExtCInvoiceWHT where 
                                 tExtCInvoiceWHT.CInvoice_ID  = tAPMatching.CInvoice_ID       and
                                 tExtCInvoiceWHT.tcVatCode    = tAPMatchingLnWHT.tcVatCode    and 
                                 tExtCInvoiceWHT.tcDomainCode = tAPMatchingLnWHT.tcDomainCode and
                                 tExtCInvoiceWHT.tcVatInOut   = tAPMatchingLnWHT.tcVatInOut   and 
                                 tExtCInvoiceWHT.CInvoiceVat_ID = 0) /* CInvoiceWHT records comming from BAPMatchign are not linked to CInvoiceVat records */
                then do:    
                    Create tExtCInvoiceWHT.
                    assign viUpdateCInvoiceWHTCounter                   = viUpdateCInvoiceWHTCounter + 1
                           tExtCInvoiceWHT.tc_Rowid                     = string(viUpdateCInvoiceWHTCounter)
                           tExtCInvoiceWHT.CInvoice_ID                  = tAPMatching.CInvoice_ID       
                           tExtCInvoiceWHT.tcVatCode                    = tAPMatchingLnWHT.tcVatCode 
                           tExtCInvoiceWHT.tcDomainCode                 = tAPMatchingLnWHT.tcDomainCode
                           tExtCInvoiceWHT.tcVatInOut                   = tAPMatchingLnWHT.tcVatInOut  
                           tExtCInvoiceWHT.tcVatDescription             = tAPMatchingLnWHT.tcVatDescription
                           tExtCInvoiceWHT.CInvoiceVat_ID               = 0  /* CInvoiceWHT records comming from BAPMatchign are not linked to CInvoiceVat records */
                           tExtCInvoiceWHT.GL_ID                        = tAPMatchingLnWHT.GL_ID
                           tExtCInvoiceWHT.tcGLCode                     = tAPMatchingLnWHT.tcGLCode
                           tExtCInvoiceWHT.Vat_ID                       = tAPMatchingLnWHT.Vat_ID
                           tExtCInvoiceWHT.Division_ID                  = tAPMatchingLnWHT.Division_ID
                           tExtCInvoiceWHT.tcDivisionCode               = tAPMatchingLnWHT.tcDivisionCode
                           tExtCInvoiceWHT.CInvoiceWHTTaxPct            = tAPMatchingLnWHT.APMatchingLnWHTTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTRecovTaxPct       = tAPMatchingLnWHT.APMatchingLnWHTRecovTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTBaseTaxPct        = tAPMatchingLnWHT.APMatchingLnWHTBaseTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTTaxType           = tAPMatchingLnWHT.APMatchingLnWHTTaxType
                           tExtCInvoiceWHT.CInvoiceWHTTaxTrType         = tAPMatchingLnWHT.APMatchingLnWHTTaxTrType
                           tExtCInvoiceWHT.CInvoiceWHTIsUpdAllow        = tAPMatchingLnWHT.APMatchingLnWHTIsUpdAllow
                           tExtCInvoiceWHT.CInvoiceWHTIsOrigFromAPM     = true.
                end. /* if not can-find (tExtCInvoiceWHT where  */                       
                /* Increase the amounts in the CInvoiceWHT */
                Find tExtCInvoiceWHT where 
                     tExtCInvoiceWHT.CInvoice_ID  = tAPMatching.CInvoice_ID       and
                     tExtCInvoiceWHT.tcVatCode    = tAPMatchingLnWHT.tcVatCode    and 
                     tExtCInvoiceWHT.tcDomainCode = tAPMatchingLnWHT.tcDomainCode and
                     tExtCInvoiceWHT.tcVatInOut   = tAPMatchingLnWHT.tcVatInOut   and 
                     tExtCInvoiceWHT.CInvoiceVat_ID = 0                       
                     no-error. /* no need to have a no-error as this is covered by the can-find and create above */
                assign  tExtCInvoiceWHT.CInvoiceWHTFeeDebitTC        = tExtCInvoiceWHT.CInvoiceWHTFeeDebitTC + tAPMatchingLnWHT.APMatchingLnWHTFeeDebTC
                        tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC       = tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC + tAPMatchingLnWHT.APMatchingLnWHTFeeCredTC
                        tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC    = tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC + tAPMatchingLnWHT.APMatchingLnWHTExpDebTC
                        tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC   = tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC + tAPMatchingLnWHT.APMatchingLnWHTExpCredTC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC     = tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC + tAPMatchingLnWHT.APMatchingLnWHTTxFeeDebTC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC + tAPMatchingLnWHT.APMatchingLnWHTTxFeeCredTC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitCC     = ? /* Unknown as the matching does not store CC-values; they will be calculated by the validation-methods of BCI */
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditCC    = ? /* Unknown as the matching does not store CC-values; they will be calculated by the validation-methods of BCI */
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitLC     = tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitLC + tAPMatchingLnWHT.APMatchingLnWHTTxFeeDebLC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC + tAPMatchingLnWHT.APMatchingLnWHTTxFeeCredLC
                        tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC        = tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC + tAPMatchingLnWHT.APMatchingLnWHTAmtDebTC
                        tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC + tAPMatchingLnWHT.APMatchingLnWHTAmtCredTC
                        tExtCInvoiceWHT.CInvoiceWHTAmtDebitCC        = ? /* Unknown as the matching does not store CC-values; they will be calculated by the validation-methods of BCI */
                        tExtCInvoiceWHT.CInvoiceWHTAmtCreditCC       = ? /* Unknown as the matching does not store CC-values; they will be calculated by the validation-methods of BCI */
                        tExtCInvoiceWHT.CInvoiceWHTAmtDebitLC        = tExtCInvoiceWHT.CInvoiceWHTAmtDebitLC + tAPMatchingLnWHT.APMatchingLnWHTAmtDebLC
                        tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC + tAPMatchingLnWHT.APMatchingLnWHTAmtCredLC.
            end. /* for each tAPMatchingLnWHT where */
        end. /* for each tAPMatchingLn where */
    end. /* for each tExtAllocateCInvoiceAPM no-lock, */
    
    /* ====================================================================================================== */
    /* Perfrom the update towards BCI and then run a method for every matching to check if the CIREC amount   */
    /* in CI is same as the Matching amount as the CIREC amount is updated by calling method BCI:AllocateCI() */
    /* Call ValidateBC and AdditionalUpdates in BCInvoice as well                                             */
    /* ====================================================================================================== */
    if viBCInvoiceAPMID <> 0  and
       viBCInvoiceAPMID <> ?
    then do :
        <I-5 {bFcOpenInstance
            &CLASS           = "BCInvoice"}>
    end. /* if viBMfgPendingVoucherAPMID <> 0  and */
    else do :
        <I-6 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BCInvoice"}>
        assign vlBCInvoiceStart = true.
    end. /* Not if viBCInvoiceAPMID <> 0  and */ 
    ALLOCATECIBLOCK: DO :
        assign vlBAPMatching = true.
        <M-9 run AllocateCInvoices
           (input-output tExtAllocateCInvoiceAPM (tAllocateCInvoice), 
            input  tExtCInvoiceVat (tApiCInvoiceVat), 
            input  tExtCInvoiceWHT (tApiCInvoiceWHT), 
            input  viBJournalEntryAPMID (iiBJournalEntryId), 
            input  vlBAPMatching (ilBAPMatchingInvoice), 
            output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
        if viExternalReturnStatus <> 0 and viLocalReturnStatus >= 0
        then assign viLocalReturnStatus = viExternalReturnStatus.
        if viExternalReturnStatus < 0 
        then Leave ALLOCATECIBLOCK.
        for each tExtAllocateCInvoiceAPM no-lock,
            each tAPMatching where 
                 tAPMatching.CInvoice_ID                = tExtAllocateCInvoiceAPM.tiCInvoiceId and 
                 tAPMatching.tlCInvoiceIsInitialStatus  = true                                 and
                 tAPMatching.tc_Status                 <> "D":U : 
            <M-29 run CheckCICirecAmountAgainstMatched
               (input  tAPMatching.APMatching_ID (iiAPMatchingID), 
                input  tAPMatching.CInvoice_ID (iiCInvoiceID), 
                input  tAPMatching.Reason_ID (iiReasonID), 
                input  tAPMatching.tcReasonCode (icReasonCode), 
                input  tAPMatching.tiCInvoicePostingYear (iiCIPostingYear), 
                input  tAPMatching.tcCInvoiceJournalCode (icCIJournalCode), 
                input  tAPMatching.tiCInvoiceVoucher (iiCIVoucher), 
                input  tAPMatching.APMatchingTotalAmountTC (idAPMatchingTotalAmountTC), 
                input  tAPMatching.APMatchingTotalAmountLC (idAPMatchingTotalAmountLC), 
                input  tAPMatching.APMatchingAIRecTaxTC (idAPMatchingAIRecTaxTC), 
                output vdCirecCinvoiceAmountTC (odCirecCInvoiceAmountTC), 
                output vdCirecCinvoiceAmountLC (odCirecCInvoiceAmountLC), 
                output vdAPMatchingARRecTaxTC (odAPMatchingARRecTaxTC), 
                output vdAPMatchingARNRecTaxTC (odAPMatchingARNRecTaxTC), 
                output vdAPMatchingAIRecTaxTC (odAPMatchingAIRecTaxTC), 
                output vdAPMatchingAINRecTaxTC (odAPMatchingAINRecTaxTC), 
                output vdInvoiceDiscountPercentage (odInvoiceDiscountPercentage), 
                input  TRUE (ilBCInvoiceIsAlreadyOpen), 
                output viExternalReturnStatus (oiReturnStatus)) in BAPMatching>
            if viExternalReturnStatus <> 0 and viLocalReturnStatus >= 0
            then assign viLocalReturnStatus = viExternalReturnStatus.
            if viExternalReturnStatus < 0 
            then Leave ALLOCATECIBLOCK.
        end. /* for each tExtAllocateCInvoiceAPM */
    END. /* ALLOCATECIBLOCK */
    
    /* ========================================================================================================= */
    /* Normally we would here close BCInvoice but we will not do it for performance                              */
    /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
    /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
    /* appserver that is executed after each call to the appserver                                               */
    /* ========================================================================================================= */
    
    if viLocalReturnStatus < 0 
    then Leave MAINEXTCIBLOCK.
    
    /* =========================================================================================================== */
    /* In case the previous actions towards BCInvoice werew about inital invoices, then we need to update the      */
    /* PostingInvoiceReferenceTxt of the matching posting. This can only be done at this point as the call towards */
    /* BCInvoice resulted in assigning the invoice-voucher and we need that info in the PostingInvoiceReferenceTxt */
    /* =========================================================================================================== */
    for each tExtAllocateCInvoiceAPM,
        each tAPMatching where 
             tAPMatching.CInvoice_ID                = tExtAllocateCInvoiceAPM.tiCInvoiceId and 
             tAPMatching.tlCInvoiceIsInitialStatus  = true                                 and
             tAPMatching.Posting_ID                <> 0                                    and
             tAPMatching.Posting_ID                <> ?                                    and
             tAPMatching.CInvoice_ID               <> 0                                    and
             tAPMatching.CInvoice_ID               <> ?                                    and
             tAPMatching.tc_Status                 <> "D":U :
        <I-32 {bFcOpenInstance
             &CLASS           = "BJournalEntry"}>
        Leave. /* Just jump out of the 'for each' when a single record is detected in the for-statement */
    end. /* for each tExtAllocateCInvoiceAPM, */
    POSTINGINVOICEREFBLOCK : DO : 
        for each tExtAllocateCInvoiceAPM no-lock,
            each tAPMatching where 
                 tAPMatching.CInvoice_ID                = tExtAllocateCInvoiceAPM.tiCInvoiceId and 
                 tAPMatching.tlCInvoiceIsInitialStatus  = true                                 and
                 tAPMatching.Posting_ID                <> 0                                    and
                 tAPMatching.Posting_ID                <> ?                                    and
                 tAPMatching.CInvoice_ID               <> 0                                    and
                 tAPMatching.CInvoice_ID               <> ?                                    and
                 tAPMatching.tc_Status                 <> "D":U :
            /* Read the invoice from the db as we do not have all the required invoice-related info in here */
            <Q-23 run CInvoiceByMatchingInfo (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input tAPMatching.CInvoice_ID, (CInvoiceID)
                output dataset tqCInvoiceByMatchingInfo) in BCInvoice >
            find first tqCInvoiceByMatchingInfo where 
                       tqCInvoiceByMatchingInfo.tiCInvoice_ID = tAPMatching.CInvoice_ID
                       no-error.
            if not available tqCInvoiceByMatchingInfo
            then do :
                assign vcMsgAPMatching     = trim(substitute(#T-25'Internal error: Cannot find the supplier invoice based on its ID (&1).':252(56460)T-25#,string(tAPMatching.CInvoice_ID)))
                       viLocalReturnStatus = -3.
                <M-24 run SetMessage
                   (input  vcMsgAPMatching (icMessage), 
                    input  '':U (icArguments), 
                    input  'tAPMatching.CInvoice_ID':U (icFieldName), 
                    input  string(tAPMatching.CInvoice_ID) (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-9701':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
                Leave POSTINGINVOICEREFBLOCK. 
            end. /* if not available tqCInvoiceByMatchingInfo */
            /* Compose the PostingInvoiceReferenceText and call BPJE:SetPostingInvoiceReferenceTxt to realy update that info in the matchign posting */
            assign tAPMatching.tiCInvoiceVoucher = tExtAllocateCInvoiceAPM.tiCInvoiceVoucher
                   vcPostingInvoiceReferenceText = string(tqCInvoiceByMatchingInfo.tiCInvoicePostingYear, "9999":U)  + "/":U +
                                                   string(tqCInvoiceByMatchingInfo.tiCInvoicePostingPeriod ,"99":U)  + " ":U +
                                                   tqCInvoiceByMatchingInfo.tcJournalCode                            + " ":U +
                                                   string(tExtAllocateCInvoiceAPM.tiCInvoiceVoucher, "999999999":U)  + " ":U +
                                                   trim(tqCInvoiceByMatchingInfo.tcCInvoiceReference).
            <M-27 run SetPostingInvoiceReferenceTxt
               (input  ? (icPostingTcRowID), 
                input  tAPMatching.Posting_ID (iiPostingID), 
                input  vcPostingInvoiceReferenceText (icPostingInvoiceReferenceTxt), 
                output viExternalReturnStatus (oiReturnStatus)) in BJournalEntry>
            if viExternalReturnStatus <> 0 and viLocalReturnStatus >= 0
            then assign viLocalReturnStatus = viExternalReturnStatus.
            if viExternalReturnStatus < 0
            then Leave POSTINGINVOICEREFBLOCK.
        end. /* for each tExtAllocateCInvoiceAPM no-lock, */
    END. /* POSTINGINVOICEREFBLOCK */
    for each tExtAllocateCInvoiceAPM,
        each tAPMatching where 
             tAPMatching.CInvoice_ID                = tExtAllocateCInvoiceAPM.tiCInvoiceId and 
             tAPMatching.tlCInvoiceIsInitialStatus  = true                                 and
             tAPMatching.Posting_ID                <> 0                                    and
             tAPMatching.Posting_ID                <> ?                                    and
             tAPMatching.CInvoice_ID               <> 0                                    and
             tAPMatching.CInvoice_ID               <> ?                                    and
             tAPMatching.tc_Status                 <> "D":U :
        /* ========================================================================================================= */
        /* Normally we would here close BJournalEntry but we will not do it for performance                          */
        /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
        /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
        /* appserver that is executed after each call to the appserver                                               */
        /* ========================================================================================================= */
        Leave. /* Just jump out of the 'for each' when a single record is detected in the for-statement */
    end. /* for first */
    if viLocalReturnStatus < 0 
    then Leave MAINEXTCIBLOCK.

END. /* MAINEXTCIBLOCK */

/* ================ */
/* Set ReturnStatus */ 
/* ================ */ 
assign oiReturnStatus = viLocalReturnStatus.