project QadFinancials > class BMFGLegalDocument > method GetLegalDocumentDetForAPMatching

Description

This procedure retrieves details for legal documents, that can be used later on by Receiver matching or by ERS.


Parameters


icLgdDomaininputcharacterLegal document domain
icLgdNbrinputcharacter
icLgdSupplinputcharacterShip-from, normally this is Supplier (for material)
itLgdEffDateinputdateLegal document effective issue date
icInvSupplinputcharacter
iiLgddLineinputintegerLegal document line number
icLgddDocTypeinputcharacterAssociated document to the Letal document line. Normally this is PO for the receipt. Parameter is optional.
icLgddOrderinputcharacterOrder line
iiLgddOrderLineinputintegerOrder line
tLgddDetMatForAPMoutputtemp-table
tLgddDetLCForAPMoutputtemp-table
tLgddDetLCDistForAPMoutputtemp-table
tLgddDetTaxForAPMoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.GetPendingVouchersLogChargesQuery
method BAPMatching.GetPendingVouchersNoLogChargesTaxFiscal
method BERSProcessor.GetERSLegalDocumentsLC
method BERSProcessor.GetERSLegalDocumentsNoLC
method BMFGLegalDocument.GetLegalDocumentAmount


program code (program5/bmfglegaldocument.p)

/* ====================================================================================== *
 * Method       : GetLgddDetMatForAPM                                                        *
 * Description  : This method retrieves details about the legal document. These details   *
 *                are primarly used for the ERS and APMatching                            *
 *                There is one specific - routine is grouping legal document lines for    *
 *                the same purchase order line                                            *
 * -------------------------------------------------------------------------------------- *
 * Parameters   : LgdDomain     Legal document domain (mandatory)                         *
 *                LgdNbr        Legal document number (mandatory)                         *
 *                LgdSuppl      Legal document supplier (mandatory)                       *
 *                LgdEffDate    Legal document effective issue date (mandatory)           *
 *                InvoiceSuppl  Invoice supplier (for material invoice supplier it is the *
 *                              same as lgd supplier, for logistic charges invoice        *
 *                              supplier is logistical supplier                           *
 *                LgddLine      Legal document line number (optional)                     *
 *                LgddDocType   Legal document line associated document (optional)        *
 *                LgddOrder     Legal document line order (optional)                      *
 *                LgddOrderLine Legal document line order line (optional)                 *
 * ====================================================================================== */

define variable doLegalDocumentForERSRcvrV2 as com.qad.eefin.bmfglegaldocument.LegalDocumentForERSRcvrV2 no-undo.
 
