project QadFinancials > class BPosting > method ExitInstance

Description

This method can be extended with code to execute when an instance of a business class is stopped.

Standard functionality in this method is
- Clean up instance data in the fcInstance table.
- Create or update a draft instance if applicable.

plus transaction handling
- Remove the instance from the transaction if it was added to a transaction in InitInstance.
- Abort the transaction if one was started from inside the business code.

PreCondition

This method is executed when :
- A business instance is stopped (release object + delete instance data)
(icDraftReference = "" + viCurrentDraftInstanceId = 0)
- A business instance is saved as a draft (release object + create draft)
(icDraftReference <> "")
- A draft instance is stopped (release object, delete copy of draft, release original draft)
(icDraftReference = "" + viCurrentDraftInstanceId <> 0)

This method is NOT run when a business instance is only closed (release object + keep instance data).


Parameters


icDraftReferenceinputcharacterDescription for the draft instance.
This parameter will have a value when creating a draft instance.
icDraftFormNameinputcharacterThis parameter will have a value when creating a draft instance.
ilDraftIsSharedinputlogicalThis parameter will have a value when creating a draft instance.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program/bposting.p)

    

<ANCESTOR-CODE>
    
    
    /* ================================================================= */
    /* If the instance contains uncommitted postings, do not wait for    */
    /* housekeeping to release the reserved number. Release it now.      */
    /* When saving a draft instance, store the number to protect it      */
    /* from being released by housekeeping.                              */
    /* (do not release numbr when cancelling a draft)                    */
    /* ================================================================= */
    if oiReturnStatus < 0 
    then return.
    assign viLocalReturnStatus = oiReturnStatus.
           oiReturnStatus      = -98.
    
    if viCurrentDraftInstanceId = 0
    then do :
        
        BNUMBERBLOCK : DO : 
            
            for each tPosting where
                     tPosting.PostingVoucher <> 0                                          and
                     tPosting.PostingVoucher <> ?                                          and
                     (tPosting.tc_Status = "N":U                                         or 
                      can-find(t_iPosting where
                               t_iPosting.tc_Rowid         = tPosting.tc_Rowid       and
                               (t_iPosting.PostingYear    <> tPosting.PostingYear    or
                                t_iPosting.tcJournalCode  <> tPosting.tcJournalCode  or
                                t_iPosting.PostingVoucher <> tPosting.PostingVoucher))):

                /* 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-69 {bFcStartAndOpenInstance
                         &ADD-TO-TRANSACTION   = "false"
                         &CLASS                = "BNumber"}>
                end. /* if viBNumberFromBPostingID = 0 or viBNumberFromBPostingID = ? */
                if icDraftReference = ""
                then do:
                    <M-1 run ReleaseNumber
                       (input  tPosting.Company_ID (iiCompanyId), 
                        input  tposting.PostingYear (iiNumbrYear), 
                        input  tPosting.tcJournalCode (icNumbrType), 
                        input  tPosting.PostingVoucher (iiNumbr), 
                        input  viFcCurrentInstanceId (iiInstanceId), 
                        input  vcFcComponentName (icClassName), 
                        output viFcReturnSuper (oiReturnStatus)) in BNumber>
                    /* Following test is needed in case the posting was created externaly (methods ApiStdMaintainTT()        */
                    /* and ApiStdMaintainMultiTT() in BCInvoiceJournalEntry, BDInvoiceJournalEntry and BPosting              */
                    /* If the number was provided externaly and the action was 'validate', then Transaction.AbortTransaction */
                    /* is called (end thus ExitInstance in all components) which will try to release a number that was never */
                    /* picked-up by a call towards BNumber.GetNumber()                                                       */
                    if viFcReturnSuper <> 0 and 
                       viFcReturnSuper <> -4 /* -4 means an invalid read : thus the number-record does not yet even exists */
                    then assign viBlockReturnStatus = viFcReturnSuper.
                end. /* if icDraftReference = "" */
                else do:
                    <M-2 run StoreNumber
                       (input  tPosting.Company_ID (iiCompanyID), 
                        input  tPosting.PostingYear (iiNumbrYear), 
                        input  tPosting.tcJournalCode (icNumbrType), 
                        input  tPosting.PostingVoucher (iiNumbr), 
                        output viFcReturnSuper (oiReturnStatus)) in BNumber>
                    if viFcReturnSuper <> 0 
                    then assign viBlockReturnStatus = viFcReturnSuper.
                end. /* not if icDraftReference = "" */
                if viBlockReturnStatus < 0
                then Leave BNUMBERBLOCK.
            end. /* for each tposting where */
        
            for each tposting where 
                     tPosting.PostingAddGLNbr <> 0                                           and
                     tPosting.PostingAddGLNbr <> ?                                           and
                     (tPosting.tc_Status = "N":U                                           or 
                      can-find(t_iPosting where
                               t_iPosting.tc_Rowid          = tPosting.tc_Rowid       and
                               (t_iPosting.PostingYear     <> tPosting.PostingYear    or
                                t_iPosting.PostingAddGLNbr <> tPosting.PostingAddGLNbr ))):
                                 
                /* 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-70 {bFcStartAndOpenInstance
                         &ADD-TO-TRANSACTION   = "false"
                         &CLASS                = "BNumber"}>
                end. /* if viBNumberFromBPostingID = 0 or viBNumberFromBPostingID = ? */
                if icDraftReference = ""
                then do:
                    <M-26 run ReleaseNumber
                       (input  tPosting.tiSharedCompanyId (iiCompanyId), 
                        input  tPosting.tiYearForNbr (iiNumbrYear), 
                        input  {&FIXEDJOURNALTYPE} (icNumbrType), 
                        input  tPosting.PostingAddGLNbr (iiNumbr), 
                        input  viFcCurrentInstanceId (iiInstanceId), 
                        input  vcFcComponentName (icClassName), 
                        output viFcReturnSuper (oiReturnStatus)) in BNumber>
                    /* Following test is needed in case the posting was created externaly (methods ApiStdMaintainTT()        */
                    /* and ApiStdMaintainMultiTT() in BCInvoiceJournalEntry, BDInvoiceJournalEntry and BPosting              */
                    /* If the number was provided externaly and the action was 'validate', then Transaction.AbortTransaction */
                    /* is called (end thus ExitInstance in all components) which will try to release a number that was never */
                    /* picked-up by a call towards BNumber.GetNumber()                                                       */
                    if viFcReturnSuper <> 0 and 
                       viFcReturnSuper <> -4 /* -4 means an invalid read : thus the number-record does not yet even exists */
                    then assign viBlockReturnStatus = viFcReturnSuper.
                end. /* if icDraftReference = "" */
                else do:
                    <M-87 run StoreNumber
                       (input  tPosting.tiSharedCompanyId (iiCompanyID), 
                        input  tPosting.tiYearForNbr (iiNumbrYear), 
                        input  {&FIXEDJOURNALTYPE} (icNumbrType), 
                        input  tPosting.PostingAddGLNbr (iiNumbr), 
                        output viFcReturnSuper (oiReturnStatus)) in BNumber>
                    if viFcReturnSuper <> 0 
                    then assign viBlockReturnStatus = viFcReturnSuper.
                end. /* Not if icDraftReference = "" */
                if viBlockReturnStatus < 0
                then Leave BNUMBERBLOCK.
                
            end. /* for each tposting where */
        
        END. /* BNUMBERBLOCK */
        
        /* Close&Stop BNumber that is started with a different transaction before the previous loop   */
        if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ?
        then do :
            <I-12 {bFcCloseAndStopInstance
                 &CLASS           = "BNumber"}>
        end. /* if viBNumberFromBPostingID <> 0 and viBNumberFromBPostingID <> ? */
        
        if viBlockReturnStatus <> 0 
        then assign viLocalReturnStatus = viBlockReturnStatus.
        
    end. /* if viCurrentDraftInstanceId = 0 */
    
    
    assign oiReturnStatus = viLocalReturnStatus.
    if oiReturnStatus < 0
    then return.