project QadFinancials > class BAPMatching > method CreateAPMatchingLNTaxFromGTM

Description

CreateAPMatchingLNTaxFromGTM; this public and remoting-enabled method will be called from GetPendingVouchers and from the UI.
Input: all required information for calling GTM
Output: temp-table like APMatchingLnTax


Parameters


icAPMatchingTcRowidinputcharacterTc_rowid field of the APMatching record that holds this matching data
icAPMatchingLnTcRowidinputcharacterTc_rowid field of the APMatchingLn record that is the parent for the new output temp-table records in APMatchingLnTaxFromGTM
icCurrencyCodeinputcharacterCurrency code
icTransactionTypeinputcharacterTransaction type (use preprocessors for this)
icDocumentReferenceinputcharacterDocumentReference
icDocumentNumberinputcharacterDocumentNumber
ilIsTaxableinputlogicalIs this taxable
ilIsTaxIncludedInTheAmountinputlogicalIsTaxIncluded
itTaxPointDateinputdateDate used for the tax calculations
itPostingDateinputdateDate of the posting that will hold these taxes
icTaxEnvCodeinputcharacterTaxEnvCode
icFromTaxZoneCodeinputcharacterFromTaxZoneCode
icToTaxZoneCodeinputcharacterToTaxZoneCode
icTaxClassCodeinputcharacterTaxClassCode
icTaxUsageCodeinputcharacterTaxUsageCode
idAmountTCinputdecimalInvoice amount with or without taxes (depending upon IsTaxIncludedInTheAmount)
idCInvoiceTCLCRateinputdecimalExchangeRate
idCInvoiceTCLCScaleinputdecimalExchangeRateScale
idCInvoiceTCCCRateinputdecimal
idCInvoiceTCCCScaleinputdecimalCInvoiceTCCCScale
icCompanyCodeinputcharacterCompanyCode
idAdjustmentFactorinputdecimalAdjustmentFactor: should be 1 when the discount-at-invoice-percentage is passed
idPaymentConditionPercentageinputdecimalPaymentConditionPercentage of the discount at invoice
ilIsTaxInCityOfShipToinputlogicalIsTaxInCityOfShipTo: true in case the TaxInCity flag of the ship-to address is true
ilIsTaxInCityOfShipFrominputlogicalIsTaxInCityOfShipFrom: true in case the TaxInCity flag of the ship-from address is true
ilNeedToStartPGTMFromHereinputlogicalNeedToStartPGTMFromHere: pass false in case the caller has already started and opened class PGTM
tAPMatchingLnTaxFromGTMoutputtemp-tableTemp-table containing the information returned by GTM converted to the format of APMatchingLnTax records
tAPMatchingLnWHTFromGTMoutputtemp-tableTemp-table containing the information returned by GTM converted to the format of APMatchingLnWHT records
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.AddAPMatchingLnLC
method BAPMatching.AddAPMatchingLnNoLC
method BAPMatching.GetLogChargesPVOFromHeaderInfo
method BAPMatching.GetPendingVouchers


program code (program7/bapmatching.p)

/* ============================================================== */
/* Exception handling                                             */
/* Clear output-temp-table and the temp-table with the GTM output */
/* ============================================================== */
assign viLocalReturnStatus = 0
       oiReturnStatus      = -98.
empty temp-table tTaxDetailDataFromGTM.
empty temp-table tAPMatchingLnTaxFromGTM.
empty temp-table tAPMatchingLnWHTFromGTM.

