project QadFinancials > class BMFGLegalDocument > method GetLegalDocumentAmount

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
icLgdSupplierinputcharacterShip-from, normally this is Supplier (for material)
itLgdEffDateinputdateLegal document effective issue date
icInvSupplierinputcharacterSupplier invoice supplier
odLgdTaxBaseLCoutputdecimal
odLgdTaxLCoutputdecimal
odLgdTotalAmountLCoutputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.ValidateComponentPreAPMCInvoiceLegalDocument


program code (program5/bmfglegaldocument.p)

/* ====================================================================================== *
 * Method       : GetLegalDocumentAmount                                                  *
 * Description  : This method does calculation of the Legal document amount. This system  *
 *                calculates amount for a certain Invoice supplier. Material on the legal *
 *                document is always invoices the the same supplier as supplier on the    *
 *                legal document. But logistical charges are invoices for the logistical  *
 *                supplier defined normally on the purchase order.                        *
 * -------------------------------------------------------------------------------------- *
 * Parameters   : LgdDomain        Legal document domain (mandatory)                      *
 *                LgdNbr           Legal document number (mandatory)                      *
 *                LgdSupplier      Legal document supplier (mandatory)                    *
 *                LgdEffDate       Legal document effective issue date (mandatory)        *
 *                InvSupplier      Supplier invoice supplier (mandatory)                  *
 *              O LgdTaxBaseLC     Base amount of the legal document.                     *
 *              O LgdTaxLC         Total tax amount of the legal document                 *
 *              O LgdTotalAmountLC Total amount of the legal document                     *
 * ====================================================================================== */

define variable doLegalDocumentDetail as com.qad.eefin.bmfglegaldocument.LegalDocumentDetail.

