project QadFinancials > class BCInvoice > method CreateCInvoiceMovementsWHT

Description

CreateCInvoiceMovementsWHT: submethod of CreateCInvoiceMovements that is only called for a tMovement with tlMovementIsAboutWHT=true.
This method will create the required PostingLines based upon the existng CInvoiceWHT-records of the Invoice


This method will first load all invoices linked to the movements or stages that got passed to the method.
Create the postingline on the control account and add the movement record.
Also update the balance fields and status fields of the invoices.
This method will also balance the LC and CC amounts of the invoice, when the invoice gets balanced in TC.


Parameters


idDebitTCinputdecimal
idCreditTCinputdecimal
idDebitLCinputdecimal
idCreditLCinputdecimal
idDebitCCinputdecimal
idCreditCCinputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.CreateCInvoiceMovements


program code (program7/bcinvoice.p)

    
    /* ================== */
    /* Exception handling */
    /* Check prerequisites */
    /* ================== */
    assign oiReturnStatus      = -98
           viLocalReturnStatus = 0
           vcMessage           = "":U.
    if not available tMovement
    then assign vcMessage = trim(substitute(#T-96'Internal error: Temp-table &2 is not available in method &1.':255(403934938)T-96#,"BCInvoice:CreateCInvoiceMovements":U,"tMovement":U)).
    if not available tCInvoice
    then assign vcMessage = trim(substitute(#T-15'Internal error: Temp-table &2 is not available in method &1.':255(201758433)T-15#,"BCInvoice:CreateCInvoiceMovements":U,"tCInvoice":U)).
    if vcMessage <> "":U 
    then do : 
        assign oiReturnStatus = -3.
        <M-42 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-620800':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return. 
    end. /* if not available tCInvoice */
    
    /* =================== */
    /* WHTProcessing-clock */
    /* =================== */
    WHTPROCESSINGBLOCK : DO :
        
        /* ============================================ */
        /* Go through all tCInvoiceWHTs of the CInvoice */
        /* and calculate TotalWHTAmount of the CInvoice */
        /* ============================================ */
        for each tCInvoiceWHT where 
                 tCInvoiceWHT.CInvoice_ID = tMovement.tiCInvoiceId and
                 tCInvoiceWHT.tc_Status   <> "D":U 
                 no-lock :
            assign vdTotalWHTAmountTCOnCInvoice = vdTotalWHTAmountTCOnCInvoice + abs(tCInvoiceWHT.CInvoiceWHTAmtDebitTC - tCInvoiceWHT.CInvoiceWHTAmtCreditTC)
                   vdTotalWHTAmountLCOnCInvoice = vdTotalWHTAmountLCOnCInvoice + abs(tCInvoiceWHT.CInvoiceWHTAmtDebitLC - tCInvoiceWHT.CInvoiceWHTAmtCreditLC)
                   vdTotalWHTAmountCCOnCInvoice = vdTotalWHTAmountCCOnCInvoice + abs(tCInvoiceWHT.CInvoiceWHTAmtDebitCC - tCInvoiceWHT.CInvoiceWHTAmtCreditCC).
        end. /* for each tCInvoiceWHT where */
        
        /* ================================================================================================================== */
        /* We have to pro-rate the MovementAmount against the total WHTAmount of the invoice as it can be a partial payment   */
        /* In case the pro-rate becomes zero then we can simply leave the block as in that case nothing no actions are needed */
        /* ================================================================================================================== */
        /* TC */
        if vdTotalWHTAmountTCOnCInvoice = 0 or 
           vdTotalWHTAmountTCOnCInvoice = ? or 
           (idDebitTC - idCreditTC)     = 0 or
           (idDebitTC - idCreditTC)     = ?
        then leave WHTPROCESSINGBLOCK.
        assign vdProRateFactorTC = absolute(idDebitTC - idCreditTC) / absolute(vdTotalWHTAmountTCOnCInvoice).
        /*LC */
        assign vdProRateFactorLC = absolute(idDebitLC - idCreditLC) / absolute(vdTotalWHTAmountLCOnCInvoice)
               no-error.
        if error-status:error
        then assign vdProRateFactorLC = vdProRateFactorTC.
        /* CC */
        assign vdProRateFactorCC = absolute(idDebitCC - idCreditCC) / absolute(vdTotalWHTAmountCCOnCInvoice)
               no-error.
        if error-status:error
        then assign vdProRateFactorCC = vdProRateFactorTC.
        /* Verification */
        if vdProRateFactorTC = 0 or 
           vdProRateFactorTC = ? or 
           vdProRateFactorLC = 0 or 
           vdProRateFactorLC = ? or 
           vdProRateFactorCC = 0 or 
           vdProRateFactorCC = ?
        then leave WHTPROCESSINGBLOCK.
        
        /* =========================================================================================================================== */
        /* Go through all tCInvoiceWHTs of the tCInvoice where Fee is filled as that are the only ones that should result in a posting */
        /* =========================================================================================================================== */
        for each tCInvoiceWHT where 
                 tCInvoiceWHT.CInvoice_ID = tMovement.tiCInvoiceId and
                 tCInvoiceWHT.tc_Status   <> "D":U and 
                 ((tCInvoiceWHT.CInvoiceWHTFeeDebitTC  <> 0 and 
                   tCInvoiceWHT.CInvoiceWHTFeeDebitTC  <> ?)   or 
                  (tCInvoiceWHT.CInvoiceWHTFeeCreditTC <> 0 and 
                   tCInvoiceWHT.CInvoiceWHTFeeCreditTC <> ?))
                 break by tCInvoiceWHT.tcGLCode :
            
            /* ================================================= */
            /* Actions per GLCode: Reset AmountToPost per GLCode */
            /* ================================================= */
            if first-of(tCInvoiceWHT.tcGLCode)
            then assign vdToPostDebitAmountTC  = 0
                        vdToPostCreditAmountTC = 0
                        vdToPostDebitAmountLC  = 0
                        vdToPostCreditAmountLC = 0
                        vdToPostDebitAmountCC  = 0
                        vdToPostCreditAmountCC = 0.
            
            /* ============================================================================================== */
            /* Sum the amounts to be posted as we only have 1 PostingLine per GL on the CInvoiceWHT.          */
            /* Note: When the real, non-wht movement is on the credit-side then WHT must be on the debit-side */
            /*       The caller should take care of this: we will post here as we receive things in tMovement */
            /* ============================================================================================== */
            if tCInvoiceWHT.CInvoiceWHTAmtDebitTC <> 0 and 
               tCInvoiceWHT.CInvoiceWHTAmtDebitTC <> ?
            then assign vdToPostDebitAmountTC  = vdToPostDebitAmountTC  + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitTC - tCInvoiceWHT.CInvoiceWHTAmtCreditTC)
                        vdToPostDebitAmountLC  = vdToPostDebitAmountLC  + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitLC - tCInvoiceWHT.CInvoiceWHTAmtCreditLC)
                        vdToPostDebitAmountCC  = vdToPostDebitAmountCC  + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitCC - tCInvoiceWHT.CInvoiceWHTAmtCreditCC).
            else assign vdToPostCreditAmountTC = vdToPostCreditAmountTC + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitTC - tCInvoiceWHT.CInvoiceWHTAmtCreditTC)
                        vdToPostCreditAmountLC = vdToPostCreditAmountLC + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitLC - tCInvoiceWHT.CInvoiceWHTAmtCreditLC)
                        vdToPostCreditAmountCC = vdToPostCreditAmountCC + absolute(tCInvoiceWHT.CInvoiceWHTAmtDebitCC - tCInvoiceWHT.CInvoiceWHTAmtCreditCC).
            
            /* reverse the posting */
            if tCInvoiceWHT.CInvoiceWHTAmtDebitTC <> 0 and 
               tCInvoiceWHT.CInvoiceWHTAmtDebitTC <> ? and
               idDebitTC                           = 0 and
               idCreditTC                         <> 0
            then assign vdToPostCreditAmountTC = vdToPostDebitAmountTC
                        vdToPostDebitAmountTC  = 0.

            if tCInvoiceWHT.CInvoiceWHTAmtCreditTC <> 0 and 
               tCInvoiceWHT.CInvoiceWHTAmtCreditTC <> ? and
               idCreditTC                           = 0 and
               idDebitTC                           <> 0
            then assign vdToPostDebitAmountTC  = vdToPostCreditAmountTC
                        vdToPostCreditAmountTC = 0.

            /* =========================================================================================== */
            /* Create a PostingLine when we have reached the last CInvoiceWHT with the same GLCode         */
            /* =========================================================================================== */
            if last-of(tCInvoiceWHT.tcGLCode)
            then do : 
                /* =========================================================================================== */
                /* As we have just summmed the WHTAmounts of the CInvoiceWHT-records up to this point, we now  */
                /* have to pro-rate this sum against the MovementAmount and the total WHTAmount of the invoice */
                /* Note we only use the TC-amounts to get the rate-factor                                      */
                /* =========================================================================================== */
                assign vdToPostDebitAmountTC  = <M-66 RoundAmount
                                                   (input  vdToPostDebitAmountTC * vdProRateFactorTC (idUnroundedAmount), 
                                                    input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>
                       vdToPostCreditAmountTC = <M-88 RoundAmount
                                                   (input  vdToPostCreditAmountTC * vdProRateFactorTC (idUnroundedAmount), 
                                                    input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>
                       vdToPostDebitAmountLC  = <M-34 RoundAmount
                                                   (input  vdToPostDebitAmountLC * vdProRateFactorLC (idUnroundedAmount), 
                                                    input  viCompanyLCId (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>
                       vdToPostCreditAmountLC = <M-65 RoundAmount
                                                   (input  vdToPostCreditAmountLC * vdProRateFactorLC (idUnroundedAmount), 
                                                    input  viCompanyLCId (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>
                       vdToPostDebitAmountCC  = <M-71 RoundAmount
                                                   (input  vdToPostDebitAmountCC * vdProRateFactorCC (idUnroundedAmount), 
                                                    input  viCompanyCCId (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>
                       vdToPostCreditAmountCC = <M-33 RoundAmount
                                                   (input  vdToPostCreditAmountCC * vdProRateFactorCC (idUnroundedAmount), 
                                                    input  viCompanyCCId (iiCurrencyID), 
                                                    input  ? (icCurrencyCode)) in BCInvoice>.

                if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} or
                   tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                then assign vdToPostDebitAmountTC  = vdToPostDebitAmountTC * -1
                            vdToPostCreditAmountTC = vdToPostCreditAmountTC * -1
                            vdToPostDebitAmountLC  = vdToPostDebitAmountLC  * -1
                            vdToPostCreditAmountLC = vdToPostCreditAmountLC * -1
                            vdToPostDebitAmountCC  = vdToPostDebitAmountCC  * -1 
                            vdToPostCreditAmountCC = vdToPostCreditAmountCC * -1.
                                                                 
                
                /* ================================================== */ 
                /* First get the PostingVatCode for the Non-Tax-code  */
                /* ================================================== */
                <Q-27 run MfgTaxCodeByTaxType (all) (Read) (NoCache)
                   (input tCInvoiceWHT.tcDomainCode, (DomainCode)
                    input {&VATTAXTYPE-NONTAX}, (TaxType)
                    output dataset tqMfgTaxCodeByTaxType) in BMfgTaxCode>
                find first tqMfgTaxCodeByTaxType where
                           tqMfgTaxCodeByTaxType.tctx2_domain   = tCInvoiceWHT.tcDomainCode and
                           tqMfgTaxCodeByTaxType.tctx2_tax_type = {&VATTAXTYPE-NONTAX}
                           no-error.
                if not available tqMfgTaxCodeByTaxType
                then do :
                    assign oiReturnStatus = -1
                           vcMessage      = trim(substitute(#T-9'Unable to find the non-taxable tax-type for domain &1.':255(615327500)T-9#,tCInvoiceWHT.tcDomainCode)).
                    <M-31 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-140942':U (icFcMsgNumber), 
                        input  '' (icFcExplanation), 
                        input  '' (icFcIdentification), 
                        input  '' (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                    leave WHTPROCESSINGBLOCK.        
                end. /* if not available tqMfgTaxCodeByTaxType */
                
                /* ============================================================================================================================================= */
                /* If tMovement.ttPaymentDate is not filled then set it to now as this value is passed as input for the TaxPointDate to method AddPostingLineTax */
                /* ============================================================================================================================================= */
                If tMovement.ttPaymentDate= ? 
                then assign tMovement.ttPaymentDate = today.
                
                /* ============================================ */ 
                /* Set the rates based upon the posted amounts  */
                /* Create a Tax posting line on the WHT-account */
                /* ============================================ */
                assign vdRateTCLC  = if tMovement.tdRateTCLC <> 0 and 
                                        tMovement.tdRateTCLC <> ?
                                     then tMovement.tdRateTCLC
                                     else absolute(vdToPostDebitAmountLC - vdToPostCreditAmountLC) / 
                                          absolute(vdToPostDebitAmountTC - vdToPostCreditAmountTC)
                       vdScaleTCLC = if tMovement.tdScaleTCLC <> 0 and 
                                        tMovement.tdScaleTCLC <> ?
                                     then tMovement.tdScaleTCLC
                                     else 1
                       vdRateTCCC  = if tMovement.tdRateTCCC <> 0 and
                                        tMovement.tdRateTCCC <> ?
                                     then tMovement.tdRateTCCC
                                     else absolute(vdToPostDebitAmountCC - vdToPostCreditAmountCC) / 
                                          absolute(vdToPostDebitAmountTC - vdToPostCreditAmountTC)
                       vdScaleTCCC = if tMovement.tdScaleTCCC <> 0 and 
                                        tMovement.tdScaleTCCC <> ?
                                     then tMovement.tdScaleTCCC 
                                     else 1.
                <M-76 run AddPostingLineTax
                   (input  tMovement.tcPostingRowId (icPostingTcRowid), 
                    input  tMovement.ttPaymentDate (itTaxPointDate), 
                    input  (if length(tMovement.tcPostingText,'CHARACTER':U) = 0 then tCInvoice.CInvoiceCIText else tMovement.tcPostingText) (icPostingLineText), 
                    input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
                    input  {&VATINOUT-INPUT} (icPostingVatInOut), 
                    input  tqMfgTaxCodeByTaxType.tctx2_tax_code (icPostingVatCode), 
                    input  tqMfgTaxCodeByTaxType.tctx2_domain (icPostingVatDomain), 
                    input  vdRateTCLC (idExchangeRate), 
                    input  vdScaleTCLC (idExchangeRateScale), 
                    input  {&EXCHANGERATETYPE-VAT} (icExchangeRateType), 
                    input  vdRateTCCC (idCCExchangeRate), 
                    input  vdScaleTCCC (idCCExchangeRateScale), 
                    input  {&VATTRANSACTIONTYPE-PURCHASE} (icTransactionType), 
                    input  {&VATTAXTRANSACTIONTYPE-APVOUCHER} (icTaxTransactionType), 
                    input  0 (idBaseDebitTC), 
                    input  0 (idBaseCreditTC), 
                    input  0 (idBaseDebitLC), 
                    input  0 (idBaseCreditLC), 
                    input  0 (idBaseDebitCC), 
                    input  0 (idBaseCreditCC), 
                    input  vdToPostDebitAmountTC (idVatDebitTC), 
                    input  vdToPostCreditAmountTC (idVatCreditTC), 
                    input  vdToPostDebitAmountLC (idVatDebitLC), 
                    input  vdToPostCreditAmountLC (idVatCreditLC), 
                    input  vdToPostDebitAmountCC (idVatDebitCC), 
                    input  vdToPostCreditAmountCC (idVatCreditCC), 
                    input  tCInvoiceWHT.tcGLCode (icNormalTaxGLCode), 
                    input  tCInvoiceWHT.tcDivisionCode (icNormalTaxDivisionCode), 
                    input  false (ilNormalTaxIsAlreadyAbsRet), 
                    input  ? (icAbsRetTaxGLCode), 
                    input  ? (icAbsRetTaxDivisionCode), 
                    input  ? (icFromTaxZone), 
                    input  ? (icToTaxZone), 
                    input  ? (icTaxEnvrionment), 
                    input  false (ilIsTaxable), 
                    input  ? (icTaxClass), 
                    input  ? (icTaxUsage), 
                    input  ? (icTaxType), 
                    input  false (ilTaxIsReverseChargeDomestic), 
                    input  ? (iiTaxAddressID), 
                    input  ? (icTaxIDFeder), 
                    input  ? (icTaxIDState), 
                    input  ? (icTaxIDMisc1), 
                    input  ? (icTaxIDMisc2), 
                    input  ? (icTaxIDMisc3), 
                    input  ? (iiOwnAddressID), 
                    input  ? (icOwnTaxIDFeder), 
                    input  ? (icOwnTaxIDState), 
                    input  ? (icOwnTaxIDMisc1), 
                    input  ? (icOwnTaxIDMisc2), 
                    input  ? (icOwnTaxIDMisc3), 
                    input  ? (iiOwnTaxIDDeclarat), 
                    input  false (ilIsSuspDel), 
                    output viWHTPostingLineID (oiNormalTaxPostingLineID), 
                    output vcDummy (ocNormalTaxPostingLineTcRowid), 
                    output viDummy (oiAbsRetTaxPostingLineID), 
                    output vcDummy (ocAbsRetTaxPostingLineTcRowid), 
                    output viDummy (oiNormalTaxPostingVatID), 
                    input  ? (ilLinkedCrCyDaemonReqExists), 
                    input  false (ilCalledFromMoveSuspDelTax), 
                    input  ? (itInvoiceExchangeRateDate), 
                    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>                
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper <  0 then leave WHTPROCESSINGBLOCK.
                
                /* ======================================================================================================================= */
                /* Update the calculated field, that is used to roll back the wht posting lines when necessary - see StopExternalInstances */
                /* ======================================================================================================================= */
                Find first tCInvoiceMovement where
                           tCInvoiceMovement.tc_ParentRowid = tCInvoice.tc_Rowid and
                           tCInvoiceMovement.tc_Status      = "N":U
                           no-error.
                if available tCInvoiceMovement
                then assign tCInvoiceMovement.tcWHTPostingLineIDs = if tCInvoiceMovement.tcWHTPostingLineIDs = "":U 
                                                                    then string(viWHTPostingLineID) 
                                                                    else tCInvoiceMovement.tcWHTPostingLineIDs + ",":U + string(viWHTPostingLineID).
                
            end. /* end last-of(tCInvoiceWHT.tcGLCode) */
            
        end. /* end for each tCInvoiceWHT */
        
    END. /*  WHTPROCESSINGBLOCK */
    
    /* ================== */
    /* Exception handling */
    /* ================== */
    assign oiReturnStatus = viLocalReturnStatus.