project QadFinancials > class BAPMatching > method GetPendingVouchersNoLogChargesTaxFiscal

Description

Submethod of GetPendingVouchersNoLogCharges


Parameters


ilAutoSelectinputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.GetPendingVouchersNoLogCharges


program code (program7/bapmatching.p)

/* ==================================================================================== *
 * Update taxes to get them from Fiscal document                                        *
 * ==================================================================================== */
for each tPendingVoucherAPM where
         num-entries(tPendingVoucherAPM.APMatchingLnPvodExtRef,',':U) = 2:

    assign vdMultiplier = 1.

    /* ================================================================================= *
     * Get pending voucher which was used for creation of the Matching Line              *
     * ================================================================================= */
    find first tqPendingVoucherForMatchingNoLC where
               tqPendingVoucherForMatchingNoLC.tcpvo_domain   = tPendingVoucherAPM.PvoDomain and
               tqPendingVoucherForMatchingNoLC.tipvod_id      = tPendingVoucherAPM.PvoID     and
               tqPendingVoucherForMatchingNoLC.tipvod_id_line = tPendingVoucherAPM.PvodLineID
               no-error.
    if not available tqPendingVoucherForMatchingNoLC
    then next.

    /* ================================================================================= *
     * Check if the supplier is a WHT supplier                                           *
     * ================================================================================= */
    <Q-57 run CreditorByCodeForWHT (all) (Read) (Cache)
       (input tAPMatching.Company_ID, (CompanyId)
        input tqPendingVoucherForMatchingNoLC.tcpvo_supplier, (CreditorCode)
        input true, (CreditorIsWHT)
        output dataset tqCreditorByCodeForWHT) in BCreditor>
    assign vlCreditorIsWht = can-find (tqCreditorByCodeForWHT where 
                                       tqCreditorByCodeForWHT.tcCreditorCode  = tqPendingVoucherForMatchingNoLC.tcpvo_supplier and 
                                       tqCreditorByCodeForWHT.tlCreditorIsWHT = true).
        
    /* ================================================================================= *
     * Make list of original tax lines                                                   *
     * ================================================================================= */
    assign vlFoundtqTaxDetailByKey    = false
           vlFoundtqWhtTaxDetailByKey = false.
    
    assign vcOriginalTaxLines = "":U.
    for each tPendingVoucherTaxAPM where
             tPendingVoucherTaxAPM.tc_ParentRowid = tPendingVoucherAPM.tc_Rowid:
        assign vcOriginalTaxLines = vcOriginalTaxLines
                                  + (if vcOriginalTaxLines = "":U
                                     then "":U
                                     else ",":U)
                                  + tPendingVoucherTaxAPM.tc_Rowid.
    end.
    
    assign vcOriginalWhtTaxLines = "":U.
    for each tPendingVoucherWHTAPM where
             tPendingVoucherWHTAPM.tc_ParentRowid = tPendingVoucherAPM.tc_Rowid:
        assign vcOriginalWhtTaxLines = vcOriginalWhtTaxLines
                                     + (if vcOriginalWhtTaxLines = "":U
                                        then "":U
                                        else ",":U)
                                     + tPendingVoucherWHTAPM.tc_Rowid.
    end.
    
    /* ================================================================================= *
     * Retrieve all tax details from the Fiscal receipt and store them on AP matching    *
     * ================================================================================= */
    assign vcLegalDocumentNbr   = entry(1,tqPendingVoucherForMatchingNoLC.tcpvo_external_ref)
           vcLegalDocumentShip  = tqPendingVoucherForMatchingNoLC.tcpvo_supplier
           vcDateFormat         = session:date-format
           session:date-format  = 'ymd'.
    assign vtEffectiveDate      = date(entry(2,tqPendingVoucherForMatchingNoLC.tcpvo_external_ref)).
    assign session:date-format  = vcDateformat.

    if not can-find(first tLgddDetMatForAPMBAPM where
                          tLgddDetMatForAPMBAPM.tclgd_domain      = vcDomainCode                                   and
                          tLgddDetMatForAPMBAPM.tclgd_nbr         = vcLegalDocumentNbr                             and
                          tLgddDetMatForAPMBAPM.tcSupplier        = tqPendingVoucherForMatchingNoLC.tcpvo_supplier and
                          tLgddDetMatForAPMBAPM.ttlgd_effdate     = vtEffectiveDate)
    then do:        
        if viBMFGLegalDocumentID = 0 or
           viBMFGLegalDocumentID = ?
        then do:
            <I-31 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "no"
                 &CLASS                = "BMFGLegalDocument"}>
        end.
        else if not valid-handle(vhBMFGLegalDocumentInst)
        then do:
            <I-14 {bFcOpenInstance
                 &CLASS           = "BMFGLegalDocument"}>
        end.

        <M-56 run GetLegalDocumentDetForAPMatching
           (input  vcDomainCode (icLgdDomain), 
            input  vcLegalDocumentNbr (icLgdNbr), 
            input  vcLegalDocumentShip (icLgdSuppl), 
            input  vtEffectiveDate (itLgdEffDate), 
            input  ? (icInvSuppl), 
            input  ? (iiLgddLine), 
            input  ? (icLgddDocType), 
            input  ? (icLgddOrder), 
            input  ? (iiLgddOrderLine), 
            output tLgddDetMatForAPMBAPM (tLgddDetMatForAPM), 
            output tLgddDetLCForAPMBAPM (tLgddDetLCForAPM), 
            output tLgddDetLCDistForAPMBAPM (tLgddDetLCDistForAPM), 
            output tLgddDetTaxForAPMBAPM (tLgddDetTaxForAPM), 
            output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus  < 0 then leave.
    end. /* if not can-find(first tLgddDetMatForAPMBAPM where */

    /* Get grouped legal document line details for the pending voucher */
    find first tLgddDetMatForAPMBAPM where
               tLgddDetMatForAPMBAPM.tclgd_domain      = vcDomainCode                                   and
               tLgddDetMatForAPMBAPM.tclgd_nbr         = vcLegalDocumentNbr                             and
               tLgddDetMatForAPMBAPM.tcSupplier        = tqPendingVoucherForMatchingNoLC.tcpvo_supplier and
               tLgddDetMatForAPMBAPM.ttlgd_effdate     = vtEffectiveDate                                and
               tLgddDetMatForAPMBAPM.tclgdd_order      = tqPendingVoucherForMatchingNoLC.tcpvo_order    and
               tLgddDetMatForAPMBAPM.tilgdd_order_line = tqPendingVoucherForMatchingNoLC.tipvod_order_line
               no-error.
               
    assign vlIsAssignTaxes = false.
    if available tLgddDetMatForAPMBAPM
    then do:
        /* If we have taxes from the legal document we will remove the taxes from the purchase order receipt */
        assign vlFoundtqTaxDetailByKey    = true
               vlFoundtqWhtTaxDetailByKey = true.

        /* ===================================================================================== *
         * We have to assigne taxes only to the pending vouchers with the same direction of flow *
         * ===================================================================================== */
        if (((tLgddDetMatForAPMBAPM.tdlgdd_qty_item >= 0 and tLgddDetMatForAPMBAPM.tlIsPOReturn = false or
              tLgddDetMatForAPMBAPM.tdlgdd_qty_item <= 0 and tLgddDetMatForAPMBAPM.tlIsPOReturn = true) and
             tPendingVoucherAPM.APMatchingLnPvodOpenQty >= 0) or
                 
           ((tLgddDetMatForAPMBAPM.tdlgdd_qty_item <= 0 and tLgddDetMatForAPMBAPM.tlIsPOReturn = false or
             tLgddDetMatForAPMBAPM.tdlgdd_qty_item >= 0 and tLgddDetMatForAPMBAPM.tlIsPOReturn = true) and
            tPendingVoucherAPM.APMatchingLnPvodOpenQty <= 0))
        then assign vlIsAssignTaxes = true.
    end.

    /* ================================================================================= *
     * When this is the next receipt of the Legal document line, remove all taxes that   *
     * where already assigned to them                                                    *
     * All taxes comming from the legal document needs to be assigned to last receiver   *
     * matching line of the legal document.                                              *
     * We can delete the taxes from previous lines only if we know they                  *
     * will be recreated on this line                                                    *
     * ================================================================================= */
    if vlIsAssignTaxes = true
    then do:
        for each tAPMatchingLn where
                 tAPMatchingLn.PvoDomain                  = tPendingVoucherAPM.PvoDomain             and
                 tAPMatchingLn.APMatchingLnPvodOrder      = tPendingVoucherAPM.APMatchingLnPvodOrder and
                 tAPMatchingLn.APMatchingLnPvodOrderLnNbr = tPendingVoucherAPM.APMatchingLnPvodOrderLnNbr:

            for each tAPMatchingLnTax where
                     tAPMatchingLnTax.tc_ParentRowid = tAPMatchingLn.tc_Rowid:
                delete tAPMatchingLnTax.
            end.
    
            for each tAPMatchingLnWHT where
                     tAPMatchingLnWHT.tc_ParentRowid = tAPMatchingLn.tc_Rowid:
                delete tAPMatchingLnWHT.
            end.
        
        end. /* for each  tAPMatchingLn where */
   
        /* ================================================================================= *
         * Go through the taxes of the Legal document line                                   *
         * ================================================================================= */
        for each tLgddDetTaxForAPMBAPM where
                 tLgddDetTaxForAPMBAPM.tc_ParentRowid = tLgddDetMatForAPMBAPM.tc_Rowid:

            /* Withholding taxes */
            if tLgddDetTaxForAPMBAPM.tctx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} and
               vlDomainIsWHT                        = true                           and
               vlCreditorIsWht                      = true
            then do:
                <M-81 run GetPendingVouchersNoLogChargesTaxFiscalWHT
                   (input  true (ilAutoSelect), 
                    input  false (ilCallGTM), 
                    output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
                if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then Return.
            end. /* end if tqPendingVoucherForMatchingNoLC.tctx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} */
            
            /* Normal taxes */
            else if tLgddDetTaxForAPMBAPM.tctx2d_tr_type <> {&VATTAXTRANSACTIONTYPE-APWHT} 
            then do:
                
                assign vdLegalDocumentExchangeRate = tLgddDetMatForAPMBAPM.tdlgd_ex_rate / tLgddDetMatForAPMBAPM.tdlgd_ex_rate2
                       vdMultiplier                = if tLgddDetMatForAPMBAPM.tlIsPOReturn = true then -1 else 1.
                
                create tPendingVoucherTaxAPM. 
                assign tPendingVoucherTaxAPM.tcDomainCode               = tLgddDetTaxForAPMBAPM.tctx2d_domain
                       tPendingVoucherTaxAPM.tcVatCode                  = tLgddDetTaxForAPMBAPM.tctx2d_tax_code
                       tPendingVoucherTaxAPM.tcVatInOut                 = {&VATINOUT-INPUT}
                       tPendingVoucherTaxAPM.APMatchingLnTaxTaxType     = tLgddDetTaxForAPMBAPM.tctx2d_tax_type
                       tPendingVoucherTaxAPM.APMatchingLnTaxTaxTrType   = tLgddDetTaxForAPMBAPM.tctx2d_tr_type
                       tPendingVoucherTaxAPM.APMatchingLnTaxBaseLC      = (if ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_taxable_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxTaxLC       = (if ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_tax_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxBaseTC      = tPendingVoucherTaxAPM.APMatchingLnTaxBaseLC
                       tPendingVoucherTaxAPM.APMatchingLnTaxTaxTC       = tPendingVoucherTaxAPM.APMatchingLnTaxTaxLC
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsAccrRcp   = (tLgddDetTaxForAPMBAPM.tltx2_rcpt_tax_point = true or tLgddDetTaxForAPMBAPM.tltx2_usage_tax_point = true)
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsDiscAtInv = (tLgddDetTaxForAPMBAPM.tltx2_inv_disc = true)
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsAbsRet    = (if tLgddDetTaxForAPMBAPM.tdtx2d_abs_ret_amt <> 0 and tLgddDetTaxForAPMBAPM.tdtx2d_abs_ret_amt <> ? then true else false)
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsUpdAllow  = tLgddDetTaxForAPMBAPM.tltx2_update_tax
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsSuspDel   = tLgddDetTaxForAPMBAPM.tltx2_stx_dltx_use
                       tPendingVoucherTaxAPM.APMatchingLnTaxTaxPct      = tLgddDetTaxForAPMBAPM.tdtx2_tax_pct
                       tPendingVoucherTaxAPM.APMatchingLnTaxRecovTaxPct = tLgddDetTaxForAPMBAPM.tdtx2_pct_recv
                       tPendingVoucherTaxAPM.APMatchingLnTaxARRecTaxTC  = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAccrRcp = true  and ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_cur_recov_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxARNRecTaxTC = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAccrRcp = true  and ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_cur_tax_amt - tLgddDetTaxForAPMBAPM.tdtx2d_cur_recov_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxAIRecTaxTC  = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAccrRcp = false and ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_cur_recov_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxAINRecTaxTC = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAccrRcp = false and ilAutoSelect = true then tLgddDetTaxForAPMBAPM.tdtx2d_cur_tax_amt - tLgddDetTaxForAPMBAPM.tdtx2d_cur_recov_amt else 0) * vdMultiplier
                       tPendingVoucherTaxAPM.APMatchingLnTaxIsRevCharge = tLgddDetTaxForAPMBAPM.tltx2_reverse_charge
                       tPendingVoucherTaxAPM.tcNormalTaxGLCode          = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                             tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                          then tLgddDetTaxForAPMBAPM.tctx2_ap_cn_acct
                                                                          else tLgddDetTaxForAPMBAPM.tctx2_ap_acct
                       tPendingVoucherTaxAPM.tcNormalTaxDivisionCode    = if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                             tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                          then tLgddDetTaxForAPMBAPM.tctx2_ap_cn_sub
                                                                          else tLgddDetTaxForAPMBAPM.tctx2_ap_sub
                       tPendingVoucherTaxAPM.tcAbsRetTaxGLCode          = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAbsRet = true 
                                                                           then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                   tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then tLgddDetTaxForAPMBAPM.tctx2_apr_cn_acct 
                                                                                else tLgddDetTaxForAPMBAPM.tctx2_apr_acct
                                                                           else "":U)
                       tPendingVoucherTaxAPM.tcAbsRetTaxDivisionCode    = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsAbsRet = true 
                                                                           then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                   tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then tLgddDetTaxForAPMBAPM.tctx2_apr_cn_sub
                                                                                else tLgddDetTaxForAPMBAPM.tctx2_apr_sub  
                                                                           else "":U)
                       tPendingVoucherTaxAPM.tcSuspDelTaxGLCode         = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsSuspDel = true 
                                                                           then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                   tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then tLgddDetTaxForAPMBAPM.tctx2_dltx_cn_acct 
                                                                                else tLgddDetTaxForAPMBAPM.tctx2_dltx_inv_acct
                                                                           else "":U)
                       tPendingVoucherTaxAPM.tcSuspDelTaxDivisionCode   = (if tPendingVoucherTaxAPM.APMatchingLnTaxIsSuspDel = true 
                                                                           then if tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                                                                   tAPMatching.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then tLgddDetTaxForAPMBAPM.tctx2_dltx_cn_sub
                                                                                else tLgddDetTaxForAPMBAPM.tctx2_dltx_inv_sub
                                                                           else "":U)
                       tPendingVoucherTaxAPM.tc_ParentRowid             = tPendingVoucherAPM.tc_Rowid
                       tPendingVoucherTaxAPM.tc_Rowid                   = string(viRowidPendingVoucherTaxAPM)
                       tPendingVoucherTaxAPM.tc_Status                  = "N":U
                       viRowidPendingVoucherTaxAPM                      = viRowidPendingVoucherTaxAPM + 1.   


                /* Purchase ordere is in foreign currency */
                if tqPendingVoucherForMatchingNoLC.tcpvo_curr <> vcCompanyLC
                then assign tPendingVoucherTaxAPM.APMatchingLnTaxBaseTC      = <M-61 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxBaseTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>
                            tPendingVoucherTaxAPM.APMatchingLnTaxTaxTC       = <M-7 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxTaxTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>
                            tPendingVoucherTaxAPM.APMatchingLnTaxARRecTaxTC  = <M-70 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxARRecTaxTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>
                            tPendingVoucherTaxAPM.APMatchingLnTaxARNRecTaxTC = <M-84 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxARNRecTaxTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>
                            tPendingVoucherTaxAPM.APMatchingLnTaxAIRecTaxTC  = <M-59 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxAIRecTaxTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>
                            tPendingVoucherTaxAPM.APMatchingLnTaxAINRecTaxTC = <M-16 RoundAmount
                                                                                  (input  tPendingVoucherTaxAPM.APMatchingLnTaxAINRecTaxTC * vdLegalDocumentExchangeRate (idUnroundedAmount), 
                                                                                   input  ? (iiCurrencyID), 
                                                                                   input  tqPendingVoucherForMatchingNoLC.tcpvo_curr (icCurrencyCode)) in BAPMatching>.

                    
                    /* Update the tax details. This may have been changed during the generate of the return.*/
                assign tPendingVoucherAPM.APMatchingLnTaxClass  = tLgddDetTaxForAPMBAPM.tctx2d_taxc
                       tPendingVoucherAPM.APMatchingLnTaxEnv    = tLgddDetTaxForAPMBAPM.tctx2d_tax_env
                       tPendingVoucherAPM.APMatchingLnTaxUsage  = tLgddDetTaxForAPMBAPM.tctx2d_tax_usage.
                             
            end. /* end else if tqPendingVoucherForMatchingNoLC.tctx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} */
        end. /* for each tLgddDetTaxForAPMBAPM where */
                


    end. /* if vlIsAssignTaxes = true */

    /* ================================================================================================== *
     * Remove all original tax records, which were created originall and are for PO receipt               *
     * ================================================================================================== */
    /* If we have created new tqPendingVoucherForMatchingNoLC-records in the previous loop on tqTaxDetailByKey    */
    /* then we will now remove the original tqPendingVoucherForMatchingNoLC-records that hold the incorrect taxes */
    /* We can distinguish the old ones from the newly created ones as the old ones haven't got 3 entries in field tctctx2d_ref */
    if vlFoundtqTaxDetailByKey = true
    then do :
        for each tPendingVoucherTaxAPM where
                 tPendingVoucherTaxAPM.tc_ParentRowid = tPendingVoucherAPM.tc_Rowid:
            if can-do( vcOriginalTaxLines, tPendingVoucherTaxAPM.tc_Rowid)
            then delete tPendingVoucherTaxAPM.
        end.     
    end. /* if vlFoundtqTaxDetailByKey = true */    
    
    if vlFoundtqWhtTaxDetailByKey = true
    then do :
        for each tPendingVoucherWHTAPM where
                 tPendingVoucherWHTAPM.tc_ParentRowid = tPendingVoucherAPM.tc_Rowid:
            if can-do( vcOriginalWhtTaxLines, tPendingVoucherWHTAPM.tc_Rowid)
            then delete tPendingVoucherWHTAPM.
        end.
    end. /* if vlFoundtqWhtTaxDetailByKey = true */    
end. /* for each tPendingVoucherAPM where  */