MAIN_BLOCK:
do on error undo, throw:
    
    /* Normalize parameters */
    if icLgdDomain     = "":U then assign icLgdDomain     = ?.
    if icLgdNbr        = "":U then assign icLgdNbr        = ?.
    if icLgdSupplier   = "":U then assign icLgdSupplier   = ?.
    if icInvSupplier   = "":U then assign icInvSupplier   = ?.
     
    /* Default output parameters */
    assign odLgdTaxBaseLC     = 0
           odLgdTaxLC         = 0
           odLgdTotalAmountLC = 0.

    /* Pre-validation block */
    if icLgdDomain   = ? or
       icLgdNbr      = ? or
       itLgdEffDate  = ? or
       icLgdSupplier = ? or
       icInvSupplier = ?
    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|itLgdEffDate=&3|icLgdSupplier=&4|icInvSupplier=&5":U
               vcContext = substitute(vcContext, icLgdDomain, icLgdNbr, itLgdEffDate, icLgdSupplier, icInvSupplier)
               vcContext = replace(vcContext, "|":U, chr(2)).
        <M-54 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-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 header details                                                               *
     * We use total legal document amoutns for calculation                                             *
     * =============================================================================================== */
    assign doLegalDocumentDetail = new com.qad.eefin.bmfglegaldocument.LegalDocumentDetail (
                                       icLgdDomain,
                                       icLgdSupplier,
                                       icLgdNbr,
                                       itLgdEffDate).
                                       
    if not doLegalDocumentDetail:available
    then do:
        assign vcMessage = #T-42'Cannot find Legal document based upon Domain, Number, Supplier and Effective date (&1, &2, &3, &4).':255(951856427)T-42#
               vcMessage = substitute(vcMessage, icLgdDomain, icLgdSupplier, icLgdNbr, itLgdEffDate)
               vcContext = "icLgdDomain=&1|icLgdSupplier=&2|icLgdNbr=&3|itLgdEffDate=&4":U
               vcContext = substitute(vcContext, icLgdDomain, icLgdSupplier, icLgdNbr, itLgdEffDate)
               vcContext = replace(vcContext, "|":U, chr(2)).
        <M-40 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-708477':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
        assign oiReturnStatus = -1.
        return.
    end.
    

    /* Get all taxes for the legal document */
    <M-84 run GetLegalDocumentDetForAPMatching
       (input  icLgdDomain (icLgdDomain), 
        input  icLgdNbr (icLgdNbr), 
        input  icLgdSupplier (icLgdSuppl), 
        input  itLgdEffDate (itLgdEffDate), 
        input  icInvSupplier (icInvSuppl), 
        input  ? (iiLgddLine), 
        input  ? (icLgddDocType), 
        input  ? (icLgddOrder), 
        input  ? (iiLgddOrderLine), 
        output tLgddDetMatForAPM (tLgddDetMatForAPM), 
        output tLgddDetLCForAPM (tLgddDetLCForAPM), 
        output tLgddDetLCDistForAPM (tLgddDetLCDistForAPM), 
        output tLgddDetTaxForAPM (tLgddDetTaxForAPM), 
        output viFcReturnSuper (oiReturnStatus)) in BMFGLegalDocument>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then return.
 
    /* =============================================================================================== *
     * Set the actual supplier to be used to check the legal document and calculate the invoice total. *
     * This supplier will change depending on whether or not this is a PO receipt or PO return.        *
     * =============================================================================================== */
    if doLegalDocumentDetail:im_tr_type = {&TRANSACTION-TYPE-ISS-PRV} then assign vcActualSupplier = doLegalDocumentDetail:lgd_shipto_site.
    else vcActualSupplier = icLgdSupplier.
                                                 
    /* =============================================================================================== *
     * Calculate amount of the Legal document for material                                             *
     * =============================================================================================== */ 
    if vcActualSupplier = icInvSupplier
    then do:

        assign odLgdTaxBaseLC     = doLegalDocumentDetail:lgd_goods_amt - vdGoodsAmtToRemove
               odLgdTaxLC         = 0
               odLgdTotalAmountLC = doLegalDocumentDetail:lgd_goods_amt - vdGoodsAmtToRemove.
               
        for each tLgddDetMatForAPM where
                 tLgddDetMatForAPM.tclgd_domain    = icLgdDomain      and
                 tLgddDetMatForAPM.tclgd_nbr       = icLgdNbr         and
                 tLgddDetMatForAPM.tcSupplier      = vcActualSupplier and
                 tLgddDetMatForAPM.ttlgd_effdate   = itLgdEffDate,
            each tLgddDetTaxForAPM where
                 tLgddDetTaxForAPM.tc_ParentRowid = tLgddDetMatForAPM.tc_Rowid:

            /* Skip WHT taxes */
            if tLgddDetTaxForAPM.tctx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} then next.
                 
            /* Retained taxes */
            if tLgddDetTaxForAPM.tdtx2d_abs_ret_amt <> 0
            then do:
                if tLgddDetTaxForAPM.tltx2d_tax_in
                then assign odLgdTotalAmountLC = odLgdTotalAmountLC + tLgddDetTaxForAPM.tdtx2d_cur_abs_ret_amt.
            end.
            
            /* Normal taxes */
            else do:
                /* Cumulate tax amount */
                assign odLgdTaxLC = odLgdTaxLC + tLgddDetTaxForAPM.tdtx2d_tax_amt.
        
                /* Calculate legal document base and total amount */
                if tLgddDetTaxForAPM.tltx2d_tax_in
                then assign odLgdTaxBaseLC     = odLgdTaxBaseLC      - tLgddDetTaxForAPM.tdtx2d_tax_amt.
                else assign odLgdTotalAmountLC = odLgdTotalAmountLC  +  tLgddDetTaxForAPM.tdtx2d_tax_amt.
            end. /* Normal taxes */
        end. /* for each tLgddDetMatForAPM where */
    end. /* if icLgdSupplier = icInvSupplier */
    
    /* =============================================================================================== *
     * Calculate amount of the legal document for logistical charges                                   *
     * =============================================================================================== */
    for each tLgddDetLCForAPM where
             tLgddDetLCForAPM.tclgd_domain    = icLgdDomain   and
             tLgddDetLCForAPM.tclgd_nbr       = icLgdNbr      and
             tLgddDetLCForAPM.tclgd_shipfrom  = icLgdSupplier and
             tLgddDetLCForAPM.ttlgd_effdate   = itLgdEffDate:
             
        /* If the logistic charge was not invoices by this invoice, skip legal document line */
        if not can-find(first tLgddDetLCDistForAPM where
                              tLgddDetLCDistForAPM.tc_ParentRowid = tLgddDetLCForAPM.tc_Rowid and
                              tLgddDetLCDistForAPM.tcSupplier     = icInvSupplier)
        then next.
        
        assign vdLgdLCLineTaxLC = 0.
        
        for each tLgddDetLCDistForAPM where
                 tLgddDetLCDistForAPM.tc_ParentRowid = tLgddDetLCForAPM.tc_Rowid,
            each tLgddDetTaxForAPM where
                 tLgddDetTaxForAPM.tc_ParentRowid = tLgddDetLCDistForAPM.tc_Rowid:
                 
            /* Skip WHT taxes */
            if tLgddDetTaxForAPM.tctx2d_tr_type = {&VATTAXTRANSACTIONTYPE-APWHT} then next.
                 
            /* Retained taxes */
            if tLgddDetTaxForAPM.tdtx2d_abs_ret_amt <> 0
            then do:
                if tLgddDetTaxForAPM.tltx2d_tax_in
                then assign odLgdTaxBaseLC = odLgdTaxBaseLC + tLgddDetTaxForAPM.tdtx2d_cur_abs_ret_amt.
            end.
            
            /* Normal taxes */
            else assign vdLgdLCLineTaxLC = vdLgdLCLineTaxLC + tLgddDetTaxForAPM.tdtx2d_tax_amt.
        end.
        
        assign odLgdTaxBaseLC     = odLgdTaxBaseLC     + tLgddDetLCForAPM.tdamount_base
               odLgdTaxLC         = odLgdTaxLC         + vdLgdLCLineTaxLC
               odLgdTotalAmountLC = odLgdTotalAmountLC + tLgddDetLCForAPM.tdamount_base + vdLgdLCLineTaxLC.
    end. /* for each tLgddDetLCForAPM */
    
    
    /* =============================================================================================== *
     * If we are getting details for legal document created during Purchase return,                    *
     * all amounts has to be oposite. So it is the same as if we have negative                         *
     * purchase order receipt                                                                          *
     * =============================================================================================== */
    assign vlIsPOReturn = false.                                                                           /* po receipt */
    if doLegalDocumentDetail:im_tr_type = {&TRANSACTION-TYPE-ISS-PRV} then assign vlIsPOReturn = true.     /* po return */
    if vlIsPOReturn = true        /* Purchase order return */
    then assign odLgdTaxBaseLC     = - odLgdTaxBaseLC
                odLgdTaxLC         = - odLgdTaxLC
                odLgdTotalAmountLC = - odLgdTotalAmountLC.

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

end. /* MAIN_BLOCK */