project QadFinancials > class BAPMatching > method StopExternalInstances

Description

Stop instances that are started in AdditionalUpdates as part of current transaction


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.AdditionalUpdates
method BAPMatching.ValidateComponent


program code (program/bapmatching.p)

    /* ============================================================================================== */
    /* Stop classes in case they were started in the past                                             */
    /* For the JournalEntry, we just remove all posting-line we created without stopping the instance */
    /* ============================================================================================== */
        
    /* ============================================================================== */
    /* Set default return-status                                                      */
    /* ============================================================================== */
    assign oiReturnStatus = -98.
    
    /* ========================= */
    /* BCInvoice                 */
    /* ========================= */
    if viBCInvoiceAPMID <> 0 and
       viBCInvoiceAPMID <> ? and 
       vlBCInvoiceStart  = true
    then do:
        <I-1 {bFcStopInstance
            &CLASS           = "BCInvoice"}>
        assign viBCInvoiceAPMID  = 0 
               vlBCInvoiceStart  = false.
    end. /* if viBCInvoiceAPMID <> 0 and */
    
    /* ========================= */
    /* BQCrossCyPosting          */
    /* ========================= */
    if viBQCrossCyPostingAPMID <> 0 and
       viBQCrossCyPostingAPMID <> ? and 
       vlBQCrossCyPostingStart = true
    then do:
        <I-8 {bFcStopInstance
            &CLASS           = "BQCrossCyPosting"}>
        assign viBQCrossCyPostingAPMID  = 0 
               vlBQCrossCyPostingStart  = false.
    end. /* if viBCInvoiceAPMID <> 0 and */

    /* =============================================================================== */
    /* JournalEntry:                                                                   */
    /* - remove all posting-lines that we created in our AddtionalUpdates              */
    /* - remove any posting in our business-instance that is not our APMatchingPosting */
    /* =============================================================================== */
    if viBJournalEntryAPMID <> 0 and 
       viBJournalEntryAPMID <> ? 
    then do :
        /* ============================================================================ */
        /* Compose list with the posting-lines in t_sAPMatching that need to be removed */
        /* Hold the posting itself: compose list with the posting itself                */
        /* ============================================================================ */
        for each t_sAPMatching where 
                 t_sAPMatching.tc_Status        <> "D":U                        and
                 t_sAPMatching.APMatchingStatus  = {&APMATCHINGSTATUS-FINISHED} and 
                 t_sAPMatching.Posting_ID       <> 0                            and 
                 t_sAPMatching.Posting_ID       <> ? :
            /* Skip the ones that were already finished at load-time */
            if can-find (first t_iAPMatching where 
                               t_iAPMatching.tc_rowid         = t_sAPMatching.tc_rowid and 
                               t_iAPMatching.APMatchingStatus = t_sAPMatching.APMatchingStatus)
            then next.
            assign vcPostingIDsToKeep = vcPostingIDsToKeep + ",":U + string(t_sAPMatching.Posting_ID).
            if t_sAPMatching.CirecPostingLine_ID <> 0 and 
               t_sAPMatching.CirecPostingLine_ID <> ?
            then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatching.CirecPostingLine_ID).

            /* delete posting lines created for Op Alloc Codes */
            for each tOpAllocCodePostingLineIDs where
                     tOpAllocCodePostingLineIDs.tcAPMatchingRowID = t_sAPMatching.tc_Rowid:
                assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tOpAllocCodePostingLineIDs.tiPostingLineID).
                delete tOpAllocCodePostingLineIDs.
            end. /* for each tOpAllocCodePostingLineIDs where */
            for each t_sAPMatchingLn where 
                     t_sAPMatchingLn.tc_Status      <> "D":U and 
                     t_sAPMatchingLn.tc_ParentRowid  = t_sAPMatching.tc_Rowid :
                if t_sAPMatchingLn.PvoPostingLine_ID <> 0 and
                   t_sAPMatchingLn.PvoPostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.PvoPostingLine_ID).
                if t_sAPMatchingLn.VarRatePostingLine_ID <> 0 and
                   t_sAPMatchingLn.VarRatePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.VarRatePostingLine_ID).
                if t_sAPMatchingLn.VarUsagePostingLine_ID <> 0 and
                   t_sAPMatchingLn.VarUsagePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.VarUsagePostingLine_ID).
                if t_sAPMatchingLn.VarExchRatePostingLine_ID <> 0 and
                   t_sAPMatchingLn.VarExchRatePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.VarExchRatePostingLine_ID).
                if t_sAPMatchingLn.VarExchRatCCPostingLine_ID <> 0 and
                   t_sAPMatchingLn.VarExchRatCCPostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.VarExchRatCCPostingLine_ID).
                if t_sAPMatchingLn.NewExpensePostingLine_ID <> 0 and
                   t_sAPMatchingLn.NewExpensePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.NewExpensePostingLine_ID).
                if t_sAPMatchingLn.RevExpensePostingLine_ID <> 0 and
                   t_sAPMatchingLn.RevExpensePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLn.RevExpensePostingLine_ID).
                for each t_sAPMatchingLnTax where 
                         t_sAPMatchingLnTax.tc_Status      <> "D":U and 
                         t_sAPMatchingLnTax.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid :
                    if t_sAPMatchingLnTax.APRatePostingLine_ID <> 0 and
                       t_sAPMatchingLnTax.APRatePostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLnTax.APRatePostingLine_ID)
                                t_sAPMatchingLnTax.APRatePostingLine_ID     = 0.
                    if t_sAPMatchingLnTax.PPVPostingLine_ID <> 0 and
                       t_sAPMatchingLnTax.PPVPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLnTax.PPVPostingLine_ID)
                                t_sAPMatchingLnTax.PPVPostingLine_ID        = 0.
                    if t_sAPMatchingLnTax.NormalTaxPostingLine_ID <> 0 and
                       t_sAPMatchingLnTax.NormalTaxPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLnTax.NormalTaxPostingLine_ID)
                                t_sAPMatchingLnTax.NormalTaxPostingLine_ID  = 0.
                    if t_sAPMatchingLnTax.AbsRetTaxPostingLine_ID <> 0 and
                       t_sAPMatchingLnTax.AbsRetTaxPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLnTax.AbsRetTaxPostingLine_ID)
                                t_sAPMatchingLnTax.AbsRetTaxPostingLine_ID  = 0.
                end. /* for each t_sAPMatchingLnTax where */
                for each t_sAPMatchingLnRevTax where 
                         t_sAPMatchingLnRevTax.tc_Status       = "N":U                    and 
                         t_sAPMatchingLnRevTax.tc_ParentRowid  = t_sAPMatchingLn.tc_Rowid and 
                         t_sAPMatchingLnRevTax.PostingLine_ID <> ?                        and
                         t_sAPMatchingLnRevTax.PostingLine_ID <> 0 :
                    assign vcPostingLineIDsToDelete        = vcPostingLineIDsToDelete + ",":U + string(t_sAPMatchingLnRevTax.PostingLine_ID).
                    delete t_sAPMatchingLnRevTax.
                end. /* for each t_sAPMatchingLnTax where */
            end. /* for each t_sAPMatchingLn where */
        end. /* for each t_sAPMatching where */
        /* ============================================================================ */
        /* Compose list with the posting-lines in tAPMatching that need to be removed   */
        /* ============================================================================ */
        for each tAPMatching where 
                 tAPMatching.tc_Status        <> "D":U                          and
                 tAPMatching.APMatchingStatus  = {&APMATCHINGSTATUS-FINISHED}   and 
                 tAPMatching.Posting_ID       <> 0                              and 
                 tAPMatching.Posting_ID       <> ? :
            /* Skip the ones that were already finished at load-time */
            if can-find (first t_iAPMatching where 
                               t_iAPMatching.tc_rowid         = tAPMatching.tc_rowid and 
                               t_iAPMatching.APMatchingStatus = tAPMatching.APMatchingStatus)
            then next.
            assign vcPostingIDsToKeep = vcPostingIDsToKeep + ",":U + string(tAPMatching.Posting_ID).
            if tAPMatching.CirecPostingLine_ID <> 0 and 
               tAPMatching.CirecPostingLine_ID <> ?
            then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatching.CirecPostingLine_ID).
            for each tAPMatchingLn where 
                     tAPMatchingLn.tc_Status      <> "D":U and 
                     tAPMatchingLn.tc_ParentRowid  = tAPMatching.tc_Rowid :
                if tAPMatchingLn.PvoPostingLine_ID <> 0 and
                   tAPMatchingLn.PvoPostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.PvoPostingLine_ID).
                if tAPMatchingLn.VarRatePostingLine_ID <> 0 and
                   tAPMatchingLn.VarRatePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.VarRatePostingLine_ID).
                if tAPMatchingLn.VarUsagePostingLine_ID <> 0 and
                   tAPMatchingLn.VarUsagePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.VarUsagePostingLine_ID).
                if tAPMatchingLn.VarExchRatePostingLine_ID <> 0 and
                   tAPMatchingLn.VarExchRatePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.VarExchRatePostingLine_ID).
                if tAPMatchingLn.VarExchRatCCPostingLine_ID <> 0 and
                   tAPMatchingLn.VarExchRatCCPostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.VarExchRatCCPostingLine_ID).
                if tAPMatchingLn.NewExpensePostingLine_ID <> 0 and
                   tAPMatchingLn.NewExpensePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.NewExpensePostingLine_ID).
                if tAPMatchingLn.RevExpensePostingLine_ID <> 0 and
                   tAPMatchingLn.RevExpensePostingLine_ID <> ?
                then assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLn.RevExpensePostingLine_ID).
                for each tAPMatchingLnTax where 
                         tAPMatchingLnTax.tc_Status      <> "D":U and 
                         tAPMatchingLnTax.tc_ParentRowid  = tAPMatchingLn.tc_Rowid :
                    if tAPMatchingLnTax.APRatePostingLine_ID <> 0 and
                       tAPMatchingLnTax.APRatePostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLnTax.APRatePostingLine_ID)
                                tAPMatchingLnTax.APRatePostingLine_ID     = 0.
                    if tAPMatchingLnTax.PPVPostingLine_ID <> 0 and
                       tAPMatchingLnTax.PPVPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLnTax.PPVPostingLine_ID)
                                tAPMatchingLnTax.PPVPostingLine_ID        = 0.
                    if tAPMatchingLnTax.NormalTaxPostingLine_ID <> 0 and
                       tAPMatchingLnTax.NormalTaxPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLnTax.NormalTaxPostingLine_ID)
                                tAPMatchingLnTax.NormalTaxPostingLine_ID  = 0.
                    if tAPMatchingLnTax.AbsRetTaxPostingLine_ID <> 0 and
                       tAPMatchingLnTax.AbsRetTaxPostingLine_ID <> ?
                    then assign vcPostingLineIDsToDelete                    = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLnTax.AbsRetTaxPostingLine_ID)
                                tAPMatchingLnTax.AbsRetTaxPostingLine_ID  = 0.
                end. /* for each tAPMatchingLnTax where */
                for each tAPMatchingLnRevTax where 
                         tAPMatchingLnRevTax.tc_Status       = "N":U                  and 
                         tAPMatchingLnRevTax.tc_ParentRowid  = tAPMatchingLn.tc_Rowid and 
                         tAPMatchingLnRevTax.PostingLine_ID <> ?                      and
                         tAPMatchingLnRevTax.PostingLine_ID <> 0 :
                    assign vcPostingLineIDsToDelete = vcPostingLineIDsToDelete + ",":U + string(tAPMatchingLnRevTax.PostingLine_ID).
                    find t_sAPMatchingLnRevTax where
                         t_sAPMatchingLnRevTax.tc_rowid = tAPMatchingLnRevTax.tc_rowid
                         no-error.
                    if available t_sAPMatchingLnRevTax
                    then delete t_sAPMatchingLnRevTax.
                    delete tAPMatchingLnRevTax.
                end. /* for each tAPMatchingLnTax where */
            end. /* for each tAPMatchingLn where */
        end. /* for each tAPMatching where */
        /* ===================================================================================================== */
        /* If - with a previous save that failed because the PostSave returned an error - a posting was made in  */
        /* method AdditionalUpdates and not by the UI, then the t_sAPMatching.Posting_ID is still empty and the  */
        /* tAPMatching.Posting_ID is filled.                                                                     */
        /* We need to remove that posting as we would otherwise create a second posting and get stuck with the   */
        /* first posting that has got no posting-lines                                                           */
        /* We have to do this here because we cannot call StopExternalInstances from method PostSave because     */
        /* that would result in problems with the transaction                                                    */
        /* ===================================================================================================== */
        for each tAPMatching where 
                 tAPMatching.tc_Status        <> "D":U                        and
                 tAPMatching.tc_Status        <> "":U                         and
                 tAPMatching.APMatchingStatus  = {&APMATCHINGSTATUS-FINISHED} and 
                 tAPMatching.Posting_ID       <> 0                            and 
                 tAPMatching.Posting_ID       <> ?,
            each t_sAPMatching where 
                 t_sAPMatching.tc_Rowid    = tAPMatching.tc_Rowid and 
                 t_sAPMatching.Posting_ID <> tAPMatching.Posting_ID :
            /* Skip the ones that were already finished at load-time */
            if can-find (first t_iAPMatching where 
                               t_iAPMatching.tc_rowid         = t_sAPMatching.tc_rowid and 
                               t_iAPMatching.APMatchingStatus = t_sAPMatching.APMatchingStatus)
            then next.
            /* Add the PostingID to the list of postings that will be deleted from the instance and set the posting-ID on the instance to zero */
            assign vcPostingIDsToDelete   = vcPostingIDsToDelete + ",":U + string(tAPMatching.Posting_ID)
                   tAPMatching.Posting_ID = 0.
            /* Release the number of the posting */
            if tAPMatching.tiPostingVoucher <> ? and
               tAPMatching.tiPostingVoucher <> 0 and 
               tAPMatching.tiPeriodYear     <> ? and 
               tAPMatching.tiPeriodYear     <> 0 and 
               tAPMatching.tcJournalCode    <> ? and 
               tAPMatching.tcJournalCode    <> "":U
            then do :
                assign vhFcComponent = ?.
                <M-15 run ReleaseNumber
                   (input  tAPMatching.Company_ID (iiCompanyId), 
                    input  tAPMatching.tiPeriodYear (iiNumbrYear), 
                    input  tAPMatching.tcJournalCode (icNumbrType), 
                    input  tAPMatching.tiPostingVoucher (iiNumbr), 
                    input  viFcCurrentInstanceId (iiInstanceId), 
                    input  vcFcComponentName (icClassName), 
                    output viFcReturnSuper (oiReturnStatus)) in BNumber>
               /* Do not check on an error of ReleaseNumber as the number will then be reset by the housekeeping anyway and this method might return -4 inn case of an invalid read: thus the number-record does not exist anyway */
            end. /* if tAPMatching.tiPostingVoucher <> ? and */
        end. /* for each tAPMatching */
        /* ================================================================================================ */
        /* Call the method in BJournalEntry that will remove the posting and/or posting-lines               */           
        /* If a PostingID is in vcPostingIDsToKeep and in vcPostingLineIDsToDelete then it will be deleted  */
        /* ================================================================================================ */            
        if (vcPostingIDsToKeep       <> "":U and 
            vcPostingIDsToKeep       <> ?) or 
           (vcPostingIDsToDelete     <> "":U and 
            vcPostingIDsToDelete     <> ?) or
           (vcPostingLineIDsToDelete <> "":U and 
            vcPostingLineIDsToDelete <> ?)
        then do :
            if vcPostingIDsToKeep <> "":U and 
               vcPostingIDsToKeep <> ?
            then assign vcPostingIDsToKeep = substring(vcPostingIDsToKeep,2,-1,"character":U).
            if vcPostingIDsToDelete <> "":U and 
               vcPostingIDsToDelete <> ?
            then assign vcPostingIDsToDelete = substring(vcPostingIDsToDelete,2,-1,"character":U).
            if vcPostingLineIDsToDelete <> "":U and 
               vcPostingLineIDsToDelete <> ?
            then assign vcPostingLineIDsToDelete = substring(vcPostingLineIDsToDelete,2,-1,"character":U).
            <I-9 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
            <M-10 run MarkInstanceDataAsDeletedExtended
               (input  vcPostingIDsToDelete (icPostingIDToDelete), 
                input  vcPostingLineIDsToDelete (icPostingLineIDToDelete), 
                input  vcPostingIDsToKeep (icPostingIDToKeep), 
                output viExternalReturnStatus (oiReturnStatus)) in BJournalEntry>
            <I-11 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
            if viExternalReturnStatus < 0 /* only check for errors */
            then do :
                assign oiReturnStatus  = viExternalReturnStatus
                       vcMsgAPMatching = trim(#T-13'Internal error: technical problems occurred while clearing the posting instance.':254(56459)T-13#) + chr(10) + 
                                         trim(substitute(#T-14'Technical information: This error occurred while running method &1 in class &2.':244(56458)T-14#,"MarkInstanceDataAsDeletedExtended":U,"BJournalEntry":U)).
                <M-12 run SetMessage
                   (input  vcMsgAPMatching (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-6913':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
                Return.
            end. /* if viExternalReturnStatus < 0 */
        end. /* if vcPostingIDsToKeep <> "":U or */
    end. /* if viBJournalEntryAPMID <> 0 and */
    
    /* ============================================================================================ */
    /* As we encountered an error if this method is executed, we will also empty all instance-less  */ 
    /* temp-tables that are used in input for calls in other classes (cfr AdditionalUpdates + subs) */
    /* Note: You can find the same piece of code in method AdditionalUpdates                        */
    /* ============================================================================================ */
    empty temp-table tExtAllocateCInvoiceAPM.
    empty temp-table tExtQCrossCyPostingAPM.
    empty temp-table tExtCInvoiceVat.
    empty temp-table tExtCInvoiceWHT.
        
    /* ============================= */
    /* Set return-status = OK        */
    /* ============================= */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.
    
<ANCESTOR-CODE>