project QadFinancials > class BPosting > method AddTaxPostingDetail

Description

detail section of the add Tax posting to the journal entry


Parameters


icPostingLineTcRowidinputcharacterField-value of tc_rowid of the tpostingline you are creating tax-information for.
This field can be left empty when you do not have a PostingLine yet.
itTaxPointDateinputdatetax point date
icDescriptioninputcharacter
icCurrencyCodeinputcharacterCurrency Code
iiCurrencyIdinputintegercurrency id
idExchangeRateinputdecimalexchange rate
idExchangeRateScaleinputdecimal
idBaseDebitinputdecimalbase TC
idBaseCreditinputdecimalbase TC
idFullDebitinputdecimalfull TC
idFullCreditinputdecimalfull TC
icInvoiceTypeinputcharacterInvoice Type : must be Invoice or Credit Note
ilCalculateVatinputlogicaldo you want to calculate the vat(yes) or do you want to work on input temptable (false) ?
icDivisionCodeinputcharacter
icVatCurrencyCodeinputcharacter
iiVatCurrencyIdinputinteger
idVatExchangeRateinputdecimalExchange rate between Transactional currency and Vat currency.
idVatExchangeRateScaleinputdecimal
icTransactionTypeinputcharacterTransaction Type
icTxclTaxClsinputcharacterTax Class
ilCustomerInvoiceinputlogicalindication if we work with a customer invoice or a supplier invoice, necessary to know if it is debit or credit
icControlGLCodeinputcharactercontrol GL Account
icTaxGLCodeinputcharacterGL Code for tax : if linked to po where accrue tax at receipt or accrue tax at usage is set, then purchase order receipt account
icCostCentreCodeinputcharacter
icProjectCodeinputcharacter
icTaxTransactionTypeinputcharactertax transaction type
iiCounterinputintegerCounter
idTotalDBinputdecimalTotal DB
idTotalCRinputdecimalTotal CR
icOwnTaxIDFederalinputcharacterOwn Tax ID federal
icOwnTaxIDStateinputcharacterOwn Tax ID State
icOwnTaxIDMisc1inputcharacterOwn Tax ID Misc 1
icOwnTaxIDMisc2inputcharacterOwn Tax ID Misc 2
icOwnTaxIDMisc3inputcharacterOwn Tax ID Misc 3
iiOwnTaxDeclarationinputintegerOwn Tax Declaration
icPostingVatTaxIdFederalinputcharacterPosting Vat Tax ID Federal
icPostingVatTaxIdStateinputcharacterPosting Vat Tax Id State
icPostingVatTaxIdMisc1inputcharacterPosting Vat Tax ID Misc1
icPostingVatTaxIdMisc2inputcharacterPosting Vat Tax Id Misc2
icPostingVatTaxIdMisc3inputcharacterPosting Vat Tax ID Misc 3
idRateCCinputdecimalRate CC
idRateScaleCCinputdecimalRate Scale CC
biFirstVatPostingLineIdinput-outputinteger
oiSecondVatPostingLineIdoutputintegerSecondVatPostingLineId: In case an additional posting-line with a postng-tax was created, this field will contain the postinglineID of the second postingline
ocPostingLineIdsoutputcharacterlist of all postingline ids that are made here, necessary for manual journal entry, separated by comma
ilLinkedCrCyDaemonReqExistsinputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPosting.AddTaxPosting


program code (program6/bposting.p)

assign oiReturnStatus = -98.

<Q-128 run MfgTaxCodeByTaxCode (all) (Read) (NoCache)
   (input tTaxDetailPosting.tx2d_tax_code, (TaxCode)
    input tTaxDetailPosting.tx2d_domain, (DomainCode)
    input ?, (DiscountAtPayment)
    input ?, (IsTaxByLine)
    output dataset tqMfgTaxCodeByTaxCode) in BMfgTaxCode>
    find first tqMfgTaxCodeByTaxCode 
            where tqMfgTaxCodeByTaxCode.tctx2_domain   = tTaxDetailPosting.tx2d_domain and
                tqMfgTaxCodeByTaxCode.tctx2_tax_code = tTaxDetailPosting.tx2d_tax_code no-error.