MAIN_BLOCK:
do on error undo, throw:
    
    /* Normalize parameters */
    if icLgdDomain     = "":U then assign icLgdDomain     = ?.
    if icLgdNbr        = "":U then assign icLgdNbr        = ?.
    if icLgdSuppl      = "":U then assign icLgdSuppl      = ?.
    if iiLgddLine      = 0    then assign iiLgddLine      = ?.
    if icLgddDocType   = "":U then assign icLgddDocType   = ?.
    if icLgddOrder     = "":U then assign icLgddOrder     = ?.
    if iiLgddOrderLine = 0    then assign iiLgddOrderLine = ?.  
    
    /* Do we need to read all data? Are not are already there? */
    if vlLegalDocumentFullyRead                                                     and
       (can-find( first tLgddDetMatForAPM where
                        tLgddDetMatForAPM.tclgd_domain      = icLgdDomain  and
                        tLgddDetMatForAPM.tclgd_nbr         = icLgdNbr     and
                        tLgddDetMatForAPM.tcSupplier        = icLgdSuppl   and
                        tLgddDetMatForAPM.ttlgd_effdate     = itLgdEffDate)        or
        can-find( first tLgddDetLCForAPM where
                        tLgddDetLCForAPM.tclgd_domain      = icLgdDomain  and
                        tLgddDetLCForAPM.tclgd_nbr         = icLgdNbr     and
                        tLgddDetLCForAPM.tclgd_shipfrom    = icLgdSuppl   and
                        tLgddDetLCForAPM.ttlgd_effdate     = itLgdEffDate))
    then return.
    
    
    /* Default output parameters */
    empty temp-table tLgddDetMatForAPM.
    empty temp-table tLgddDetLCForAPM.
    empty temp-table tLgddDetLCDistForAPM.
    empty temp-table tLgddDetTaxForAPM.
    assign vlLegalDocumentFullyRead = (iiLgddLine = ? and icLgddDocType = ? and icLgddOrder = ? and iiLgddOrderLine = ?)
           vdGoodsAmtToRemove       = 0.

    /* Pre-validation block */
    if icLgdDomain = ? or
       icLgdNbr    = ? or
       icLgdSuppl  = ?
    then do:
        assign vcMessage = #T-34'Not all mandatory input parameters are populated or they have wrong value.':255(999890330)T-34#
               vcContext = "icLgdDomain=&1|icLgdNbr=&2|icLgdSuppl=&3|iiLgddLine=&4|icLgddDocType=&5|icLgddOrder=&6|iiLgddOrderLine=&7":U
               vcContext = substitute(vcContext, icLgdDomain, icLgdNbr, icLgdSuppl, iiLgddLine, icLgddDocType, icLgddOrder, iiLgddOrderLine)
               vcContext = replace(vcContext, "|":U, chr(2)).
        <M-54 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-577830':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
        assign oiReturnStatus = -1.
        return.
    end. /* Pre-validation block */

    /* Get legal document data */
    /* Run the data object to obtain the legal document data required to build the merged records */
    publish "Logging.BusinessCode":U ("START-SUB,LegalDocumentForERSRcvrV2":U,
                                      replace(substitute("In:icLgdDomain=&1|In:icLgdSuppl=&2|In:icLgdNbr=&3|In:itLgdEffDate=&4|In:icLgddOrder=&5|In:iiLgddOrderLine=&6":U,
                                                         icLgdDomain,icLgdSuppl,icLgdNbr,itLgdEffDate,icLgddOrder,iiLgddOrderLine),
                                              "|":U, chr(10))).
    
    
    doLegalDocumentForERSRcvrV2 = new com.qad.eefin.bmfglegaldocument.LegalDocumentForERSRcvrV2 (
                                                          icLgdDomain,
                                                          icLgdSuppl,
                                                          icLgdNbr,
                                                          itLgdEffDate,
                                                          icLgddOrder,
                                                          iiLgddOrderLine).
                                                          
    publish "Logging.DatabaseAccess":U ("query doLegalDocumentForERSRcvrV2":U + chr(10) + doLegalDocumentForERSRcvrV2:QueryInfo(), ?).
    publish "Logging.BusinessCode":U ("END-SUB,LegalDocumentForERSRcvrV2":U, "":U).


    LEGALDOCUMENTLOOP:
    do while doLegalDocumentForERSRcvrV2:Available:
    
        /* ================================================================================================================== *
         * FIN-18064: Legal document with UNP line fails to create invoice in ERS Processor.                                  *
         * It was decided that UNP (Unplanned Receipts) and TRANS (Transfer) lines should not be processed in AP.  Therefore, *
         * we add up the total of these lines and then remove it from the goods amount in GetLegalDocumentAmount.             *
         * ================================================================================================================== */
        if doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-UNP}   or
           doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-TRANS} or
           doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-NONE}
        then assign vdGoodsAmtToRemove = vdGoodsAmtToRemove + doLegalDocumentForERSRcvrV2:lgdd_ext_price.

        /* ================================================================================================================== *
         * Material Legal document line                                                                                       *
         * ================================================================================================================== */
        if doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-DO} or    /* Distribution order */
           doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-PO} or    /* Purchase order receipt */
           doLegalDocumentForERSRcvrV2:lgdd_doc_type = "":U                                /* Purchase order return */ 
        then do:

            /* ===================================================================================================================== *
             * If the transaction type is ISS-PRV then we can say the document is a PO Return, if it is not then it is a PO Receipt  *
             * ===================================================================================================================== */
            assign vlIsPOReturn = false.                                                                                   /* PO Receipt */ 
            if doLegalDocumentForERSRcvrV2:im_tr_type = {&TRANSACTION-TYPE-ISS-PRV} then assign vlIsPOReturn = true.       /* PO Return */
    
            /* Get supplier */        
            assign vcSupplier = if vlIsPOReturn = false
                                then doLegalDocumentForERSRcvrV2:lgd_shipfrom
                                else doLegalDocumentForERSRcvrV2:lgd_ship.

        

            /* ============================================================================= *
             * Calculated triangular Unit of Measure convesion factor between Legal document *
             * unit of measure and purchase order unit of measure. We have to go through the *
             * base unit of measure, as both UoM conversion factors on PO and LC are         *
             * expressed agains this                                                         *
             * ============================================================================= */
            assign vdUoMConvLDBase = 1.
            if doLegalDocumentForERSRcvrV2:lgdd_um_conv  <> 0
            then assign vdUoMConvLDBase = doLegalDocumentForERSRcvrV2:lgdd_um_conv.
            
            
            assign vdUomConvPOBase = 1.
            if doLegalDocumentForERSRcvrV2:pod_um_conv <> 0
            then assign vdUomConvPOBase = doLegalDocumentForERSRcvrV2:pod_um_conv.
            
            assign vdUomConvLDPO = vdUoMConvLDBase / vdUomConvPOBase.


            find first tLgddDetMatForAPM where
                       tLgddDetMatForAPM.tclgd_domain      = doLegalDocumentForERSRcvrV2:lgd_domain     and
                       tLgddDetMatForAPM.tclgd_nbr         = doLegalDocumentForERSRcvrV2:lgd_nbr        and
                       tLgddDetMatForAPM.tcSupplier        = vcSupplier                                 and
                       tLgddDetMatForAPM.ttlgd_effdate     = doLegalDocumentForERSRcvrV2:lgd_effdate    and
                       tLgddDetMatForAPM.tclgdd_order      = doLegalDocumentForERSRcvrV2:lgdd_order     and
                       tLgddDetMatForAPM.tilgdd_order_line = doLegalDocumentForERSRcvrV2:lgdd_order_line
                       no-error.
          
            if not available tLgddDetMatForAPM
            then do:
               /* create the temp table record */
               create tLgddDetMatForAPM.
               assign tLgddDetMatForAPM.tc_Rowid          = doLegalDocumentForERSRcvrV2:lgd_domain          + ',':U +
                                                            doLegalDocumentForERSRcvrV2:lgd_nbr             + ',':U +
                                                            vcSupplier                                      + ',':U +
                                                            string(doLegalDocumentForERSRcvrV2:lgd_effdate) + ',':U +
                                                            doLegalDocumentForERSRcvrV2:lgdd_order          + ',':U +
                                                            string(doLegalDocumentForERSRcvrV2:lgdd_order_line)
                      tLgddDetMatForAPM.tclgd_domain      = doLegalDocumentForERSRcvrV2:lgd_domain     
                      tLgddDetMatForAPM.tclgd_nbr         = doLegalDocumentForERSRcvrV2:lgd_nbr        
                      tLgddDetMatForAPM.tcSupplier        = vcSupplier
                      tLgddDetMatForAPM.tclgd_ship        = doLegalDocumentForERSRcvrV2:lgd_ship
                      tLgddDetMatForAPM.tclgd_shipfrom    = doLegalDocumentForERSRcvrV2:lgd_shipfrom
                      tLgddDetMatForAPM.ttlgd_effdate     = doLegalDocumentForERSRcvrV2:lgd_effdate    
                      tLgddDetMatForAPM.tclgdd_order      = doLegalDocumentForERSRcvrV2:lgdd_order
                      tLgddDetMatForAPM.tilgdd_order_line = doLegalDocumentForERSRcvrV2:lgdd_order_line
                      tLgddDetMatForAPM.tclgd_doc_type    = doLegalDocumentForERSRcvrV2:lgd_doc_type
                      tLgddDetMatForAPM.tclgdd_type       = doLegalDocumentForERSRcvrV2:lgdd_type
                      tLgddDetMatForAPM.tclgdd_part       = doLegalDocumentForERSRcvrV2:lgdd_part
                      tLgddDetMatForAPM.tclgdd_um         = doLegalDocumentForERSRcvrV2:lgdd_um
                      tLgddDetMatForAPM.tdlgdd_um_conv    = doLegalDocumentForERSRcvrV2:lgdd_um_conv 
                      tLgddDetMatForAPM.tcpod_um          = doLegalDocumentForERSRcvrV2:pod_um
                      tLgddDetMatForAPM.tdpod_um_conv     = doLegalDocumentForERSRcvrV2:pod_um_conv 
                      tLgddDetMatForAPM.tdlgd_ex_rate     = doLegalDocumentForERSRcvrV2:lgd_ex_rate
                      tLgddDetMatForAPM.tdlgd_ex_rate2    = doLegalDocumentForERSRcvrV2:lgd_ex_rate2
                      tLgddDetMatForAPM.tllgd_type        = doLegalDocumentForERSRcvrV2:lgd_type
                      tLgddDetMatForAPM.tclgd_trans_curr  = doLegalDocumentForERSRcvrV2:lgd_trans_curr
                      tLgddDetMatForAPM.tlIsPOReturn      = vlIsPOReturn.
                      
            end. /* if not available tLgddDetMatForAPM ... */
        
            assign vdlgdd_qty_item        = doLegalDocumentForERSRcvrV2:lgdd_qty_item
                   vdlgdd_qty_item_po_uom = vdlgdd_qty_item * vdUomConvLDPO.
  
            /* First calculate the weighted average price. The current weighted average will be stored in the temp-table and the results of */
            /* previous calculations used to calculate the new price.                                                                       */
            assign tLgddDetMatForAPM.tdlgdd_price        = ((tLgddDetMatForAPM.tdlgdd_price * tLgddDetMatForAPM.tdlgdd_qty_item) +
                                                            (doLegalDocumentForERSRcvrV2:lgdd_ext_price)) /
                                                           (tLgddDetMatForAPM.tdlgdd_qty_item + vdlgdd_qty_item)
                   tLgddDetMatForAPM.tdlgdd_price_po_uom = ((tLgddDetMatForAPM.tdlgdd_price_po_uom * tLgddDetMatForAPM.tdlgdd_qty_item_po_uom) +
                                                            (doLegalDocumentForERSRcvrV2:lgdd_ext_price)) /
                                                           (tLgddDetMatForAPM.tdlgdd_qty_item_po_uom + vdlgdd_qty_item_po_uom)
                   tLgddDetMatForAPM.tdlgdd_dsc_amt      = tLgddDetMatForAPM.tdlgdd_dsc_amt + doLegalDocumentForERSRcvrV2:lgdd_dsc_amt
                   tLgddDetMatForAPM.tdlgdd_dsc_amt_qty_uom = ((tLgddDetMatForAPM.tdlgdd_dsc_amt_qty_uom * tLgddDetMatForAPM.tdlgdd_qty_item) +
                                                            (doLegalDocumentForERSRcvrV2:lgdd_dsc_amt)) /
                                                           (tLgddDetMatForAPM.tdlgdd_qty_item + vdlgdd_qty_item)

                   tLgddDetMatForAPM.tdlgdd_dsc_amt_po_uom = ((tLgddDetMatForAPM.tdlgdd_dsc_amt_po_uom * tLgddDetMatForAPM.tdlgdd_qty_item_po_uom) +
                                                            (doLegalDocumentForERSRcvrV2:lgdd_dsc_amt)) /
                                                           (tLgddDetMatForAPM.tdlgdd_qty_item_po_uom + vdlgdd_qty_item_po_uom) .
                   
        
            /* Update the total quantity received on the legal document */                                                      
            assign tLgddDetMatForAPM.tdlgdd_qty_item        = tLgddDetMatForAPM.tdlgdd_qty_item        + vdlgdd_qty_item
                   tLgddDetMatForAPM.tdlgdd_qty_item_po_uom = tLgddDetMatForAPM.tdlgdd_qty_item_po_uom + vdlgdd_qty_item_po_uom.

            /* Get all taxes related to legal document line */
            if doLegalDocumentForERSRcvrV2:im_tr_type <> "":U and
               doLegalDocumentForERSRcvrV2:im_tr_type <> ?
            then assign vcTaxReference = substitute("&1&2":U,                           /* Automatically generated legal document */
                                                    string(doLegalDocumentForERSRcvrV2:lgd_shipfrom, 'x(8)':U),
                                                    string(doLegalDocumentForERSRcvrV2:lgd_nbr, 'x(12)':U)).

            else assign vcTaxReference = substitute("&1,&2,&3/&4/&5":U,                 /* Manually created legal document */
                                                    trim(doLegalDocumentForERSRcvrV2:lgd_shipfrom),
                                                    trim(doLegalDocumentForERSRcvrV2:lgd_nbr),
                                                    string(year(doLegalDocumentForERSRcvrV2:lgd_effdate) mod 100,"99"),
                                                    string(month(doLegalDocumentForERSRcvrV2:lgd_effdate),"99"),
                                                    string(day(doLegalDocumentForERSRcvrV2:lgd_effdate),"99")).
            
           
            <M-22 run GetLegalDocumentDetForAPMatchingTax
               (input  doLegalDocumentForERSRcvrV2:lgd_domain (icTaxDomain), 
                input  vcTaxReference (icTaxReference), 
                input  doLegalDocumentForERSRcvrV2:lgdd_order (icOrderNumber), 
                input  ? (icLcCharge), 
                input  doLegalDocumentForERSRcvrV2:lgdd_line (iiTaxLine), 
                input  ? (icTaxTrailer), 
                input  tLgddDetMatForAPM.tc_Rowid (icParentRowId), 
                output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0 then return.    

        end. /* Material Legal document line */
        

        /* ================================================================================================================== */
        /* Logistic charge Legal document line                                                                                */
        /* ================================================================================================================== */
        else if doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-LC}
        then do:
           /* create the temp table record */
           create tLgddDetLCForAPM.
           assign tLgddDetLCForAPM.tc_Rowid         = doLegalDocumentForERSRcvrV2:lgd_domain          + ',':U +
                                                      doLegalDocumentForERSRcvrV2:lgd_nbr             + ',':U +
                                                      doLegalDocumentForERSRcvrV2:lgd_shipfrom        + ',':U +
                                                      string(doLegalDocumentForERSRcvrV2:lgd_effdate) + ',':U +
                                                      doLegalDocumentForERSRcvrV2:lgdd_order          + ',':U +
                                                      string(doLegalDocumentForERSRcvrV2:lgdd_order_line)
                  tLgddDetLCForAPM.tdoid_lgdd_det   = doLegalDocumentForERSRcvrV2:oid_lgdd_det 
                  tLgddDetLCForAPM.tclgd_domain     = doLegalDocumentForERSRcvrV2:lgd_domain
                  tLgddDetLCForAPM.tclgd_nbr        = doLegalDocumentForERSRcvrV2:lgd_nbr
                  tLgddDetLCForAPM.tclgd_doc_type   = doLegalDocumentForERSRcvrV2:lgd_doc_type
                  tLgddDetLCForAPM.ttlgd_effdate    = doLegalDocumentForERSRcvrV2:lgd_effdate
                  tLgddDetLCForAPM.tclgd_ship       = doLegalDocumentForERSRcvrV2:lgd_ship
                  tLgddDetLCForAPM.tclgd_shipfrom   = doLegalDocumentForERSRcvrV2:lgd_shipfrom
                  tLgddDetLCForAPM.tclgd_trans_curr = doLegalDocumentForERSRcvrV2:lgd_trans_curr
                  tLgddDetLCForAPM.tdlgd_ex_rate    = doLegalDocumentForERSRcvrV2:lgd_ex_rate
                  tLgddDetLCForAPM.tdlgd_ex_rate2   = doLegalDocumentForERSRcvrV2:lgd_ex_rate2
                  tLgddDetLCForAPM.tllgd_type       = doLegalDocumentForERSRcvrV2:lgd_type
                  tLgddDetLCForAPM.tilgdd_line      = doLegalDocumentForERSRcvrV2:lgdd_line
                  tLgddDetLCForAPM.tclc_charge      = doLegalDocumentForERSRcvrV2:lgdd_order
                  tLgddDetLCForAPM.tdamount         = doLegalDocumentForERSRcvrV2:lgdd_price
                  tLgddDetLCForAPM.tdlgdd_dsc_amt   = doLegalDocumentForERSRcvrV2:lgdd_dsc_amt
                  tLgddDetLCForAPM.tlIsPOReturn     = vlIsPOReturn.
            
            /* Get logistic charge distribution details */
            <M-71 run GetLegalDocumentDetForAPMatchingLCDist
               (input  doLegalDocumentForERSRcvrV2:lgd_domain (icLgdDomain), 
                input  doLegalDocumentForERSRcvrV2:lgd_nbr (icLgdNbr), 
                input  doLegalDocumentForERSRcvrV2:lgd_shipfrom (icLgdShipFrom), 
                input  doLegalDocumentForERSRcvrV2:lgd_effdate (itLgdEffDate), 
                input  doLegalDocumentForERSRcvrV2:lgdd_order (icLgddLcCharge), 
                input  doLegalDocumentForERSRcvrV2:lgdd_line (iiLgddLine), 
                input  doLegalDocumentForERSRcvrV2:lgdd_order_line (iiLgddOrderLine), 
                input  tLgddDetLCForAPM.tdamount - tLgddDetLCForAPM.tdlgdd_dsc_amt (idTotalAmountLC), 
                output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0 then return.    
        end. /* else if doLegalDocumentForERSRcvrV2:lgdd_doc_type = {&LEGALDOCUMENT-LINE-TYPE-LC} */

                
        /* Get next record */
        doLegalDocumentForERSRcvrV2:next.
    end.     /* LEGALDOCUMENTLOOP */


    /* =================================================================================================== */
    /* Calcualte all prices/amounts also in the way, they does not continue any taxes (both inclusive and  */
    /* exclusive                                                                                           */
    /* =================================================================================================== */
    for each tLgddDetMatForAPM:
        assign vdTotalTaxInclusive = 0.
        
        for each tLgddDetTaxForAPM where 
                 tLgddDetTaxForAPM.tc_ParentRowid = tLgddDetMatForAPM.tc_Rowid and
                 tLgddDetTaxForAPM.tltx2d_tax_in  = true
                 no-lock:
            assign vdTotalTaxInclusive = vdTotalTaxInclusive + tLgddDetTaxForAPM.tdtx2d_cur_tax_amt.
        end.

        assign  tLgddDetMatForAPM.tdlgdd_price_base        = (tLgddDetMatForAPM.tdlgdd_price        - tLgddDetMatForAPM.tdlgdd_dsc_amt_qty_uom) - (vdTotalTaxInclusive / tLgddDetMatForAPM.tdlgdd_qty_item)
                tLgddDetMatForAPM.tdlgdd_price_base_po_uom = (tLgddDetMatForAPM.tdlgdd_price_po_uom - tLgddDetMatForAPM.tdlgdd_dsc_amt_po_uom)  - (vdTotalTaxInclusive / tLgddDetMatForAPM.tdlgdd_qty_item_po_uom).

    end. /* for each tLgddDetMatForAPM ...*/
    
    for each tLgddDetLCForAPM:
        
        assign vdTotalTaxInlusiveByLgdLine = 0.
        
        for each tLgddDetLCDistForAPM where
                 tLgddDetLCDistForAPM.tc_ParentRowid = tLgddDetLCForAPM.tc_Rowid:

            assign vdTotalTaxInclusiveByLgdDist = 0.
            
            for each tLgddDetTaxForAPM where 
                     tLgddDetTaxForAPM.tc_ParentRowid = tLgddDetLCDistForAPM.tc_Rowid and
                     tLgddDetTaxForAPM.tltx2d_tax_in  = true
                     no-lock:
                assign vdTotalTaxInlusiveByLgdLine  = vdTotalTaxInlusiveByLgdLine  + tLgddDetTaxForAPM.tdtx2d_cur_tax_amt
                       vdTotalTaxInclusiveByLgdDist = vdTotalTaxInclusiveByLgdDist + tLgddDetTaxForAPM.tdtx2d_cur_tax_amt.
            end. /* for each tLgddDetTaxForAPM where */
            
            assign tLgddDetLCDistForAPM.tdAmountBaseLC = tLgddDetLCDistForAPM.tdAmountLC - vdTotalTaxInclusiveByLgdDist.
            
        end. /* for each tLgddDetLCDistForAPM where */
        
        assign  tLgddDetLCForAPM.tdamount_base = tLgddDetLCForAPM.tdamount - vdTotalTaxInlusiveByLgdLine .
    end. /* for each tLgddDetLCForAPM ...*/


    /* Finally block */
    finally:
        if valid-object(doLegalDocumentForERSRcvrV2) then delete object doLegalDocumentForERSRcvrV2 no-error.
    end.

end. /* MAIN_BLOCK */