project QadFinancials > class BPosting > method MassReversePeriodicCostingPostings

Description

MassReversePeriodicCostingPostings; this method is called from the Periodic-costing module to reverse a bunch of postings of a certain company, journal (type periodic costing and layer official or management) and period.
The method returns a temp-table holding the main info of the postings that are reversed.


Parameters


icCompanyCodeinputcharacterCompanyCode: Mandatory.
This should be the current company
icJournalCodeinputcharacterJornalCode: Mandatory.
The type of this journal should be Periodic-costing and it should be in a management or official layer
iiPostingYearinputintegerPostingYear: Mandatory. accounting-year format 9999
iiPostingPeriodinputintegerPostingPeriod: Mandatory. accounting-period format 99
ilOnlyReverseUnreversedOnesinputlogicalOnlyReverseUnreversedOnes:
Pass true in case you only want to reverse postings that have not yet been reversed or that are no reversals of other postings.
Pass false in case you just want to reverse all postings matching the filter regardless their reversal-properties
ilReversalByCorrectioninputlogicalReversalByCorrection;
Pass true in case debit of the reversal posting should be the credit of the reversed posting (and vice versa)
Pass true in case debit and credit of the reversal posting should be the same as the debit and credit of the reversed posting but multiplied by -1
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPosting.ApiMassReversePeriodicCostingPostingsByDomain


program code (program8/bposting.p)

