project QadFinancials > class BAPMatching > method ValidateComponentPreAPMCInvoice

Description

This method updates Supplier invoice about total matched amount and about all tax details - values are taken from one receiver matching.


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.ValidateComponentPreAPM


program code (program4/bapmatching.p)

    /* ================== */
    /* Exception handling */
    /* ================== */
    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0.
    
    /* ================== */
    /* Start Block        */
    /* ================== */
    UPDATECINVOICEBLOCK : DO :
        
        /* =========================================================================== *
         *  Validation block                                                           *
         * =========================================================================== */
        if not available t_sAPMatching
        then do :
            assign vcMsgAPMatching     = #T-19'There is missing active record in table t_sAPMatching':255(70397)T-19#
                   viLocalReturnStatus = -1.
            <M-2 run SetMessage
               (input  vcMsgAPMatching (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-8407':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            Leave UPDATECINVOICEBLOCK.
        end. /* if not available t_sAPMatching */
        if t_sAPMatching.tlCInvoiceIsInitialStatus <> true
        then do :
            assign vcMsgAPMatching     = trim(substitute(#T-23'The can only update the supplier invoice (&1/&2/&3) from Receiver Matching if the invoice is still in the Initial status.':255(69168)T-23#,string(t_sAPMatching.tiCInvoicePostingYear),t_sAPMatching.tcCInvoiceJournalCode,string(t_sAPMatching.tiCInvoiceVoucher)))
                   viLocalReturnStatus = -1.
            <M-6 run SetMessage
               (input  vcMsgAPMatching (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-8411':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            Leave UPDATECINVOICEBLOCK.
        end. /* if t_sAPMatching.tlCInvoiceIsInitialStatus <> true */

        /* Skip update of supplier invoice when then invoice remains in the initial status and *
         * there are taxes on the header level. In this case supplier invoice is not updated   *
         * and does not correspond with Receiver Matching amounts                              */
        if t_sAPMatching.APMatchingStatus <> {&APMATCHINGSTATUS-FINISHED}
        then do:
            for each t_sAPMatchingLn where
                     t_sAPMatchingLn.tc_ParentRowid = t_sAPMatching.tc_Rowid and
                     t_sAPMatchingLn.tc_Status <> "D":U,
                each t_sAPMatchingLnTax where
                     t_sAPMatchingLnTax.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid and 
                     t_sAPMatchingLnTax.tc_Status      <> "D":U:
                    
                <Q-87 assign vlFcQueryRecordsAvailable = MfgTaxCodeByTaxCode (NoCache)
                   (input t_sAPMatchingLnTax.tcVatCode, (TaxCode)
                    input t_sAPMatchingLnTax.tcDomainCode, (DomainCode)
                    input ?, (DiscountAtPayment)
                    input No, (IsTaxByLine)) in BMfgTaxCode >
                if vlFcQueryRecordsAvailable <> false
                then Leave UPDATECINVOICEBLOCK.
            end. /* for each t_sAPMatchingLn where */
        end.
        
        /* ==================================================== */
        /* Caluclate the total amount of the invoice: This the: */
        /* + matched-amount (base, without taxes)               */
        /* + Taxes-AccruedAtReceipt (recov and non-recov)       */
        /* + Taxes-AccruedAt (non-recoverable only)             */
        /* + Manualy entered tax-lines                          */
        /* ==================================================== */
        assign vdTotalInvoiceAmountTC = 0
               vdTotalInvoiceAmountTC = 0.
        /* =================================================================== */
        /* Go through all Lines and Taxes of the matching: do not check on the */
        /* parent-rowid as this is not yet filled in the input table records   */
        /* =================================================================== */
        for each t_sAPMatchingLn where
                 t_sAPMatchingLn.tc_ParentRowid = t_sAPMatching.tc_Rowid and
                 t_sAPMatchingLn.tc_Status <> "D":U 
                 no-lock :
            /* Add the base */
            assign vdTotalInvoiceAmountTC = vdTotalInvoiceAmountTC + t_sAPMatchingLn.APMatchingLnMatchAmtTC
                   vdTotalInvoiceAmountLC = vdTotalInvoiceAmountLC + t_sAPMatchingLn.APMatchingLnMatchAmtLC.
                   
            for each t_sAPMatchingLnTax where
                     t_sAPMatchingLnTax.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid and 
                     t_sAPMatchingLnTax.tc_Status      <> "D":U
                     no-lock :
                /* Do not count the ones that are marked as retained-tax */
                if t_sAPMatchingLnTax.APMatchingLnTaxIsAbsRet = false
                then assign vdTotalInvoiceAmountTC  = vdTotalInvoiceAmountTC + 
                                                      t_sAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC + 
                                                      t_sAPMatchingLnTax.APMatchingLnTaxARRecTaxTC + 
                                                      t_sAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC + 
                                                      t_sAPMatchingLnTax.APMatchingLnTaxAIRecTaxTC
                            vdTotalInvoiceAmountLC  = vdTotalInvoiceAmountLC + 
                                                      t_sAPMatchingLnTax.APMatchingLnTaxTaxLC.
            end. /* for each t_sAPMatchingLnTax where */
        end. /* for each t_sAPMatchingLn where */
        
        /* ==================================================================== */
        /* Go through all extra posting lines and add the amount to the total   */
        /* ==================================================================== */
        for each t_sAPMatchingCost where
                 t_sAPMatchingCost.tc_ParentRowid = t_sAPMatching.tc_Rowid and
                 t_sAPMatchingCost.tc_Status       <> "D"
                 no-lock:
             assign vdTotalInvoiceAmountTC = vdTotalInvoiceAmountTC + t_sAPMatchingCost.tdDebitTC - t_sAPMatchingCost.tdCreditTC.    
        end. /* for each t_sAPMatchingCost where */
        
        /* ==================================================================== */
        /* Convert the data in t_sAPMatchingLnTax into tApiCInvoice            */
        /* ==================================================================== */
        empty temp-table tExtCInvoiceVat.
        for each t_sAPMatchingLn where 
                 t_sAPMatchingLn.tc_ParentRowid = t_sAPMatching.tc_Rowid and
                 t_sAPMatchingLn.tc_Status <> "D":U 
                 no-lock:
            /* Reset the counter for the taxes within a single line */
            assign viAPMatchingLnTaxCounter = 0.  
            for each t_sAPMatchingLnTax where
                     t_sAPMatchingLnTax.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid and 
                     t_sAPMatchingLnTax.tc_Status      <> "D":U
                     no-lock :
                find tExtCInvoiceVat where 
                     tExtCInvoiceVat.tcVatCode   = t_sAPMatchingLnTax.tcVatCode         and 
                     tExtCInvoiceVat.TxuTaxUsage = t_sAPMatchingLn.APMatchingLnTaxUsage and 
                     tExtCInvoiceVat.TxenvTaxEnv = t_sAPMatchingLn.APMatchingLnTaxEnv   and
                     tExtCInvoiceVat.TxclTaxCls  = t_sAPMatchingLn.APMatchingLnTaxClass
                     no-error.
                /* Create the tExtCInvoiceVat records: combination of VatCode-TaxClass-TaxUsage-TaxEnv should be unique */
                if not available tExtCInvoiceVat
                then do:    
                    create tExtCInvoiceVat.
                    assign viAPMatchingLnTaxCounter                       = viAPMatchingLnTaxCounter + 1
                           viUpdateCInvoiceVatCounter                     = viUpdateCInvoiceVatCounter + 1
                           tExtCInvoiceVat.tcVatCode                      = t_sAPMatchingLnTax.tcVatCode        
                           tExtCInvoiceVat.TxuTaxUsage                    = t_sAPMatchingLn.APMatchingLnTaxUsage  
                           tExtCInvoiceVat.TxenvTaxEnv                    = t_sAPMatchingLn.APMatchingLnTaxEnv   
                           tExtCInvoiceVat.TxclTaxCls                     = t_sAPMatchingLn.APMatchingLnTaxClass
                           tExtCInvoiceVat.CInvoice_ID                    = t_sAPMatching.CInvoice_ID
                           tExtCInvoiceVat.Vat_ID                         = t_sAPMatchingLnTax.Vat_ID
                           tExtCInvoiceVat.NormalTaxGL_ID                 = t_sAPMatchingLnTax.NormalTaxGL_ID
                           tExtCInvoiceVat.NormalTaxDivision_ID           = t_sAPMatchingLnTax.NormalTaxDivision_ID
                           tExtCInvoiceVat.AbsRetTaxGL_ID                 = t_sAPMatchingLnTax.AbsRetTaxGL_ID
                           tExtCInvoiceVat.AbsRetTaxDivision_ID           = t_sAPMatchingLnTax.AbsRetTaxDivision_ID 
                           tExtCInvoiceVat.CInvoiceVatVatBaseDebitCC      = ?
                           tExtCInvoiceVat.CInvoiceVatVatBaseCreditCC     = ?
                           tExtCInvoiceVat.CInvoiceVatVatDebitCC          = ?
                           tExtCInvoiceVat.CInvoiceVatVatCreditCC         = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatBaseDebitVC    = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatBaseCreditVC   = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatDebitVC        = ?
                           tExtCInvoiceVat.tdCInvoiceVatVatCreditVC       = ?
                           tExtCInvoiceVat.tlIsRecalculateTax             = false
                           tExtCInvoiceVat.tcDomainCode                   = t_sAPMatchingLnTax.tcDomainCode
                           tExtCInvoiceVat.tcVatInOut                     = t_sAPMatchingLnTax.tcVatInOut
                           tExtCInvoiceVat.tcNormalTaxGLCode              = t_sAPMatchingLnTax.tcNormalTaxGLCode
                           tExtCInvoiceVat.tcNormalTaxDivisionCode        = t_sAPMatchingLnTax.tcNormalTaxDivisionCode
                           tExtCInvoiceVat.tcAbsRetTaxGLCode              = t_sAPMatchingLnTax.tcAbsRetTaxGLCode
                           tExtCInvoiceVat.tcAbsRetTaxDivisionCode        = t_sAPMatchingLnTax.tcAbsRetTaxDivisionCode
                           tExtCInvoiceVat.tc_Rowid                       = string(viUpdateCInvoiceVatCounter)
                           tExtCInvoiceVat.CInvoiceVatIsTaxable           = t_sAPMatchingLn.APMatchingLnIsTaxable
                           tExtCInvoiceVat.CInvoiceVatIsUpdAllow          = t_sAPMatchingLnTax.APMatchingLnTaxIsUpdAllow
                           tExtCInvoiceVat.CInvoiceVatIsAbsRet            = t_sAPMatchingLnTax.APMatchingLnTaxIsAbsRet
                           tExtCInvoiceVat.CInvoiceVatIsAccrRcpUs         = t_sAPMatchingLnTax.APMatchingLnTaxIsAccrRcp
                           tExtCInvoiceVat.CInvoiceVatTaxTrType           = t_sAPMatchingLnTax.APMatchingLnTaxTaxTrType
                           tExtCInvoiceVat.CInvoiceVatIsRevCharge         = t_sAPMatchingLnTax.APMatchingLnTaxIsRevCharge
                           tExtCInvoiceVat.CInvoiceVatIsSuspDel           = t_sAPMatchingLnTax.APMatchingLnTaxIsSuspDel
                           tExtCInvoiceVat.SuspDelTaxGL_ID                = t_sAPMatchingLnTax.SuspDelTaxGL_ID
                           tExtCInvoiceVat.SuspDelTaxDivision_ID          = t_sAPMatchingLnTax.SuspDelTaxDivision_ID
                           tExtCInvoiceVat.tcSuspDelTaxGLCode             = t_sAPMatchingLnTax.tcSuspDelTaxGLCode
                           tExtCInvoiceVat.tcSuspDelTaxDivisionCode       = t_sAPMatchingLnTax.tcSuspDelTaxDivisionCode
                           tExtCInvoiceVat.CInvoiceVatUILinkedRowID       = "1":U /* should just be the same for all taxes of a certain matching / invoice */
                           tExtCInvoiceVat.CInvoiceVatSequence            = viAPMatchingLnTaxCounter
                           tExtCInvoiceVat.TxtyTaxType                    = t_sAPMatchingLnTax.APMatchingLnTaxTaxType.
                end. /* if not can-find (tExtCInvoiceVat where */
                assign vlTaxIsDebit =   (t_sAPMatchingLnTax.APMatchingLnTaxTaxTC >= 0 and 
                                         t_sAPMatchingLn.APMatchingLnPvodOpenAmtTC >= 0 and 
                                         t_sAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE}) or 
                                        (t_sAPMatchingLnTax.APMatchingLnTaxTaxTC <= 0 and 
                                         t_sAPMatchingLn.APMatchingLnPvodOpenAmtTC <= 0 and 
                                         t_sAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}) or 
                                        (t_sAPMatchingLnTax.APMatchingLnTaxTaxTC < 0 and 
                                         t_sAPMatchingLn.APMatchingLnPvodOpenAmtTC <= 0 and 
                                         t_sAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE}) or 
                                        (t_sAPMatchingLnTax.APMatchingLnTaxTaxTC > 0 and 
                                         t_sAPMatchingLn.APMatchingLnPvodOpenAmtTC >= 0 and 
                                         t_sAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}).
                       
              
                assign tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then t_sAPMatchingLnTax.APMatchingLnTaxBaseTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then t_sAPMatchingLnTax.APMatchingLnTaxBaseTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then <M-88 GetAmountPreferencingExpected
                                                                                    (input  t_sAPMatchingLnTax.APMatchingLnTaxBaseLC (idExpectedAmount), 
                                                                                     input  t_sAPMatchingLnTax.APMatchingLnTaxBaseTC * t_sAPMatching.tdCInvoiceExchangeRate * t_sAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then <M-84 GetAmountPreferencingExpected
                                                                                    (input  t_sAPMatchingLnTax.APMatchingLnTaxBaseLC (idExpectedAmount), 
                                                                                     input  t_sAPMatchingLnTax.APMatchingLnTaxBaseTC * t_sAPMatching.tdCInvoiceExchangeRate * t_sAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatDebitTC          = tExtCInvoiceVat.CInvoiceVatVatDebitTC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then t_sAPMatchingLnTax.APMatchingLnTaxTaxTC 
                                                                            else 0)                                        
                       tExtCInvoiceVat.CInvoiceVatVatCreditTC         = tExtCInvoiceVat.CInvoiceVatVatCreditTC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then t_sAPMatchingLnTax.APMatchingLnTaxTaxTC 
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatDebitLC          = tExtCInvoiceVat.CInvoiceVatVatDebitLC + 
                                                                           (if vlTaxIsDebit = true
                                                                            then <M-70 GetAmountPreferencingExpected
                                                                                    (input  t_sAPMatchingLnTax.APMatchingLnTaxTaxLC (idExpectedAmount), 
                                                                                     input   t_sAPMatchingLnTax.APMatchingLnTaxTaxTC * t_sAPMatching.tdCInvoiceExchangeRate * t_sAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatVatCreditLC         = tExtCInvoiceVat.CInvoiceVatVatCreditLC - 
                                                                           (if vlTaxIsDebit = false
                                                                            then <M-71 GetAmountPreferencingExpected
                                                                                    (input  t_sAPMatchingLnTax.APMatchingLnTaxTaxLC (idExpectedAmount), 
                                                                                     input   t_sAPMatchingLnTax.APMatchingLnTaxTaxTC * t_sAPMatching.tdCInvoiceExchangeRate * t_sAPMatching.tdCInvoiceRateScale (idCalculatedAmount), 
                                                                                     input  viCompanyLCId (iiCurrencyId)) in BAPMatching>
                                                                            else 0)
                       tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC      = tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC + t_sAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC + t_sAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC
                       tExtCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC     = if tExtCInvoiceVat.CInvoiceVatIsSuspDel then abs(tExtCInvoiceVat.CInvoiceVatVatDebitTC - tExtCInvoiceVat.CInvoiceVatVatCreditTC) else 0.
                /* In case of Absorbed or Retained: ste the CreditTaxTX equal to the DebitTaxTX and vice-versa */
                if tExtCInvoiceVat.CInvoiceVatIsAbsRet = true
                then if tExtCInvoiceVat.CInvoiceVatVatDebitTC <> 0 and
                        vlTaxIsDebit                           = true
                     then assign tExtCInvoiceVat.CInvoiceVatVatCreditTC = tExtCInvoiceVat.CInvoiceVatVatDebitTC
                                 tExtCInvoiceVat.CInvoiceVatVatCreditLC = tExtCInvoiceVat.CInvoiceVatVatDebitLC.
                     else assign tExtCInvoiceVat.CInvoiceVatVatDebitTC  = tExtCInvoiceVat.CInvoiceVatVatCreditTC 
                                 tExtCInvoiceVat.CInvoiceVatVatDebitLC  = tExtCInvoiceVat.CInvoiceVatVatCreditLC.
            end. /* for each t_sAPMatchingLnTax where */
        end.  /* for each t_sAPMatchingLn where */
        
        /* ==================================================================== */
        /* Convert the data in t_sAPMatchingLnWHT into tExtCInvoiceWHT          */
        /* ==================================================================== */
        empty temp-table tExtCInvoiceWHT.
        for each t_sAPMatchingLn where 
                 t_sAPMatchingLn.tc_ParentRowid = t_sAPMatching.tc_Rowid and
                 t_sAPMatchingLn.tc_Status <> "D":U 
                 no-lock:
            /* Go through all WHT records of the APMLine */
            for each t_sAPMatchingLnWHT where
                     t_sAPMatchingLnWHT.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid and 
                     t_sAPMatchingLnWHT.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  = t_sAPMatching.CInvoice_ID       and
                                 tExtCInvoiceWHT.tcVatCode    = t_sAPMatchingLnWHT.tcVatCode    and 
                                 tExtCInvoiceWHT.tcDomainCode = t_sAPMatchingLnWHT.tcDomainCode and
                                 tExtCInvoiceWHT.tcVatInOut   = t_sAPMatchingLnWHT.tcVatInOut   and 
                                 tExtCInvoiceWHT.CInvoiceVat_ID = 0) /* CInvoiceWHT records comming from BAPMatching are not linked to CInvoiceVat records */
                then do:    
                    Create tExtCInvoiceWHT.
                    assign viUpdateCInvoiceWHTCounter                   = viUpdateCInvoiceWHTCounter + 1
                           tExtCInvoiceWHT.tc_Rowid                     = string(viUpdateCInvoiceWHTCounter)
                           tExtCInvoiceWHT.CInvoice_ID                  = t_sAPMatching.CInvoice_ID       
                           tExtCInvoiceWHT.tcVatCode                    = t_sAPMatchingLnWHT.tcVatCode 
                           tExtCInvoiceWHT.tcDomainCode                 = t_sAPMatchingLnWHT.tcDomainCode
                           tExtCInvoiceWHT.tcVatInOut                   = t_sAPMatchingLnWHT.tcVatInOut  
                           tExtCInvoiceWHT.tcVatDescription             = t_sAPMatchingLnWHT.tcVatDescription
                           tExtCInvoiceWHT.CInvoiceVat_ID               = 0  /* CInvoiceWHT records comming from BAPMatchign are not linked to CInvoiceVat records */
                           tExtCInvoiceWHT.GL_ID                        = t_sAPMatchingLnWHT.GL_ID
                           tExtCInvoiceWHT.tcGLCode                     = t_sAPMatchingLnWHT.tcGLCode
                           tExtCInvoiceWHT.Vat_ID                       = t_sAPMatchingLnWHT.Vat_ID
                           tExtCInvoiceWHT.Division_ID                  = t_sAPMatchingLnWHT.Division_ID
                           tExtCInvoiceWHT.tcDivisionCode               = t_sAPMatchingLnWHT.tcDivisionCode
                           tExtCInvoiceWHT.CInvoiceWHTTaxPct            = t_sAPMatchingLnWHT.APMatchingLnWHTTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTRecovTaxPct       = t_sAPMatchingLnWHT.APMatchingLnWHTRecovTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTBaseTaxPct        = t_sAPMatchingLnWHT.APMatchingLnWHTBaseTaxPct
                           tExtCInvoiceWHT.CInvoiceWHTTaxType           = t_sAPMatchingLnWHT.APMatchingLnWHTTaxType
                           tExtCInvoiceWHT.CInvoiceWHTTaxTrType         = t_sAPMatchingLnWHT.APMatchingLnWHTTaxTrType
                           tExtCInvoiceWHT.CInvoiceWHTIsUpdAllow        = t_sAPMatchingLnWHT.APMatchingLnWHTIsUpdAllow
                           tExtCInvoiceWHT.CInvoiceWHTIsOrigFromAPM     = true.
                end. /* if not can-find (tExtCInvoiceWHT where  */                       
                /* Increase the amounts in the CInvoiceWHT */
                Find tExtCInvoiceWHT where 
                     tExtCInvoiceWHT.CInvoice_ID  = t_sAPMatching.CInvoice_ID          and
                     tExtCInvoiceWHT.tcVatCode    = t_sAPMatchingLnWHT.tcVatCode    and 
                     tExtCInvoiceWHT.tcDomainCode = t_sAPMatchingLnWHT.tcDomainCode and
                     tExtCInvoiceWHT.tcVatInOut   = t_sAPMatchingLnWHT.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 + t_sAPMatchingLnWHT.APMatchingLnWHTFeeDebTC
                        tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC       = tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC + t_sAPMatchingLnWHT.APMatchingLnWHTFeeCredTC
                        tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC    = tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC + t_sAPMatchingLnWHT.APMatchingLnWHTExpDebTC
                        tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC   = tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC + t_sAPMatchingLnWHT.APMatchingLnWHTExpCredTC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC     = tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC + t_sAPMatchingLnWHT.APMatchingLnWHTTxFeeDebTC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC + t_sAPMatchingLnWHT.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 + t_sAPMatchingLnWHT.APMatchingLnWHTTxFeeDebLC
                        tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC + t_sAPMatchingLnWHT.APMatchingLnWHTTxFeeCredLC
                        tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC        = tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC + t_sAPMatchingLnWHT.APMatchingLnWHTAmtDebTC
                        tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC + t_sAPMatchingLnWHT.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 + t_sAPMatchingLnWHT.APMatchingLnWHTAmtDebLC
                        tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC + t_sAPMatchingLnWHT.APMatchingLnWHTAmtCredLC.
            end. /* for each t_sAPMatchingLnWHT where */
        end.  /* for each t_sAPMatchingLn where */    

        /* ============================================================================================== *
         * Brazilian specific                                                                             *
         * When the supplier invoice and receiver matching is created based on Legal document, then the   *
         * invoice amount has to be exactly the same as legal document amount                             *
         * ============================================================================================== */
        if vlDomainIsFiscalConfirm
        then do:
            <M-69 run ValidateComponentPreAPMCInvoiceLegalDocument
               (input-output vdTotalInvoiceAmountLC (bdTotalInvoiceAmountLC), 
                input-output vdTotalInvoiceAmountTC (bdTotalInvoiceAmountTC), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viLocalReturnStatus < 0 then leave UPDATECINVOICEBLOCK.
        end.        
                
        /* ==================================================================== */
        /* Open BCInvoice and call the method that will update the invoice-data */
        /* Note that this has to be done in a different transaction             */
        /* ==================================================================== */
        if viBCInvoiceAPMID = ? or
           viBCInvoiceAPMID = 0
        then do:
            <I-15 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION = "true"
                 &CLASS              = "BCInvoice"}>
            assign vlBCInvoiceStart = true.
        end. /* if viBCInvoiceAPMID = ? or */
        else do:
            <I-28 {bFcOpenInstance
                 &CLASS           = "BCInvoice"}>
        end. /* Not if viBCInvoiceAPMID = ? or */
        
        <M-29 run UpdateCInvoiceFromAPM
           (input  ? (iiCompanyId), 
            input  t_SAPMatching.CInvoice_ID (iiCInvoiceId), 
            input  ? (iiCInvoiceRegistrationNr), 
            input  vdTotalInvoiceAmountTC (idCInvoiceTotalAmount), 
            input  vdTotalInvoiceAmountLC (idCInvoiceTotalAmountLC), 
            input  tExtCInvoiceVat (tApiCInvoiceVat), 
            input  tExtCInvoiceWHT (tApiCInvoiceWHT), 
            output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>    
        <I-30 {bFcCloseInstance
             &CLASS           = "BCInvoice"}>
        if viExternalReturnStatus <> 0 then assign viLocalReturnStatus = viExternalReturnStatus.
        if viExternalReturnStatus <  0 then Leave UPDATECINVOICEBLOCK.
    END. /* UPDATECINVOICEBLOCK */   
    
    /* ================== */
    /* Exception handling */
    /* ================== */
    assign oiReturnStatus  = viLocalReturnStatus.