project QadFinancials > class BPosting > method AddTaxPostingPartTwo

Description

second part of tax posting


Parameters


ilCustomerInvoiceinputlogicaldo we work with customer or supplier invoice ?
icControlGLCodeinputcharactercontrol GL Account
icDescriptioninputcharacter
iiCurrencyIdinputintegercurrency ID
icCurrencyCodeinputcharacterCurrency Code
idExchangeRateinputdecimalExchange Rate
idExchangeRateScaleinputdecimalScale factor of the exchange rate
icDivisionCodeinputcharacterDivisionCode
idRateCCinputdecimalExchange Rate in CC
idRateScaleCCinputdecimalExchange Rate Scale CC
iiMasterIdinputintegerMaster id
iiTransactionIdinputintegerTransaction ID
icTxclTaxClsinputcharacterTax Class
icTransactionTypeinputcharacterTransaction Type
itTaxPointDateinputdateTax point date
icInvoiceTypeinputcharacterDinvoice TYpe
icCostCentreCodeinputcharacter
icProjectCodeinputcharacter
ilIsReverseChargeinputlogicalIs Reverse Charge flag
icTaxTransactionTypeinputcharacterTax Transaction Type
ilDebitinputlogicalPost on Debit or Credit for tax transaction type AR Transactions and AP Transaction
biPostingLineSequenceCounterinput-outputintegerThe highest value for field PostingLineSequence in the PostingLines of the current posting
oiPostingLineIDoutputinteger
ilLinkedCrCyDaemonReqExistsinputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPosting.AddTaxPostingDetail


program code (program6/bposting.p)

