project QadFinancials > class BAPMatching > method CalculatePostAPMLnQuery

Description

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


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.CalculatePostAPMLn


program code (program7/bapmatching.p)

    
    /* ===================================================================================================== */
    /* 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                         */
    /* Note: the result-set is indexed on APMatchingLine_ID                                                  */
    /* ===================================================================================================== */        
    <Q-23 run APMatchingLnForPvodDetails (all) (Read) (NoCache)
       (input tAPMatching.APMatching_ID, (APMatchingID)
        input ?, (CompanyId)
        output dataset tqAPMatchingLnForPvodDetails) in BAPMatching >
    
    /* ====================================================================================================== */
    /* Specific actions required in case a PVO is originated from the Fiscal Receiving                        */
    /* Pending vouchers originated from Fiscal Receiving can be distinguished by this:                        */
    /*     pvo_mstr.pvo_external_ref consists of two comma-sep entries (where the first                       */
    /*     entry is the LegalDocumentNumber and the second one is the transaction/effective-date)             */
    /* For PVO's originated from the Fiscal Receiving this query will only return the taxes belonging to the  */
    /* POReceipt that preceeded the Fiscal Receipt.                                                           */
    /* As adapting the query to return the correct underlying taxes was not feasible, we will here:           */
    /*   1 Query the taxes of the Fiscal Receipt with another query                                           */
    /*   2 Create a new record in tqAPMatchingLnForPvodDetails for every record of step 1                     */
    /*   3 Remove the ones of the Receipt - but only in case there are taxes for the Fiscal Receipt           */
    /* ====================================================================================================== */
    /* Sample: We only want the tx2d_det records that actually belong to the pvod, so we delete the ones that */
    /*         do not really belong to the pvod                                                               */
    /* Sample Data:                                                                                           */
    /*     tctx2d_ref: Either just "R0000125",                                                                */
    /*                 Either "lorre,X11-LD3,11/10/12" with the date in format YY MM DD (12 October)          */
    /*     pvo_external_ref: "X11-LD3,11/10/12" with the date in format YY MM DD (12 October)                 */
    /*     pvo_supplier: lorre                                                                                */
    /* ====================================================================================================== */
    /* We need to manualy define a buffer on tqAPMatchingLnForPvodDetails as CB doesn't support this */
    define buffer btqAPMatchingLnForPvodDetails for tqAPMatchingLnForPvodDetails.
    /* Go through all pending-voucher-details that relate to fiscal-receipts */
    for each tqAPMatchingLnForPvodDetails where
             num-entries(tqAPMatchingLnForPvodDetails.tcpvo_external_ref,',':U) = 2 
             break by tqAPMatchingLnForPvodDetails.tcpvod_domain
                   by tqAPMatchingLnForPvodDetails.tipvod_id
                   by tqAPMatchingLnForPvodDetails.tipvod_id_line : 
        /* Last-of is needed as tqAPMatchingLnForPvodDetails holds a records for every tax-code linked to the pending-voucher-detail */
        if last-of (tqAPMatchingLnForPvodDetails.tipvod_id_line)
        then do :
            /* Query on the tax-details: Check if we can find tax-details related to the fiscal-receipt of this pending-voucher-detail */
            /* Note: it was not able to take all conditions into account with this query call and therefore some conditions are there when going through the result-set of this query */
            assign vcTx2dReference = tqAPMatchingLnForPvodDetails.tcpvo_supplier + ',':U +  tqAPMatchingLnForPvodDetails.tcpvo_external_ref
                   viTx2dLine      = if tqAPMatchingLnForPvodDetails.tcpvod_order_type = '01':U 
                                     then tqAPMatchingLnForPvodDetails.tipvod_order_line
                                     else tqAPMatchingLnForPvodDetails.tipvod_id_line.
            <Q-18 run TaxDetailByKey (all) (Read) (NoCache)
               (input tqAPMatchingLnForPvodDetails.tcpvo_domain, (Tx2dDomain)
                input vcTx2dReference, (Tx2dReference)
                input tqAPMatchingLnForPvodDetails.tcpvo_order, (Tx2dNumber)
                input viTx2dLine, (Tx2dLine)
                input ?, (Tx2dTrailer)
                input ?, (Tx2dTransactionType)
                input ?, (Tx2dTaxCode)
                output dataset tqTaxDetailByKey) in BMfgTaxDetail>
            /* Indicate we have not yet found a tqTaxDetailByKey-record by setting vlFoundtqTaxDetailByKey to false */
            assign vlFoundtqTaxDetailByKey = false.
            /* Go through all tqTaxDetailByKey with some additional conditions as the query could not hold all conditions */
            for each tqTaxDetailByKey where
                     tqTaxDetailByKey.tctx2d_domain = tqAPMatchingLnForPvodDetails.tcpvo_domain              AND
                     tqTaxDetailByKey.tctx2d_ref    = vcTx2dReference                                           AND
                     tqTaxDetailByKey.tctx2d_nbr    = tqAPMatchingLnForPvodDetails.tcpvo_order               AND
                     (tqTaxDetailByKey.tctx2d_tr_type = '45' or
                      tqTaxDetailByKey.tctx2d_tr_type = '43' or
                      tqTaxDetailByKey.tctx2d_tr_type = '21' or
                      tqTaxDetailByKey.tctx2d_tr_type = '27' or 
                      tqTaxDetailByKey.tctx2d_tr_type = '25' or
                      tqTaxDetailByKey.tctx2d_tr_type = '60' or
                      tqTaxDetailByKey.tctx2d_tr_type = '17' or
                      tqTaxDetailByKey.tctx2d_tr_type = '30')                                                   AND
                     ((tqTaxDetailByKey.titx2d_line = tqAPMatchingLnForPvodDetails.tipvod_order_line and 
                       tqAPMatchingLnForPvodDetails.tcpvod_order_type = '01') or
                      (tqTaxDetailByKey.titx2d_line = tqAPMatchingLnForPvodDetails.tipvod_id_line and
                       tqAPMatchingLnForPvodDetails.tcpvod_order_type = '02' and 
                       tqAPMatchingLnForPvodDetails.tipvod_order_line = 0) or
                      (tqTaxDetailByKey.titx2d_line = 0 and 
                       tqAPMatchingLnForPvodDetails.tcpvod_order_type = '01')) :
                /* Copy the current tqAPMatchingLnForPvodDetails record into a new record and copy and overwrite the tax-info in the new records with the info of tqTaxDetailByKey */
                Create btqAPMatchingLnForPvodDetails.
                Buffer-copy tqAPMatchingLnForPvodDetails to btqAPMatchingLnForPvodDetails.
                Buffer-copy tqTaxDetailByKey to btqAPMatchingLnForPvodDetails.
                /* Indicate we have found a tqTaxDetailByKey-record by setting vlFoundtqTaxDetailByKey to true */
                assign vlFoundtqTaxDetailByKey = true.
            end. /* If can-find (first tqTaxDetailByKey where */
            /* If we have created new tqAPMatchingLnForPvodDetails-records in the previous loop on tqTaxDetailByKey    */
            /* then we will now remove the original tqAPMatchingLnForPvodDetails-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 btqAPMatchingLnForPvodDetails where
                         btqAPMatchingLnForPvodDetails.tcpvod_domain   = tqAPMatchingLnForPvodDetails.tcpvod_domain  and
                         btqAPMatchingLnForPvodDetails.tipvod_id       = tqAPMatchingLnForPvodDetails.tipvod_id      and 
                         btqAPMatchingLnForPvodDetails.tipvod_id_line  = tqAPMatchingLnForPvodDetails.tipvod_id_line and
                         rowid(btqAPMatchingLnForPvodDetails)         <> rowid(tqAPMatchingLnForPvodDetails)         and 
                         num-entries(btqAPMatchingLnForPvodDetails.tctx2d_ref,",":U) <> 3: 
                    delete btqAPMatchingLnForPvodDetails.
                end. /* for each btqAPMatchingLnForPvodDetails where */
                delete tqAPMatchingLnForPvodDetails.
            end. /* if vlFoundtqTaxDetailByKey = true */
        end. /* if last-of (tqAPMatchingLnForPvodDetails.tipvod_id_line) */
    end. /* for each tqAPMatchingLnForPvodDetails where */