project QadFinancials > class BJournalEntry > method CreateReplaceEntry


Parameters


icPostingTcRowidinputcharacterrowid of the already loaded instance wherefor a new, reversing entry is required.
iiNewPeriodYearinputintegerYear of the reverse entry
iiNewPeriodPeriodinputintegerPeriod of the reverse entry
icNewJournalCodeinputcharacterJournalCode of the reverse entry
iiNewVoucherinputinteger
itNewPostingDateinputdateposting-date of the reverse posting
icNewPostingTextinputcharacterposting-text of the reverse posting
oiNewPostingIDoutputintegerPosting_ID of the new, reversed posting
iiOriginVoucherinputinteger
ilIsValidateJournalTypeinputlogicalThis parameter is used, to identify, whether method is executed from User Interface by user or not. If this method is executed from UI, additional validations for type of journal is executed.
icReportingJournalCodeinputcharacter
iiBQCrossCyPostingIDinputintegerInstance of BQCrossCyPosting
ilCallFromDaemoninputlogical
oiNewPostingLineIDoutputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ReplaceCInvoicePosting


program code (program9/bjournalentry.p)

    
    /* =================================================== */
    /* Replace uhnknown values / validate input paramaters */
    /* =================================================== */
    if icPostingTcRowid       = ? then assign icPostingTcRowid       = "":U.
    if iiNewPeriodYear        = ? then assign iiNewPeriodYear        = 0.
    if iiNewPeriodPeriod      = ? then assign iiNewPeriodPeriod      = 0.
    if icNewJournalCode       = ? then assign icNewJournalCode       = "":U.
    if iiNewVoucher           = ? then assign iiNewVoucher           = 0.
    if icNewPostingText       = ? then assign icNewPostingText       = "":U.
    if icReportingJournalCode = ? then assign icReportingJournalCode = "":U.
    
    if iiBQCrossCyPostingID <> 0 and 
       iiBQCrossCyPostingID <> ? and
       ( viBQCrossCyPostingFromPostingID = 0 or
         viBQCrossCyPostingFromPostingID = ? )
    then assign viBQCrossCyPostingFromPostingID = iiBQCrossCyPostingID.         
    
    if icPostingTcRowid  = "":U or
       iiNewPeriodYear   = 0    or
       iiNewPeriodPeriod = 0    or 
       icNewJournalCode  = "":U
    then do :
        assign vcMsgJE        = trim(#T-19'You must enter the GL calendar year, GL period, daybook, and ID of the original journal entry in order to reverse it.':255(2737)T-19#) + chr(10) + 
                                trim(substitute(#T-38'Year: &1.':255(2738)T-38#,string(iiNewPeriodYear,"9999":U))) + chr(10) + 
                                trim(substitute(#T-80'GL period: &1.':255(2739)T-80#,string(iiNewPeriodPeriod,">>99":U))) + chr(10) + 
                                trim(substitute(#T-35'Daybook: &1.':255(2740)T-35#,string(icNewJournalCode,"9999":U))) + chr(10) + 
                                trim(substitute(#T-55'Row ID of the originating journal entry: &1.':255(2741)T-55#,icPostingTcRowid))
               oiReturnStatus = -1.
        <M-70 run SetMessage
           (input  vcMsgJE (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  icPostingTcRowid (icRowid), 
            input  'qadfin-383164':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        Return.
    end. /* if icPostingTcRowid    = "":U or */
    find tPosting where 
         tPosting.tc_Rowid = icPostingTcRowid
         no-error.
    if not available tPosting
    then do : 
        assign vcMsgJE        = trim(substitute(#T-96'Internal error: cannot find the originating journal entry based on its row ID &1.':255(2742)T-96#,icPostingTcRowid))
               oiReturnStatus = -1.
        <M-49 run SetMessage
           (input  vcMsgJE (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  icPostingTcRowid (icRowid), 
            input  'qadfin-856698':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        Return.
    end. /* if not available tPosting */
    assign viPostingId = tPosting.Posting_ID.

    <Q-10 run PeriodPrim (all) (Read) (NoCache)
       (input tPosting.Company_ID, (CompanyId)
        input ?, (PeriodId)
        input iiNewPeriodYear, (PeriodYear)
        input iiNewPeriodPeriod, (PeriodPeriod)
        output dataset tqPeriodPrim) in BPeriod>
    find first tqPeriodPrim no-lock no-error.
    if not available tqPeriodPrim
    then do : 
        assign vcMsgJE        = trim(substitute(#T-86'The GL calendar year &1 and GL period &2 are invalid.':255(2743)T-86#,string(iiNewPeriodYear),string(iiNewPeriodPeriod)))
               oiReturnStatus = -1. 
        <M-47 run SetMessage
           (input  vcMsgJE (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  icPostingTcRowid (icRowid), 
            input  'qadfin-764740':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        Return.
    end. /* if not available tqPeriodPrim */
    
    /* ==================================================================================================== */
    /* Validate type of journal, which is reversed. Do this validation only from those tasks, controled     */
    /* by end user. If this is called from bussiness, he should be aware of all aspects of this operation   */
    /* ==================================================================================================== */
    if ilIsValidateJournalType = true
    then do: 
         assign vcMsgJE = "":U.
         
         <Q-72 run JournalByJournalLayer (all) (Read) (NoCache)
            (input tPosting.Company_ID, (CompanyId)
             input icNewJournalCode, (JournalCode)
             input ?, (CandoJournalTypeCode)
             input ?, (LayerCode)
             input ?, (CandoLayerTypeCode)
             input ?, (JournalID)
             input ?, (JournalIsActive)
             output dataset tqJournalByJournalAndLayer) in BJournal>
        
        find first tqJournalByJournalAndLayer no-error.
        
        if not available tqJournalByJournalAndLayer
        then assign vcMsgJE = trim(substitute(#T-13'The daybook &1 is invalid.':255(2744)T-13#, icNewJournalCode)).
        else do:
            if tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-JOURNALENTRY} and
               tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-REVALCREDITORDOC} and
               tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-REVALCREDITOR} and
               tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-REVALDEBTORDOC} and
               tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-REVALDEBTOR} and
               tqJournalByJournalAndLayer.tcJournalTypeCode <> {&JOURNALTYPE-REVALGL}
            then assign vcMsgJE = trim(substitute(#T-9'The daybook must be of type &1.':255(2745)T-9#,{&JOURNALTYPE-JOURNALENTRY-TR}, {&JOURNALTYPE-REVALCREDITORDOC-TR}, {&JOURNALTYPE-REVALCREDITOR-TR}, {&JOURNALTYPE-REVALDEBTORDOC-TR}, {&JOURNALTYPE-REVALDEBTOR-TR})) + chr(10) + 
                                  trim(substitute(#T-3'Daybook: &1.':255(2740)T-3#, icNewJournalCode)) + chr(10) + 
                                  trim(substitute(#T-57'The daybook &1 is of type &2.':255(2746)T-57#, tqJournalByJournalAndLayer.tcJournalCode,tqJournalByJournalAndLayer.tcJournalTypeCode)).        end.

        if vcMsgJE <> "":U
        then do :
            <M-18 run SetMessage
               (input  vcMsgJE (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  icPostingTcRowid (icRowid), 
                input  'qadfin-191258':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            assign oiReturnStatus = -1.
            Return.
        end. /* if vcMsgJE <> "":U */
    end. /* if ilIsCalledFromUI = true */         
         
    /* ============================================================================ */
    /* Create new posting based on original one                                     */
    /* ============================================================================ */
    <M-15 run CopyPostingToNew
       (input  icPostingTcRowid (icPostingRowId), 
        output vcNewPostingRowId (ocNewPostingRowId), 
        input  false (ilLinkedCrCyDaemonReqExists), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper <  0 then return.
   
    /* ============================================================================ */
    /* Update values of new posting                                                 */
    /* ============================================================================ */
    find tPosting where
         tPosting.tc_Rowid = vcNewPostingRowId no-error.
    if not available tqPeriodPrim
    then do: 
        assign vcMsgJE = substitute(#T-97'Cannot find newly created posting with RowId = &1.':255(69154)T-97#,vcNewPostingRowId).
        <M-94 run SetMessage
           (input  vcMsgJE (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  icPostingTcRowid (icRowid), 
            input  'qadfin-777284':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        assign oiReturnStatus = -1.
        return.
    end.

    assign tPosting.PostingOriginatorReference = substitute("&1/&2/&3":U, tPosting.PostingYear,
                                                                          tPosting.tcJournalCode,
                                                                          string(tPosting.PostingVoucher,"999999999":U))
           tPosting.PostingYear                = iiNewPeriodYear
           tPosting.PostingPeriod              = iiNewPeriodPeriod
           tPosting.PostingYearPeriod          = iiNewPeriodYear * 100 + iiNewPeriodPeriod  
           tPosting.tcJournalCode              = icNewJournalCode
           tPosting.tcReportingJournalCode     = icReportingJournalCode
           tPosting.PostingText                = icNewPostingText
           tPosting.PostingDate                = itNewPostingDate
           tPosting.PostingAddGLNbrDate        = itNewPostingDate
           tPosting.PostingVoucher             = 0 
           tPosting.PostingIsReversing         = false
           tPosting.PostingIsReplacement       = true           
           tPosting.tiOriginVoucher            = iiOriginVoucher
           oiNewPostingID                      = tPosting.Posting_ID.
 
    /* assign new posting voucher */
    if iiNewVoucher <> 0
    then assign tPosting.PostingVoucher = iiNewVoucher.
    else do:
        /* 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-26 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "false"
                 &CLASS                = "BNumber"}>
        end. /* if viBNumberFromBPostingID = 0 or viBNumberFromBPostingID = ? */
        /* Get the new number */
        <M-25 run GetNumber
           (input  tPosting.Company_ID (iiCompanyId), 
            input  tPosting.PostingYear (iiNumbrYear), 
            input  tPosting.tcJournalCode (icNumbrType), 
            output tPosting.PostingVoucher (oiNumber), 
            input  viFcCurrentInstanceId (iiInstanceId), 
            input  vcFcComponentName (icClassName), 
            output viExternalReturnStatus (oiReturnStatus)) in BNumber>
        /* Close&Stop BNumber that is started with a different transaction before the previous loop   */
        if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ?
        then do :
            <I-79 {bFcCloseAndStopInstance
                 &CLASS           = "BNumber"}>
        end. /* if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ? */
        if viExternalReturnStatus <> 0 then assign oiReturnStatus = viExternalReturnStatus.
        if viExternalReturnStatus < 0 then return.
    end. /* Not if iiNewVoucher <> 0 */

    if not can-find (tCommitNumber where
                     tCommitNumber.tcNumberType   = {&COMMITNUMBERTYPE-POSTINGVOUCHER} and
                     tCommitNumber.tcPostingRowID = tPosting.tc_Rowid)
    then do :
        create tCommitNumber.
        assign tCommitNumber.tcNumberType   = {&COMMITNUMBERTYPE-POSTINGVOUCHER}
               tCommitNumber.tcPostingRowID = tPosting.tc_Rowid.             
    end.  
    <Q-29 run GLByGLSystemType (all) (Read) (NoCache)
       (input tPosting.Company_ID, (CompanyId)
        input {&GLSYSTEMTYPE-ROUND}, (GLSystemTypeCode)
        input {&GLTYPECODE-SYST}, (GlTypeCode)
        input ?, (GlIsDivisionAccount)
        output dataset tqGLByGLSystemType) in BGL>
    
    find first tqGLByGLSystemType no-error.
    if available tqGLByGLSystemType
    then do:
        for each tPostingLine where
                 tPostingLine.tc_ParentRowid = tPosting.tc_Rowid and
                 tPostingLine.tcGLCode       = tqGLByGLSystemType.tcGLCode:
            
            for each tPostingVat where
                     tPostingVat.tc_ParentRowid = tPostingLine.tc_Rowid :
                delete tpostingvat.
            end.

            for each tPostingWHT where
                     tPostingWHT.tc_ParentRowid = tPostingLine.tc_Rowid :
                delete tPostingWHT.
            end.
        
            for each tPostingVatDelay where
                     tPostingVatDelay.tc_ParentRowid = tPostingLine.tc_Rowid :
	         delete tPostingVatDelay.
            end.
        
            for each tPostingSaf where
                     tPostingSaf.tc_ParentRowid = tPostingLine.tc_Rowid :
                delete tPostingSaf.
            end.
       
            delete tPostingLine.
        end.    
    end.

    /* If Cross-Company Posting exists then create cross company daemon request to create reverse cross-company posting */
    if NOT ilCallFromDaemon 
    then do:
        for each tPostingLine where
            tPostingLine.tc_ParentRowid = tPosting.tc_Rowid :

            /* Check if PostingLine is for Cross-company Account */   
            <Q-8 run GLById (all) (Read) (NoCache)
               (input tPosting.Company_ID, (CompanyId)
                input tPostingLine.GL_ID, (GLId)
                output dataset tqGLById) in BGL>  

            find first tqGLById where 
                       tqGLById.tiGL_ID = tPostingLine.GL_ID no-error.    
            if available tqGLById and tqGLById.tcGLTypeCode = {&GLTYPECODE-CROSS} 
            then do:
                if tPostingLine.CrossCompanyPostingLine_ID = 0 or tPostingLine.CrossCompanyPostingLine_ID = ?
                then do :
                    assign vcMsgJE = trim (#T-23'The Cross-Company information is not available yet, reversing the invoice is not possible. Please check the Cross-Company Daemon queue.':255(95096510)T-23#)
                           oiReturnStatus = -1.
                    <M-30 run SetMessage
                       (input  vcMsgJE (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue),              
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  icPostingTcRowid (icRowid), 
                        input  'qadfin-606914':U (icFcMsgNumber), 
                        input  '' (icFcExplanation), 
                        input  '' (icFcIdentification), 
                        input  '' (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                    return.
                end.
                else do:
                    /* Get the cross-Company PostingLine Detail*/ 
                    <Q-59 run PostingLineByPostingLineId (all) (Read) (NoCache)
                       (input ?, (CompanyId)
                        input tPostingLine.CrossCompanyPostingLine_ID, (PostingLineId)
                        input ?, (JournalTypeCode)
                        output dataset tqPostingLineByPostingLineId) in BPosting>

                    find first tqPostingLineByPostingLineId where 
                               tqPostingLineByPostingLineId.tiPostingLine_ID = tPostingLine.CrossCompanyPostingLine_ID 
                               no-error.                
    
                    if not available tqPostingLineByPostingLineId
                    then do :
                        assign vcMsgJE        = trim (substitute( #T-11'Internal error: the Cross-Company transaction line with ID = &1 was not found.':255(286884953)T-11#,tPostingLine.CrossCompanyPostingLine_ID))
                               vcContextText  = substitute("PostingLine_ID=&1|CrossCompanyPostingLine_ID=&2":U, tPostingLine.PostingLine_ID , tPostingLine.CrossCompanyPostingLine_ID )
                               vcContextText  = replace(vcContextText,"|":U,chr(2))
                               oiReturnStatus = -1.
                        <M-7 run SetMessage
                           (input  vcMsgJE (icMessage), 
                            input  '':U (icArguments), 
                            input  '':U (icFieldName), 
                            input  '':U (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  icPostingTcRowid (icRowid), 
                            input  'qadfin-601287':U (icFcMsgNumber), 
                            input  '' (icFcExplanation), 
                            input  '' (icFcIdentification), 
                            input  vcContextText (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                        return.
                    end.
                    else do:
                        Create tQCrossCyPostingFromBPosting.
                        Assign tQCrossCyPostingFromBPosting.Company_ID                = tPostingLine.CrossCompany_ID
                               tQCrossCyPostingFromBPosting.CrossCompanyGL_ID         = if available tqPostingLineByPostingLineId then tqPostingLineByPostingLineId.tiGL_ID else 0
                               tQCrossCyPostingFromBPosting.CrossCompanyGLIsCredit    = tPosting.PostingIsReversingBySign
                               tQCrossCyPostingFromBPosting.Currency_ID               = tPostingLine.Currency_ID
                               tQCrossCyPostingFromBPosting.OriginatorCompany_ID      = tPostingLine.Company_ID
                               tQCrossCyPostingFromBPosting.OriginatorPosting_ID      = tPostingLine.Posting_ID
                               tQCrossCyPostingFromBPosting.OriginatorPostingLine_ID  = tPostingLine.PostingLine_ID
                               tQCrossCyPostingFromBPosting.QCrossCyPostingInfo       = trim(tqPostingLineByPostingLineId.tcCompanyCode) + "/":U + tPosting.PostingText
                               tQCrossCyPostingFromBPosting.QCrossCyPosting_ID        = tPostingLine.CrossCompanyPosting_ID
                               tQCrossCyPostingFromBPosting.QCrossCyPostingAmountTC   = if(tPostingLine.PostingLineCreditTC <> 0 and tPostingLine.PostingLineCreditTC <> ?) then - tPostingLine.PostingLineCreditTC else - tPostingLine.PostingLineDebitTC
                               tQCrossCyPostingFromBPosting.QCrossCyPostingCreateDate = today
                               tQCrossCyPostingFromBPosting.QCrossCyPostingCreateTime = Time
                               tQCrossCyPostingFromBPosting.tc_Rowid                  = string(- tPostingLine.PostingLine_ID)
                               tQCrossCyPostingFromBPosting.CInvoice_ID               = 0
                               tQCrossCyPostingFromBPosting.DInvoice_ID               = 0
                               tQCrossCyPostingFromBPosting.APMatchingGL_ID           = 0
                               tQCrossCyPostingFromBPosting.ComplPostingRevPosting_ID = tPostingLine.CrossCompanyPosting_ID
                               tQCrossCyPostingFromBPosting.APMatchingDivision_ID     = 0
                               tQCrossCyPostingFromBPosting.APMatchingCostCentre_ID   = 0
                               tQCrossCyPostingFromBPosting.APMatchingProject_ID      = 0.
                    end. /* else do: */
                end. /* tPostingLine.CrossCompanyPosting_ID <> 0 and tPostingLine.CrossCompanyPosting_ID <> ? */
            end.  /* if available tqGLById and tqGLById.tcGLTypeCode = {&GLTYPECODE-CROSS} */               
        end.  /* for each tPostingLine */           
    end. /* if NOT ilCallFromDaemon */

    for each tPostingLine where
             tPostingLine.tc_ParentRowid = tPosting.tc_Rowid:
        assign tPostingLine.Posting_ID          = tPosting.Posting_ID
               tPostingLine.PostingLineText     =   icNewPostingText.
        for each tPostingVat where 
                 tPostingVat.tc_ParentRowid = tPostingLine.tc_Rowid:
            assign tPostingVat.PostingLine_ID         = tPostingLine.PostingLine_ID
                   tPostingVat.Posting_ID             = tPostingLine.Posting_ID
                   tPostingVat.PostingVatTaxPointDate = itNewPostingDate.
        end. /* for each tPostingVat where*/

        for each tPostingSaf where
                 tPostingSaf.tc_ParentRowid = tPostingLine.tc_Rowid:
            assign tPostingSaf.PostingLine_ID = tPostingLine.PostingLine_ID.
        end. /* for each tPostingSaf where */
    end. /* for each tPostingLine where */
                      
    /* ================================================== */
    /* Turn New-OI-Allocations into Linked-OI-Allocations */
    /* ================================================== */
    for each tPostingLine where
             tPostingLine.tc_ParentRowid   = tPosting.tc_Rowid and 
             tPostingLine.tcAllocationType = "N":U :
        assign tPostingLine.tcAllocationType = "L":U.
    end. /* for each tPostingLine where */

    /* get the postingline_id for the cross-company Account */
    for each tPostingLine where
             tPostingLine.tc_ParentRowid = tPosting.tc_Rowid :
        <Q-78 run GLById (all) (Read) (NoCache)
           (input tPosting.Company_ID, (CompanyId)
            input tPostingLine.GL_ID, (GLId)
            output dataset tqGLById) in BGL>     
        find first tqGLById where 
            tqGLById.tiGL_ID = tPostingLine.GL_ID no-error.    
        if available tqGLById and 
            tqGLById.tcGLTypeCode = {&GLTYPECODE-CROSS} 
        then
        oiNewPostingLineID = tPostingLine.PostingLine_ID.
    end. 
    
    /* ================================================== */
    /* Remove original (reversed) posting from instance   */
    /* ================================================== */
    <M-31 run RemovePostingFromInstance
       (input  viPostingId (iiPostingID), 
        output oiReturnStatus (oiReturnStatus)) in BJournalEntry>
    if oiReturnStatus < 0 then return.