project QadFinancials > class BJournalEntry > method CreateReverseEntries

Description

Method to make a set of undo postings. List of postings that are to be undone is passed through a reference temp-table. It's an envelope method to CreateReverseEntry.


Parameters


tReverseEntriesinput-outputtemp-table
icPostingTextSuffixinputcharacterIf specified then the original Posting text will be extended with it.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BRevaluation.AdditionalUpdatesPosting


program code (program9/bjournalentry.p)

for each tReverseEntries:
    /* skip those postings already loaded in instance of component */
    if can-find(first tPosting where
                      tPosting.Posting_ID = tReverseEntries.tiOriPostingId)
    then next.

    if vcLstOriIds <> "":U
    then assign vcLstOriIds = vcLstOriIds + ",":U.

    assign vcLstOriIds = vcLstOriIds + string(tReverseEntries.tiOriPostingId).
end.

/* When all postings were read already, we don't need to load them again */
if vcLstOriIds <> "":U
then do:                 
    <M-3 run DataLoad
       (input  '':U (icRowids), 
        input  '':U (icPkeys), 
        input  vcLstOriIds (icObjectIds), 
        input  '':U (icFreeform), 
        input  true (ilKeepPrevious), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    
    if viFcReturnSuper < 0 or oiReturnStatus  = 0 then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0 then return.
end. 


/* ================================================================= */
/* All new Vouchers will be copied to tApiPosting for possible undo. */
/* ================================================================= */
empty temp-table tApiPosting.
assign viCounter = 0.

POSTING_BLOCK:
for each tReverseEntries:
    find bPosting where
         bPosting.Posting_ID = tReverseEntries.tiOriPostingId
         no-error.

    if not available bPosting
    then do:
        <M-4 run SetMessage
           (input  #T-12'Cannot retrieve loaded data.':80(2762)T-12# (icMessage), 
            input  '':U (icArguments), 
            input  'tReverseEntries.tiOriPostingId':U (icFieldName), 
            input  string(tReverseEntries.tiOriPostingId) (icFieldValue), 
            input  'S':U (icType), 
            input  2 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QADFIN-2828':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

        assign oiReturnStatus = -1.
        leave POSTING_BLOCK.
    end.

    <M-13 run AddPostingHeader
       (input  bPosting.Company_ID (iiCompanyId), 
        input  bPosting.PostingYear (iiPeriodYear), 
        input  bPosting.PostingPeriod (iiPeriodPeriod), 
        input  bPosting.tcJournalCode (icJournalCode), 
        input  '':U (icReportingJournalCode), 
        input  0 (iiVoucher), 
        input  bPosting.PostingDate (itPostingDate), 
        input  bPosting.PostingValueDate (itValueDate), 
        input  bPosting.PostingText (icPostingText), 
        input  bPosting.PostingBusinessRelationTxt (icPostingBusinessRelationText), 
        input  bPosting.PostingInvoiceReferenceTxt (icPostingInvoiceReferenceText), 
        input  bPosting.PostingParentText (icPostingParentText), 
        input  bPosting.Period_ID (iiBPeriodId), 
        input  bPosting.PostingOriginAddressCode (icPostingOriginAddressCode), 
        input  bPosting.PostingOriginDocument (icPostingOriginDocument), 
        input  bPosting.PostingOriginDocumentType (icPostingOriginDocumentType), 
        input  bPosting.PostingBatchNumber (icBatchNumber), 
        input  ? (icBankImpLineRef), 
        output viPostingId (oiPostingId), 
        output vcPostingRowId (ocRowid), 
        output viPostingVoucher (oiPostingVoucher), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper <> 0
    then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 
    then Leave POSTING_BLOCK.

    /* assign some values */
    find first tPosting where tPosting.tc_Rowid = vcPostingRowId no-error.
    if not available tPosting
    then next.

    assign tReverseEntries.tiRevPostingId       = tPosting.Posting_ID
           tPosting.PostingIsReversing          = true
           tPosting.PostingOriginatorReference  = substitute("&1/&2/&3":U, 
                                                             bPosting.PostingYear,
                                                             bPosting.tcJournalCode,
                                                             string(bPosting.PostingVoucher,"999999999":U))
           tPosting.PostingText                 = if icPostingTextSuffix <> "":U  and
                                                  icPostingTextSuffix <> "?":U and
                                                  icPostingTextSuffix <> ?
                                                  then trim(tPosting.PostingText) + " ":U + trim(icPostingTextSuffix)
                                                  else trim(tPosting.PostingText).

    create tApiPosting.

    assign viCounter                  = viCounter - 1
           tApiPosting.tc_Rowid       = string(viCounter)
           tApiPosting.Posting_ID     = tPosting.Posting_ID
           tApiPosting.PostingVoucher = tPosting.PostingVoucher
           tApiPosting.tcJournalCode  = tPosting.tcJournalCode
           tApiPosting.PostingYear    = tPosting.PostingYear
           tApiPosting.PostingYearPeriod    = tPosting.PostingYear * 100 + tPosting.PostingPeriod.
           tApiPosting.PostingText    = tPosting.PostingText.

    for each bPostingLine where
             bPostingLine.tc_ParentRowid = bPosting.tc_Rowid:

        /* create posting lines */
        empty temp-table tDefaultSafs.
        for each bPostingSaf where
                 bPostingSaf.tc_ParentRowid = bPostingLine.tc_Rowid:
            create tDefaultSafs.
            assign tDefaultSafs.tcSafCode        = bPostingSaf.tcSafCode
                   tDefaultSafs.tcSafConceptCode = bPostingSaf.tcSafConceptCode.
        end.
        <M-14 run AddStandardPosting
           (input  vcPostingRowId (icPostingtcRowid), 
            input  bPostingLine.tcGLCode (icGLCode), 
            input  bPostingLine.tcDivisionCode (icDivisionCode), 
            input  bPostingLine.tcCostCentreCode (icCostCentreCode), 
            input  '':U (icCostCentreText), 
            input  bPostingLine.tcProjectCode (icProjectCode), 
            input  bPostingLine.tcProjectDescription (icProjectText), 
            input  bPostingLine.tcIntercoBusinessRelationCode (icIntercoBusinessRelationCode), 
            input  bPostingLine.tcCurrencyCode (icCurrencyCode), 
            input  bPostingLine.PostingLineDebitTC (idDebitTC), 
            input  bPostingLine.PostingLineDebitLC (idDebitLC), 
            input  bPostingLine.PostingLineDebitCC (idDebitCC), 
            input  bPostingLine.PostingLineDebitPC (idDebitPC), 
            input  bPostingLine.PostingLineCreditTC (idCreditTC), 
            input  bPostingLine.PostingLineCreditLC (idCreditLC), 
            input  bPostingLine.PostingLineCreditCC (idCreditCC), 
            input  bPostingLine.PostingLineCreditPC (idCreditPC), 
            input  bPostingLine.PostingLineQTY (idQty), 
            input  bPostingLine.PostingLineText (icLineText), 
            input  bPostingLine.PostingLineSafText (icSafText), 
            input  tDefaultSafs (tDefaultSafs), 
            input  '':U (icExchangeRateType), 
            input  bPostingLine.PostingLineExchangeRate (idExchangeRate), 
            input  bPostingLine.PostingLineRateScale (idExchangeRateScale), 
            input  bPostingLine.PostingLineCCRate (idPostingLineCCRate), 
            input  bPostingLine.PostingLineCCScale (idPostingLineCCScale), 
            output viPostingLineId (oiPostingLineId), 
            input  0 (iiSafStructureId), 
            input  '':U (icSafStructureCode), 
            input  '':U (icAllocationKey), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 
        then Leave POSTING_BLOCK.

        /* find the tPostingLine before assign value to it */
        find first tPostingLine where tPostingLine.PostingLine_ID = viPostingLineId no-error.
        if not available tPostingLine
        then next.

        assign tPostingLine.PostingLineDebitTC  = bPostingLine.PostingLineCreditTC
                tPostingLine.PostingLineDebitLC  = bPostingLine.PostingLineCreditLC
                tPostingLine.PostingLineDebitCC  = bPostingLine.PostingLineCreditCC
                tPostingLine.PostingLineCreditTC = bPostingLine.PostingLineDebitTC
                tPostingLine.PostingLineCreditLC = bPostingLine.PostingLineDebitLC
                tPostingLine.PostingLineCreditCC = bPostingLine.PostingLineDebitCC.

        if bPostingLine.tcAllocationType = "N":U
        then assign tPostingLine.tcAllocationType = "L":U.

        for each bPostingVat where
                 bPostingVat.tc_ParentRowid = bPostingLine.tc_Rowid:
            <M-9 run AddDetailLine
               (input  'PostingVat':U (icTable), 
                input  tPostingLine.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

            if viFcReturnSuper < 0 or
               oiReturnStatus  = 0
            then assign oiReturnStatus = viFcReturnSuper.

            if oiReturnStatus < 0
            then leave POSTING_BLOCK.

            buffer-copy bPostingVat
                 except bPostingVat.PostingLine_ID
                        bPostingVat.PostingVat_ID
                        bPostingVat.tc_Rowid
                        bPostingVat.tc_ParentRowid
                        bPostingVat.tc_Status
                     to tPostingVat
                 assign tPostingVat.PostingVatBaseDebitTC  = bPostingVat.PostingVatBaseCreditTC
                        tPostingVat.PostingVatBaseDebitLC  = bPostingVat.PostingVatBaseCreditLC
                        tPostingVat.PostingVatBaseDebitCC  = bPostingVat.PostingVatBaseCreditCC
                        tPostingVat.PostingVatBaseCreditTC = bPostingVat.PostingVatBaseDebitTC
                        tPostingVat.PostingVatBaseCreditLC = bPostingVat.PostingVatBaseDebitLC
                        tPostingVat.PostingVatBaseCreditCC = bPostingVat.PostingVatBaseDebitCC.
        end.
    end.
end.

/* Release Numbers */
if oiReturnStatus < 0
then do :
    for each tApiPosting where
             tApiPosting.PostingVoucher > 0:
        /* Start&Open BNumber (AddToTrx=false) as in a loop below we will be calling BNumber repetively and this */
        /* is more performing then using vhFcComponent=?. BNumber will be closed and stopped after the loop      */
        if viBNumberFromBPostingID = 0 or viBNumberFromBPostingID = ?
        then do :
            <I-63 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "false"
                 &CLASS                = "BNumber"}>
        end. /* if viBNumberFromBPostingID = 0 or viBNumberFromBPostingID = ? */
        /* Release the number - do not catch errors of this call as we are already here in an error-situation */
        <M-11 run ReleaseNumber
           (input  tApiPosting.Company_ID (iiCompanyId), 
            input  tApiPosting.PostingYear (iiNumbrYear), 
            input  tApiPosting.tcJournalCode (icNumbrType), 
            input  tApiPosting.PostingVoucher (iiNumbr), 
            input  viFcCurrentInstanceId (iiInstanceId), 
            input  vcFcComponentName (icClassName), 
            output viFcReturnSuper (oiReturnStatus)) in BNumber>
    end. /* for each tApiPosting where */
    /* Close&Stop BNumber that is started with a different transaction before the previous loop   */
    if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ?
    then do :
        <I-78 {bFcCloseAndStopInstance
             &CLASS           = "BNumber"}>
    end. /* if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ? */
end. /* if oiReturnStatus < 0 */

empty temp-table tApiPosting.