MAINPCREVERSALBLOCK: DO :    
    /* ================================================================ */
    /* Replace unknown values in the input parameters and then validate */
    /* the input as stated in the descriptions of the parameters itself */
    /* ================================================================ */
    if icCompanyCode   = "":U or 
       icCompanyCode   = ?    or
       icJournalCode   = "":U or 
       icJournalCode   = ?    or
       iiPostingYear   = 0    or
       iiPostingYear   = ?    or
       iiPostingPeriod = 0    or
       iiPostingPeriod = ? 
    then do :
        assign vcMessage      = trim(substitute(#T-82'The entity (&1), daybook (&2), year (&3) and period (&4) are all mandatory parameters for the API method.':255(73479107)T-82#,icCompanyCode,icJournalCode,iiPostingYear,iiPostingPeriod))
               oiReturnStatus = -3.
        <M-21 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-821842':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        Leave MAINPCREVERSALBLOCK.
    end. /* if icCompanyCode = "":U or */

    /* Get the correct company ID */
    if icCompanyCode = vcCompanyCode
    then assign viMassCompanyId = viCompanyId.
    else do:
        <Q-47 run CompanyPrim (all) (Read) (Cache)
           (input ?, (LookupCompanyId)
            input icCompanyCode, (CompanyCode)
            output dataset tqCompanyPrim) in BCompany>
        find tqCompanyPrim where
             tqCompanyPrim.tcCompanyCode = icCompanyCode
             no-lock no-error.
        if not available tqCompanyPrim
        then do:
            assign vcMessage      = substitute(#T-61'Invalid company code &1 passed to method MassReversePeriodicCostingPostings':255(823739986)T-61#, icCompanyCode)
                   oiReturnStatus = -3.
            <M-26 run SetMessage
               (input  vcMessage (icMessage), 
                input  '' (icArguments), 
                input  '' (icFieldName), 
                input  icCompanyCode (icFieldValue), 
                input  'S' (icType), 
                input  2 (iiSeverity), 
                input  '' (icRowid), 
                input  'qadfin-340074':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
            Leave MAINPCREVERSALBLOCK.
        end. /* if not available tqCompanyPrim */
        assign viMassCompanyId = tqCompanyPrim.tiCompany_ID.
    end. /* else do: */

    /* =========================================================== */
    /* Skip calculate until we cleaned up the data in the instance */
    /* =========================================================== */
    assign vlDataLoadSkipCalculate = true.

    /* ================================================================ */
    /* Call DataLoad with a free-form that looks like this: ?           */
    /* for each Posting where Posting.Company_ID = viCompanyID and      */
    /*          Posting.PostingYear = iiPostingYear and                 */
    /*          Posting.PostingPeriod = iiPostingPeriod and             */
    /*          Posting.Journal_ID = viJournalCode                      */
    /* Note the journal can be found in tqJournalByJournalCodeWithLayer */
    /* If no postings were loaded then just raise a warning and leave   */
    /* ================================================================ */
    assign vcFreeForm = "For each Posting where ":U + 
                        "Posting.Company_ID = ":U +  string(viMassCompanyId) + " and ":U + 
                        "Posting.PostingYear = ":U +  string(iiPostingYear) + " and ":U +
                        "Posting.PostingPeriod = ":U +  string(iiPostingPeriod) + " and ":U + 
                        "Posting.Journal_ID = ":U +  string(tqJournalByJournalCodeWithLayer.tiJournal_ID).
    /* =============================================================================== */
    /* In case ilOnlyReverseUnreversedOnes=true the extend the condition to make sure  */
    /* we don't load the postings that are a just a reversal of another posting.       */
    /* Note that we will still load the postings that have already been reversed: they */ 
    /* need to be taken out by another condition later on the method as we can't check */ 
    /* in a posting itself whether it has already been reversed or not                 */
    /* =============================================================================== */
    if ilOnlyReverseUnreversedOnes = true 
    then assign vcFreeForm = vcFreeForm + " and Posting.PostingIsReversing = false ":U + 
                                          " and (Posting.PostingOriginatorReference = '':U or Posting.PostingOriginatorReference = ?)":U.
    <M-22 run DataLoad
       (input  '':U (icRowids), 
        input  '':U (icPkeys), 
        input  '':U (icObjectIds), 
        input  vcFreeForm (icFreeform), 
        input  true (ilKeepPrevious), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0 and viFcReturnSuper <> -4 /* -4 means no data was read and in this case this is captured in this method itself */
    then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 and viFcReturnSuper <> -4 /* -4 means no data was read and in this case this is captured in this method itself */
    then Leave MAINPCREVERSALBLOCK.
    If not can-find (first tPosting) /* This cover return -4 from DataLoad-method */
    then do :
        /* Here we raise a warning but we still leave the block as there is no any more actions to take */
        assign oiReturnStatus = +1
               vcMessage           = trim(substitute(#T-196'No postings were found that matched entity (&1), daybook (&2), year (&3), and period (&4).  No postings were deleted, and no further action can be taken.':255(733738028)T-196#, icCompanyCode,icJournalCode,iiPostingYear,iiPostingPeriod)).
        <M-69 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-597693':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        Leave MAINPCREVERSALBLOCK.
    end. /* If not can-find (first tPosting) */
    
    /* =============================================================================== */
    /* Load also counter part of mirroring posting, which was created as part of the   */
    /* Periodic costing with activated Mirroring accounting                            */
    /* =============================================================================== */
    <M-66 run DataLoadMirrorPosting  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAINPCREVERSALBLOCK.
    
    
    /* =============================================================================== */
    /* In case ilOnlyReverseUnreversedOnes=true then we didn't load the postings that  */
    /* are a just a reversal of another posting.                                       */
    /* However; we now need to take out the postings that have already been reversed:  */
    /* they need to be taken here as we couldn't skip them to be loaded as a posting   */
    /* itself doesn't hold an indication whether it has already been reversed or not.  */
    /* Go through all loaded postings and remove the ones that are alreadey reversed.  */
    /* Raise warning and leave in case there are no postings left anymore              */
    /* =============================================================================== */
    if ilOnlyReverseUnreversedOnes = true 
    then do :
        /* No calculate was done, so fill the tcJournalCode ourselves */
        <Q-41 run JournalPrim (all) (Read) (Cache)
           (input viMassCompanyId, (CompanyId)
            input '', (JournalCode)
            input 0, (JournalID)
            output dataset tqJournalPrim) in BJournal>
        for each tPosting:
            find tqJournalPrim where
                 tqJournalPrim.tiJournal_ID = tPosting.Journal_ID
                 no-lock no-error.
            if not available tqJournalPrim
            then do:
                assign oiReturnStatus = -3
                       vcMessage      = substitute(#T-86'Daybook with ID &1 could not be found.':255(549888144)T-86#, tPosting.Journal_ID).
                <M-74 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  2 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-671076':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BPosting>                    
                leave MAINPCREVERSALBLOCK.
            end. /* if not available tqJournalPrim */
            assign tPosting.tcJournalCode = tqJournalPrim.tcJournalCode.

            /* Get the PostingOriginatorReference to look for in other postings */ 
            assign vcPostingOriginatorReference = trim(string(tPosting.PostingYear)) + "/":U + 
                                                  trim(string(tPosting.tcJournalCode)) + "/":U + 
                                                  trim(string(tPosting.PostingVoucher,"999999999":U)).
            /* Look into the db for another posting with such PostingOriginatorReference - if found then delete the current tposting as we don't need it anymore in this method */
            /* No need to look into our business-instance as we did not loaded any postngs with PostingOriginatorReference filled */
            <Q-79 run PostingByReverseOriginatorRef (all) (Read) (NoCache)
               (input tPosting.Company_ID, (CompanyId)
                input vcPostingOriginatorReference, (PostingOriginatorReference)
                input ?, (PostingIsReplacement)
                input true, (PostingIsReversing)
                input tPosting.tc_Rowid, (SkipRowid)
                output dataset tqPostingByReverseOriginatorRef) in BPosting >
            if can-find (first tqPostingByReverseOriginatorRef where 
                               tqPostingByReverseOriginatorRef.tiCompany_ID                 = tPosting.Company_ID          and 
                               tqPostingByReverseOriginatorRef.tlPostingIsReversing         = true                         and
                               tqPostingByReverseOriginatorRef.tcPostingOriginatorReference = vcPostingOriginatorReference)
            then do :
                /* Delete all detail lines of the posting before deleting the posting */
                for each tPostingLine where
                         tPostingLine.Posting_ID = tPosting.Posting_ID:
                    delete tPostingLine.
                end. /* for each tPostingLine where */
                for each tPostingSaf where
                         tPostingSaf.Posting_ID = tPosting.Posting_ID:
                    delete tPostingSaf.
                end. /* for each tPostingSaf where */
                for each tPostingVat where
                         tPostingVat.Posting_ID = tPosting.Posting_ID:
                    delete tPostingVat.
                end. /* for each tPostingVat where */
                for each tPostingWHT where
                         tPostingWHT.Posting_ID = tPosting.Posting_ID:
                    delete tPostingWHT.
                end. /* for each tPostingWHT where */
                for each tPostingVatDelay where
                         tPostingVatDelay.Posting_ID = tPosting.Posting_ID:
                    delete tPostingVatDelay.
                end. /* for each tPostingVatDelay where */

                delete tPosting.
                next.
            end. /* if can-find  */
        end. /* for each tPosting */
        If not can-find (first tPosting) 
        then do :
            /* Here we raise a warning but we still leave the block as there is no any more actions to take */
            assign oiReturnStatus = (if oiReturnStatus >= 0 then +1 else oiReturnStatus)
                   vcMessage      = trim(substitute(#T-58'No postings were found that matched entity (&1), daybook (&2), year (&3), and period (&4).  No postings were deleted, and no further action can be taken.':255(733738028)T-58#, icCompanyCode,icJournalCode,iiPostingYear,iiPostingPeriod)).
            <M-51 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'W':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-633480':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>
            Leave MAINPCREVERSALBLOCK.
        end. /* If not can-find (first tPosting) */
    end. /* if ilOnlyReverseUnreversedOnes = true  */ 

    /* ================================================= */
    /* Run calculate method that was skipped in dataload */
    /* ================================================= */
    assign vlDataLoadSkipCalculate = false.
    <M-3 run Calculate  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
    then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then Leave MAINPCREVERSALBLOCK.
END. /* MAINPCREVERSALBLOCK */