project QadFinancials > class BAPMatching > method CalculatePostAPMLn

Description

CalculatePostAPMLn: submethod of CalculatePost that is in the same segment.
This was done as the complete method became too big.


Parameters


ilUseDelTaxinputlogicalTrue in case delayed taxes are applicable for the domain
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.CalculatePost


program code (program7/bapmatching.p)

/* ================================================================================ */
/* CalculatePostAPMLn: submethod of CalculatePost that is in the same segment.      */
/* This was done as the complete method became too big.                             */
/* Method that will be executed each time instances are loaded                      */
/* Note that this method is in Segment-7 as one of the methods that are called      */
/* from here (not needed for record availability, just for performant segmentation  */
/* Repetitively called queries are started / stopped in the parent method           */
/* ================================================================================ */

/* ======================== */
/* Set default ReturnStatus */
/* Start processing block   */
/* ======================== */
assign oiReturnStatus = -98.
CALCULATEAPMLNBLOCK: DO :

    /* ===================================================================================================== */
    /* Read once the pending-voucher details and related tables for all matching-lines of a single matching. */
    /* This is much more performing than executing the query for every matching-line                         */
    /* After this call, temp-table tqAPMatchingLnForPvodDetails will be filled                               */
    /* Note: the result-set is indexed on APMatchingLine_ID                                                  */
    /* ===================================================================================================== */
    <M-87 run CalculatePostAPMLnQuery  (output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
    if viFcReturnSuper <> 0 
    then assign viBlockReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 
    then Leave CALCULATEAPMLNBLOCK.
    
    /* ================================================================= */
    /* Go through all APMatchingLines of the tAPMatching                 */
    /* Catch the corresponding t_i record to check for changes done here */
    /* ================================================================= */
    for each tAPMatchingLn where
             tAPMatchingLn.tc_ParentRowid = tAPMatching.tc_Rowid,
        each t_iAPMatchingLn where
             t_iAPMatchingLn.tc_Rowid = tAPMatchingLn.tc_Rowid :
        
        /* ============================================================= */
        /* Skip records that don't have the prim-key of the PVOD filled  */
        /* ============================================================= */
        if tAPMatchingLn.PvoDomain        = "":U  or
           tAPMatchingLn.PvoDomain        = ?     or 
           tAPMatchingLn.PvoID            = 0     or 
           tAPMatchingLn.PvoID            = ?     or 
           tAPMatchingLn.PvodLineID       = 0     or
           tAPMatchingLn.PvodLineID       = ? 
        then next.
        
        /* ===================================================================================================================== */
        /* Run once a query that returns all kind of information on the Pvod and related details                                 */
        /* If CompanyID = 0 then take all companies, not only current one                                                        */
        /* Note: the result-set of the query can contain multiple records for a single pvod_det as taxes are joined in the query */
        /*       but as we do not need the tax-information here, we do not have to go through all records in the result-set      */ 
        /* ===================================================================================================================== */
        find first tqAPMatchingLnForPvodDetails where 
                   tqAPMatchingLnForPvodDetails.tiAPMatchingLn_ID = tAPMatchingLn.APMatchingLn_ID
                   no-lock no-error.
                   /* first option is needed as they can be multiple ones / where clause can be omitted as the parameters are always filled */
        if not available tqAPMatchingLnForPvodDetails
        then do:
            assign viBlockReturnStatus  = -1
                   vcMsgAPMatching      = trim(substitute(#T-70'The pending invoice on the AP matching cannot be found based on its domain (&1), pending invoice ID (&2), and pending invoice line ID (&3).':255(999890410)T-70#,string(tAPMatchingLn.PvoDomain),string(tAPMatchingLn.PvoID),string(tAPMatchingLn.PvodLineID))) + chr(10) + 
                                          trim(substitute(#T-71'The system cannot default all pending invoice related fields.':241(999890411)T-71#))  + chr(10) + 
                                          trim(substitute(#T-72'Entity ID: &1.':213(999890412)T-72#,string(tAPMatching.Company_ID))).
            <M-52 run SetMessage
               (input  vcMsgAPMatching (icMessage), 
                input  '':U (icArguments), 
                input  'tAPMatchingLn.PvodLineID':U (icFieldName), 
                input  string(tAPMatchingLn.PvodLineID) (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tAPMatchingLn.tc_Rowid (icRowid), 
                input  'QadFin-9451':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            Leave CALCULATEAPMLNBLOCK.
        end. /* if not available tqAPMatchingLnForPvodDetails */
        
        /* ================================================================ */
        /* Don't re-assign the costing-method fields that were redundantly  */ 
        /* stored here as they will not be changed in between               */
        /* Involved fields:                                                 */
        /* - tAPMatchingLn.APMatchingLnPvodCostMtdGL                        */
        /* - tAPMatchingLn.APMatchingLnPvodCostMtdGL                        */
        /* Don't re-assign the PL-invoice-number that was redundantly       */
        /* stored here as they will not be changed in between               */
        /* Involved fields:                                                 */
        /* - tAPMatchingLn.APMatchingLnPvodPLiKeyId                         */
        /* - tAPMatchingLn.APMatchingLnPvodPLidLine                         */
        /* Don't re-assign the tax-fields that were redundantly             */
        /* stored here as they will not be changed in between               */
        /* ================================================================ */
        
        /* ================================================================================================ */
        /* Update all redundantly stored fields that might have been changed since the APM was last saved   */ 
        /* ================================================================================================ */
        assign tAPMatchingLn.APMatchingLnPvodInvoicedTC = tqAPMatchingLnForPvodDetails.tdpvo_vouchered_amt
               tAPMatchingLn.APMatchingLnPvodAccruedTC  = tqAPMatchingLnForPvodDetails.tdpvo_accrued_amt
               tAPMatchingLn.APMatchingLnPvodOpenQty    = (if tqAPMatchingLnForPvodDetails.tcpvo_lc_charge <> "":U and 
                                                              tqAPMatchingLnForPvodDetails.tcpvo_lc_charge <> ?
                                                           then 1 
                                                           else tqAPMatchingLnForPvodDetails.tdpvod_trans_qty - tqAPMatchingLnForPvodDetails.tdpvod_vouchered_qty)
               tAPMatchingLn.APMatchingLnPvodOpenAmtTC  = <M-53 RoundAmount
                                                             (input  tAPMatchingLn.APMatchingLnPvodOpenQty * tAPMatchingLn.APMatchingLnPvodUnitPrice (idUnroundedAmount), 
                                                              input  ? (iiCurrencyID), 
                                                              input  tAPMatchingLn.APMatchingLnPvodCurrency (icCurrencyCode)) in BAPMatching>.
               
        /* ================================================================================================ */
        /* Assign values for some calculated fields containing pvod values                                  */
        /* ================================================================================================ */
        assign tAPMatchingLn.ttAPMatchingLnPvodEffDate = tqAPMatchingLnForPvodDetails.ttpvod_eff_date.

        /* ================================================================================================ */
        /* Calculate total TAX values stored in calcaulated fields in t_i tables                            */ 
        /* ================================================================================================ */
        assign t_iAPMatchingLn.tdAPMatchingLnARRecTaxTC  = 0
               t_iAPMatchingLn.tdAPMatchingLnARNRecTaxTC = 0
               t_iAPMatchingLn.tdAPMatchingLnAIRecTaxTC  = 0
               t_iAPMatchingLn.tdAPMatchingLnAINRecTaxTC = 0.

        for each t_iAPMatchingLnTax where
                 t_iAPMatchingLnTax.tc_ParentRowid  = t_iAPMatchingLn.tc_Rowid:
            /* ============================================ */
            /* Assign the tax-amount sums on the line-level */
            /* ============================================ */
            assign t_iAPMatchingLn.tdAPMatchingLnARRecTaxTC  = t_iAPMatchingLn.tdAPMatchingLnARRecTaxTC
                                                             + t_iAPMatchingLnTax.APMatchingLnTaxARRecTaxTC
                   t_iAPMatchingLn.tdAPMatchingLnARNRecTaxTC = t_iAPMatchingLn.tdAPMatchingLnARNRecTaxTC
                                                             + t_iAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC
                   t_iAPMatchingLn.tdAPMatchingLnAIRecTaxTC  = t_iAPMatchingLn.tdAPMatchingLnAIRecTaxTC
                                                             + t_iAPMatchingLnTax.APMatchingLnTaxAIRecTaxTC
                   t_iAPMatchingLn.tdAPMatchingLnAINRecTaxTC = t_iAPMatchingLn.tdAPMatchingLnAINRecTaxTC
                                                             + t_iAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC.
        end. /* for each t_iAPMatchingLnTax where */
        
        
        /* ================================================================ */
        /* Re-assign the some redundantly stored fields that are based on   */
        /* some other fields that were re-assigned in the previous block    */
        /* ================================================================ */
        assign /* Fill-in the receipt-amount-TC */
               tAPMatchingLn.APMatchingLnReceiptAmtTC = 
                    <M-54 RoundAmount
                       (input  tAPMatchingLn.APMatchingLnPvodUnitPrice * (if tAPMatchingLn.APMatchingLnIsPvodFinished = true then tAPMatchingLn.APMatchingLnPvodOpenQty else tAPMatchingLn.APMatchingLnMatchQty) (idUnroundedAmount), 
                        input  ? (iiCurrencyID), 
                        input  tAPMatching.tcCurrencyCode (icCurrencyCode)) in business>
               /* Fill-in the receipt-amount-LC */
               tAPMatchingLn.APMatchingLnReceiptAmtLC = 
                    <M-55 RoundAmount
                       (input  tAPMatchingLn.APMatchingLnReceiptAmtTC * tAPMatchingLn.APMatchingLnPvodExchRate2 / tAPMatchingLn.APMatchingLnPvodExchRate (idUnroundedAmount), 
                        input  viCompanyLCId (iiCurrencyID), 
                        input  ? (icCurrencyCode)) in business>
               /* Fill-in the Var-Usage-TC */
               tAPMatchingLn.APMatchingLnVarUsageTC = 
                    (if tAPMatchingLn.APMatchingLnIsPvodFinished <> true
                     then 0 
                     else <M-56 RoundAmount
                             (input  (tAPMatchingLn.APMatchingLnMatchQty - tAPMatchingLn.APMatchingLnPvodOpenQty) * tAPMatchingLn.APMatchingLnPvodUnitPrice (idUnroundedAmount), 
                              input  ? (iiCurrencyID), 
                              input  tAPMatching.tcCurrencyCode (icCurrencyCode)) in business>)
               /* Fill-in the Var-Usage-LC */
               tAPMatchingLn.APMatchingLnVarUsageLC = 
                    (if tAPMatchingLn.APMatchingLnIsPvodFinished <> true
                     then 0 
                     else <M-57 RoundAmount
                             (input  tAPMatchingLn.APMatchingLnVarUsageTC * tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCinvoiceRateScale (idUnroundedAmount), 
                              input  viCompanyLCId (iiCurrencyID), 
                              input  ? (icCurrencyCode)) in business>)
               /* Fill-in the ExchangeRate-LC */
               tAPMatchingLn.APMatchingLnVarExchRateLC = 
                    (if tAPMatchingLn.APMatchingLnPvodExchRate2 / tAPMatchingLn.APMatchingLnPvodExchRate = tAPMatching.tdCInvoiceExchangeRate * tAPMatching.tdCinvoiceRateScale
                     then 0
                     else tAPMatchingLn.APMatchingLnMatchAmtLC
                          - tAPMatchingLn.APMatchingLnReceiptAmtLC
                          - tAPMatchingLn.APMatchingLnVarUsageLC 
                          - tAPMatchingLn.APMatchingLnVarRateLC).
        
        /* ==================================================================================== */
        /* Compose the Amount and the Qty of this Pvod that is located on other APMatchingLines */
        /* linked to this Pvod and with APMatching.APMatchingStatus = Initial                   */
        /* The field that are assigned here, exist just for informational reasons (no BL-logic) */
        /* ==================================================================================== */
        <Q-58 run APMatchingLnByStatusPvod (all) (Read) (NoCache)
           (input ?, (CompanyId)
            input tAPMatchingLn.PvoDomain, (PvoDomain)
            input tAPMatchingLn.PvoID, (PvoID)
            input tAPMatchingLn.PvodLineID, (PvodLineID)
            input {&APMATCHINGSTATUS-INITIAL}, (APMatchingStatus)
            input tAPMatchingLn.tc_Rowid, (SkipAPMatchingLnRowid)
            output dataset tqAPMatchingLnByStatusPvod) in BAPMatching >  
        for each tqAPMatchingLnByStatusPvod no-lock :
            assign tAPMatchingLn.tdAPMatchingLnPendingMatchQty   = tAPMatchingLn.tdAPMatchingLnPendingMatchQty   + tqAPMatchingLnByStatusPvod.tdAPMatchingLnMatchQty
                   tAPMatchingLn.tdAPMatchingLnPendingMatchAmtTC = tAPMatchingLn.tdAPMatchingLnPendingMatchAmtTC + tqAPMatchingLnByStatusPvod.tdAPMatchingLnMatchAmtTC.
        end. /* for each tqAPMatchingLnByStatusPvod */
        
        /* ===================================================================================================== */
        /* Simply mark the lines as being changed in case the newly assigned values differ from the initial ones */
        /* ===================================================================================================== */
        buffer-compare tAPMatchingLn  
                       to t_iAPMatchingLn
                       save vlTiEqualsT.
        if vlTiEqualsT = false
        then assign tAPMatchingLn.tc_Status = "C":U.
        
        /* ============================================================================================================================ */
        /* No need to recalculate the APMatchingLnTax records as non of the APMatchingLn-fields that are the base for them are changed: */
        /* APMatchingLnMatchAmtTC, APMatchingLnIsTaxable, APMatchingLnTaxEnv, APMatchingLnTaxClass and APMatchingLnTaxUsage             */
        /* However, we need to store the tax-details of the pvod in the t_iAPMatchingLnTax-table so we can check for differences when   */
        /* this matching gets saved. The t_iAPMatchingLnTax records are created with a tc_parentrowid that points to the pvod           */
        /* ============================================================================================================================ */
        for each tqAPMatchingLnForPvodDetails where 
                 tqAPMatchingLnForPvodDetails.tiAPMatchingLn_ID = tAPMatchingLn.APMatchingLn_ID
                 no-lock :
            if not can-find (first t_iAPMatchingLnTax where 
                                   t_iAPMatchingLnTax.tc_ParentRowid = trim(tqAPMatchingLnForPvodDetails.tcpvo_domain) + "-":U + trim(string(tqAPMatchingLnForPvodDetails.tipvo_id)) + "-":U + trim(string(tqAPMatchingLnForPvodDetails.tipvod_id_line)) and 
                                   t_iAPMatchingLnTax.tcVatCode      = tqAPMatchingLnForPvodDetails.tctx2_tax_code)
            then do :
                create t_iAPMatchingLnTax. 
                assign t_iAPMatchingLnTax.tcDomainCode               = tqAPMatchingLnForPvodDetails.tctx2_domain
                       t_iAPMatchingLnTax.tcVatCode                  = tqAPMatchingLnForPvodDetails.tctx2_tax_code
                       t_iAPMatchingLnTax.tcVatInOut                 = {&VATINOUT-INPUT}
                       t_iAPMatchingLnTax.APMatchingLnTaxTaxType     = tqAPMatchingLnForPvodDetails.tctx2d_tax_type
                       t_iAPMatchingLnTax.APMatchingLnTaxTaxTrType   = tqAPMatchingLnForPvodDetails.tctx2d_tr_type
                       t_iAPMatchingLnTax.APMatchingLnTaxBaseTC      = tqAPMatchingLnForPvodDetails.tdtx2d_tottax 
                       t_iAPMatchingLnTax.APMatchingLnTaxBaseLC      = tqAPMatchingLnForPvodDetails.tdtx2d_taxable_amt 
                       t_iAPMatchingLnTax.APMatchingLnTaxTaxTC       = tqAPMatchingLnForPvodDetails.tdtx2d_cur_tax_amt 
                       t_iAPMatchingLnTax.APMatchingLnTaxTaxLC       = tqAPMatchingLnForPvodDetails.tdtx2d_tax_amt 
                       t_iAPMatchingLnTax.APMatchingLnTaxIsAccrRcp   = (tqAPMatchingLnForPvodDetails.tltx2_rcpt_tax_point = true or tqAPMatchingLnForPvodDetails.tltx2_usage_tax_point = true)
                       t_iAPMatchingLnTax.APMatchingLnTaxIsDiscAtInv = (tqAPMatchingLnForPvodDetails.tltx2_inv_disc = true)
                       t_iAPMatchingLnTax.APMatchingLnTaxIsAbsRet    = (if tqAPMatchingLnForPvodDetails.tdtx2d_abs_ret_amt <> 0 and tqAPMatchingLnForPvodDetails.tdtx2d_abs_ret_amt <> ? then true else false)
                       t_iAPMatchingLnTax.APMatchingLnTaxIsUpdAllow  = tqAPMatchingLnForPvodDetails.tltx2_update_tax
                       t_iAPMatchingLnTax.APMatchingLnTaxTaxPct      = tqAPMatchingLnForPvodDetails.tdtx2_tax_pct
                       t_iAPMatchingLnTax.APMatchingLnTaxRecovTaxPct = tqAPMatchingLnForPvodDetails.tdtx2_pct_recv
                       t_iAPMatchingLnTax.APMatchingLnTaxARRecTaxTC  = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAccrRcp = true  then tqAPMatchingLnForPvodDetails.tdtx2d_cur_recov_amt else 0) 
                       t_iAPMatchingLnTax.APMatchingLnTaxARNRecTaxTC = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAccrRcp = true  then tqAPMatchingLnForPvodDetails.tdtx2d_cur_tax_amt - tqAPMatchingLnForPvodDetails.tdtx2d_cur_recov_amt else 0)
                       t_iAPMatchingLnTax.APMatchingLnTaxAIRecTaxTC  = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAccrRcp = false then tqAPMatchingLnForPvodDetails.tdtx2d_cur_recov_amt else 0) 
                       t_iAPMatchingLnTax.APMatchingLnTaxAINRecTaxTC = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAccrRcp = false then tqAPMatchingLnForPvodDetails.tdtx2d_cur_tax_amt - tqAPMatchingLnForPvodDetails.tdtx2d_cur_recov_amt else 0)
                       t_iAPMatchingLnTax.APMatchingLnTaxIsRevCharge = tqAPMatchingLnForPvodDetails.tltx2_reverse_charge
                       t_iAPMatchingLnTax.APMatchingLnTaxIsSuspDel   = if ilUseDelTax then tqAPMatchingLnForPvodDetails.tltx2_stx_dltx_use else false
                       t_iAPMatchingLnTax.tcNormalTaxGLCode          = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                          tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                       then tqAPMatchingLnForPvodDetails.tctx2_ap_cn_acct
                                                                       else tqAPMatchingLnForPvodDetails.tctx2_ap_acct
                       t_iAPMatchingLnTax.tcNormalTaxDivisionCode    = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                          tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                       then tqAPMatchingLnForPvodDetails.tctx2_ap_cn_sub
                                                                       else tqAPMatchingLnForPvodDetails.tctx2_ap_sub
                       t_iAPMatchingLnTax.tcAbsRetTaxGLCode          = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAbsRet = true 
                                                                        then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                             then tqAPMatchingLnForPvodDetails.tctx2_apr_cn_acct
                                                                             else tqAPMatchingLnForPvodDetails.tctx2_apr_acct
                                                                        else "":U)
                       t_iAPMatchingLnTax.tcAbsRetTaxDivisionCode    = (if t_iAPMatchingLnTax.APMatchingLnTaxIsAbsRet = true 
                                                                        then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                             then tqAPMatchingLnForPvodDetails.tctx2_apr_cn_sub
                                                                             else tqAPMatchingLnForPvodDetails.tctx2_apr_sub  
                                                                        else "":U)
                       t_iAPMatchingLnTax.tcSuspDelTaxGLCode         = (if t_iAPMatchingLnTax.APMatchingLnTaxIsSuspDel = true 
                                                                        then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                             then tqAPMatchingLnForPvodDetails.tctx2_dltx_cn_acct 
                                                                             else tqAPMatchingLnForPvodDetails.tctx2_dltx_inv_acct
                                                                        else "":U)
                       t_iAPMatchingLnTax.tcSuspDelTaxDivisionCode   = (if t_iAPMatchingLnTax.APMatchingLnTaxIsSuspDel = true 
                                                                        then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                             then tqAPMatchingLnForPvodDetails.tctx2_dltx_cn_sub
                                                                             else tqAPMatchingLnForPvodDetails.tctx2_dltx_inv_sub
                                                                        else "":U)
                       t_iAPMatchingLnTax.tc_ParentRowid             = trim(tqAPMatchingLnForPvodDetails.tcpvo_domain) + "-":U + trim(string(tqAPMatchingLnForPvodDetails.tipvo_id)) + "-":U + trim(string(tqAPMatchingLnForPvodDetails.tipvod_id_line))
                       t_iAPMatchingLnTax.tc_Rowid                   = string(viRowidPendingVoucherTaxAPM)
                       viRowidPendingVoucherTaxAPM                   = viRowidPendingVoucherTaxAPM + 1.
            end. /* if not can-find (first t_iAPMatchingLnTax where */
        end. /* for each tqAPMatchingLnForPvodDetails where */
        
    end. /* for each tAPMatchingLn where */
    
END. /* CALCULATEAPMLNBLOCK */

/* ============================= */
/* Check for errors in the block */
/* ============================= */
if viBlockReturnStatus <> 0 
then assign oiReturnStatus = viBlockReturnStatus.
if viBlockReturnStatus < 0
then Return.

/* ======================== */
/* Set ReturnStatus = OK    */
/* ======================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.