project QadFinancials > class BCInvoice > method CreateCInvoiceMovementsDelTax

Description

Calculates the amount for the creation of the tax posting lines and calculates the amount for the undo of the delayed tax account


Parameters


ilIsCallFromBankingEntryOrOIAdjinputlogical
tCInvoiceVatDelayDeltainput-outputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.CreateCInvoiceMovements


program code (program7/bcinvoice.p)

MAIN_BLOCK:
do on error undo, throw:

    /* ============================================================== */
    /* Precondition                                                   */
    /* Delayed taxes are handled in this routine for Banking entry    */
    /* and for Open item adjustment. Payment module is handling       */
    /* delayed taxes inside component                                 */
    /* ============================================================== */
    if ilIsCallFromBankingEntryOrOIAdj = ? or
       ilIsCallFromBankingEntryOrOIAdj = false
    then leave MAIN_BLOCK.
    
    /* ============================================================== */
    /* Get entity definition for the delayed taxes                    */
    /* ============================================================== */
    <Q-79 run CompanyPropertyBySuspDelTax (all) (Read) (Cache)
       (input tCInvoice.Company_ID, (CompanyId)
        input ?, (DelTax)
        input ?, (SuspTax)
        output dataset tqCompanyPropertyBySuspDelTax) in BCompanyProperty>
            
    find first tqCompanyPropertyBySuspDelTax where
               tqCompanyPropertyBySuspDelTax.tiCompany_ID = tCInvoice.Company_ID
               no-error.
    if not available tqCompanyPropertyBySuspDelTax
    then do:
        assign vcMessage =  #T-93'Cannot read tax definition from the entity definition.':255(375465225)T-93#
               vcContext = "tCInvoice.Company_ID=&1":U
               vcContext = substitute(vcContext, tCInvoice.Company_ID).
        <M-45 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-867292':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>           
        assign oiReturnStatus = -1.
        leave MAIN_BLOCK.
    end.
    
    /* ============================================================== */
    /* calculate the amount for the creation of the tax posting lines */
    /* ============================================================== */
    for each tCInvoiceVat where 
             tCInvoiceVat.CInvoice_ID = tMovement.tiCInvoiceId:
    
        assign vdDelTaxAmountTC = 0.
    
        if tCInvoiceVat.CInvoiceVatIsSuspDel = true
        then do:
            if tMovement.tlIsUndoPayment = false
            then do:
            
                /* Is this the last payment of the invoice ? */
                assign vlIsLastPayment = ((tCInvoice.CInvoiceType           = {&INVOICETYPE-INVOICE} or 
                                           tCInvoice.CInvoiceType           = {&INVOICETYPE-INVOICECORRECTION}) and
                                          tCInvoice.CInvoiceBalanceCreditTC = tMovement.tdAmountDebitTC) OR
                                         ((tCInvoice.CInvoiceType           = {&INVOICETYPE-CREDITNOTE} or 
                                           tCInvoice.CInvoiceType           = {&INVOICETYPE-CREDITNOTECORRECTION}) and
                                           tCInvoice.CInvoiceBalanceDebitTC = tMovement.tdAmountCreditTC).
                   
                /* Make an extra check to see if we can find a document which has not been paid, for example is in status 'For Collection' */
                /* If we can find such document then this is not the last payment */                           
                if tqCompanyPropertyBySuspDelTax.tlCompanyPropertyIsDelPaid and
                   vlIsLastPayment                                          and
                   (tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-LASTPAY} or
                    tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-PROPORPAY})
                then do:
                    <Q-20 run CDocumentInvoiceXrefByStatus (all) (Read) (NoCache)
                       (input tCInvoice.CInvoice_ID, (CInvoiceId)
                        input ?, (DocumentStatus)
                        input ?, (DocumentType)
                        input ?, (CompanyId)
                        input ?, (DInvoiceId)
                        output dataset tqCDocumentInvoiceXrefByStatus) in BCDocument>
                    if can-find (first tqCDocumentInvoiceXrefByStatus where
                                       tqCDocumentInvoiceXrefByStatus.tiCInvoice_ID      = tCInvoice.CInvoice_ID      and
                                       tqCDocumentInvoiceXrefByStatus.tcCDocumentStatus <> {&DOCUMENTSTATUS-PAID}     and
                                       tqCDocumentInvoiceXrefByStatus.tcCDocumentStatus <> {&DOCUMENTSTATUS-PAIDCON}  and
                                       tqCDocumentInvoiceXrefByStatus.tcCDocumentStatus <> {&DOCUMENTSTATUS-VOID}     and
                                       tqCDocumentInvoiceXrefByStatus.tcCDocumentStatus <> {&DOCUMENTSTATUS-INIT}     and
                                       tqCDocumentInvoiceXrefByStatus.tcCDocumentStatus <> {&DOCUMENTSTATUS-BOUNCED})
                    then assign vlIsLastPayment = false.
                end.                                           
            
                /* ================================================================================================ *
                 * Payment of the Supplier invoice in the Banking entry (IsDelTaxAtPartialPayment  = true)          *
                 * Payment of the Supplier payment in the Banking entry (tMovement.tiCDocumentId <> 0) and delayed  *
                 * tax applied when Paid                                                                            *
                 * ================================================================================================ */
                if tMovement.tiCDocumentId              <> 0    or
                   tMovement.tlIsDelTaxAtPartialPayment  = true
                then do:
                    /* Adjust sign of the discount -> it follows the payment direction. But we need DB and CR  */
                    assign vdDiscountTC = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
                                             tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}
                                          then - tMovement.tdMovementDiscountTC 
                                          else tMovement.tdMovementDiscountTC.
                
                    /* Delayed tax applied during first payment */
                    /* Delayed tax applied during last payment */
                    /* Delayed tax applied proportionally but this is the last payment - take remaining amount */
                    if (tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-FIRSTPAY})    or
                       (tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-LASTPAY} and
                        vlIsLastPayment)                                                                     or
                       (tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-PROPORPAY} and
                        vlIsLastPayment)
                    then do:
                        /* Check, if there is not already created a request to move delayed tax from the delayed    *
                         * tax account to normal tax account. This can happen when there is also used WHT.          *
                         * If this is the next reqeust, just ignore it. CInvoiceVatSuspDelTaxAmtTC field is updated *
                         * later                                                                                    */
                        assign vdDelTaxAmountTC = tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC.
                        
                        for each tCInvoiceVatDelayDelta where
                                 tCInvoiceVatDelayDelta.tiCInvoiceVat_ID = tCInvoiceVat.CInvoiceVat_ID:
                            assign vdDelTaxAmountTC = vdDelTaxAmountTC - tCInvoiceVatDelayDelta.tdDelTaxAmtDeltaTC.
                        end.
                    end.
                    
                    /* Delayed tax applied proportionally but this is not the last payment */
                    else if tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-PROPORPAY}
                         then assign vdDelTaxAmountTC = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
                                                           tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                                         then abs((tMovement.tdAmountDebitTC - vdDiscountTC)  / tCInvoice.CInvoiceOriginalCreditTC) * tCInvoiceVat.CInvoiceVatVatDebitTC
                                                         else abs((tMovement.tdAmountCreditTC - vdDiscountTC) / tCInvoice.CInvoiceOriginalDebitTC)  * tCInvoiceVat.CInvoiceVatVatCreditTC
                                     vdDelTaxAmountTC = <M-2 RoundAmount
                                                           (input  vdDelTaxAmountTC (idUnroundedAmount), 
                                                            input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                                            input  ? (icCurrencyCode)) in business>.
                    else assign vdDelTaxAmountTC = 0.
                end. /* if tMovement.tiCDocumentId <> 0 */
                
                /* ============================================================================================= *
                 * Payment of the supplier invoice in Open iteam adjustment (IsDelTaxAtPartialPayment  = false)  *
                 * ============================================================================================= */
                else do:
                    if vlIsLastPayment and
                       (tMovement.tdAmountDebitTC  <> 0 or
                        tMovement.tdAmountCreditTC <> 0) 
                    then assign vdDelTaxAmountTC = tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC.
                end.
                                               
                    
            end. /* end tMovement.tlIsUndoPayment = false */
            else do:
                /* ========================================================================= */
                /* calculate the amount for the undo of the tax posting lines of the payment */
                /* ========================================================================= */
                if tMovement.tiCDocumentId <> 0
                then do:
                    
                    assign vcStatusList = {&DOCUMENTSTATUS-ACCEPT} + ',':U + {&DOCUMENTSTATUS-DISCONT} + ',':U + {&DOCUMENTSTATUS-INCASSO} + ',':U + {&DOCUMENTSTATUS-PORTFOLIO}.
                    <Q-3 assign vlFcQueryRecordsAvailable = CDocumentInvoiceXrefPostByStatus (NoCache)
                        (input tCInvoice.CInvoice_ID, (CInvoiceId)
                         input vcStatusList, (DocumentStatus)
                         input ?, (DocumentType)
                         input ?, (CompanyId)
                         input ?, (DInvoiceId)
                         input {&GLTYPECODE-VAT}, (GLTypeCode)
                         input tMovement.tiCDocumentId, (CDocumentId)) in BCDocument >
                    if vlFcQueryRecordsAvailable <> false
                    then do:
                        if tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-FIRSTPAY} or
                           tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-LASTPAY}
                        then assign vdDelTaxAmountTC = if (tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or
                                                           tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION})
                                                       then tCInvoiceVat.CInvoiceVatVatDebitTC
                                                       else tCInvoiceVat.CInvoiceVatVatCreditTC.
                        else if tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-PROPORPAY}
                             then assign vdDelTaxAmountTC = if ((tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICE} or 
                                                                 tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICECORRECTION}) and
                                                                 abs(tCInvoice.CInvoiceOriginalCreditTC) = abs(tCInvoice.CInvoiceBalanceCreditTC) + tMovement.tdAmountCreditTC)
                                                            then (tCInvoiceVat.CInvoiceVatVatDebitTC - tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC)            
                                                            else if ((tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTE} or 
                                                                      tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTECORRECTION}) and
                                                                      abs(tCInvoice.CInvoiceOriginalDebitTC) = abs(tCInvoice.CInvoiceBalanceDebitTC) + tMovement.tdAmountDebitTC)
                                                                 then (tCInvoiceVat.CInvoiceVatVatCreditTC - tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC)                                                
                                                                 else if (tCInvoice.CInvoiceType  = {&INVOICETYPE-INVOICE} or 
                                                                          tCInvoice.CInvoiceType  = {&INVOICETYPE-INVOICECORRECTION})
                                                                      then (tMovement.tdAmountCreditTC / abs(tCInvoice.CInvoiceOriginalCreditTC)) * tCInvoiceVat.CInvoiceVatVatDebitTC
                                                                      else (tMovement.tdAmountDebitTC  / abs(tCInvoice.CInvoiceOriginalDebitTC)) * tCInvoiceVat.CInvoiceVatVatCreditTC
                                         vdDelTaxAmountTC = <M-4 RoundAmount
                                                              (input  vdDelTaxAmountTC (idUnroundedAmount), 
                                                               input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                                               input  ? (icCurrencyCode)) in business>.   
                             else assign vdDelTaxAmountTC = 0.
                    end. /* end vlFcQueryRecordsAvailable <> false */
                end. /*  if tMovement.tiCDocumentId <> 0 */
                /* ==================================================================================================== */
                /* calculate the amount for the undo of the tax posting lines of the payment for Cross-Company Invoices */
                /* ==================================================================================================== */
                else do:
                    if (tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-FIRSTPAY} or
                        tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-LASTPAY})
                    then assign vdDelTaxAmountTC = if (tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICE} or 
                                                       tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICECORRECTION}) and
                                                       abs(tCInvoice.CInvoiceOriginalCreditTC) = abs(tCInvoice.CInvoiceBalanceCreditTC) + abs(tMovement.tdAmountCreditTC)
                                                   then tCInvoiceVat.CInvoiceVatVatDebitTC         
                                                   else if (tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTE} or 
                                                            tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTECORRECTION}) and
                                                            abs(tCInvoice.CInvoiceOriginalDebitTC) = abs(tCInvoice.CInvoiceBalanceDebitTC) + abs(tMovement.tdAmountDebitTC)
                                                   then tCInvoiceVat.CInvoiceVatVatCreditTC 
                                                   else 0.
                    else if tqCompanyPropertyBySuspDelTax.tcCompanyPropertyDelTax = {&SUSPDELAYTAX-PROPORPAY}
                    then assign vdDelTaxAmountTC = if ((tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICE} or 
                                                        tCInvoice.CInvoiceType                  = {&INVOICETYPE-INVOICECORRECTION}) and
                                                        abs(tCInvoice.CInvoiceOriginalCreditTC) = abs(tCInvoice.CInvoiceBalanceCreditTC) + tMovement.tdAmountCreditTC)
                                                   then (tCInvoiceVat.CInvoiceVatVatDebitTC - tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC)
                                                   else if ((tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTE} or 
                                                             tCInvoice.CInvoiceType                 = {&INVOICETYPE-CREDITNOTECORRECTION}) and
                                                             abs(tCInvoice.CInvoiceOriginalDebitTC) = abs(tCInvoice.CInvoiceBalanceDebitTC) + tMovement.tdAmountDebitTC)
                                                   then (tCInvoiceVat.CInvoiceVatVatCreditTC - tCInvoiceVat.CInvoiceVatSuspDelTaxAmtTC)
                                                   else if (tCInvoice.CInvoiceType  = {&INVOICETYPE-INVOICE} or 
                                                            tCInvoice.CInvoiceType  = {&INVOICETYPE-INVOICECORRECTION})
                                                   then (tMovement.tdAmountCreditTC / abs(tCInvoice.CInvoiceOriginalCreditTC)) * tCInvoiceVat.CInvoiceVatVatDebitTC
                                                   else (tMovement.tdAmountDebitTC  / abs(tCInvoice.CInvoiceOriginalDebitTC)) * tCInvoiceVat.CInvoiceVatVatCreditTC
                                vdDelTaxAmountTC = <M-58 RoundAmount
                                                   (input  vdDelTaxAmountTC (idUnroundedAmount), 
                                                    input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in business>.
                     else assign vdDelTaxAmountTC = 0.
                end. /*end tMovement.tiCDocumentId = 0 */
                assign vdDelTaxAmountTC = - vdDelTaxAmountTC.
            end. /* end else tMovement.tlIsUndoPayment = true */
            
            /* Create request for the routine to update delayed taxes and generate postings */
            if vdDelTaxAmountTC <> 0
            then do:
                find tCInvoiceVatDelayDelta where
                     tCInvoiceVatDelayDelta.tiCInvoiceVat_ID     = tCInvoiceVat.CInvoiceVat_ID and
                     tCInvoiceVatDelayDelta.tlIsGenerateJEDelTax = true                        and
                     tCInvoiceVatDelayDelta.tlIsGenerateJETax    = true
                     no-error.
                if not available tCInvoiceVatDelayDelta
                then do:
                    create tCInvoiceVatDelayDelta.
                    
                    assign tCInvoiceVatDelayDelta.tiCInvoice_ID        = tCInvoice.CInvoice_ID
                           tCInvoiceVatDelayDelta.tiCInvoiceVat_ID     = tCInvoiceVat.CInvoiceVat_ID
                           tCInvoiceVatDelayDelta.tcPostingRowId       = tMovement.tcPostingRowId
                           tCInvoiceVatDelayDelta.tdDelTaxAmtDeltaTC   = 0
                           tCInvoiceVatDelayDelta.tlIsGenerateJEDelTax = true
                           tCInvoiceVatDelayDelta.tlIsGenerateJETax    = true.
                 end. 
                 
                 assign tCInvoiceVatDelayDelta.tdDelTaxAmtDeltaTC = tCInvoiceVatDelayDelta.tdDelTaxAmtDeltaTC
                                                                  + vdDelTaxAmountTC.
            end. /* if vdDelTaxAmountTC <> 0 */        
        end. /* if tCInvoiceVat.CInvoiceVatIsSuspDel = true */
    end. /* for each tCInvoiceVat */
end. /* MAIN_BLOCK */