/* ================== */
/* Start block        */
/* ================== */
PGTMCALLINGBLOCK: DO :

    /* ==================================== */
    /* Parameter validations and defaulting */
    /* ==================================== */
    assign vcMsgAPMatching = "":U.
    if icAPMatchingLnTcRowid = "":U and icAPMatchingLnTcRowid = ? /* do not validate the existance with this rowid as this can be a tPendingVoucherAPM-record as well */
    then assign vcMsgAPMatching = trim(substitute(#T-10'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-10#,trim(#T-11'Row ID of the matching line':40(68614)t-11#))).
    if icAPMatchingTcRowid = "":U and icAPMatchingTcRowid = ? 
    then assign vcMsgAPMatching = trim(substitute(#T-35'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-35#,trim(#T-36'Row ID of the Matching':40(68750)t-36#))).
    else do :
        find tAPMatching where 
             tAPMatching.tc_Rowid = icAPMatchingTcRowid
             no-lock no-error.
        if not available tAPMatching
        then assign vcMsgAPMatching = trim(substitute(#T-37'The system cannot call GTM because an invalid row ID (&1) of the matching was received.':255(68751)t-37#,icAPMatchingTcRowid)).
    end. /* Not if icAPMatchingTcRowid = "":U and icAPMatchingTcRowid = ?  */
    if vcMsgAPMatching = "":U and icCurrencyCode = "":U or icCurrencyCode = ?
    then assign vcMsgAPMatching = trim(substitute(#T-6'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-6#,trim(#T-7'Currency':20(68612)T-7#))).
    if vcMsgAPMatching = "":U and ilIsTaxable = ?
    then assign vcMsgAPMatching = trim(substitute(#T-8'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-8#,trim(#T-9'Is Taxable':20(68613)T-9#))).
    if vcMsgAPMatching = "":U and ilIsTaxIncludedInTheAmount = ?
    then assign vcMsgAPMatching = trim(substitute(#T-12'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-12#,trim(#T-13'Is Tax included in the Amount':40(68615)T-13#))).
    if vcMsgAPMatching = "":U and itTaxPointDate = ?
    then assign vcMsgAPMatching = trim(substitute(#T-14'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-14#,trim(#T-15'Tax Point Date':20(68616)T-15#))).
    if vcMsgAPMatching = "":U and itPostingDate = ?
    then assign vcMsgAPMatching = trim(substitute(#T-16'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-16#,trim(#T-17'Posting Date':20(68617)T-17#))).
    if vcMsgAPMatching = "":U and (icTaxEnvCode = "":U or icTaxEnvCode = ?)
    then assign vcMsgAPMatching = trim(substitute(#T-18'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-18#,trim(#T-19'Tax Environment':20(68618)T-19#))).
    if vcMsgAPMatching = "":U and ilIsTaxInCityOfShipFrom = ?
    then assign vcMsgAPMatching = trim(substitute(#T-22'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-22#,trim(#T-23'In City field of the tax for the ship-from.':60(68619)t-23#))).
    if vcMsgAPMatching = "":U and ilIsTaxInCityOfShipTo = ?
    then assign vcMsgAPMatching = trim(substitute(#T-24'The system cannot call GTM because a mandatory parameter (&1) is missing.':255(68611)t-24#,trim(#T-25'In City field of the tax for the ship-to.':60(68620)t-25#))).
    if vcMsgAPMatching = "":U and icCompanyCode <> "":U and icCompanyCode <> ? and icCompanyCode <> vcCompanyCode
    then assign vcMsgAPMatching = trim(substitute(#T-34'The system cannot calculate taxes because the entity code (&1) specified is not the current entity (&2).':255(68784)t-34#,icCompanyCode,vcCompanyCode)).
    else assign icCompanyCode = vcCompanyCode.
    if idAmountTC = ? 
    then assign idAmountTC = 0.
    if icTransactionType = "":U or icTransactionType = ?
    then assign icTransactionType = {&VATTAXTRANSACTIONTYPE-APVOUCHER}.
    if idCInvoiceTCLCRate = ? or idCInvoiceTCLCRate = 0
    then assign idCInvoiceTCLCRate = 1.
    if idCInvoiceTCLCScale = ? or idCInvoiceTCLCScale = 0
    then assign idCInvoiceTCLCScale = 1.
    if idAdjustmentFactor = 0 or idAdjustmentFactor = ?
    then assign idAdjustmentFactor = 1.
    if idPaymentConditionPercentage = ? 
    then assign idPaymentConditionPercentage = 0.
    if vcMsgAPMatching <> "":U
    then do :
        assign viLocalReturnStatus = -3.
        <M-5 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-7687':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave PGTMCALLINGBLOCK.
    end. /* if vcMsgAPMatching <> "":U */
    
    /* =============== */
    /* Start&Open PGTM */
    /* =============== */
    if viPGTMFromAPMatchingID = 0 or
       viPGTMFromAPMatchingID = ?
    then do :
        /* Error in case PGTM is not started yet and we shouldn't start it from here */
        if ilNeedToStartPGTMFromHere = false
        then do :
            assign viLocalReturnStatus = -3
                   vcMsgAPMatching     = trim(substitute(#T-27'Internal error: class PGTM is not started yet although it was indicated that it was already started.':255(68661)t-27#)).
            <M-26 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-7698':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            Leave PGTMCALLINGBLOCK.
        end. /* if ilNeedToStartPGTMFromHere = false */
        <I-1 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION = "false"
             &CLASS              = "PGTM"}>
    end. /* if viPGTMFromAPMatchingID = 0 and */
    if not valid-handle (vhPGTMFromAPMatchingInst)
    then do :
        assign viLocalReturnStatus = -3
               vcMsgAPMatching     = trim(substitute(#T-29'Internal error. The PGTM class is not started and opened yet although it was indicated that it was already started.':255(68660)T-29#)).
        <M-28 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-7703':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        Leave PGTMCALLINGBLOCK.
    end. /* if not valid-handle (vhPGTMFromAPMatchingInst) */
    
    /* ============================= */
    /* Call PGTM.CalculateTaxByParam */
    /* ============================= */
    <M-4 run CalculateTaxByParam
       (input  icCurrencyCode (icCurrencyCode), 
        input  icTransactionType (icTransactionType), 
        input  icDocumentReference (icDocumentReference), 
        input  icDocumentNumber (icDocumentNumber), 
        input  ilIsTaxable (ilTaxable), 
        input  ilIsTaxIncludedInTheAmount (ilTaxIsIncluded), 
        input  itTaxPointDate (itTaxPointDate), 
        input  itPostingDate (itPostingDate), 
        input  icTaxEnvCode (icTaxEnvCode), 
        input  icFromTaxZoneCode (icFromTaxZoneCode), 
        input  icToTaxZoneCode (icToTaxZoneCode), 
        input  icTaxClassCode (icTaxClassCode), 
        input  icTaxUsageCode (icTaxUsageCode), 
        input  idAmountTC (idAmountTC), 
        input  idCInvoiceTCLCScale (idExchangeRateScale), 
        input  idCInvoiceTCLCRate (idExchangeRate), 
        input  icCompanyCode (icCompanyCode), 
        input  idAdjustmentFactor (idAdjustmentFactor), 
        input  idPaymentConditionPercentage (idPaymentConditionPercentage), 
        input  ilIsTaxInCityOfShipTo (ilIsTaxInCityFromCompany), 
        input  ilIsTaxInCityOfShipFrom (ilIsTaxInCityFromShipTo), 
        input  tAPMatching.tiShipToAddress_ID (iiShipToAddressID), 
        input  tAPMatching.tiCreditorAddr_ID (iiShipFromAddressID), 
        input  '' (icShipToAddrLine1), 
        input  '' (icShipToAddrLine2), 
        input  '' (icShipToAddrLine3), 
        input  '' (icShipToAddrCity), 
        input  '' (icShipToAddrState), 
        input  '' (icShipToAddrZip), 
        input  '' (icShipToAddrCountry), 
        input  '' (icShipToAddrCounty), 
        input  ? (icCustomerCode), 
        input  false (ilIsAvataxCommit), 
        input  '' (icInvoiceType), 
        output tTaxParamExtAPMatching (tTaxParametersExtended), 
        input-output tTaxDetailDataFromGTM (tTaxDetailDataExt), 
        output viExternalReturnStatus (oiReturnStatus)) in PGTM>
    
    /* =============== */
    /* Close&Stop PGTM */
    /* =============== */
    if ilNeedToStartPGTMFromHere  = true  and 
       viPGTMFromAPMatchingID    <> 0     and 
       viPGTMFromAPMatchingID    <> ?
    then do :
         <I-3 {bFcCloseAndStopInstance
              &CLASS           = "PGTM"}>
    end. /* if viPGTMFromAPMatchingID <> 0 and */
    
    /* ========================================== */
    /* Error-hanlding on PGTM.CalculateTaxByParam */
    /* ========================================== */
    if viExternalReturnStatus <> 0 
    then assign viLocalReturnStatus = viExternalReturnStatus.
    if viExternalReturnStatus < 0
    then Leave PGTMCALLINGBLOCK.
    
    /* ================================================================ */
    /* Get setting indicating if Suspended/Delayed taxes are activated */
    /* ================================================================ */
    <Q-38 run CompanyPropertyBySuspDelTax (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input {&SUSPDELAYTAX-NOTAPPLICABLE}, (DelTax)
        input ?, (SuspTax)
        output dataset tqCompanyPropertyBySuspDelTax) in BCompanyProperty >
    find first tqCompanyPropertyBySuspDelTax where
               tqCompanyPropertyBySuspDelTax.tiCompany_ID = viCompanyId and
               tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax <> {&SUSPDELAYTAX-NOTAPPLICABLE}
               no-error.
    assign vlUseDelTax = if available tqCompanyPropertyBySuspDelTax
                         then true
                         else false.
    
    /* ========================================================= */
    /* Find out if the creditor uses withholding taxes.          */
    /* ========================================================= */
    <Q-50 run CreditorByCodeForWHT (all) (Read) (Cache)
       (input tAPMatching.Company_ID, (CompanyId)
        input tAPMatching.tcCreditorCode, (CreditorCode)
        input true, (CreditorIsWHT)
        output dataset tqCreditorByCodeForWHT) in BCreditor>
    assign vlCreditorIsWht = can-find (tqCreditorByCodeForWHT where 
                                       tqCreditorByCodeForWHT.tcCreditorCode  = tAPMatching.tcCreditorCode and 
                                       tqCreditorByCodeForWHT.tlCreditorIsWHT = true).
    
    /* ===================================================================================================== */        
    /* Convert the tTaxDetailDataFromGTM into the tAPMatchingLnTaxFromGTM that are the output of this method */
    /* ===================================================================================================== */
    for each tTaxDetailDataFromGTM :
        if tTaxDetailDataFromGTM.tx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT}  and
           (vlDomainIsWht = TRUE and vlCreditorIsWht = TRUE)
        then do:
            create tAPMatchingLnWHTFromGTM. 
            assign tAPMatchingLnWHTFromGTM.tcDomainCode               = tTaxDetailDataFromGTM.tx2d_domain
                   tAPMatchingLnWHTFromGTM.tcVatCode                  = tTaxDetailDataFromGTM.tx2d_tax_code
                   tAPMatchingLnWHTFromGTM.tcVatInOut                 = {&VATINOUT-INPUT}
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTaxType     = tTaxDetailDataFromGTM.tx2d_tax_type
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTaxTrType   = tTaxDetailDataFromGTM.tx2d_tr_type
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTxFeeDebTC  = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then 0
                                                                        else tTaxDetailDataFromGTM.tx2d_tottax
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTxFeeCredTC = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then tTaxDetailDataFromGTM.tx2d_tottax
                                                                        else 0
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTxFeeDebLC  = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then 0
                                                                        else tTaxDetailDataFromGTM.tx2d_taxable_amt
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTxFeeCredLC = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then tTaxDetailDataFromGTM.tx2d_taxable_amt
                                                                        else 0
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTFeeDebTC    = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then 0
                                                                        else idAmountTC
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTFeeCredTC   = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then idAmountTC
                                                                        else 0
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTAmtDebTC    = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then 0
                                                                        else tTaxDetailDataFromGTM.tx2d_cur_tax_amt
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTAmtCredTC   = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then tTaxDetailDataFromGTM.tx2d_cur_tax_amt
                                                                        else 0
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTAmtDebLC    = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then 0
                                                                        else tTaxDetailDataFromGTM.tx2d_tax_amt
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTAmtCredLC   = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then tTaxDetailDataFromGTM.tx2d_tax_amt
                                                                        else 0
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTIsUpdAllow  = tTaxDetailDataFromGTM.tlTx2UpdateTax
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTTaxPct      = tTaxDetailDataFromGTM.tdTx2TaxPerc
                   tAPMatchingLnWHTFromGTM.APMatchingLnWHTRecovTaxPct = tTaxDetailDataFromGTM.tdTx2RecovTaxPerc
                   tAPMatchingLnWHTFromGTM.tcGLCode                   = (if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APCNGL       else tTaxDetailDataFromGTM.tcTx2APGL)
                   tAPMatchingLnWHTFromGTM.tcDivisionCode             = (if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APCNDivision else tTaxDetailDataFromGTM.tcTx2APDivision)
                   tAPMatchingLnWHTFromGTM.APMatchingLnTax_ID         = ? /* not linked to any tax record here */
                   tAPMatchingLnWHTFromGTM.tc_ParentRowid             = icAPMatchingLnTcRowid
                   tAPMatchingLnWHTFromGTM.tc_Rowid                   = string(viRowidPendingVoucherWHTAPM)
                   tAPMatchingLnWHTFromGTM.tc_Status                  = "N":U
                   viRowidPendingVoucherWHTAPM                        = viRowidPendingVoucherWHTAPM + 1.
            <Q-82 run VATPrim (all) (Read) (NoCache)
               (input ?, (VatId)
                input tTaxDetailDataFromGTM.tx2d_tax_code, (VatCode)
                input ?, (DomainId)
                input ?, (DomainCode)
                input ?, (VatInOut)
                output dataset tqVATPrim) in BVAT >
            find first tqVatPrim where
                       tqVATPrim.tcVatCode = tTaxDetailDataFromGTM.tx2d_tax_code
                       no-lock no-error.
            assign tAPMatchingLnWHTFromGTM.tcVatDescription = if available tqVATPrim
                                                              then tqVATPrim.tcVatDescription
                                                              else "":U.
            
            /* ================================================================= */                                                          
            /* Retrieve tax base for the tax code to get the tax base percentage */
            /* ================================================================= */   
            assign tAPMatchingLnWHTFromGTM.APMatchingLnWHTBaseTaxPct = 100.
            
            <Q-87 run MfgTaxCodeByTaxCode (all) (Read) (Cache)
               (input tAPMatchingLnWHTFromGTM.tcVatCode, (TaxCode)
                input tAPMatchingLnWHTFromGTM.tcDomainCode, (DomainCode)
                input ?, (DiscountAtPayment)
                input ?, (IsTaxByLine)
                output dataset tqMfgTaxCodeByTaxCode) in BMfgTaxCode>    
                         
            find first tqMfgTaxCodeByTaxCode where
                       tqMfgTaxCodeByTaxCode.tctx2_tax_code = tAPMatchingLnWHTFromGTM.tcVatCode 
                       no-error.
            if available tqMfgTaxCodeByTaxCode
            then do:            
                if tqMfgTaxCodeByTaxCode.tctx2_base <> "" and
                   tqMfgTaxCodeByTaxCode.tctx2_base <> ?
                then do:                          
                    <Q-66 run GeneralizedCodeForSecurity (all) (Read) (Cache)
                       (input tAPMatchingLnWHTFromGTM.tcDomainCode, (DomainCode)
                        input 'txb_base', (CodeFldName)
                        input tqMfgTaxCodeByTaxCode.tctx2_base, (CodeValue)
                        output dataset tqGeneralizedCodeForSecurity) in BMfgGeneralizedCode>
                    find first tqGeneralizedCodeForSecurity where
                               tqGeneralizedCodeForSecurity.tccode_domain  = tAPMatchingLnWHTFromGTM.tcDomainCode and
                               tqGeneralizedCodeForSecurity.tccode_fldname = 'txb_base'   and
                               tqGeneralizedCodeForSecurity.tccode_value   = tqMfgTaxCodeByTaxCode.tctx2_base
                               no-lock no-error.
                    if available tqGeneralizedCodeForSecurity
                    then do:
                        assign vcWHTBasePercentage = substring(tqGeneralizedCodeForSecurity.tccode_cmmt, 1, 9, "CHARACTER").
                        if vcWHTBasePercentage <> "" and
                           vcWHTBasePercentage <> ?
                        then assign tAPMatchingLnWHTFromGTM.APMatchingLnWHTBaseTaxPct = decimal(vcWHTBasePercentage) no-error.
                        if error-status:error
                        then assign tAPMatchingLnWHTFromGTM.APMatchingLnWHTBaseTaxPct = 100.
                    end. /* if available tqGeneralizedCodeForSecurity */
                end. /* if tqMfgTaxCodeByTaxCode.tctx2_base <> "" and */   
            end. /* if available tqMfgTaxCodeByTaxCode */   
                                                                                                              
        end. /* end if tTaxDetailDataFromGTM.tx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} */
        else if tTaxDetailDataFromGTM.tx2d_tr_type <> {&VATTAXTRANSACTIONTYPE-APWHT} then do:
            create tAPMatchingLnTaxFromGTM. 
            assign tAPMatchingLnTaxFromGTM.tcDomainCode               = tTaxDetailDataFromGTM.tx2d_domain
                   tAPMatchingLnTaxFromGTM.tcVatCode                  = tTaxDetailDataFromGTM.tx2d_tax_code
                   tAPMatchingLnTaxFromGTM.tcVatInOut                 = {&VATINOUT-INPUT}
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxTaxType     = tTaxDetailDataFromGTM.tx2d_tax_type
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxTaxTrType   = tTaxDetailDataFromGTM.tx2d_tr_type
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxBaseTC      = tTaxDetailDataFromGTM.tx2d_tottax
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxBaseLC      = tTaxDetailDataFromGTM.tx2d_taxable_amt
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxTaxTC       = tTaxDetailDataFromGTM.tx2d_cur_tax_amt
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxTaxLC       = tTaxDetailDataFromGTM.tx2d_tax_amt
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAccrRcp   = (tTaxDetailDataFromGTM.tx2d_rcpt_tax_point = true or tTaxDetailDataFromGTM.tx2d_usage_tax_point = true)
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsDiscAtInv = (tTaxDetailDataFromGTM.tlTx2InvoiceDisc = true)
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAbsRet    = (if tTaxDetailDataFromGTM.tx2d_abs_ret_amt <> 0 and tTaxDetailDataFromGTM.tx2d_abs_ret_amt <> ? then true else false)
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsSuspDel   = if vlUseDelTax then tTaxDetailDataFromGTM.tltx2SuspDelayTax else false
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsUpdAllow  = tTaxDetailDataFromGTM.tlTx2UpdateTax
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxTaxPct      = tTaxDetailDataFromGTM.tdTx2TaxPerc
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxRecovTaxPct = tTaxDetailDataFromGTM.tdTx2RecovTaxPerc
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxARRecTaxTC  = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAccrRcp = true  then tTaxDetailDataFromGTM.tx2d_cur_recov_amt else 0) 
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxARNRecTaxTC = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAccrRcp = true  then tTaxDetailDataFromGTM.tx2d_cur_tax_amt - tTaxDetailDataFromGTM.tx2d_cur_recov_amt else 0)
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxAIRecTaxTC  = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAccrRcp = false then tTaxDetailDataFromGTM.tx2d_cur_recov_amt else 0) 
                   tAPMatchingLnTaxFromGTM.APMatchingLnTaxAINRecTaxTC = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAccrRcp = false then tTaxDetailDataFromGTM.tx2d_cur_tax_amt - tTaxDetailDataFromGTM.tx2d_cur_recov_amt else 0)
                   tAPMatchingLnTaxFromGTM.tcNormalTaxGLCode          = (if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APCNGL       else tTaxDetailDataFromGTM.tcTx2APGL)
                   tAPMatchingLnTaxFromGTM.tcNormalTaxDivisionCode    = (if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APCNDivision else tTaxDetailDataFromGTM.tcTx2APDivision)
                   tAPMatchingLnTaxFromGTM.tcAbsRetTaxGLCode          = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAbsRet = true then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APRCNGL            else tTaxDetailDataFromGTM.tcTx2APRGL           else "":U)
                   tAPMatchingLnTaxFromGTM.tcAbsRetTaxDivisionCode    = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsAbsRet = true then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tcTx2APRCNDivision      else tTaxDetailDataFromGTM.tcTx2APRDivision     else "":U)
                   tAPMatchingLnTaxFromGTM.tcSuspDelTaxGLCode         = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsSuspDel = true then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tctx2DelayAPCNGL       else tTaxDetailDataFromGTM.tctx2DelayAPGL       else "":U)
                   tAPMatchingLnTaxFromGTM.tcSuspDelTaxDivisionCode   = (if tAPMatchingLnTaxFromGTM.APMatchingLnTaxIsSuspDel = true then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tTaxDetailDataFromGTM.tctx2DelayAPCNDivision else tTaxDetailDataFromGTM.tctx2DelayAPDivision else "":U)
                   tAPMatchingLnTaxFromGTM.tc_ParentRowid             = icAPMatchingLnTcRowid
                   tAPMatchingLnTaxFromGTM.tc_Rowid                   = string(viRowidPendingVoucherTaxAPM)
                   tAPMatchingLnTaxFromGTM.tc_Status                  = "N":U
                   viRowidPendingVoucherTaxAPM                        = viRowidPendingVoucherTaxAPM + 1.
        end. /* end else if tTaxDetailDataFromGTM.tx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} */
    end. /* for each tTaxDetailDataFromGTM */
    
END. /* PGTMCALLINGBLOCK. */

/* ======================================== */
/* Clear the temp-table with the GTM output */
/* ======================================== */
empty temp-table tTaxDetailDataFromGTM.

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