/* ilDebit : if you have a debit amount in addtaxposting, you have to post it here on other side, so credit */
    /* several situations : retained, absorbed, recoverable taxes */
    /* posting on absorbed(customer invoice) or retained account (supplier invoice) */
    <M-17 run AddDetailLine
       (input  'PostingLine':U (icTable), 
        input  tPosting.tc_Rowid (icParentRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        return.
    end.
    
    <M-24 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 biPostingLineSequenceCounter              = biPostingLineSequenceCounter + 1
           oiPostingLineID                           = tPostingLine.PostingLine_ID
           tPostingLine.PostingLineSequence          = biPostingLineSequenceCounter
           tPostingLine.Posting_ID                   = tPosting.Posting_ID
           tPostingLine.PostingLineText              = icDescription
           tPostingLine.tcGLTypeCode                 = {&GLTYPECODE-VAT}
           tPostingLine.tlGLIsAutomaticAccount       = yes /* do not allow manual changes on this line */
           tPostingLine.Currency_ID                  = iiCurrencyId
           tPostingLine.tcCurrencyCode               = icCurrencyCode
           tPostingLine.PostingLineExchangeRate      = idExchangeRate
           tPostingLine.PostingLineRateScale         = idExchangeRateScale
           tPostingLine.tlPostingLineIsLocalCurrency = (iiCurrencyId = viCompanyLCId)
           tPostingLine.PostingLineDebitTC           = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if ilDebit then abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt) else 0
                                                       else if ilCustomerInvoice 
                                                       then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} then 0 else abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt) 
                                                            else 0
                                                       else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-CREDITNOTE} or
                                                               icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} AND ilDebit then tTaxDetailPosting.tx2d_cur_abs_ret_amt else 0 
                                                            else abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt)
           tPostingLine.PostingLineCreditTC          = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if ilDebit then 0 else abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt)
                                                       else if ilCustomerInvoice 
                                                       then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} then abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt) else 0
                                                            else abs(tTaxDetailPosting.tx2d_cur_abs_ret_amt) 
                                                       else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-CREDITNOTE} or
                                                               icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} AND ilDebit then 0 else - tTaxDetailPosting.tx2d_cur_abs_ret_amt
                                                            else 0
           tPostingLine.PostingLineDebitLC           = if tPostingLine.PostingLineDebitTC <> ? and tPostingLine.PostingLineDebitTC <> 0
                                                       then <M-52 RoundAmount
                                                               (input  (tPostingLine.PostingLineDebitTC * idExchangeRate * idExchangeRateScale) (idUnroundedAmount), 
                                                                input  viCompanyLCId (iiCurrencyID), 
                                                                input  '':U (icCurrencyCode)) in BPosting>
                                                       else 0
           tPostingLine.PostingLineCreditLC          = if tPostingLine.PostingLineCreditTC <> ? and tPostingLine.PostingLineCreditTC <> 0
                                                       then <M-85 RoundAmount
                                                               (input  (tPostingLine.PostingLineCreditTC  * idExchangeRate * idExchangeRateScale) (idUnroundedAmount), 
                                                                input  viCompanyLCId (iiCurrencyID), 
                                                                input  '':U (icCurrencyCode)) in BPosting>
                                                       else 0
           tPostingLine.tlLinkedCrCyDaemonReqExists  = ilLinkedCrCyDaemonReqExists
           tPostingVat.PostingLine_ID                = tPostingLine.PostingLine_ID
           tPostingVat.PostingVatTaxPointDate        = itTaxPointDate
           tPostingVat.PostingVatBaseDebitTC         = 0 
           tPostingVat.PostingVatBaseCreditTC        = 0 
           tPostingVat.PostingVatBaseDebitLC         = 0 
           tPostingVat.PostingVatBaseCreditLC        = 0 
           tPostingVat.PostingVatBaseDebitCC         = 0 
           tPostingVat.PostingVatBaseCreditCC        = 0 
           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                    = icTxclTaxCls
           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     = ilIsReverseCharge
           tPostingVat.PostingVatIsAbsRet            = true
           tPostingVat.PostingVatOwnTaxIDFeder       = vcOwnTaxIdFederal
           tPostingVat.PostingVatOwnTaxIDState       = vcOwnTaxIdState
           tPostingVat.PostingVatOwnTaxIDMisc1       = vcOwnTaxIdMisc1
           tPostingVat.PostingVatOwnTaxIDMisc2       = vcOwnTaxIdMisc2
           tPostingVat.PostingVatOwnTaxIDMisc3       = vcOwnTaxIdMisc3
           tPostingVat.PostingVatOwnTaxDeclarat      = viOwnTaxDeclaration
           tPostingVat.PostingVatTaxIDFeder          = vcPostingVatTaxIdFederal
           tPostingVat.PostingVatTaxIDState          = vcPostingVatTaxIdState
           tPostingVat.PostingVatTaxIDMisc1          = vcPostingVatTaxIdMisc1
           tPostingVat.PostingVatTaxIDMisc2          = vcPostingVatTaxIdMisc2
           tPostingVat.PostingVatTaxIDMisc3          = vcPostingVatTaxIdMisc3
           tPostingVat.PostingVatTaxDebitTC          = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if ilDebit then abs(tTaxDetailPosting.tx2d_cur_tax_amt) else 0
                                                       else if ilCustomerInvoice
                                                       then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} then 0 else abs(tTaxDetailPosting.tx2d_cur_tax_amt)
                                                            else 0
                                                       else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-CREDITNOTE} or
                                                               icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} AND ilDebit then tTaxDetailPosting.tx2d_cur_tax_amt else 0 
                                                            else abs(tTaxDetailPosting.tx2d_cur_tax_amt)
           tPostingVat.PostingVatTaxCreditTC         = if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APTRANS} or icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARTRANS}
                                                       then if ilDebit then 0 else abs(tTaxDetailPosting.tx2d_cur_tax_amt)
                                                       else if ilCustomerInvoice
                                                       then if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-ARPAYMENT} then abs(tTaxDetailPosting.tx2d_cur_tax_amt) else 0
                                                            else abs(tTaxDetailPosting.tx2d_cur_tax_amt)
                                                       else if icInvoiceType = {&INVOICETYPE-INVOICE} or icInvoiceType = {&INVOICETYPE-CREDITNOTE} or
                                                               icInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                            then if icTaxTransactionType = {&VATTAXTRANSACTIONTYPE-APPAYMENT} AND ilDebit then 0 else - tTaxDetailPosting.tx2d_cur_tax_amt
                                                            else 0
           tPostingVat.tdTotalVatDebitTC             = tPostingVat.PostingVatTaxDebitTC
           tPostingVat.tdTotalVatCreditTC            = tPostingVat.PostingVatTaxCreditTC
           tPostingVat.PostingVatTaxDebitLC          = if tPostingVat.PostingVatTaxDebitTC <> ? and tPostingVat.PostingVatTaxDebitTC <> 0
                                                       then <M-59 RoundAmount
                                                               (input  (tPostingVat.PostingVatTaxDebitTC  * idExchangeRate * idExchangeRateScale) (idUnroundedAmount), 
                                                                input  viCompanyLCId (iiCurrencyID), 
                                                                input  '':U (icCurrencyCode)) in BPosting>
                                                       else 0
           tPostingVat.PostingVatTaxCreditLC         = if tPostingVat.PostingVatTaxCreditTC <> ? and tPostingVat.PostingVatTaxCreditTC <> 0
                                                       then <M-42 RoundAmount
                                                               (input  (tPostingVat.PostingVatTaxCreditTC  * idExchangeRate * idExchangeRateScale) (idUnroundedAmount), 
                                                                input  viCompanyLCId (iiCurrencyID), 
                                                                input  '':U (icCurrencyCode)) in BPosting>
                                                       else 0
           tPostingVat.VatPeriod_ID                  = tqVatPeriodByStartEndDate.tiVatPeriod_ID
           tPostingVat.PeriodMark_ID                 = tqVatPeriodByStartEndDate.tiPeriodMark_ID.

    <Q-31 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 >
    /* second vat rule is always that from retained or absorbed (and last one) */
    find last tqVatRuleByVatIDCode no-error.
    if not available tqVatRuleByVatIDCode
    then do:
        assign vcMessage = trim(substitute(#T-33'A second full rule for tax code &1 in domain &2 cannot be found.':250(63686)T-33#, tTaxDetailPosting.tx2d_tax_code, ttaxdetailPosting.tx2d_domain))
               oiReturnStatus = -1.
        <M-32 run SetMessage (input  vcMessage (icMessage),
                         input  '':U (icArguments),
                         input  '':U (icFieldName),
                         input  '':U (icFieldValue),
                         input  'E':U (icType),
                         input  3 (iiSeverity),
                         input  '':U (icRowid),
                         input  'QadFin-5456':U (icFcMsgNumber),
                         input  '' (icFcExplanation),
                         input  '' (icFcIdentification),
                         input  '' (icFcContext),
                         output viFcReturnSuper (oiReturnStatus)) in BPosting>
        leave.
    end.
    
    assign tPostingVat.VatRule_ID    = tqVatRuleByVatIDCode.tiVatRule_ID.

    if viCompanyCCId <> 0 and viCompanyCCId <> ?
    then do:
        if viCompanyCCId = iiCurrencyId
        then assign tPostingLine.PostingLineDebitCC    = tPostingLine.PostingLineDebitTC
                    tPostingLine.PostingLineCreditCC   = tPostingLine.PostingLineCreditTC
                    tPostingLine.PostingLineCCRate     = 1
                    tPostingLine.PostingLineCCScale    = 1.
        else if vlDomainIsStatutory = false
             then assign tPostingLine.PostingLineDebitCC  = tPostingLine.PostingLineDebitLC
                         tPostingLine.PostingLineCreditCC = tPostingLine.PostingLineCreditLC
                         tPostingLine.PostingLineCCRate   = tPostingLine.PostingLineExchangeRate
                         tPostingLine.PostingLineCCScale  = tPostingLine.PostingLineRateScale.
             else assign tPostingLine.PostingLineCCRate     = idRateCC
                         tPostingLine.PostingLineCCScale    = idRateScaleCC
                         tPostingLine.PostingLineDebitCC    = <M-14 RoundAmount
                                                                 (input  tPostingLine.PostingLineDebitLC * idRateCC * idRateScaleCC (idUnroundedAmount), 
                                                                  input  viCompanyCCId (iiCurrencyID), 
                                                                  input  '':U (icCurrencyCode)) in business>
                         tPostingLine.PostingLineCreditCC   = <M-15 RoundAmount
                                                                 (input  tPostingLine.PostingLineCreditLC * idRateCC * idRateScaleCC (idUnroundedAmount), 
                                                                  input  viCompanyCCId (iiCurrencyID), 
                                                                  input  '':U (icCurrencyCode)) in business>.
    end. /* if viCompanyCCId <> 0 and viCompanyCCId <> ? */

    if ilCustomerInvoice
    then do:
        /* absorbed account : tqMfgTaxCodeByTaxCode.tctx2_ara_acct against the control account */
        assign vcParam = if icInvoiceType = {&INVOICETYPE-CREDITNOTE} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                          then tqMfgTaxCodeByTaxCode.tctx2_ara_cn_acct
                          else tqMfgTaxCodeByTaxCode.tctx2_ara_acct.
        <Q-2 run GLByIDCode (all) (Read) (NoCache)
           (input tPosting.Company_ID, (CompanyId)
            input ?, (GLID)
            input vcParam, (GLCode)
            output dataset tqGLByIDCode) in BGL >
        find first tqGLByIDCode where
                   tqGLByIDCode.tcGLCode = vcParam
             no-lock no-error.
        if not available tqGLByIDCode
        then do:
            assign vcMessage      = trim(substitute(#T-35'The tax absorbed account &1 cannot be found.':250(63296)T-35#, vcParam))
                   oiReturnStatus = -1.
            <M-34 run SetMessage (input  vcMessage (icMessage),
                             input  '':U (icArguments),
                             input  '':U (icFieldName),
                             input  '':U (icFieldValue),
                             input  'E':U (icType),
                             input  3 (iiSeverity),
                             input  '':U (icRowid),
                             input  'QadFin-6436':U (icFcMsgNumber),
                             input  '' (icFcExplanation),
                             input  '' (icFcIdentification),
                             input  '' (icFcContext),
                             output viFcReturnSuper (oiReturnStatus)) in BPosting>
            return.
        end.
        
        if tqGLByIDCode.tlGLIsDivisionAccount
        then do:
            if icDivisionCode <> "":U and icDivisionCode <> ?
            then assign vcDivisionCode = icDivisionCode.
            else assign vcDivisionCode = tqGLByIDCode.tcDivisionCode. 
        end.

        assign tPostingLine.GL_ID           = tqGLByIDCode.tiGL_ID
               tPostingLine.tcGLCode        = tqGLByIDCode.tcGLCode
               tPostingLine.tcGLDescription = tqGLByIDCode.tcGLDescription
               tPostingLine.tcDivisionCode  = vcDivisionCode.
    end.
    else do:
        /* retained account : tqMfgTaxCodeByTaxCode.tctx2_apr_acct against the control account */
        assign vcParam = if icInvoiceType = {&INVOICETYPE-CREDITNOTE} or icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                          then tqMfgTaxCodeByTaxCode.tctx2_apr_cn_acct
                          else tqMfgTaxCodeByTaxCode.tctx2_apr_acct.
        <Q-16 run GLByIDCode (all) (Read) (NoCache)
           (input tPosting.Company_ID, (CompanyId)
            input ?, (GLID)
            input vcParam, (GLCode)
            output dataset tqGLByIDCode) in BGL >
        find first tqGLByIDCode where
                   tqGLByIDCode.tcGLCode = vcParam
             no-lock no-error.
        if not available tqGLByIDCode
        then do:
            assign vcMessage      = trim(substitute(#T-37'The tax retained account &1 cannot be found.':250(63297)T-37#, vcParam))
                   oiReturnStatus = -1.
            <M-36 run SetMessage (input  vcMessage (icMessage),
                             input  '':U (icArguments),
                             input  '':U (icFieldName),
                             input  '':U (icFieldValue),
                             input  'E':U (icType),
                             input  3 (iiSeverity),
                             input  '':U (icRowid),
                             input  'QadFin-6437':U (icFcMsgNumber),
                             input  '' (icFcExplanation),
                             input  '' (icFcIdentification),
                             input  '' (icFcContext),
                             output viFcReturnSuper (oiReturnStatus)) in BPosting>
            return.
        end.
        
        if tqGLByIDCode.tlGLIsDivisionAccount
        then do:
            if icDivisionCode <> "":U and icDivisionCode <> ?
            then assign vcDivisionCode = icDivisionCode.
            else assign vcDivisionCode = tqGLByIDCode.tcDivisionCode. 
        end.

        assign tPostingLine.GL_ID           = tqGLByIDCode.tiGL_ID
               tPostingLine.tcGLCode        = tqGLByIDCode.tcGLCode
               tPostingLine.tcGLDescription = tqGLByIDCode.tcGLDescription
               tPostingLine.tcDivisionCode  = vcDivisionCode.
        
    end.