project QadFinancials > class BCInvoice > method AdditionalUpdatesAllCreateCAWHT

Description

This method is a submethod of AdditionalUpdatesCreateCA that will hanlde the actions in case WHT is involved on the invoices


Parameters


icCAPostingTcRowidinputcharacterTc_rowid of the adjustment-posting
idMainControlDebitTCinputdecimalThe Debit-amount that the caller has posted on the control-account for the invoice identofied by tCInvoice.CInvoice_ID
idMainControlCreditTCinputdecimalThe Credit-amount that the caller has posted on the control-account for the invoice identofied by tCInvoice.CInvoice_ID
ilSwitchDebitCreditinputlogicalThis parameter needs to be set to true when the amounts for the movement on the invoice and the amounts for the movement on the linked invoice are the same but credit becomes debit and vice-versa
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.AdditionalUpdatesAllCreateCA


program code (program8/bcinvoice.p)

     
    /* ============================================================================== */
    /* General note: BJournalEntry is already started and opened by the caller-method */
    /* ============================================================================== */
    
    /* =================== */
    /* Exception Handling  */
    /* =================== */
    assign oiReturnStatus  = -98.
    
    /* ======================================================================================================================================= */
    /* The caller of this method is located in the same segment as this method and as such we have some records available already: verify this */
    /* This method should only be called in case WHT is involved - the caller should have checked this                                         */
    /* ======================================================================================================================================= */
    if not available tCInvoice or 
       not available tqCInvoiceForCreateCA
    then do :
        assign vcMessage      = trim(#T-62'Internal error: some records that are expected to be available are missing.':255(189078370)T-62#)
               oiReturnStatus = -1.
        <M-3 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-944392':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        return.
    end. /* if not available tqCInvoiceForCreateCA */
    if (tCInvoice.CInvoiceWHTTotAmtDebitTC = 0 or 
        tCInvoice.CInvoiceWHTTotAmtDebitTC = ?) and  
       (tCInvoice.CInvoiceWHTTotAmtCreditTC = 0 or 
        tCInvoice.CInvoiceWHTTotAmtCreditTC = ?)
    then do :    
        assign vcMessage      = trim(#T-20'Internal error: This method should only be called in case WHT is involved':255(502239995)T-20#)
               oiReturnStatus = -1.
        <M-4 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-788960':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        return.
    end. /* if (tCInvoice.CInvoiceWHTTotAmtDebitTC = 0 or */
    
    /* ============================================================================================================================= */
    /* Calculate the WHT-amounts for the posting-line on the control-account of the CInvoice identified by tCInvoice.CInvoice_ID      */
    /* ============================================================================================================================= */
    assign vdAmountTC = Absolute(tCInvoice.CInvoiceWHTTotAmtDebitTC - tCInvoice.CInvoiceWHTTotAmtCreditTC)
           vdAmountLC = Absolute(tCInvoice.CInvoiceWHTTotAmtDebitLC - tCInvoice.CInvoiceWHTTotAmtCreditLC)
           vdAmountCC = Absolute(tCInvoice.CInvoiceWHTTotAmtDebitCC - tCInvoice.CInvoiceWHTTotAmtCreditCC).
    /* Negative amount needed for corrections */
    if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} or 
       tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
    then assign vdAmountTC = vdAmountTC * -1
                vdAmountLC = vdAmountLC * -1 
                vdAmountCC = vdAmountCC * -1.
    /* Movement should be on debit for Invoice and for invoice-correction */
    /* Movement should be on credit for Credit-note and for Credit-note-correction */
    if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
       tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
    then assign vdWHTControlDebitTC  = vdAmountTC
                vdWHTControlDebitLC  = vdAmountLC
                vdWHTControlDebitCC  = vdAmountCC
                vdWHTControlCreditTC = 0
                vdWHTControlCreditLC = 0 
                vdWHTControlCreditCC = 0.
    else assign vdWHTControlDebitTC  = 0
                vdWHTControlDebitLC  = 0
                vdWHTControlDebitCC  = 0
                vdWHTControlCreditTC = vdAmountTC
                vdWHTControlCreditLC = vdAmountLC
                vdWHTControlCreditCC = vdAmountCC.
    
    /* ======================================================================================================================== */
    /* Create the main movement record for WHT-amount on the control account of the invoice identified by tCInvoice.CInvoice_ID */
    /* Next to it, create the simalar on for the invoice identified by tCInvoice.LinkedCInvoice_ID (=tqCInvoiceForCreateCA)     */
    /* ======================================================================================================================== */
    create tMovement.
    assign tMovement.tdAmountDebitTC            = vdWHTControlDebitTC
           tMovement.tdAmountCreditTC           = vdWHTControlCreditTC
           tMovement.tdAmountDebitLC            = vdWHTControlDebitLC
           tMovement.tdAmountCreditLC           = vdWHTControlCreditLC
           tMovement.tdAmountDebitCC            = vdWHTControlDebitCC
           tMovement.tdAmountCreditCC           = vdWHTControlCreditCC
           tMovement.tdRateTCLC                 = tCInvoice.CInvoiceExchangeRate
           tMovement.tdScaleTCLC                = tCInvoice.CinvoiceRateScale
           tMovement.tdRateTCCC                 = tCInvoice.CInvoiceCCRate
           tMovement.tdScaleTCCC                = tCInvoice.CInvoiceCCScale
           tMovement.tiCInvoiceId               = tCInvoice.CInvoice_ID
           tMovement.tcGLAccountDivisionCode    = tCInvoice.tcDivisionCode
           tMovement.tiPostingLine_ID           = 0 
           tMovement.tcPostingRowId             = icCAPostingTcRowid
           tMovement.tcPostingText              = tCInvoice.CInvoiceDescription
           tMovement.tdCInvoiceHoldAmountTC     = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
           tMovement.tdMovementDiscountTC       = 0
           tMovement.tlIsUndoPayment            = false
           tMovement.tlMovementIsAboutWHT       = false
           tMovement.ttPaymentDate              = tCInvoice.CInvoicePostingDate
           tMovement.tlIsDelTaxAtPartialPayment = false.
    Create btMovement.
    Buffer-copy tMovement
        except tMovement.tiCInvoiceId
               tMovement.tcGLAccountDivisionCode
        to btMovement
        assign btMovement.tiCInvoiceId            = tqCInvoiceForCreateCA.tiCInvoice_ID
               btMovement.tcGLAccountDivisionCode = tqCInvoiceForCreateCA.tcDivisionCode.
    if ilSwitchDebitCredit = true
    then assign vdTemp                      = btMovement.tdAmountDebitTC
                btMovement.tdAmountDebitTC  = btMovement.tdAmountCreditTC
                btMovement.tdAmountCreditTC = vdTemp
                vdTemp                      = btMovement.tdAmountDebitLC
                btMovement.tdAmountDebitLC  = btMovement.tdAmountCreditLC
                btMovement.tdAmountCreditLC = vdTemp
                vdTemp                      = btMovement.tdAmountDebitCC
                btMovement.tdAmountDebitCC  = btMovement.tdAmountCreditCC
                btMovement.tdAmountCreditCC = vdTemp.
    else assign btMovement.tdAmountDebitTC  = btMovement.tdAmountDebitTC * -1
                btMovement.tdAmountCreditTC = btMovement.tdAmountCreditTC * -1
                btMovement.tdAmountDebitLC  = btMovement.tdAmountDebitLC * -1
                btMovement.tdAmountCreditLC = btMovement.tdAmountCreditLC * -1
                btMovement.tdAmountDebitCC  = btMovement.tdAmountDebitCC * -1
                btMovement.tdAmountCreditCC = btMovement.tdAmountCreditCC * -1.
    
    /* ====================================================================================================================== */
    /* Create the movement record on the WHT-accounts for the WHT-amount of the invoice identified by tCInvoice.CInvoice_ID  */
    /* The debit of the previous tMovement becomes here the credit and vice-versa - but the signs remain the same             */
    /* Next to it, create the simalar on for the invoice identified by tCInvoice.LinkedCInvoice_ID (=tqCInvoiceForCreateCA)   */
    /* ====================================================================================================================== */
    create tMovement.
    assign tMovement.tdAmountDebitTC         = vdWHTControlCreditTC
           tMovement.tdAmountCreditTC        = vdWHTControlDebitTC
           tMovement.tdAmountDebitLC         = vdWHTControlCreditLC
           tMovement.tdAmountCreditLC        = vdWHTControlDebitLC
           tMovement.tdAmountDebitCC         = vdWHTControlCreditCC
           tMovement.tdAmountCreditCC        = vdWHTControlDebitCC
           tMovement.tdRateTCLC              = tCInvoice.CInvoiceExchangeRate
           tMovement.tdScaleTCLC             = tCInvoice.CinvoiceRateScale
           tMovement.tdRateTCCC              = tCInvoice.CInvoiceCCRate
           tMovement.tdScaleTCCC             = tCInvoice.CInvoiceCCScale
           tMovement.tiCInvoiceId            = tCInvoice.CInvoice_ID
           tMovement.tcGLAccountDivisionCode = tCInvoice.tcDivisionCode
           tMovement.tiPostingLine_ID        = 0 
           tMovement.tcPostingRowId          = icCAPostingTcRowid
           tMovement.tcPostingText           = tCInvoice.CInvoiceDescription
           tMovement.tdCInvoiceHoldAmountTC  = ? /* Set to unknown-value to make sure this field in the invoice is not updated */
           tMovement.tdMovementDiscountTC    = 0
           tMovement.tlIsUndoPayment         = false
           tMovement.tlMovementIsAboutWHT    = true
           tMovement.ttPaymentDate           = tCInvoice.CInvoicePostingDate.
    Create btMovement.
    Buffer-copy tMovement 
        except tMovement.tiCInvoiceId
               tMovement.tcGLAccountDivisionCode
        to btMovement
        assign btMovement.tiCInvoiceId            = tqCInvoiceForCreateCA.tiCInvoice_ID
               btMovement.tcGLAccountDivisionCode = tqCInvoiceForCreateCA.tcDivisionCode.
    if ilSwitchDebitCredit = true
    then assign vdTemp                      = btMovement.tdAmountDebitTC
                btMovement.tdAmountDebitTC  = btMovement.tdAmountCreditTC
                btMovement.tdAmountCreditTC = vdTemp
                vdTemp                      = btMovement.tdAmountDebitLC
                btMovement.tdAmountDebitLC  = btMovement.tdAmountCreditLC
                btMovement.tdAmountCreditLC = vdTemp
                vdTemp                      = btMovement.tdAmountDebitCC
                btMovement.tdAmountDebitCC  = btMovement.tdAmountCreditCC
                btMovement.tdAmountCreditCC = vdTemp.
    else assign btMovement.tdAmountDebitTC  = btMovement.tdAmountDebitTC * -1
                btMovement.tdAmountCreditTC = btMovement.tdAmountCreditTC * -1
                btMovement.tdAmountDebitLC  = btMovement.tdAmountDebitLC * -1
                btMovement.tdAmountCreditLC = btMovement.tdAmountCreditLC * -1
                btMovement.tdAmountDebitCC  = btMovement.tdAmountDebitCC * -1
                btMovement.tdAmountCreditCC = btMovement.tdAmountCreditCC * -1.
    
    /* ================================================================ */
    /* Get a WHT-number,retrieved for the entity/domain of the invoice  */
    /* ================================================================ */
    assign vhFcComponent = ?.
    <M-33 run GetNumber
       (input  tCInvoice.Company_ID (iiCompanyId), 
        input  9999 (iiNumbrYear), 
        input  'WHT':U (icNumbrType), 
        output viWHTNumber (oiNumber), 
        input  viFcCurrentInstanceId (iiInstanceId), 
        input  vcFcComponentName (icClassName), 
        output viFcReturnSuper (oiReturnStatus)) in BNumber>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then Return.
    assign vcReleaseWHTNumberListCI = vcReleaseWHTNumberListCI + chr(4) + string(viWHTNumber).
    
    /* ================================================= */
    /* Get the WHT-rates between invoice currency and LC */
    /* Get the WHT-rates between invoice currency and CC */
    /* ================================================= */
    if tCInvoice.CInvoiceCurrency_ID = viCompanyLCId
    then assign vdWHTTCLCExchangeRate = 1
                vdWHTTCLCRateScale    = 1.
    else do:
        
        <M-63 run GetExRateByEntityInvoiceFlag
           (input  tCInvoice.CInvoiceDate (itCInvoiceDate), 
            input  tCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
            input  tCInvoice.CInvoiceCurrency_ID (iiFromCurrencyId), 
            input  '':U (icFromCurrencyCode), 
            input  viCompanyLCId (iiToCurrencyId), 
            input  '':U (icToCurrencyCode), 
            input  ? (iiExchangeRateTypeId), 
            input  {&EXCHANGERATETYPE-VAT} (icExchangeRateTypeCode), 
            output vdWHTTCLCExchangeRate (odCInvoiceExchangeRate), 
            output vdWHTTCLCRateScale (odCInvoiceRateScale), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper <  0 then Return.
    end. /* else if tCInvoice.CInvoiceCurrency_ID = viCompanyLCId */
    if tCInvoice.CInvoiceCurrency_ID = viCompanyCCId
    then assign vdWHTTCCCExchangeRate = 1
                vdWHTTCCCRateScale    = 1.
    else do:
        
        <M-50 run GetExRateByEntityInvoiceFlag
           (input  tCInvoice.CInvoiceDate (itCInvoiceDate), 
            input  tCInvoice.CInvoicePostingDate (itCInvoicePostingDate), 
            input  tCInvoice.CInvoiceCurrency_ID (iiFromCurrencyId), 
            input  '':U (icFromCurrencyCode), 
            input  viCompanyCCId (iiToCurrencyId), 
            input  '':U (icToCurrencyCode), 
            input  ? (iiExchangeRateTypeId), 
            input  {&EXCHANGERATETYPE-VAT} (icExchangeRateTypeCode), 
            output vdWHTTCCCExchangeRate (odCInvoiceExchangeRate), 
            output vdWHTTCCCRateScale (odCInvoiceRateScale), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper <  0 then Return.
    end. /* else tCInvoice.CInvoiceCurrency_ID */      
    
    /* ======================================================================================================================== */
    /* Create the WHT - Note that only the sign of fields tdWHTTaxableFeeTC and tdWHTAmountTC matters based on the invoice-type */
    /* ======================================================================================================================== */
    create tInvoiceWHTPaymentForCI.
    assign tInvoiceWHTPaymentForCI.tiCInvoiceID              = tCInvoice.CInvoice_ID
           tInvoiceWHTPaymentForCI.tdAllocatedAmountTC       = Absolute(idMainControlDebitTC - idMainControlCreditTC) +  
                                                               Absolute(vdWHTControlDebitTC - vdWHTControlCreditTC)
           tInvoiceWHTPaymentForCI.tdDiscountAmountTC        = 0
           tInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC         = Absolute(idMainControlDebitTC - idMainControlCreditTC) +  
                                                               Absolute(vdWHTControlDebitTC - vdWHTControlCreditTC)
           tInvoiceWHTPaymentForCI.tdWHTAmountTC             = Absolute(vdWHTControlDebitTC - vdWHTControlCreditTC)
           tInvoiceWHTPaymentForCI.tlCreateWHT               = true                   
           tInvoiceWHTPaymentForCI.tiWHTNumber               = viWHTNumber 
           tInvoiceWHTPaymentForCI.tcPaymentReference        = string(tCInvoice.CInvoicePostingYear,"9999") + "/" +
                                                               trim(string(tCInvoice.tcCAJournalCode)) + "/" +
                                                               trim(string(tCInvoice.tiCAVoucher))
           tInvoiceWHTPaymentForCI.tcCurrencyCode            = tCInvoice.tcCurrencyCode
           tInvoiceWHTPaymentForCI.tiCDocumentInvoiceXrefID  = ?
           tInvoiceWHTPaymentForCI.tiBankStateAllocID        = ?
           tInvoiceWHTPaymentForCI.tiOIAjustLnID             = ?
           tInvoiceWHTPaymentForCI.tiCACInvoiceID            = tCInvoice.CInvoice_ID
           tInvoiceWHTPaymentForCI.ttPostingDate             = tCInvoice.CInvoicePostingDate
           tInvoiceWHTPaymentForCI.tcWHTStatus               = {&WHTSTATUS-DUE}
           tInvoiceWHTPaymentForCI.ttSettlementDate          = ?
           tInvoiceWHTPaymentForCI.tlUpdateAllAmounts        = false
           tInvoiceWHTPaymentForCI.tdPaymentWHTRateTCLC      = vdWHTTCLCExchangeRate
           tInvoiceWHTPaymentForCI.tdPaymentWHTScaleTCLC     = vdWHTTCLCRateScale
           tInvoiceWHTPaymentForCI.tdPaymentWHTRateTCCC      = vdWHTTCCCExchangeRate
           tInvoiceWHTPaymentForCI.tdPaymentWHTScaleTCCC     = vdWHTTCCCRateScale
           tInvoiceWHTPaymentForCI.tdPaymentAccRateTCLC      = tCInvoice.CInvoiceExchangeRate
           tInvoiceWHTPaymentForCI.tdPaymentAccScaleTCLC     = tCInvoice.CInvoiceRateScale
           tInvoiceWHTPaymentForCI.tdPaymentAccRateTCCC      = tCInvoice.CInvoiceCCRate
           tInvoiceWHTPaymentForCI.tdPaymentAccScaleTCCC     = tCInvoice.CInvoiceCCScale
           tInvoiceWHTPaymentForCI.tiPaymentAccountingYear   = tCInvoice.CInvoicePostingYear
           tInvoiceWHTPaymentForCI.tiPaymentAccountingPeriod = tCInvoice.CInvoicePostingPeriod.
    if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} Or 
       tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
    then assign tInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC = tInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC * -1
                tInvoiceWHTPaymentForCI.tdWHTAmountTC     = tInvoiceWHTPaymentForCI.tdWHTAmountTC * -1.
    
    /* ======================================================================================================================================== */
    /* As the invoice identified by tCInvoice.CInvoice_ID is not yet stored in the database and as the method we call in BWithholdingTax wants  */
    /* to use the CI-related data from the db (and it will thus not be able to find it), we have to make sure we pass in this information via   */
    /* temp-table tInvoiceWHTPaymentCIWHTForCI.                                                                                                 */
    /* Note that we need to create a tInvoiceWHTPaymentCIWHTForCI-record for every tCInvoiceWHT-record                                          */
    /* ======================================================================================================================================== */
    for each tCInvoiceWHT where 
             tCInvoiceWHT.CInvoice_ID = tCInvoice.CInvoice_ID and 
             tCInvoiceWHT.tc_Status <> "D":U 
             no-lock :
        /* Assign fields based upon the instance-data */
        Create tInvoiceWHTPaymentCIWHTForCI.
        assign tInvoiceWHTPaymentCIWHTForCI.tiCInvoice_ID                = tInvoiceWHTPaymentForCI.tiCInvoiceID /* This is the link hetween both tables */
               tInvoiceWHTPaymentCIWHTForCI.tiCInvoiceCurrency_ID        = tCInvoice.CInvoiceCurrency_ID
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceCCRate             = tCInvoice.CInvoiceCCRate
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceCCScale            = tCInvoice.CInvoiceCCScale
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceExchangeRate       = tCInvoice.CInvoiceExchangeRate
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceRateScale          = tCInvoice.CInvoiceRateScale
               tInvoiceWHTPaymentCIWHTForCI.tcCInvoiceReference          = tCInvoice.CInvoiceReference
               tInvoiceWHTPaymentCIWHTForCI.tlCInvoiceIsWHT              = tCInvoice.CInvoiceIsWHT
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceOriginalCreditTC   = tCInvoice.CInvoiceOriginalCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceOriginalDebitTC    = tCInvoice.CInvoiceOriginalDebitTC
               tInvoiceWHTPaymentCIWHTForCI.ttCInvoicePostingDate        = tCInvoice.CInvoicePostingDate
               tInvoiceWHTPaymentCIWHTForCI.tiCInvoicePostingYear        = tCInvoice.CInvoicePostingYear
               tInvoiceWHTPaymentCIWHTForCI.tcCInvoiceType               = tCInvoice.CInvoiceType
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceVatBaseCreditTC    = tCInvoice.CInvoiceVatBaseCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceVatBaseDebitTC     = tCInvoice.CInvoiceVatBaseDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceVatCreditTC        = tCInvoice.CInvoiceVatCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceVatDebitTC         = tCInvoice.CInvoiceVatDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tiCInvoiceVoucher            = tCInvoice.CInvoiceVoucher
               tInvoiceWHTPaymentCIWHTForCI.tiCompany_ID                 = viCompanyId   /* Invoices can only be created in the current company */
               tInvoiceWHTPaymentCIWHTForCI.tcCompanyCode                = vcCompanyCode /* Invoices can only be created in the current company */
               tInvoiceWHTPaymentCIWHTForCI.tiLocalCurrencyCurrency_ID   = viCompanyLCId /* Invoices can only be created in the current company */
               tInvoiceWHTPaymentCIWHTForCI.tcLocalCurrencyCurrencyCode  = vcCompanyLC   /* Invoices can only be created in the current company */
               tInvoiceWHTPaymentCIWHTForCI.tcCostCentreCode             = tCInvoice.tcCostCentreCode
               tInvoiceWHTPaymentCIWHTForCI.tiCreditor_ID                = tCInvoice.Creditor_ID
               tInvoiceWHTPaymentCIWHTForCI.tcCreditorCode               = tCInvoice.tcCreditorCode
               tInvoiceWHTPaymentCIWHTForCI.tiDivision_ID                = tCInvoice.Division_ID
               tInvoiceWHTPaymentCIWHTForCI.tcDivisionCode               = tCInvoice.tcDivisionCode
               tInvoiceWHTPaymentCIWHTForCI.tiJournal_ID                 = tCInvoice.Journal_ID
               tInvoiceWHTPaymentCIWHTForCI.tcJournalCode                = tCInvoice.tcJournalCode
               tInvoiceWHTPaymentCIWHTForCI.tcProjectCode                = tCInvoice.tcProjectCode
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTBaseAmtCreditTC = tCInvoice.CInvoiceWHTBaseAmtCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTBaseAmtDebitTC  = tCInvoice.CInvoiceWHTBaseAmtDebitTC  
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTTotAmtCreditTC  = tCInvoice.CInvoiceWHTTotAmtCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTTotAmtDebitTC   = tCInvoice.CInvoiceWHTTotAmtDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tiCInvoiceWHT_ID             = tCInvoiceWHT.CInvoiceWHT_ID
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTAmtCreditTC     = tCInvoiceWHT.CInvoiceWHTAmtCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTAmtDebitTC      = tCInvoiceWHT.CInvoiceWHTAmtDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTExpenseCreditTC = tCInvoiceWHT.CInvoiceWHTExpenseCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTExpenseDebitTC  = tCInvoiceWHT.CInvoiceWHTExpenseDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTFeeCreditTC     = tCInvoiceWHT.CInvoiceWHTFeeCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTFeeDebitTC      = tCInvoiceWHT.CInvoiceWHTFeeDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTTaxFeeCreditTC  = tCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC
               tInvoiceWHTPaymentCIWHTForCI.tdCInvoiceWHTTaxFeeDebitTC   = tCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC
               tInvoiceWHTPaymentCIWHTForCI.tiGL_ID                      = tCInvoiceWHT.GL_ID
               tInvoiceWHTPaymentCIWHTForCI.tcGLCode                     = tCInvoiceWHT.tcGLCode
               tInvoiceWHTPaymentCIWHTForCI.tdtx2_tax_pct                = tCInvoiceWHT.CInvoiceWHTTaxPct
               tInvoiceWHTPaymentCIWHTForCI.tctx2_tax_type               = tCInvoiceWHT.CInvoiceWHTTaxType
               tInvoiceWHTPaymentCIWHTForCI.tltx2_update_tax             = tCInvoiceWHT.CInvoiceWHTIsUpdAllow
               tInvoiceWHTPaymentCIWHTForCI.tiVat_ID                     = tCInvoiceWHT.Vat_ID
               tInvoiceWHTPaymentCIWHTForCI.tcVatCode                    = tCInvoiceWHT.tcVatCode.
        /* Assign fields based upon the creditor */               
        <Q-38 run CreditorByIDCodeSharedSet (all) (Read) (Cache)
           (input ?, (CompanyId)
            input tCInvoice.Creditor_ID, (CreditorID)
            output dataset tqCreditorByIDCodeSharedSet) in BCreditor>
        Find tqCreditorByIDCodeSharedSet where
             tqCreditorByIDCodeSharedSet.tiCreditor_ID = tCInvoice.Creditor_ID
             no-lock no-error.
        If available tqCreditorByIDCodeSharedSet
        then assign tInvoiceWHTPaymentCIWHTForCI.tiSharedSet_ID      = tqCreditorByIDCodeSharedSet.tiSharedSet_ID
                    tInvoiceWHTPaymentCIWHTForCI.tcSharedSetCode     = tqCreditorByIDCodeSharedSet.tcSharedSetCode
                    tInvoiceWHTPaymentCIWHTForCI.tiWHTCertFormat_ID  = tqCreditorByIDCodeSharedSet.tiWHTCertFormat_ID
                    tInvoiceWHTPaymentCIWHTForCI.tcWHTCertFormatCode = tqCreditorByIDCodeSharedSet.tcWHTCertFormatCode.
    end. /* for each tCInvoiceWHT where */
    
    /* ========================================================================================================= */
    /* Create the similar WHT for the invoice identified by tCInvoice.LinkedCInvoice_ID (=tqCInvoiceForCreateCA) */
    /* Note that only the sign of fields tdWHTTaxableFeeTC and tdWHTAmountTC matters based on the invoice-type  */
    /* ========================================================================================================= */
    Create btInvoiceWHTPaymentForCI.
    Buffer-copy tInvoiceWHTPaymentForCI 
        except tInvoiceWHTPaymentForCI.tiCInvoiceID
        to btInvoiceWHTPaymentForCI
        assign btInvoiceWHTPaymentForCI.tiCInvoiceId      = tqCInvoiceForCreateCA.tiCInvoice_ID /* keep the value for tInvoiceWHTPaymentForCI.tiCACInvoiceID as it is as both these tInvoiceWHTPaymentForCI are based upon the same supplier-adjustment */
               btInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC = absolute(tInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC)
               btInvoiceWHTPaymentForCI.tdWHTAmountTC     = absolute(tInvoiceWHTPaymentForCI.tdWHTAmountTC).
    if tqCInvoiceForCreateCA.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION} Or 
       tqCInvoiceForCreateCA.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
    then assign btInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC = btInvoiceWHTPaymentForCI.tdWHTTaxableFeeTC * -1
                btInvoiceWHTPaymentForCI.tdWHTAmountTC     = btInvoiceWHTPaymentForCI.tdWHTAmountTC * -1.
    
    /* ================== */
    /* Exception handling */
    /* ================== */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.