project QadFinancials > class BDInvoice > 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/bdinvoice.p)

<ANCESTOR-CODE>

    /* ================================================================= */
    /* If the instance contains uncommitted dinvoices, 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 viCurrentDraftInstanceId = 0
    then for each tDInvoice where
             tDInvoice.tc_Status = "N":U and
             tDInvoice.DInvoiceVoucher <> 0 and
             tDInvoice.DInvoiceVoucher <> ?:
             
        if vlJOurnalQueryStarted = FALSE
        then do:
            <Q-64 run JournalBeginsForJEDefault  (Start) in BJournal >
            assign vlJOurnalQueryStarted = TRUE.
        end. /* if vlJOurnalQueryStarted = FALSE */     
             
        <Q-97 run JournalBeginsForJEDefault (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input tDInvoice.tcJournalCode, (JournalCode)
            input {&JOURNALCONTROL-LOGISTIC}, (JournalControl)
            input '':U, (JournalTypeCode)
            output dataset tqJournalBeginsForJEDefault) in BJournal >

        /* Obtain the type ofthe daybook to be used. */
        find first tqJournalBeginsForJEDefault where
                   tqJournalBeginsForJEDefault.tcJournalCode    = tDInvoice.tcJournalCode and
                   tqJournalBeginsForJEDefault.tcJournalControl = {&JOURNALCONTROL-LOGISTIC}
        no-lock no-error.
        
        if available tqJournalBeginsForJEDefault
        then
            assign vlJournalIsOperational = TRUE.
        else
            assign vlJournalIsOperational = FALSE.
                  
        assign vhFcComponent = ?.
        if icDraftReference = ""
        then do:
           /* Release the voucher number */
            if vlCCNIsConsecutNr      = FALSE or
               vlJournalIsOperational = TRUE
            then do:
                <M-1 run ReleaseNumber
                   (input  viCompanyId (iiCompanyId), 
                    input  tDInvoice.DInvoicePostingYear (iiNumbrYear), 
                    input  tDInvoice.tcJournalCode (icNumbrType), 
                    input  tDInvoice.DInvoiceVoucher (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 = -4 /* -4 means an invalid read : thus the number-record does not yet even exists */
                then assign viFcReturnSuper = 0.
            end. /* if vlCCNIsConsecutNr = FALSE */
        end.
        else do:
            if vlCCNIsConsecutNr      = FALSE or
               vlJournalIsOperational = TRUE
            then do:
                <M-3 run StoreNumber (input  viCompanyId (iiCompanyID), 
                          input  tDInvoice.DInvoicePostingYear (iiNumbrYear), 
                          input  tDInvoice.tcJournalCode (icNumbrType), 
                          input  tDInvoice.DInvoiceVoucher (iiNumbr), 
                          output viFcReturnSuper (oiReturnStatus)) in BNumber>
             end. /* if vlCCNIsConsecutNr = FALSE */
        end.
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
        then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then return.
    end.

    if viCurrentDraftInstanceId = 0
    then for each tDInvoice where
             tDInvoice.tc_Status = "N":U and
             tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTE} and
             tDInvoice.tiDAVoucher <> 0 and
             tDInvoice.tiDAVoucher <> ?:
             
        if vlJOurnalQueryStarted = FALSE
        then do:
            <Q-60 run JournalBeginsForJEDefault  (Start) in BJournal >
            assign vlJOurnalQueryStarted = TRUE.
        end. /* if vlJOurnalQueryStarted = FALSE */      
        
        /* Obtain the type of daybook to be used. */
        <Q-61 run JournalBeginsForJEDefault (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input tDInvoice.tcDAJournalCode, (JournalCode)
            input {&JOURNALCONTROL-LOGISTIC}, (JournalControl)
            input '':U, (JournalTypeCode)
            output dataset tqJournalBeginsForJEDefault) in BJournal >


        find first tqJournalBeginsForJEDefault where
                   tqJournalBeginsForJEDefault.tcJournalCode    = tDInvoice.tcDAJournalCode and
                   tqJournalBeginsForJEDefault.tcJournalControl = {&JOURNALCONTROL-LOGISTIC}
        no-lock no-error.
        
        if available tqJournalBeginsForJEDefault
        then
            assign vlJournalIsOperational = TRUE.
        else
            assign vlJournalIsOperational = FALSE.      
             
        assign vhFcComponent = ?.
        if icDraftReference = ""
        then do:
            /* Release the voucher number. */
            if vlCCNIsConsecutNr = FALSE or
               vlJournalIsOperational = TRUE
            then do:
                <M-2 run ReleaseNumber
                   (input  viCompanyId (iiCompanyId), 
                    input  tDInvoice.DInvoicePostingYear (iiNumbrYear), 
                    input  tDInvoice.tcDAJournalCode (icNumbrType), 
                    input  tDInvoice.tiDAVoucher (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 = -4 /* -4 means an invalid read : thus the number-record does not yet even exists */
                then assign viFcReturnSuper = 0.
            end. /* if vlCCNIsConsecutNr = FALSE */
        end.     /* if icDraftReference = ""     */
        else do:
            if vlCCNIsConsecutNr = FALSE or
               vlJournalIsOperational = TRUE
            then do:
                <M-4 run StoreNumber (input  viCompanyId (iiCompanyID), 
                          input  tDInvoice.DInvoicePostingYear (iiNumbrYear), 
                          input  tDInvoice.tcDAJournalCode (icNumbrType), 
                          input  tDInvoice.tiDAVoucher (iiNumbr), 
                          output viFcReturnSuper (oiReturnStatus)) in BNumber>
            end. /* if vlCCNIsConsecutNr = FALSE */
        end.     /* NOT if icDraftReference = "" */
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
        then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then return.
    end. /* if viCurrentDraftInstanceId = 0 */
    
    
    if vlJOurnalQueryStarted = TRUE
    then do:
        <Q-82 run JournalBeginsForJEDefault  (Stop) in BJournal >
        assign vlJOurnalQueryStarted = FALSE.
    end. /* if vlJOurnalQueryStarted = TRUE */