if not available tqMfgTaxCodeByTaxCode
then do:
    assign vcMessage      = trim(substitute(#T-144'A Manufacturing Tax Record for domain &1 and tax code &2 cannot be found.':150(49948)T-144#, tTaxDetailPosting.tx2d_domain, tTaxDetailPosting.tx2d_tax_code))
           oiReturnStatus = -3.
    <M-129 run AddTaxPostingSetMessage
       (input  vcMessage (icMessage), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    return.
end.

/* search gl based on taxcode */
if icTaxGLCode <> "":U 
then assign vcGLCode = icTaxGLCode.
else do:
    assign vcGLCode          = if ilCustomerInvoice
                               then if (icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-FINANCECHARGE}) 
                                    then tqMfgTaxCodeByTaxCode.tctx2_ar_acct 
                                    else tqMfgTaxCodeByTaxCode.tctx2_ar_cn_acct
                               else if (icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-FINANCECHARGE}) 
                                    then tqMfgTaxCodeByTaxCode.tctx2_ap_acct 
                                    else tqMfgTaxCodeByTaxCode.tctx2_ap_cn_acct
           vlIsReverseCharge = tqMfgTaxCodeByTaxCode.tltx2_reverse_charge.
    if vcGLCode = "":U
    then do:
        assign vcMessage      = if ilCustomerInvoice
                                then if (icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-FINANCECHARGE}) then trim(#T-136'Sales Tax Account':255(63591)T-136#) else trim(#T-137'Sales Tax CN Account':25(48660)T-137#)
                                else if (icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-FINANCECHARGE}) then trim(#T-138'AP Tax Account':255(63593)T-138#) else trim(#T-139'AP Tax CN Account':255(63594)T-139#)
               vcMessage      = trim(substitute(#T-135'&1 cannot be found':150(63589)T-135#, (vcMessage + " ":U + vcGLCode)))
               oiReturnStatus = -3.
        <M-124 run AddTaxPostingSetMessage
           (input  vcMessage (icMessage), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        return.
    end.
end.

if icPostingLineTcRowid <> "":U
then do :
    find tPostingLine where 
         tPostingLine.tc_ParentRowid = tPosting.tc_Rowid and 
         tPostingLine.tc_Rowid       = icPostingLineTcRowid 
         no-error.
    if not available tPostingLine 
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-140'Internal error: cannot find the posting line based upon its primary information.':255(64105)T-140#, vcGLCode)) + chr(10) + 
                                trim(substitute(#T-141'Rowid of the Posting: &1':255(64106)T-141#, tPosting.tc_Rowid)) + chr(10) + 
                                trim(substitute(#T-142'Rowid of the Posting-line: &1':255(64108)T-142#, icPostingLineTcRowid)) + chr(10) + 
                                trim(substitute(#T-143'The expected GL account on the posting-line is &1.':255(64109)T-143#, vcGLCode)).   
        <M-126 run AddTaxPostingSetMessage
           (input  vcMessage (icMessage), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        return.
    end. /* if not available tPostingLine */
end. /* if icPostingLineTcRowid <> "":U */
else do :
    if (tTaxDetailPosting.tx2d_cur_recov_amt <> 0 or 
        ilCustomerInvoice                         or 
        ilCalculateVat                            or 
        tTaxDetailPosting.tx2d_cur_recov_amt = 0) and
       (if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} and tTaxDetailPosting.tx2d_abs_ret_amt <> 0 then false else true)
    then do:
        <M-125 run AddDetailLine
           (input  'PostingLine':U (icTable), 
            input  tPosting.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        if viFcReturnSuper <> 0
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then return.
    end.
end. /* Not if icPostingLineTcRowid <> "":U */
        
if (tTaxDetailPosting.tx2d_cur_recov_amt <> 0 or 
    ilCustomerInvoice                         or 
    ilCalculateVat                            or 
    tTaxDetailPosting.tx2d_cur_recov_amt = 0) and
   (if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} and tTaxDetailPosting.tx2d_abs_ret_amt <> 0 then false else true)
then do:
    <M-107 run AddDetailLine
       (input  'PostingVat':U (icTable), 
        input  tPostingLine.tc_Rowid (icParentRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        return.
    end.

    assign biFirstVatPostingLineId = tPostingLine.PostingLine_ID
           vcDivisionCode          = if (icDivisionCode <> '':U and icDivisionCode <> ?) 
                                     then icDivisionCode 
                                     else "":U.
           ocPostingLineIds        = if ocPostingLineIds = "":U 
                                     then string(biFirstVatPostingLineId) 
                                     else if lookup(string(biFirstVatPostingLineId), ocPostingLineIds) = 0 
                                          then ocPostingLineIds + ",":U + string(biFirstVatPostingLineId) 
                                          else ocPostingLineIds.

    <Q-108 run GLByIDCode (all) (Read) (NoCache)
       (input tPosting.Company_ID, (CompanyId)
        input ?, (GLID)
        input vcGLCode, (GLCode)
        output dataset tqGLByIDCode) in BGL >
    find first tqGLByIDCode where 
               tqGLByIDCode.tcGLCode = vcGLCode 
               no-lock no-error.
    if not available tqGLByIDCode
    then do:
        assign vcMessage = trim(substitute(#T-131'The tax GL account &1 cannot found':150(54017)T-131#, vcGLCode))
               oiReturnStatus = -3.
        <M-120 run AddTaxPostingSetMessage
           (input  vcMessage (icMessage), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        return.
    end.

    if tqGLByIDCode.tlGLIsDivisionAccount
    then assign vcDivisionCode = if (icDivisionCode <> "":U and icDivisionCode <> ?) then icDivisionCode
                                 else if (tqMfgTaxCodeByTaxCode.tctx2_ap_sub <> "":U and tqMfgTaxCodeByTaxCode.tctx2_ap_sub <> ?) then tqMfgTaxCodeByTaxCode.tctx2_ap_sub 
                                 else tqGLByIDCode.tcDivisionCode .

    assign tPostingLine.PostingLineSequence          = iiCounter + 1
           tPostingLine.Posting_ID                   = tPosting.Posting_ID
           tPostingLine.PostingLineText              = icDescription
           tPostingLine.tcGLTypeCode                 = {&GLTYPECODE-VAT}
           tPostingLine.tlGLIsAutomaticAccount       = tqGLByIDCode.tlGLIsAutomaticAccount
           tPostingLine.tiVatPostID                  = biFirstVatPostingLineId
           tPostingLine.GL_ID                        = tqGLByIDCode.tiGL_ID
           tPostingLine.tcGLCode                     = tqGLByIDCode.tcGLCode
           tPostingLine.tcGLDescription              = tqGLByIDCode.tcGLDescription
           tPostingLine.Currency_ID                  = iiCurrencyId
           tPostingLine.tcCurrencyCode               = icCurrencyCode
           tPostingLine.PostingLineExchangeRate      = idExchangeRate
           tPostingLine.PostingLineRateScale         = idExchangeRateScale
           tPostingLine.tlPostingLineIsLocalCurrency = (iiCurrencyId = viCompanyLCId)
           tPostingLine.tcDivisionCode               = vcDivisionCode
           tPostingLine.tlLinkedCrCyDaemonReqExists  = ilLinkedCrCyDaemonReqExists
           tPostingVat.PostingLine_ID                = biFirstVatPostingLineId
           tPostingVat.PostingVatTaxPointDate        = itTaxPointDate
           tPostingVat.PostingVatBaseDebitTC         = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if idBaseDebit <> 0 
                                                            then tTaxDetailPosting.tx2d_tottax 
                                                            else 0
                                                       else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                       then if idBaseDebit <> 0 
                                                            then tTaxDetailPosting.tx2d_tottax 
                                                            else 0
                                                       else if ilCustomerInvoice
                                                            then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                 then if tTaxDetailPosting.tx2d_tottax < 0 
                                                                      then if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                           then 0 
                                                                           else abs(tTaxDetailPosting.tx2d_tottax) 
                                                                      else 0
                                                                 else tTaxDetailPosting.tx2d_tottax
                                                            else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                 then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} 
                                                                      then 0 
                                                                      else tTaxDetailPosting.tx2d_tottax 
                                                                 else 0
           tPostingVat.PostingVatBaseCreditTC        = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if idBaseCredit <> 0 
                                                            then tTaxDetailPosting.tx2d_tottax 
                                                            else 0
                                                       else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                            then if idBaseCredit <> 0 
                                                                 then - tTaxDetailPosting.tx2d_tottax 
                                                                 else 0
                                                            else if ilCustomerInvoice
                                                                 then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} 
                                                                           then 0 
                                                                           else tTaxDetailPosting.tx2d_tottax
                                                                      else 0
                                                                 else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then if tTaxDetailPosting.tx2d_tottax < 0 
                                                                           then if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                                then 0 
                                                                                else abs(tTaxDetailPosting.tx2d_tottax) 
                                                                           else 0
                                                                      else tTaxDetailPosting.tx2d_tottax
           tPostingVat.tdTotalVatDebitTC             = idTotalDB
           tPostingVat.tdTotalVatCreditTC            = idTotalCR
           tPostingVat.tdFullDebitTC                 = idFullDebit
           tPostingVat.tdFullCreditTC                = idFullCredit
           tPostingVat.Currency_ID                   = iiVatCurrencyId
           tPostingVat.tcCurrencyCode                = icVatCurrencyCode
           tPostingVat.PostingVatExchangeRate        = idVatExchangeRate
           tPostingVat.PostingVatRateScale           = idVatExchangeRateScale
           tPostingVat.tcVatCode                     = tTaxDetailPosting.tx2d_tax_code
           tPostingVat.tcDomainCode                  = tTaxDetailPosting.tx2d_domain
           tPostingVat.tcVatInOut                    = if ilCustomerInvoice then {&VATINOUT-OUTPUT} else {&VATINOUT-INPUT}
           tPostingVat.TxtyTaxType                   = tTaxDetailPosting.tx2d_tax_type 
           tPostingVat.TxclTaxCls                    = tTaxDetailPosting.tx2d_taxc
           tPostingVat.TxuTaxUsage                   = tTaxDetailPosting.tx2d_tax_usage
           tPostingVat.PostingVatTaxTransType        = tTaxDetailPosting.tx2d_tr_type
           tPostingVat.PostingVatInOut               = if ilCustomerInvoice then {&VATINOUT-OUTPUT} else {&VATINOUT-INPUT}
           tPostingVat.PostingVatTransType           = icTransactionType
           tPostingVat.TxenvTaxEnv                   = tTaxDetailPosting.tx2d_tax_env
           tPostingVat.FromTxzTaxZone                = tTaxDetailPosting.tx2d_zone_from
           tPostingVat.ToTxzTaxZone                  = tTaxDetailPosting.tx2d_zone_to
           tPostingVat.PostingVatIsReverseCharge     = vlIsReverseCharge
           tPostingVat.PostingVatTaxDebitTC          = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if idBaseDebit <> 0
                                                            then if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                 then tTaxDetailPosting.tx2d_cur_recov_amt 
                                                                 else tTaxDetailPosting.tx2d_cur_tax_amt 
                                                            else 0
                                                       else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                            then if idBaseDebit <> 0 
                                                                 then if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                      then tTaxDetailPosting.tx2d_cur_recov_amt 
                                                                      else tTaxDetailPosting.tx2d_cur_tax_amt 
                                                                 else 0
                                                            else if ilCustomerInvoice
                                                                 then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then if tTaxDetailPosting.tx2d_cur_tax_amt < 0 
                                                                           then if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                                then 0 
                                                                                else abs(tTaxDetailPosting.tx2d_cur_tax_amt) 
                                                                           else 0
                                                                      else if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                           then tTaxDetailPosting.tx2d_cur_recov_amt
                                                            
                                                                           else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT}
                                                                                then 0
                                                                                else tTaxDetailPosting.tx2d_cur_tax_amt
                                                                 else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then tTaxDetailPosting.tx2d_cur_recov_amt
                                                                      else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                                           then tTaxDetailPosting.tx2d_cur_tax_amt
                                                                           else 0
           tPostingVat.PostingVatTaxCreditTC         = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if idBaseCredit <> 0 
                                                            then if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                 then tTaxDetailPosting.tx2d_cur_recov_amt 
                                                                 else tTaxDetailPosting.tx2d_cur_tax_amt 
                                                            else 0
                                                       else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                            then if idBaseCredit <> 0 
                                                                 then if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                      then - tTaxDetailPosting.tx2d_cur_recov_amt 
                                                                      else - tTaxDetailPosting.tx2d_cur_tax_amt 
                                                                 else 0
                                                            else if ilCustomerInvoice
                                                                 then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then if tTaxDetailPosting.tx2d_cur_recov_amt <> 0 
                                                                           then tTaxDetailPosting.tx2d_cur_recov_amt 
                                                                           else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} 
                                                                                then 0 
                                                                                else tTaxDetailPosting.tx2d_cur_tax_amt
                                                                      else if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT}
                                                                           then tTaxDetailPosting.tx2d_cur_tax_amt
                                                                           else 0
                                                                 else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                      then if tTaxDetailPosting.tx2d_cur_tax_amt < 0 
                                                                           then if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                                then 0 
                                                                                else abs(tTaxDetailPosting.tx2d_cur_tax_amt) 
                                                                           else 0
                                                                      else tTaxDetailPosting.tx2d_cur_recov_amt 
           tPostingLine.PostingLineDebitTC           = tPostingLine.PostingLineDebitTC + tPostingVat.PostingVatTaxDebitTC
           tPostingLine.PostingLineCreditTC          = tPostingLine.PostingLineCreditTC + tPostingVat.PostingVatTaxCreditTC
           tPostingVat.PostingVatIsAbsRet            = false
           tPostingVat.PostingVatOwnTaxIDFeder       = icOwnTaxIdFederal
           tPostingVat.PostingVatOwnTaxIDState       = icOwnTaxIdState
           tPostingVat.PostingVatOwnTaxIDMisc1       = icOwnTaxIdMisc1
           tPostingVat.PostingVatOwnTaxIDMisc2       = icOwnTaxIdMisc2
           tPostingVat.PostingVatOwnTaxIDMisc3       = icOwnTaxIdMisc3
           tPostingVat.PostingVatOwnTaxDeclarat      = iiOwnTaxDeclaration
           tPostingVat.PostingVatTaxIDFeder          = icPostingVatTaxIdFederal
           tPostingVat.PostingVatTaxIDState          = icPostingVatTaxIdState
           tPostingVat.PostingVatTaxIDMisc1          = icPostingVatTaxIdMisc1
           tPostingVat.PostingVatTaxIDMisc2          = icPostingVatTaxIdMisc2
           tPostingVat.PostingVatTaxIDMisc3          = icPostingVatTaxIdMisc3
           tPostingVat.PostingVatTaxDebitLC          = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                       then if idBaseDebit <> 0 
                                                            then <M-75 RoundAmount
                                                                    (input  tPostingVat.PostingVatTaxDebitTC *  idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                                                     input  viCompanyLCId (iiCurrencyID), 
                                                                     input  vcCompanyLC (icCurrencyCode)) in BPosting>
                                                            else 0
                                                       else if tPostingVat.PostingVatTaxDebitTC <> ? and tPostingVat.PostingVatTaxDebitTC <> 0
                                                            then if tTaxDetailPosting.tx2d_recov_amt <> 0 
                                                                 then tTaxDetailPosting.tx2d_recov_amt 
                                                                 else if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                                      then 
                                                                          if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                                          then 
                                                                              tTaxDetailPosting.tx2d_tax_amt
                                                                          else 
                                                                              abs(tTaxDetailPosting.tx2d_tax_amt) 
                                                                      else tTaxDetailPosting.tx2d_tax_amt
                                                            else 0
           tPostingVat.PostingVatTaxCreditLC         = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                       then if idBaseCredit <> 0 
                                                            then <M-7 RoundAmount
                                                                    (input  tPostingVat.PostingVatTaxCreditTC *  idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                                                     input  viCompanyLCId (iiCurrencyID), 
                                                                     input  vcCompanyLC (icCurrencyCode)) in BPosting>
                                                            else 0
                                                       else if tPostingVat.PostingVatTaxCreditTC <> ? and tPostingVat.PostingVatTaxCreditTC <> 0
 														    then if tTaxDetailPosting.tx2d_recov_amt <> 0  
                                                                 then tTaxDetailPosting.tx2d_recov_amt  
															 	 else if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                                      then 
                                                                         if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
  																         then 
																	         tTaxDetailPosting.tx2d_tax_amt
																	     else 
																	         abs(tTaxDetailPosting.tx2d_tax_amt) 
																      else tTaxDetailPosting.tx2d_tax_amt
														    else 0
           tPostingVat.PostingVatBaseDebitLC         = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                       then if idBaseDebit <> 0 
                                                            then <M-68 RoundAmount
                                                                    (input  tPostingVat.PostingVatBaseDebitTC *  idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                                                     input  viCompanyLCId (iiCurrencyID), 
                                                                     input  vcCompanyLC (icCurrencyCode)) in BPosting> 
                                                            else 0
                                                       else if tPostingVat.PostingVatBaseDebitTC <> ? and tPostingVat.PostingVatBaseDebitTC <> 0
                                                            then if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                                 then abs(tTaxDetailPosting.tx2d_taxable_amt) 
                                                                 else tTaxDetailPosting.tx2d_taxable_amt
                                                            else 0
           tPostingVat.PostingVatBaseCreditLC        = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT}
                                                       then if idBaseCredit <> 0 
                                                            then <M-59 RoundAmount
                                                                    (input  tPostingVat.PostingVatBaseCreditTC *  idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                                                     input  viCompanyLCId (iiCurrencyID), 
                                                                     input  vcCompanyLC (icCurrencyCode)) in BPosting> 
                                                            else 0
                                                       else if tPostingVat.PostingVatBaseCreditTC <> ? and tPostingVat.PostingVatBaseCreditTC <> 0
                                                            then if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                                 then abs(tTaxDetailPosting.tx2d_taxable_amt) 
                                                                 else tTaxDetailPosting.tx2d_taxable_amt
                                                            else 0
           tPostingLine.PostingLineDebitLC           = tPostingLine.PostingLineDebitLC + tPostingVat.PostingVatTaxDebitLC
           tPostingLine.PostingLineCreditLC          = tPostingLine.PostingLineCreditLC + tPostingVat.PostingVatTaxCreditLC.
           
    <Q-110 run VatRuleByVatIDCode (all) (Read) (NoCache)
       (input tTaxDetailPosting.tx2d_tax_code, (VatCode)
        input ?, (VatID)
        input {&VATRULEBASE-FULL}, (VatRuleBaseFull)
        input if ilcustomerInvoice then {&VATINOUT-OUTPUT} else {&VATINOUT-INPUT}, (VatInOut)
        input ttaxdetailPosting.tx2d_domain, (DomainCode)
        output dataset tqVatRuleByVatIDCode) in BVAT >
    find first tqVatRuleByVatIDCode no-error.
    if not available tqVatRuleByVatIDCode
    then do:
        assign vcMessage = trim(substitute(#T-130'A full rule for tax code &1 in domain &2 cannot be found.':250(59039)T-130#, tTaxDetailPosting.tx2d_tax_code, ttaxdetailPosting.tx2d_domain ))
               oiReturnStatus = -1.
        <M-121 run AddTaxPostingSetMessage
           (input  vcMessage (icMessage), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
       return.
    end.

    assign tPostingVat.VatPeriod_ID  = tqVatPeriodByStartEndDate.tiVatPeriod_ID
           tPostingVat.PeriodMark_ID = tqVatPeriodByStartEndDate.tiPeriodMark_ID
           tPostingVat.VatRule_ID    = tqVatRuleByVatIDCode.tiVatRule_ID.

    <M-122 run AddTaxPostingFillCCAmounts
       (input  iiCurrencyId (iiCurrencyId), 
        input  idRateCC (idExchangeRate), 
        input  idRateScaleCC (idExchangeRateScale), 
        output viLocalReturnStatus (oiReturnStatus)) in BPosting>
    if viLocalReturnStatus < 0 or (viLocalReturnStatus > 0 and oiReturnStatus = -98)
    then assign oiReturnStatus = viLocalReturnStatus.
    if viLocalReturnStatus < 0 then return.
end.

if tTaxDetailPosting.tx2d_abs_ret_amt <> 0
then do:
    <M-109 run AddTaxPostingPartTwo
       (input  ilCustomerInvoice (ilCustomerInvoice), 
        input  icControlGLCode (icControlGLCode), 
        input  icDescription (icDescription), 
        input  iiCurrencyId (iiCurrencyId), 
        input  icCurrencyCode (icCurrencyCode), 
        input  idExchangeRate (idExchangeRate), 
        input  idExchangeRateScale (idExchangeRateScale), 
        input  icDivisionCode (icDivisionCode), 
        input  idRateCC (idRateCC), 
        input  idRateScaleCC (idRateScaleCC), 
        input  0 (iiMasterId), 
        input  0 (iiTransactionId), 
        input  icTxclTaxCls (icTxclTaxCls), 
        input  icTransactionType (icTransactionType), 
        input  itTaxPointDate (itTaxPointDate), 
        input  icInvoiceType (icInvoiceType), 
        input  icCostCentreCode (icCostCentreCode), 
        input  icProjectCode (icProjectCode), 
        input  vlIsReverseCharge (ilIsReverseCharge), 
        input  icTaxTransactionType (icTaxTransactionType), 
        input  if idBaseDebit = 0 then true else false (ilDebit), 
        input-output iiCounter (biPostingLineSequenceCounter), 
        output oiSecondVatPostingLineId (oiPostingLineID), 
        input  ilLinkedCrCyDaemonReqExists (ilLinkedCrCyDaemonReqExists), 
        output viLocalReturnStatus (oiReturnStatus)) in BPosting>
    if viLocalReturnStatus < 0 or (viLocalReturnStatus > 0 and oiReturnStatus = -98)
    then assign oiReturnStatus = viLocalReturnStatus.
    if viLocalReturnStatus < 0 then return.
    assign ocPostingLineIds = if ocPostingLineIds = "":U 
                              then string(oiSecondVatPostingLineId) 
                              else if lookup(string(oiSecondVatPostingLineId), ocPostingLineIds) = 0 
                                   then ocPostingLineIds + ",":U + string(oiSecondVatPostingLineId) 
                                   else ocPostingLineIds.
end.

if oiReturnStatus < 0 and oiReturnStatus <> -98 then return.
assign oiReturnStatus = 0.