project QadFinancials > class BAPMatching > method UpdateCInvoiceFromUIAPM

Description

UpdateCInvoiceFromAPM: this method can be called from the UI and it will update the total amount of the invoice and the tax-details of the invoice


Parameters


iiAPMatchingIDinputintegerID of the Matching
iiCInvoiceIDinputintegerID of the invoice that is being matched
iiPostingIDinputintegerPostingID: ID of the posting (optional)
tSPTAPMatchingLninputtemp-tableTemp-table holding the Lines of the matching
tSPTAPMatchingLnTaxinputtemp-tableTemp-table holding the Taxes of the Lines of the matching
tSPTAPMatchingLnWHTinputtemp-tableTemp-table holding the WHT of the Lines of the matching
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.UpdateCInvoiceFromAPMClassTables


program code (program8/bapmatching.p)

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

/* =========== */
/* Start Block */
/* =========== */
UPDATECINVOICEBLOCK : DO :

    /* ============== */
    /* Validate input */
    /* ============== */
    if iiAPMatchingID = 0 or 
       iiAPMatchingID = ?
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-2'Internal error. This method (&1) requires a receiver matching ID, but none was supplied.':255(69159)T-2#,"UpdateCInvoiceFromAPM":U))
               viLocalReturnStatus = -1.
        <M-1 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-7931':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if iiAPMatchingID = 0 or */
    find tAPMatching where 
         tAPMatching.APMatching_ID = iiAPMatchingID
         no-lock no-error.
    if not available tAPMatching
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-7'Internal error. The method (&1) received an invalid ID (&2) for the matching.':255(69160)T-7#,"UpdateCInvoiceFromAPM":U,string(iiAPMatchingID)))
               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-7932':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if not available tAPMatching */
    if tAPMatching.tc_Status = "D":U 
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-22'Internal error. The method (&1) cannot be called for matchings that are marked as deleted.':255(69161)T-22#,"UpdateCInvoiceFromAPM":U))
               viLocalReturnStatus = -1.
        <M-21 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-7935':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if tAPMatching.tc_Status = "D":U */
    if iiCInvoiceID = 0 or 
       iiCInvoiceID = ?
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-31'Internal error. The method (&1) requires a supplier invoice ID, but none was supplied.':255(69180)T-31#,"UpdateCInvoiceFromAPM":U))
               viLocalReturnStatus = -1.
        <M-30 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-7938':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if iiCInvoiceID = 0 or */
    if iiPostingID <> 0 and 
       iiPostingID <> ? and 
       (viBJournalEntryAPMID = 0 or 
        viBJournalEntryAPMID = ?)
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-24'Internal error. The method (&1) received a Posting ID (&2). However, the class does not refer to a posting.':255(69162)T-24#,"UpdateCInvoiceFromAPM":U,string(iiPostingID)))
               viLocalReturnStatus = -1.
        <M-23 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-7937':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if iiPostingID <> 0 and */
    if tAPMatching.tlCInvoiceIsInitialStatus <> true
    then do :
        assign vcMsgAPMatching     = trim(substitute(#T-33'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-33#,string(tAPMatching.tiCInvoicePostingYear),tAPMatching.tcCInvoiceJournalCode,string(tAPMatching.tiCInvoiceVoucher)))
               viLocalReturnStatus = -1.
        <M-32 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-7939':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave UPDATECINVOICEBLOCK.
    end. /* if tAPMatching.tlCInvoiceIsInitialStatus <> true */
    
    /* ==================================================== */
    /* 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.
    /* =================================================================== */
    /* 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 tSPTAPMatchingLn where
             tSPTAPMatchingLn.tc_Status <> "D":U 
             no-lock :
        /* Add the base */
        assign vdTotalInvoiceAmountTC  = vdTotalInvoiceAmountTC + tSPTAPMatchingLn.APMatchingLnMatchAmtTC.
        for each tSPTAPMatchingLnTax where
                 tSPTAPMatchingLnTax.tc_ParentRowid  = tSPTAPMatchingLn.tc_Rowid and 
                 tSPTAPMatchingLnTax.tc_Status      <> "D":U
                 no-lock :
            /* Do not count the ones that are marked as retained-tax */
            if tSPTAPMatchingLnTax.APMatchingLnTaxIsAbsRet = false
            then assign vdTotalInvoiceAmountTC  = vdTotalInvoiceAmountTC + 
                                                  tSPTAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC + 
                                                  tSPTAPMatchingLnTax.APMatchingLnTaxARRecTaxTC + 
                                                  tSPTAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC + 
                                                  tSPTAPMatchingLnTax.APMatchingLnTaxAIRecTaxTC.
        end. /* for each tSPTAPMatchingLnTax where */
    end. /* for each tSPTAPMatchingLn where */
    /* ===================================== */
    /* Get the manualy entered posting-lines */
    /* ===================================== */
    if viBJournalEntryAPMID     <> ? and 
       viBJournalEntryAPMID     <> 0 and 
       iiPostingID              <> ? and 
       iiPostingID              <> 0
    then do :
        <I-10 {bFcOpenInstance
             &CLASS           = "BJournalEntry"}>
        <M-11 run GetPostingAmount
           (input  iiPostingID (iiPostingId), 
            output vdDummy (odPostingAmountDebitLC), 
            output vdDummy (odPostingAmountCreditLC), 
            output vdSumManualJELinesCreditTC (odPostingAmountCreditTC), 
            output vdDummy (odPostingAmountCreditCC), 
            output vdSumManualJELinesDebitTC (odPostingAmountDebitTC), 
            output vdDummy (odPostingAmountDebitCC), 
            output vcDummy (ocCurrencyCode), 
            output viExternalReturnStatus (oiReturnStatus)) in BJournalEntry> 
        <I-12 {bFcCloseInstance
             &CLASS           = "BJournalEntry"}>
        if viExternalReturnStatus <> 0 
        then do :
            assign vcMsgAPMatching     = trim(#T-15'An internal error occurred while retrieving the amounts on the manually entered journal entry lines.':254(57521)T-15#) + Chr(10) + 
                                         trim(substitute(#T-16'Instance ID of the journal entry: &1.':251(57512)T-16#,string(viBJournalEntryAPMID))) + chr(10) + 
                                         trim(substitute(#T-17'Posting ID of the journal entry: &1.':251(57513)T-17#,string(iiPostingID))) + chr(10) + 
                                         trim(substitute(#T-18'Date and Invoice of the matching: &1 - &2 / &3 / &4.':251(56745)T-18#,string(tAPMatching.APMatchingDate),string(tAPMatching.tiCInvoicePostingYear),tAPMatching.tcCInvoiceJournalCode,string(tAPMatching.tiCInvoiceVoucher)))
                   viLocalReturnStatus = -1.
            <M-13 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-7933':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            Leave UPDATECINVOICEBLOCK.
        end. /* if viExternalReturnStatus <> 0 */
        assign vdTotalInvoiceAmountTC = vdTotalInvoiceAmountTC + vdSumManualJELinesDebitTC - vdSumManualJELinesCreditTC.
    end. /* if viBJournalEntryAPMID     <> ? and */
    
    /* ==================================================================== */
    /* Convert the data in tSPTAPMatchingLnTax into tApiCInvoice            */
    /* ==================================================================== */
    empty temp-table tExtCInvoiceVat.
    for each tSPTAPMatchingLn where 
             tSPTAPMatchingLn.tc_Status <> "D":U 
             no-lock:
        /* Reste the counter for the taxes within a single line */
        assign viAPMatchingLnTaxCounter = 0.  
        for each tSPTAPMatchingLnTax where
                 tSPTAPMatchingLnTax.tc_ParentRowid  = tSPTAPMatchingLn.tc_Rowid and 
                 tSPTAPMatchingLnTax.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.tcVatCode   = tSPTAPMatchingLnTax.tcVatCode         and 
                             tExtCInvoiceVat.TxuTaxUsage = tSPTAPMatchingLn.APMatchingLnTaxUsage and 
                             tExtCInvoiceVat.TxenvTaxEnv = tSPTAPMatchingLn.APMatchingLnTaxEnv   and
                             tExtCInvoiceVat.TxclTaxCls  = tSPTAPMatchingLn.APMatchingLnTaxClass)
            then do:    
                Create tExtCInvoiceVat.
                assign viAPMatchingLnTaxCounter                       = viAPMatchingLnTaxCounter + 1
                       viUpdateCInvoiceVatCounter                     = viUpdateCInvoiceVatCounter + 1
                       tExtCInvoiceVat.tcVatCode                      = tSPTAPMatchingLnTax.tcVatCode        
                       tExtCInvoiceVat.TxuTaxUsage                    = tSPTAPMatchingLn.APMatchingLnTaxUsage  
                       tExtCInvoiceVat.TxenvTaxEnv                    = tSPTAPMatchingLn.APMatchingLnTaxEnv   
                       tExtCInvoiceVat.TxclTaxCls                     = tSPTAPMatchingLn.APMatchingLnTaxClass
                       tExtCInvoiceVat.CInvoice_ID                    = tAPMatching.CInvoice_ID
                       tExtCInvoiceVat.Vat_ID                         = tSPTAPMatchingLnTax.Vat_ID
                       tExtCInvoiceVat.NormalTaxGL_ID                 = tSPTAPMatchingLnTax.NormalTaxGL_ID
                       tExtCInvoiceVat.NormalTaxDivision_ID           = tSPTAPMatchingLnTax.NormalTaxDivision_ID
                       tExtCInvoiceVat.AbsRetTaxGL_ID                 = tSPTAPMatchingLnTax.AbsRetTaxGL_ID
                       tExtCInvoiceVat.AbsRetTaxDivision_ID           = tSPTAPMatchingLnTax.AbsRetTaxDivision_ID 
                       tExtCInvoiceVat.CInvoiceVatVatBaseDebitCC      = ?
                       tExtCInvoiceVat.CInvoiceVatVatBaseCreditCC     = ?
                       tExtCInvoiceVat.CInvoiceVatVatDebitCC          = ?
                       tExtCInvoiceVat.CInvoiceVatVatCreditCC         = ?
                       tExtCInvoiceVat.tdCInvoiceVatVatBaseDebitVC    = ?
                       tExtCInvoiceVat.tdCInvoiceVatVatBaseCreditVC   = ?
                       tExtCInvoiceVat.tdCInvoiceVatVatDebitVC        = ?
                       tExtCInvoiceVat.tdCInvoiceVatVatCreditVC       = ?
                       tExtCInvoiceVat.tlIsRecalculateTax             = false
                       tExtCInvoiceVat.tcDomainCode                   = tSPTAPMatchingLnTax.tcDomainCode
                       tExtCInvoiceVat.tcVatInOut                     = tSPTAPMatchingLnTax.tcVatInOut
                       tExtCInvoiceVat.tcNormalTaxGLCode              = tSPTAPMatchingLnTax.tcNormalTaxGLCode
                       tExtCInvoiceVat.tcNormalTaxDivisionCode        = tSPTAPMatchingLnTax.tcNormalTaxDivisionCode
                       tExtCInvoiceVat.tcAbsRetTaxGLCode              = tSPTAPMatchingLnTax.tcAbsRetTaxGLCode
                       tExtCInvoiceVat.tcAbsRetTaxDivisionCode        = tSPTAPMatchingLnTax.tcAbsRetTaxDivisionCode
                       tExtCInvoiceVat.tc_Rowid                       = string(viUpdateCInvoiceVatCounter)
                       tExtCInvoiceVat.CInvoiceVatIsTaxable           = tSPTAPMatchingLn.APMatchingLnIsTaxable
                       tExtCInvoiceVat.CInvoiceVatIsUpdAllow          = tSPTAPMatchingLnTax.APMatchingLnTaxIsUpdAllow
                       tExtCInvoiceVat.CInvoiceVatIsAbsRet            = tSPTAPMatchingLnTax.APMatchingLnTaxIsAbsRet
                       tExtCInvoiceVat.CInvoiceVatIsAccrRcpUs         = tSPTAPMatchingLnTax.APMatchingLnTaxIsAccrRcp
                       tExtCInvoiceVat.CInvoiceVatTaxTrType           = tSPTAPMatchingLnTax.APMatchingLnTaxTaxTrType
                       tExtCInvoiceVat.CInvoiceVatIsRevCharge         = tSPTAPMatchingLnTax.APMatchingLnTaxIsRevCharge
                       tExtCInvoiceVat.CInvoiceVatIsSuspDel           = tSPTAPMatchingLnTax.APMatchingLnTaxIsSuspDel
                       tExtCInvoiceVat.SuspDelTaxGL_ID                = tSPTAPMatchingLnTax.SuspDelTaxGL_ID
                       tExtCInvoiceVat.SuspDelTaxDivision_ID          = tSPTAPMatchingLnTax.SuspDelTaxDivision_ID
                       tExtCInvoiceVat.tcSuspDelTaxGLCode             = tSPTAPMatchingLnTax.tcSuspDelTaxGLCode
                       tExtCInvoiceVat.tcSuspDelTaxDivisionCode       = tSPTAPMatchingLnTax.tcSuspDelTaxDivisionCode
                       tExtCInvoiceVat.CInvoiceVatUILinkedRowID       = "1":U /* should just be the same for all taxes of a certain matching / invoice */
                       tExtCInvoiceVat.CInvoiceVatSequence            = viAPMatchingLnTaxCounter
                       tExtCInvoiceVat.TxtyTaxType                    = tSPTAPMatchingLnTax.APMatchingLnTaxTaxType.
            end. /* if not can-find (tExtCInvoiceVat where */
            find tExtCInvoiceVat where 
                 tExtCInvoiceVat.tcVatCode   = tSPTAPMatchingLnTax.tcVatCode         and 
                 tExtCInvoiceVat.TxuTaxUsage = tSPTAPMatchingLn.APMatchingLnTaxUsage and 
                 tExtCInvoiceVat.TxenvTaxEnv = tSPTAPMatchingLn.APMatchingLnTaxEnv   and
                 tExtCInvoiceVat.TxclTaxCls  = tSPTAPMatchingLn.APMatchingLnTaxClass
                 no-error. /* no need to have a no-error as this is covered by the can-find and create above */
            /* Set the taxbase-value, tax-value and non-recoverable tax-value and hold an indication if it concerns a Debit or not */
            /* The non-recoverable-tax, actual-tax and tax-base should be negative in case it concerns an invoice-correction or a credit-note-correction */
            assign vlTaxIsDebit  = (tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION})
                   vdTaxBaseTC   = absolute(tSPTAPMatchingLnTax.APMatchingLnTaxBaseTC)
                   vdTaxTaxTC    = absolute(tSPTAPMatchingLnTax.APMatchingLnTaxTaxTC)
                   vdNonRecTaxTC = absolute(tSPTAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC + tSPTAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC).
                   
            if tAPMatching.tcCInvoiceType                 = {&INVOICETYPE-INVOICE} and 
               tSPTAPMatchingLnTax.APMatchingLnTaxTaxTC   < 0 then
               vdTaxTaxTC    = tSPTAPMatchingLnTax.APMatchingLnTaxTaxTC.       
                   
            if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or 
               tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
            then assign vdTaxBaseTC   = vdTaxBaseTC * -1
                        vdTaxTaxTC    = vdTaxTaxTC * -1
                        vdNonRecTaxTC = vdNonRecTaxTC * -1.
            assign tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitTC + 
                                                                       (if vlTaxIsDebit = true
                                                                        then vdTaxBaseTC  
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditTC + 
                                                                       (if vlTaxIsDebit = false
                                                                        then vdTaxBaseTC 
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC      = tExtCInvoiceVat.CInvoiceVatVatBaseDebitLC + 
                                                                       (if vlTaxIsDebit = true
                                                                        then <M-46 RoundAmount
                                                                                (input  vdTaxBaseTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idUnroundedAmount), 
                                                                                 input  viCompanyLCId (iiCurrencyID), 
                                                                                 input  ? (icCurrencyCode)) in BAPMatching>
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC     = tExtCInvoiceVat.CInvoiceVatVatBaseCreditLC + 
                                                                       (if vlTaxIsDebit = false
                                                                        then <M-98 RoundAmount
                                                                                (input  vdTaxBaseTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idUnroundedAmount), 
                                                                                 input  viCompanyLCId (iiCurrencyID), 
                                                                                 input  ? (icCurrencyCode)) in BAPMatching>
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatDebitTC          = tExtCInvoiceVat.CInvoiceVatVatDebitTC + 
                                                                       (if vlTaxIsDebit = true
                                                                        then vdTaxTaxTC 
                                                                        else 0)                                        
                   tExtCInvoiceVat.CInvoiceVatVatCreditTC         = tExtCInvoiceVat.CInvoiceVatVatCreditTC + 
                                                                       (if vlTaxIsDebit = false
                                                                        then vdTaxTaxTC 
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatDebitLC          = tExtCInvoiceVat.CInvoiceVatVatDebitLC + 
                                                                       (if vlTaxIsDebit = true
                                                                        then <M-59 RoundAmount
                                                                                (input  vdTaxTaxTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idUnroundedAmount), 
                                                                                 input  viCompanyLCId (iiCurrencyID), 
                                                                                 input  ? (icCurrencyCode)) in BAPMatching>
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatVatCreditLC         = tExtCInvoiceVat.CInvoiceVatVatCreditLC + 
                                                                       (if vlTaxIsDebit = false
                                                                        then <M-751 RoundAmount
                                                                                (input  vdTaxTaxTC  * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCInvoiceRateScale (idUnroundedAmount), 
                                                                                 input  viCompanyLCId (iiCurrencyID), 
                                                                                 input  ? (icCurrencyCode)) in BAPMatching>
                                                                        else 0)
                   tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC      = tExtCInvoiceVat.CInvoiceVatNonRecTaxAmtTC + vdNonRecTaxTC
                   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
                 then assign tExtCInvoiceVat.CInvoiceVatVatCreditTC = tExtCInvoiceVat.CInvoiceVatVatDebitTC
                             tExtCInvoiceVat.CInvoiceVatVatCreditLC = tExtCInvoiceVat.CInvoiceVatVatDebitLC.
                 else assign tExtCInvoiceVat.CInvoiceVatVatDebitTC  = tExtCInvoiceVat.CInvoiceVatVatCreditTC 
                             tExtCInvoiceVat.CInvoiceVatVatDebitLC  = tExtCInvoiceVat.CInvoiceVatVatCreditLC.
        end. /* for each tSPTAPMatchingLnTax where */
    end.  /* for each tSPTAPMatchingLn where */
    /* ==================================================================== */
    /* Convert the data in tSPTAPMatchingLnWHT into tApiCInvoiceWHT         */
    /* ==================================================================== */
    empty temp-table tExtCInvoiceWHT.
    for each tSPTAPMatchingLn where 
             tSPTAPMatchingLn.tc_Status <> "D":U 
             no-lock:
        for each tSPTAPMatchingLnWHT where
                 tSPTAPMatchingLnWHT.tc_ParentRowid  = tSPTAPMatchingLn.tc_Rowid and 
                 tSPTAPMatchingLnWHT.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    = tSPTAPMatchingLnWHT.tcVatCode    and 
                             tExtCInvoiceWHT.tcDomainCode = tSPTAPMatchingLnWHT.tcDomainCode and
                             tExtCInvoiceWHT.tcVatInOut   = tSPTAPMatchingLnWHT.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                    = tSPTAPMatchingLnWHT.tcVatCode 
                       tExtCInvoiceWHT.tcDomainCode                 = tSPTAPMatchingLnWHT.tcDomainCode
                       tExtCInvoiceWHT.tcVatInOut                   = tSPTAPMatchingLnWHT.tcVatInOut  
                       tExtCInvoiceWHT.CInvoiceVat_ID               = 0  /* CInvoiceWHT records comming from BAPMatchign are not linked to CInvoiceVat records */
                       tExtCInvoiceWHT.tcVatDescription             = tSPTAPMatchingLnWHT.tcVatDescription
                       tExtCInvoiceWHT.Vat_ID                       = tSPTAPMatchingLnWHT.Vat_ID
                       tExtCInvoiceWHT.GL_ID                        = tSPTAPMatchingLnWHT.GL_ID
                       tExtCInvoiceWHT.tcGLCode                     = tSPTAPMatchingLnWHT.tcGLCode
                       tExtCInvoiceWHT.Division_ID                  = tSPTAPMatchingLnWHT.Division_ID
                       tExtCInvoiceWHT.tcDivisionCode               = tSPTAPMatchingLnWHT.tcDivisionCode
                       tExtCInvoiceWHT.CInvoiceWHTTaxPct            = tSPTAPMatchingLnWHT.APMatchingLnWHTTaxPct
                       tExtCInvoiceWHT.CInvoiceWHTRecovTaxPct       = tSPTAPMatchingLnWHT.APMatchingLnWHTRecovTaxPct
                       tExtCInvoiceWHT.CInvoiceWHTBaseTaxPct        = tSPTAPMatchingLnWHT.APMatchingLnWHTBaseTaxPct
                       tExtCInvoiceWHT.CInvoiceWHTTaxType           = tSPTAPMatchingLnWHT.APMatchingLnWHTTaxType
                       tExtCInvoiceWHT.CInvoiceWHTTaxTrType         = tSPTAPMatchingLnWHT.APMatchingLnWHTTaxTrType
                       tExtCInvoiceWHT.CInvoiceWHTIsUpdAllow        = tSPTAPMatchingLnWHT.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    = tSPTAPMatchingLnWHT.tcVatCode    and 
                 tExtCInvoiceWHT.tcDomainCode = tSPTAPMatchingLnWHT.tcDomainCode and
                 tExtCInvoiceWHT.tcVatInOut   = tSPTAPMatchingLnWHT.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 + tSPTAPMatchingLnWHT.APMatchingLnWHTFeeDebTC
                    tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC       = tExtCInvoiceWHT.CInvoiceWHTFeeCreditTC + tSPTAPMatchingLnWHT.APMatchingLnWHTFeeCredTC
                    tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC    = tExtCInvoiceWHT.CInvoiceWHTExpenseDebitTC + tSPTAPMatchingLnWHT.APMatchingLnWHTExpDebTC
                    tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC   = tExtCInvoiceWHT.CInvoiceWHTExpenseCreditTC + tSPTAPMatchingLnWHT.APMatchingLnWHTExpCredTC
                    tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC     = tExtCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC + tSPTAPMatchingLnWHT.APMatchingLnWHTTxFeeDebTC
                    tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC + tSPTAPMatchingLnWHT.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 + tSPTAPMatchingLnWHT.APMatchingLnWHTTxFeeDebLC
                    tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC    = tExtCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC + tSPTAPMatchingLnWHT.APMatchingLnWHTTxFeeCredLC
                    tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC        = tExtCInvoiceWHT.CInvoiceWHTAmtDebitTC + tSPTAPMatchingLnWHT.APMatchingLnWHTAmtDebTC
                    tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditTC + tSPTAPMatchingLnWHT.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 + tSPTAPMatchingLnWHT.APMatchingLnWHTAmtDebLC
                    tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC       = tExtCInvoiceWHT.CInvoiceWHTAmtCreditLC + tSPTAPMatchingLnWHT.APMatchingLnWHTAmtCredLC.
        end. /* for each tSPTAPMatchingLnWHT where */
    end.  /* for each tSPTAPMatchingLn where */        
    
    /* ==================================================================== */
    /* Open BCInvoice and call the method that will update the invoice-data */
    /* Note that this has to be done in a different transaction             */
    /* ==================================================================== */
    <I-25 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION = "false"
         &CLASS              = "BCInvoice"}>
    <M-29 run ApiUpdateCInvoiceFromAPM
       (input  iiCInvoiceID (iiCInvoiceId), 
        input  ? (iiCompanyId), 
        input  ? (iiCInvoiceRegistrationNr), 
        input  vdTotalInvoiceAmountTC (idCinvoiceTotalAmount), 
        input  tExtCInvoiceVat (tApiCInvoiceVat), 
        input  tExtCInvoiceWHT (tApiCInvoiceWHT), 
        output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
    <I-38 {bFcCloseAndStopInstance
         &CLASS           = "BCInvoice"}>
    if viExternalReturnStatus <> 0 
    then assign viLocalReturnStatus = viExternalReturnStatus.
    if viExternalReturnStatus < 0
    then Leave UPDATECINVOICEBLOCK.
    
END. /* UPDATECINVOICEBLOCK */   

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus  = viLocalReturnStatus.