project QadFinancials > class BCInvoice > method DefaultValuesTax

Description

Default the tax records on the tax tab


Parameters


icCInvoiceRowidinputcharacter
iiCInvoiceIdinputinteger
icCurrencyCodeinputcharactercurrency of the invoice
icDocumentReferenceinputcharacteryear/daybook + voucher
itTaxPointDateinputdatetax point date
icShipFromTaxZoneinputcharacterTax Zone of the supplier address
icShipToTaxZoneinputcharacterTax Zone of the Ship To address
icTxclTaxClassinputcharacterTax Class from the creditor
icTxuTaxUsageinputcharacterTax Usage of the Creditor
idInvoiceAmountTCinputdecimalInvoice Amount TC
idExchangeRateinputdecimalExchange Rate
idExchangeRateScaleinputdecimalscale factor of the invoice
icPaymentConditionCodeinputcharacterPaymentCondition of the creditor
itPostingDateinputdatePosting Date
ilTaxableinputlogicalTaxable
ilCreditorIsWhtinputlogicalAssigned to true if the supplier is enabled for withholding tax
icInvoiceTypeinputcharacterInvoice Type
ilShipToAddressIsTaxInCityinputlogicaltax in city flag of the ship to address
idVatExchangeRateinputdecimalVat Exchange Rate
idVatRateScaleinputdecimalScale factor of Vat exchange rate
ilIsLogisticMatchinginputlogicalIs Logistic Matching flag
ocTxenvTaxEnvoutputcharacterTax envrionment
ilCInvoiceIsTaxExcludedinputlogical
itCInvoiceDateinputdate
idCCExchangeRateinputdecimal
idCCExchangeRateScaleinputdecimal
iiShipToAddressIDinputinteger
iiShipFromAddressIDinputinteger
icCreditorCodeinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ApiStdMaintainTTWithIntPost
method BCInvoice.CreateCInvoices
method BCInvoice.DefaultValuesGeneralInfo
method BCInvoice.DefaultValuesTaxInfo
method BCInvoice.UpdateCInvoiceFromAPM


program code (program3/bcinvoice.p)

assign oiReturnStatus = -98.
empty temp-table tTaxDetailCInvoice.

/* Validate input parameters */
if icShipFromTaxZone       = ? then assign icShipFromTaxZone       = "":U.
if icShipToTaxZone         = ? then assign icShipToTaxZone         = "":U.
if icTxclTaxClass          = ? then assign icTxclTaxClass          = "":U.
if ilCreditorIsWht         = ? then assign ilCreditorIsWht = false.
 

if icCurrencyCode = vcCompanyCC   /* TC = SC */
then do:
    assign idCCExchangeRate       = 1
           idCCExchangeRateScale  = 1.
end.               
else if vlDomainIsStatutory = false         /* TC/LC Rate */
then do:
    assign idCCExchangeRate       = idExchangeRate
           idCCExchangeRateScale  = idExchangeRateScale.           
end.           

if icShipFromTaxZone = "":U or
   icShipToTaxZone   = "":U
then do:
    <M-8 run SetMessage
       (input  trim(#T-10'Tax zone of supplier and ship-to are mandatory.':150(733750230)T-10#) (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  icCInvoiceRowid (icRowid), 
        input  'QadFin-5665':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>

    assign oiReturnStatus = -1.
    return.
end.

/* Delete the old records */
for each tCInvoiceVat where
         tCInvoiceVat.tc_ParentRowid = icCInvoiceRowid:
    if tCInvoiceVat.tc_Status = "N":U
    then delete tCInvoiceVat.
    else assign tCInvoiceVat.tc_Status = "D":U.
end.


/* Delete the old WHT records */
for each tCInvoiceWHT where
         tCInvoiceWHT.tc_ParentRowid = icCInvoiceRowid:
    if tCInvoiceWHT.tc_Status = "N":U
    then delete tCInvoiceWHT.
    else assign tCInvoiceWHT.tc_Status = "D":U.  
end.


/* Do the tax stuff */
assign vhFcComponent = ?.

<M-24 run GetTaxEnvironmentAndCalculateTax
   (input  icShipFromTaxZone (icShipFromTaxZone), 
    input  icShipToTaxZone (icShipToTaxZone), 
    input  icTxclTaxClass (icTxclTaxCls), 
    input  icCurrencyCode (icCurrencyCode), 
    input  {&VATTAXTRANSACTIONTYPE-APVOUCHER} (icTransactionType), 
    input  icDocumentReference (icDocumentReference), 
    input  ? (icDocumentNumber), 
    input  not ilcinvoiceistaxExcluded (ilTaxIsIncluded), 
    input  itTaxPointDate (itTaxPointDate), 
    input  itPostingDate (itPostingDate), 
    input  icTxuTaxUsage (icTaxUsageCode), 
    input  idInvoiceAmountTC (idAmountTC), 
    input  idExchangeRateScale (idExchangeRateScale), 
    input  idExchangeRate (idExchangeRate), 
    input  vcCompanyCode (icCompanyCode), 
    input  icPaymentConditionCode (icPaymentCondition), 
    input  vlIsCompanyTaxInCityCI (ilIsTaxInCityFromCompany), 
    input  ilShipToAddressIsTaxInCity (ilIsTaxInCityFromShipTo), 
    input  ilTaxable (ilIsTaxable), 
    input  ? (idAdjustmentFactor), 
    input  iiShipFromAddressID (iiShipFromAddressID), 
    input  iiShipToAddressID (iiShipToAddressID), 
    input  icCreditorCode (icCustomerCode), 
    input  '' (icInvoiceType), 
    input-output tTaxDetailCInvoice (tTaxDetail), 
    output tTaxParamExtCInv (tTaxParametersExtVAT), 
    output ocTxenvTaxEnv (ocTxenvTaxEnv), 
    output viFcReturnSuper (oiReturnStatus)) in BVAT>

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

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

/* Count the number of records retrieved from gtm for real taxes, non-wht            */
/* because if multiple tax records founds, all amounts must be zero, for tax and wht */
/* if only one tax record is found, the wht can be calculated                        */ 
assign viFirstCInvoiceVatID = 0
       viNrOfTaxRecords     = 0.

for each tTaxDetailCInvoice:
    if tTaxDetailCInvoice.tx2d_tr_type <> {&VATTAXTRANSACTIONTYPE-APWHT}
    then assign viNrOfTaxRecords    = viNrOfTaxRecords + 1.
end.

assign vlParam                  = true
       viFcCount1               = 0
       viCInvoiceSequenceNo     = 0
       vcCompanyPropertyDelTax = "":U.

<Q-43 run CompanyPropertyBySuspDelTax (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&SUSPDELAYTAX-NOTAPPLICABLE}, (DelTax)
    input ?, (SuspTax)
    output dataset tqCompanyPropertyBySuspDelTax) in BCompanyProperty >

find first tqCompanyPropertyBySuspDelTax no-error.

if available tqCompanyPropertyBySuspDelTax
then assign vlAvailCompPropBySuspDelTax = true
            vcCompanyPropertyDelTax     = tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax.
else assign vlAvailCompPropBySuspDelTax = false.

for each tTaxDetailCInvoice:        

    /* if not WHT tax */
    if tTaxDetailCInvoice.tx2d_tr_type <> {&VATTAXTRANSACTIONTYPE-APWHT}
    then do:             
        <M-7 run AddDetailLine
           (input  'CInvoiceVat':U (icTable), 
            input  icCInvoiceRowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    
        if viFcReturnSuper < 0 or
           viFcReturnSuper > 0 and
           oiReturnStatus  = -98
        then assign oiReturnStatus = viFcReturnSuper.
    
        if oiReturnStatus  < 0 and
           oiReturnStatus <> -98
        then return.
        
        if viFirstCInvoiceVatID = 0
        then assign viFirstCInvoiceVatID = tCInvoiceVat.CInvoiceVat_ID.
        
        assign vcCInvoiceVatRowId                       = if vlParam
                                                          then tCInvoiceVat.tc_Rowid
                                                          else "":U
               tCInvoiceVat.CInvoice_ID                 = iiCInvoiceId
               viCInvoiceSequenceNo                     = viCInvoiceSequenceNo + 1               
               tCInvoiceVat.CInvoiceVatSequence         = viCInvoiceSequenceNo     
               tCInvoiceVat.CInvoiceVatUILinkedRowID    = if vlParam
                                                          then "0":U
                                                          else vcCInvoiceVatRowId
               tCInvoiceVat.CInvoiceVatVatBaseDebitTC   = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then <M-25 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_tottax (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                          else 0
               tCInvoiceVat.CInvoiceVatVatBaseCreditTC  = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then 0
                                                          else <M-26 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_tottax (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatBaseDebitLC   = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then <M-34 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_taxable_amt (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
                                                          else 0
               tCInvoiceVat.CInvoiceVatVatBaseCreditLC  = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then 0
                                                          else <M-33 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_taxable_amt (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatBaseDebitCC   = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                              <M-64 RoundAmount
                                                                  (input  tCInvoiceVat.CInvoiceVatVatBaseDebitTC * idCCExchangeRate * idCCExchangeRateScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatBaseCreditCC  = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                              <M-77 RoundAmount
                                                                  (input  tCInvoiceVat.CInvoiceVatVatBaseCreditTC * idCCExchangeRate * idCCExchangeRateScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatDebitTC       = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then <M-27 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_cur_tax_amt (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
                                                          else if icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                                               then <M-28 RoundAmount
                                                                       (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) * -1 (idUnroundedAmount), 
                                                                        input  ? (iiCurrencyID), 
                                                                        input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
                                                               else <M-40 RoundAmount
                                                                       (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) (idUnroundedAmount), 
                                                                        input  ? (iiCurrencyID), 
                                                                        input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
               tCInvoiceVat.CInvoiceVatVatCreditTC      = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                          then <M-29 RoundAmount
                                                                  (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                          else if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                               then <M-39 RoundAmount
                                                                       (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) * -1 (idUnroundedAmount), 
                                                                        input  ? (iiCurrencyID), 
                                                                        input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                               else <M-30 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_cur_tax_amt (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatDebitLC       = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then <M-35 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_tax_amt (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice> 
                                                          else <M-36 RoundAmount
                                                                  (input  abs(tTaxDetailCInvoice.tx2d_abs_ret_amt) (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice> 
               tCInvoiceVat.CInvoiceVatVatCreditLC      = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else
                                                          if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then <M-37 RoundAmount
                                                                  (input  abs(tTaxDetailCInvoice.tx2d_abs_ret_amt) (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
                                                          else <M-38 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_tax_amt (idUnroundedAmount), 
                                                                   input  viCompanyLCId (iiCurrencyID), 
                                                                   input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
               tCInvoiceVat.CInvoiceVatVatDebitCC       = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else <M-795 RoundAmount
                                                                  (input  tCInvoiceVat.CInvoiceVatVatDebitTC * idccExchangeRate * idccExchangeRateScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  vcCompanyCC (icCurrencyCode)) in BCInvoice>                              
                                                                               
               tCInvoiceVat.CInvoiceVatVatCreditCC      = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else <M-60 RoundAmount
                                                                  (input  tCInvoiceVat.CInvoiceVatVatCreditTC * idccExchangeRate * idccExchangeRateScale (idUnroundedAmount), 
                                                                   input  viCompanyCCId (iiCurrencyID), 
                                                                   input  vcCompanyCC (icCurrencyCode)) in BCInvoice>                                                                                                                                     
               tCInvoiceVat.tdCInvoiceVatExchangeRateCC = idCCExchangeRate                                                             
               tCInvoiceVat.CInvoiceVatIsTaxable        = ilTaxable
               tCInvoiceVat.TxclTaxCls                  = tTaxDetailCInvoice.tx2d_taxc
               tCInvoiceVat.TxenvTaxEnv                 = ocTxenvTaxEnv
               tCInvoiceVat.TxuTaxUsage                 = tTaxDetailCInvoice.tx2d_tax_usage
               tCInvoiceVat.TxtyTaxType                 = tTaxDetailCInvoice.tx2d_tax_type
               tCInvoiceVat.tcDomainCode                = tTaxDetailCInvoice.tx2d_domain
               tCInvoiceVat.tcVatCode                   = tTaxDetailCInvoice.tx2d_tax_code
               tCInvoiceVat.tcVatInOut                  = {&VATINOUT-INPUT}
               tCInvoiceVat.CInvoiceVatIsAbsRet         = tTaxDetailCInvoice.tx2d_abs_ret_amt <> 0
               tCInvoiceVat.CInvoiceVatIsUpdAllow       = tTaxDetailCInvoice.tlTx2UpdateTax
               tCInvoiceVat.CInvoiceVatNonRecTaxAmtTC   = if viNrOfTaxRecords > 1 and
                                                         not ilCInvoiceIsTaxExcluded
                                                          then 0
                                                          else <M-31 RoundAmount
                                                                  (input  tTaxDetailCInvoice.tx2d_cur_tax_amt - tTaxDetailCInvoice.tx2d_cur_recov_amt (idUnroundedAmount), 
                                                                   input  ? (iiCurrencyID), 
                                                                   input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
               tCInvoiceVat.CInvoiceVatIsAccrRcpUs      = (tTaxDetailCInvoice.tx2d_rcpt_tax_point = true or tTaxDetailCInvoice.tx2d_usage_tax_point = true)
               tCInvoiceVat.CInvoiceVatTaxTrType        = tTaxDetailCInvoice.tx2d_tr_type
               tCInvoiceVat.tcNormalTaxGLCode           = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then tTaxDetailCInvoice.tcTx2APGL
                                                          else tTaxDetailCInvoice.tcTx2APCNGL
               tCInvoiceVat.tcNormalTaxDivisionCode     = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                             icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                          then tTaxDetailCInvoice.tcTx2APDivision
                                                          else tTaxDetailCInvoice.tcTx2APCNDivision
               tCInvoiceVat.tcAbsRetTaxGLCode           = if tCInvoiceVat.CInvoiceVatIsAbsRet
                                                          then if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                  icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                               then tTaxDetailCInvoice.tcTx2APRGL
                                                               else tTaxDetailCInvoice.tcTx2APRCNGL
                                                          else "":U
               tCInvoiceVat.tcAbsRetTaxDivisionCode     = if tCInvoiceVat.CInvoiceVatIsAbsRet
                                                          then if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                  icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                               then tTaxDetailCInvoice.tcTx2APRDivision
                                                               else tTaxDetailCInvoice.tcTx2APRCNDivision
                                                          else "":U
               tCInvoiceVat.CInvoiceVatIsRevCharge      = tTaxDetailCInvoice.tlTx2ReverseCharge.
                              
               if vlAvailCompPropBySuspDelTax and vcCompanyPropertyDelTax <> '':U and tTaxDetailCInvoice.tltx2SuspDelayTax
               then 
                   assign 
                       tCInvoiceVat.tcSuspDelTaxGLCode = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                         icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                             then tTaxDetailCInvoice.tctx2DelayAPGL
                                                             else tTaxDetailCInvoice.tctx2DelayAPCNGL
                       tCInvoiceVat.tcSuspDelTaxDivisionCode   = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                 icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                              then tTaxDetailCInvoice.tctx2DelayAPDivision
                                                              else tTaxDetailCInvoice.tctx2DelayAPCNDivision
                       tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC = <M-42 RoundAmount
                                                                (input  tTaxDetailCInvoice.tx2d_cur_tax_amt (idUnroundedAmount), 
                                                                 input  ? (iiCurrencyID), 
                                                                 input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                       tCInvoiceVat.CInvoiceVatIsSuspDel       = true.
               else 
                   assign 
                       tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC = 0
                        tCInvoiceVat.CInvoiceVatIsSuspDel       = false.
                        
               if tCInvoiceVat.CInvoiceVatIsAbsRet
               then do:
                   if icInvoiceType = {&INVOICETYPE-INVOICE} or
                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                   then
                       assign tCInvoiceVat.CInvoiceVatVatCreditTC = tCInvoiceVat.CInvoiceVatVatDebitTC
                              tCInvoiceVat.CInvoiceVatVatCreditLC = tCInvoiceVat.CInvoiceVatVatDebitLC
                              tCInvoiceVat.CInvoiceVatVatCreditCC = tCInvoiceVat.CInvoiceVatVatDebitCC.
                              
                    else if icInvoiceType =  {&INVOICETYPE-CREDITNOTE} or
                            icInvoiceType =  {&INVOICETYPE-CREDITNOTECORRECTION}
                    then
                        assign tCInvoiceVat.CInvoiceVatVatDebitTC = tCInvoiceVat.CInvoiceVatVatCreditTC
                               tCInvoiceVat.CInvoiceVatVatDebitLC = tCInvoiceVat.CInvoiceVatVatCreditLC
                               tCInvoiceVat.CInvoiceVatVatDebitCC = tCInvoiceVat.CInvoiceVatVatCreditCC.
                end.                                               
  
        /* read the mfg tax code to retrieve the absorbed retained flag, because looking at the amount is not a good solution */
        /* when the amount is zero, you don't get the right result                                                            */
        if tTaxDetailCInvoice.tx2d_abs_ret_amt = 0
        then do:
            <Q-53 run MfgTaxCodeByTaxCode (all) (Read) (Cache)
               (input tTaxDetailCInvoice.tx2d_tax_code, (TaxCode)
                input tTaxDetailCInvoice.tx2d_domain, (DomainCode)
                input ?, (DiscountAtPayment)
                input ?, (IsTaxByLine)
                output dataset tqMfgTaxCodeByTaxCode) in BMfgTaxCode> 
                
             find first tqMfgTaxCodeByTaxCode where 
                        tqMfgTaxCodeByTaxCode.tctx2_tax_code = tTaxDetailCInvoice.tx2d_tax_code and
                        tqMfgTaxCodeByTaxCode.tctx2_domain   = tTaxDetailCInvoice.tx2d_domain
                        no-error.
             if available tqMfgTaxCodeByTaxCode
             then assign tCInvoiceVat.CInvoiceVatIsAbsRet = tqMfgTaxCodeByTaxCode.tltx2_apr_use.
        end.
    end. /* if tax type <> witholding tax */
    
    else do:                    
        /* if WHT */
        /* first check that the domain & supplier is wht enabled */
        if vlIsWHTOnCompany and ilCreditorIsWht
        then do:
            <M-14 run AddDetailLine
               (input  'CInvoiceWHT':U (icTable), 
                input  icCInvoiceRowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        
             if viFcReturnSuper < 0 or
                viFcReturnSuper > 0 and
                oiReturnStatus  = -98
             then assign oiReturnStatus = viFcReturnSuper.
         
             if oiReturnStatus  < 0 and
                oiReturnStatus <> -98
             then return.
             
             /************/            
             /* TC to LC */
             /************/        
             if icCurrencyCode <> "":U and
                icCurrencyCode = vcCompanyLC 
             then do:
                 assign vdCInvoiceExchangeRateLC      = 1
                        vdCInvoiceExchangeRateScaleLC = 1.
             end.           
             else do:
                                      
                 <M-72 run GetExRateByEntityInvoiceFlag
                    (input  itCInvoiceDate (itCInvoiceDate), 
                     input  itPostingDate (itCInvoicePostingDate), 
                     input  ? (iiFromCurrencyId), 
                     input  icCurrencyCode (icFromCurrencyCode), 
                     input  viCompanyLCId (iiToCurrencyId), 
                     input  vcCompanyLC (icToCurrencyCode), 
                     input  0 (iiExchangeRateTypeId), 
                     input  {&EXCHANGERATETYPE-VAT} (icExchangeRateTypeCode), 
                     output vdCInvoiceExchangeRateLC (odCInvoiceExchangeRate), 
                     output vdCInvoiceExchangeRateScaleLC (odCInvoiceRateScale), 
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                 if viFcReturnSuper < 0 or viLocalReturnStatus = 0
                 then assign viLocalReturnStatus = viFcReturnSuper.                        
             end. /* else do */
                          
             /************/            
             /* TC to CC */
             /************/        
             if icCurrencyCode <> "":U and
                icCurrencyCode = vcCompanyCC 
             then do:
                 assign vdCInvoiceExchangeRateCC      = 1
                        vdCInvoiceExchangeRateScaleCC = 1.
             end.           
             else do:
                                      
                 <M-12 run GetExRateByEntityInvoiceFlag
                    (input  itCInvoiceDate (itCInvoiceDate), 
                     input  itPostingDate (itCInvoicePostingDate), 
                     input  ? (iiFromCurrencyId), 
                     input  icCurrencyCode (icFromCurrencyCode), 
                     input  viCompanyCCId (iiToCurrencyId), 
                     input  vcCompanyCC (icToCurrencyCode), 
                     input  0 (iiExchangeRateTypeId), 
                     input  {&EXCHANGERATETYPE-VAT} (icExchangeRateTypeCode), 
                     output vdCInvoiceExchangeRateCC (odCInvoiceExchangeRate), 
                     output vdCInvoiceExchangeRateScaleCC (odCInvoiceRateScale), 
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                 if viFcReturnSuper < 0 or viLocalReturnStatus = 0
                 then assign viLocalReturnStatus = viFcReturnSuper.                        
             end. /* else do */
            
             assign tCInvoiceWHT.CInvoice_ID                = iiCInvoiceId
                    tCInvoiceWHT.CInvoiceVat_ID             = viFirstCInvoiceVatID
                    tCInvoiceWHT.CInvoiceWHTFeeDebitTC      = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then 0
                                                                   else <M-46 RoundAmount
                                                                           (input  tTaxDetailCInvoice.tx2d_totamt (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                    tCInvoiceWHT.CInvoiceWHTFeeCreditTC     = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then <M-5 RoundAmount
                                                                           (input  tTaxDetailCInvoice.tx2d_totamt (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                                   else 0
                    tCInvoiceWHT.CInvoiceWHTExpenseDebitTC  = 0
                    tCInvoiceWHT.CInvoiceWHTExpenseCreditTC = 0                 
                    tCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC   = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then 0
                                                                   else <M-75 RoundAmount
                                                                           (input  tTaxDetailCInvoice.tx2d_tottax (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                    tCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC  = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then <M-95 RoundAmount
                                                                           (input  tTaxDetailCInvoice.tx2d_tottax (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                                   else 0                              
                    tCInvoiceWHT.CInvoiceWHTTaxFeeDebitLC   = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then 0
                                                                   else <M-79 RoundAmount
                                                                           (input  tCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC * vdCInvoiceExchangeRateLC * vdCInvoiceExchangeRateScaleLC (idUnroundedAmount), 
                                                                            input  viCompanyLCId (iiCurrencyID), 
                                                                            input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
                    tCInvoiceWHT.CInvoiceWHTTaxFeeCreditLC  = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then <M-84 RoundAmount
                                                                           (input  tCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC * vdCInvoiceExchangeRateLC * vdCInvoiceExchangeRateScaleLC (idUnroundedAmount), 
                                                                            input  viCompanyLCId (iiCurrencyID), 
                                                                            input  vcCompanyLC (icCurrencyCode)) in BCInvoice>
                                                                   else 0
                    tCInvoiceWHT.CInvoiceWHTTaxFeeDebitCC   = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else <M-73 RoundAmount
                                                                      (input  tCInvoiceWHT.CInvoiceWHTTaxFeeDebitTC * vdCInvoiceExchangeRateCC * vdCInvoiceExchangeRateScaleCC (idUnroundedAmount), 
                                                                       input  viCompanyCCId (iiCurrencyID), 
                                                                       input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
    
                    tCInvoiceWHT.CInvoiceWHTTaxFeeCreditCC  = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else   <M-1 RoundAmount
                                                                        (input  tCInvoiceWHT.CInvoiceWHTTaxFeeCreditTC * vdCInvoiceExchangeRateCC * vdCInvoiceExchangeRateScaleCC (idUnroundedAmount), 
                                                                         input  viCompanyCCId (iiCurrencyID), 
                                                                         input  vcCompanyCC (icCurrencyCode)) in BCInvoice>   
                                                                                 
                    tCInvoiceWHT.CInvoiceWHTAmtCreditTC     = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                      icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                   then <M-51 RoundAmount
                                                                           (input  tTaxDetailCInvoice.tx2d_cur_tax_amt (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
                                                                   else if icInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                                                        then <M-954 RoundAmount
                                                                                (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) * -1 (idUnroundedAmount), 
                                                                                 input  ? (iiCurrencyID), 
                                                                                 input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
                                                                        else <M-81 RoundAmount
                                                                                (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) (idUnroundedAmount), 
                                                                                 input  ? (iiCurrencyID), 
                                                                                 input  icCurrencyCode (icCurrencyCode)) in BCInvoice> 
                    tCInvoiceWHT.CInvoiceWHTAmtDebitTC      = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else if icInvoiceType = {&INVOICETYPE-INVOICE} 
                                                                   then <M-89 RoundAmount
                                                                           (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) (idUnroundedAmount), 
                                                                            input  ? (iiCurrencyID), 
                                                                            input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                                   else if icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                                        then <M-98 RoundAmount
                                                                                (input  abs(tTaxDetailCInvoice.tx2d_cur_abs_ret_amt) * -1 (idUnroundedAmount), 
                                                                                 input  ? (iiCurrencyID), 
                                                                                 input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                                        else <M-63 RoundAmount
                                                                                (input  tTaxDetailCInvoice.tx2d_cur_tax_amt (idUnroundedAmount), 
                                                                                 input  ? (iiCurrencyID), 
                                                                                 input  icCurrencyCode (icCurrencyCode)) in BCInvoice>
                                                                
                    tCInvoiceWHT.CInvoiceWHTAmtCreditLC     = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else <M-83 RoundAmount
                                                                      (input  tCInvoiceWHT.CInvoiceWHTAmtcreditTC * vdCInvoiceExchangeRateLC * vdCInvoiceExchangeRateScaleLC (idUnroundedAmount), 
                                                                       input  viCompanyLCId (iiCurrencyID), 
                                                                       input  vcCompanyLC (icCurrencyCode)) in BCInvoice> 
                                                           
                    tCInvoiceWHT.CInvoiceWHTAmtDebitLC      = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else <M-190 RoundAmount
                                                                      (input  tCInvoiceWHT.CInvoiceWHTAmtDebitTC * vdCInvoiceExchangeRateLC * vdCInvoiceExchangeRateScaleLC (idUnroundedAmount), 
                                                                       input  viCompanyLCId (iiCurrencyID), 
                                                                       input  vcCompanyLC (icCurrencyCode)) in BCInvoice>

                    tCInvoiceWHT.CInvoiceWHTAmtDebitCC      = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else <M-47 RoundAmount
                                                                      (input  tCInvoiceWHT.CInvoiceWHTAmtDebitTC * vdCInvoiceExchangeRateCC * vdCInvoiceExchangeRateScaleCC (idUnroundedAmount), 
                                                                       input  viCompanyCCId (iiCurrencyID), 
                                                                       input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
    
                    tCInvoiceWHT.CInvoiceWHTAmtCreditCC     = if viNrOfTaxRecords > 1
                                                              then 0
                                                              else <M-96 RoundAmount
                                                                      (input  tCInvoiceWHT.CInvoiceWHTAmtCreditTC * vdCInvoiceExchangeRateCC * vdCInvoiceExchangeRateScaleCC (idUnroundedAmount), 
                                                                       input  viCompanyCCId (iiCurrencyID), 
                                                                       input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
           
                    tCInvoiceWHT.tcVatInOut                 = {&VATINOUT-INPUT}
                    tCInvoiceWHT.tcDomainCode               = tTaxDetailCInvoice.tx2d_domain
                    tCInvoiceWHT.CInvoiceWHTTaxPct          = tTaxDetailCInvoice.tdTx2TaxPerc
                    tCInvoiceWHT.CInvoiceWHTRecovTaxPct     = tTaxDetailCInvoice.tdTx2RecovTaxPerc
                    tCInvoiceWHT.CInvoiceWHTTaxType         = tTaxDetailCInvoice.tx2d_tax_type
                    tCInvoiceWHT.CInvoiceWHTTaxTrType       = tTaxDetailCInvoice.tx2d_tr_type
                    tCInvoiceWHT.tcVatCode                  = tTaxDetailCInvoice.tx2d_tax_code
                    tCInvoiceWHT.tcGLCode                   = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                 icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                              then tTaxDetailCInvoice.tcTx2APGL
                                                              else tTaxDetailCInvoice.tcTx2APCNGL
                    tCInvoiceWHT.tcDivisionCode             = if icInvoiceType = {&INVOICETYPE-INVOICE} or
                                                                 icInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                              then tTaxDetailCInvoice.tcTx2APDivision
                                                              else tTaxDetailCInvoice.tcTx2APCNDivision.                                                              
                                                              
            /*****************************/
            /* Get Tax Description Field */
            /*****************************/
            if tTaxDetailCInvoice.tx2d_tax_code <> "":U and
               tTaxDetailCInvoice.tx2d_tax_code <> ?
            then do:     
                                       
                <Q-80 run MfgTaxCodeByTaxCode (all) (Read) (Cache)
                   (input tTaxDetailCInvoice.tx2d_tax_code, (TaxCode)
                    input tTaxDetailCInvoice.tx2d_domain, (DomainCode)
                    input ?, (DiscountAtPayment)
                    input ?, (IsTaxByLine)
                    output dataset tqMfgTaxCodeByTaxCode) in BMfgTaxCode> 
                
                find first tqMfgTaxCodeByTaxCode
                     where tqMfgTaxCodeByTaxCode.tctx2_tax_code = tTaxDetailCInvoice.tx2d_tax_code no-error.
                if available tqMfgTaxCodeByTaxCode
                then do:
                    assign tCInvoiceWHT.tcVatDescription      = tqMfgTaxCodeByTaxCode.tctx2_desc
                           tCInvoiceWHT.CInvoiceWHTBaseTaxPct = 100.     

                    if tqMfgTaxCodeByTaxCode.tctx2_base <> "" and
                       tqMfgTaxCodeByTaxCode.tctx2_base <> ?
                    then do:
                        <Q-85 run GeneralizedCodeForSecurity (all) (Read) (Cache)
                           (input vcDomainCode, (DomainCode)
                            input 'txb_base', (CodeFldName)
                            input tqMfgTaxCodeByTaxCode.tctx2_base, (CodeValue)
                            output dataset tqGeneralizedCodeForSecurity) in BMfgGeneralizedCode>
                        find first tqGeneralizedCodeForSecurity where
                                   tqGeneralizedCodeForSecurity.tccode_domain  = vcDomainCode 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 tCInvoiceWHT.CInvoiceWHTBaseTaxPct = decimal(vcWHTBasePercentage) no-error.
                            if error-status:error
                            then assign tCInvoiceWHT.CInvoiceWHTBaseTaxPct = 100.
                        end. /* if available tqGeneralizedCodeForSecurity */
                    end. /* if tqMfgTaxCodeByTaxCode.tctx2_base <> "" and */   
                end. /* if available tqMfgTaxCodeByTaxCode */      
                          
            end. /* if icWHTCode <> "":U */                                           
                                
        end. /* vlIsWHTOnCompany or ilCreditorIsWht */          
    end. /* else if tax type = wht */                                                                        
end. /*for each tTaxDetailCInvoice: */
/* assign the CInvoiceVat_ID */
for each tCInvoiceWHT:
    assign tCInvoiceWHT.CInvoiceVat_ID = viFirstCInvoiceVatID.
end. 
 /* for  each tCInvoiceWHT*/    
    
if oiReturnStatus = -98
then assign oiReturnStatus = 0.