project QadFinancials > class BDebtorBalance > method RebuildHistory

Description

RebuildHistory: method that removes (part of) the history and raises new daemon-requests for the entered selection criteria.


Parameters


icCompanyCodeinputcharacter
icDebtorCodeinputcharacter
icCurrencyCodeinputcharacter
iiFromPeriodYearinputinteger
iiFromPeriodPeriodinputinteger
tDHistRebuildResultoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program8/bdebtorbalance.p)

/* ===================================== */
/* Initialisation and Exception handling */
/* ===================================== */
assign oiReturnStatus = -98.
empty temp-table tDHistRebuildResult.

/* ================================================================================================= */
/* Validate: no transaction should be active at this moment because this method needs to do several  */
/* commits itself as every loop on tqDBalanceByCompanyDebtorCurr needs to have its own transaction */
/* ================================================================================================= */
if viTransactionID <> 0 and
   viTransactionID <> ?
then do :
    assign oiReturnStatus = -1
           vcMessage      = trim(substitute(#T-14'Internal error: the rebuild cannot be started when a transaction is already active.':255(65597)t-14#)).
    <M-1 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QadFin-7207':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
    Return.
end. /* if viTransactionID <> 0 and */

/* ============= */
/* Start Block   */
/* ============= */
MAJORBLOCK: DO :
    
    /* ========================================================================================= */
    /* Input parameter validations:                                                              */
    /* - Company and currency should exist when filled                                           */
    /* - Customer wherefor you want to do the rebuild (Optional)                                 */
    /*  (note that in case icCompanyCode was filled, the customer should exist for that company) */
    /* - Period-year from where you want to do the rebuild (Optional)                            */
    /*  (note that in case icCompanyCode was filled, the period should exist for that company)   */
    /* - Period-period from where you want to do the rebuild (Optional)                          */
    /*  (note that in case icCompanyCode was filled, the period should exist for that company)   */
    /*  (note that this paramater can only be filled in case period-year was also filled)        */
    /* ========================================================================================= */
    <M-19 run RebuildHistoryValidateInputCheckDaemon
       (input-output icCompanyCode (bcCompanyCode), 
        input-output icDebtorCode (bcDebtorCode), 
        input-output icCurrencyCode (bcCurrencyCode), 
        input-output iiFromPeriodYear (biFromPeriodYear), 
        input-output iiFromPeriodPeriod (biFromPeriodPeriod), 
        output viResolvedCompanyID (oiResolvedCompanyID), 
        output viResolvedDebtorID (oiResolvedDebtorID), 
        output viResolvedCurrencyID (oiResolvedCurrencyID), 
        output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
    if viFcReturnSuper <> 0 
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then Leave MAJORBLOCK.
    
    
    /* =========================================================== */
    /* Query call to read the DBal-records that need to be removed */
    /* =========================================================== */
    <Q-20 run DBalanceByCompanyDebtorCurr (all) (Read) (NoCache)
       (input viResolvedCompanyID, (CompanyId)
        input ?, (DBalanceID)
        input icCompanyCode, (CompanyCode)
        input icDebtorCode, (DebtorCode)
        input viResolvedDebtorID, (DebtorID)
        input icCurrencyCode, (CurrencyCode)
        input viResolvedCurrencyID, (CurrencyID)
        output dataset tqDBalanceByCompanyDebtorCurr) in BDebtorBalance >
    
    /* ========================================================== */
    /* Go through all read DBal records (Company/Debtor/Currency) */
    /* ========================================================== */
    for each tqDBalanceByCompanyDebtorCurr : 
        
        /* ============= */
        /* Start Block   */
        /* ============= */
        MINORBLOCK: DO :
            
            /* =========================================================== */
            /* Create an output record with the indication 'unknown error' */
            /* Clear the tFcMessages to make sure we start clean           */
            /* Stop the exteranl instance                                  */
            /* =========================================================== */
            empty temp-table tFcMessages.
            Create tDHistRebuildResult.
            assign tDHistRebuildResult.tcTopic  = tqDBalanceByCompanyDebtorCurr.tcCompanyCode + "/":U + tqDBalanceByCompanyDebtorCurr.tcDebtorCode + "/":U + tqDBalanceByCompanyDebtorCurr.tcCurrencyCode + ": ":U + trim(#T-35'Remove old balance':255(69687)T-35#)
                   tDHistRebuildResult.tcResult = trim(#T-15'Internal error: unknown error occurred.':255(65596)t-15#)
                   viMinorBlockReturnStatus     = 0.
            
            /* ================= */
            /* Load the instance */
            /* ================= */
            <M-5 run DataLoad
               (input  ? (icRowids), 
                input  string(tqDBalanceByCompanyDebtorCurr.tiDBalance_ID) (icPkeys), 
                input  ? (icObjectIds), 
                input  ? (icFreeform), 
                input  false (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORBLOCK.
            find tDBalance where 
                 tDBalance.DBalance_ID = tqDBalanceByCompanyDebtorCurr.tiDBalance_ID
                 no-error.
            if not available tDBalance
            then do :
                assign viMinorBlockReturnStatus = -3
                       vcMessage                = trim(substitute(#T-26'Internal error. The rebuild failed because the system cannot find the balance record based on its ID (&1).':255(69668)t-26#,string(tqDBalanceByCompanyDebtorCurr.tiDBalance_ID))).
                <M-25 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-8245':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                Leave MINORBLOCK.
            end. /* if not available tDBalance */
                 
            /* =========================================================================== */
            /* Mark the instance - or part of it in case a period was entered - as deleted */
            /* If only part of it gets deleted, then change the DHist and CBlance record   */
            /* =========================================================================== */
            if iiFromPeriodYear = 0
            then assign tDBalance.tc_Status = "D":U.
            else do :
                /* Mark the later DHists as deleted */
                for each tDHist where
                         tDHist.tc_ParentRowid       = tDBalance.tc_Rowid and 
                         tDHist.DHistYearPeriodFrom >= iiFromPeriodYear * 100 + iiFromPeriodPeriod and 
                         tDHist.tc_Status           <> "D":U :
                    assign tDHist.tc_Status = "D":U.
                end. /* for each tDHist where */

                assign tDBalance.DBalanceTC       = 0
                       tDBalance.DBalanceLC       = 0
                       tDBalance.DBalanceCC       = 0
                       tDBalance.tc_Status        = "C":U.
                
                for each tDHist where 
                         tDHist.tc_ParentRowid       = tDBalance.tc_Rowid and 
                         tDHist.tc_Status           <> "D":U 
                         break by tDHist.Company_ID
                               by tDHist.Debtor_ID
                               by tDHist.Currency_ID
                               by tDHist.Journal_ID
                               by tDHist.GL_ID
                               by tDHist.Division_ID
                               by tDHist.Project_ID
                               by tDHist.CostCentre_ID
                               by tDHist.DHistYearperiodFrom desc:
                    if first-of(tDHist.CostCentre_ID)
                    then do:
                        assign tDHist.DHistYearPeriodTill = 999999
                               tDHist.tc_Status           = "C":U
                               tDBalance.DBalanceTC       = tDBalance.DBalanceTC + tDHist.DHistBalanceTC
                               tDBalance.DBalanceLC       = tDBalance.DBalanceLc + tDHist.DHistBalanceLC
                               tDBalance.DBalanceCC       = tDBalance.DBalanceCC + tDHist.DHistBalanceCC.
                     end. /* first-of */
                end. /* For each tDHist */
            end. /* Not if iiFromPeriodYear = 0 */
        
            /* ============================================ */
            /* ValidateBC, AddUpd and DataSave (commit trx) */
            /* ============================================ */
            <M-27 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORBLOCK.
            <M-28 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORBLOCK.
            <M-29 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORBLOCK.
            
        /* ============= */
        /* End Block     */
        /* ============= */
        END. /* MINORBLOCK */
        
        /* ================================================================= */
        /* Error Handling: keep track and store any errors/warnings occured  */
        /* ================================================================= */
        if viMinorBlockReturnStatus = 0
        then assign tDHistRebuildResult.tcResult = trim(#T-30'Correctly processed':255(65598)T-30#).
        else do :
            /* Add general result to the result temp-table */
            if viMinorBlockReturnStatus > 0
            then assign tDHistRebuildResult.tcResult = trim(#T-31'Warning occurred:':255(65599)t-31#) + " ":U + string(viMinorBlockReturnStatus).
            else assign tDHistRebuildResult.tcResult = trim(#T-32'Error occurred.':255(65600)t-32#) + " ":U + string(viMinorBlockReturnStatus).
            /* Add the messages to the result temp-table */
            for each tFcMessages : 
                assign tDHistRebuildResult.tcResult = tDHistRebuildResult.tcResult + chr(10) + 
                                                      tFcMessages.tcFcMsgNumber + "; ":U + tFcMessages.tcFcMessage.
            end. /* for each tFcMessages */
            /* Put the return of this single transaction to the return of the method */
            if viMinorBlockReturnStatus < viLocalReturnStatus
            then assign viLocalReturnStatus = viMinorBlockReturnStatus.
        end. /* if viMinorBlockReturnStatus <> 0 */
        
    end. /* for each tqDBalanceByCompanyDebtorCurr */
    
    
    /* ===================== */
    /* Read the DI-movements */
    /* ===================== */
    <Q-22 run DInvoiceMovementForHistRebuild (all) (Read) (NoCache)
       (input viResolvedCompanyID, (CompanyId)
        input iiFromPeriodYear * 100 + iifromPeriodPeriod, (FromPeriodYearPeriod)
        input icCompanyCode, (CompanyCode)
        input viResolvedDebtorID, (DebtorID)
        input icDebtorCode, (DebtorCode)
        input viResolvedCurrencyID, (CurrencyID)
        input icCurrencyCode, (CurrencyCode)
        output dataset tqDInvoiceMovementForHistRebuild) in BDInvoice >
    
    /* ============================================================= */
    /* Go through all read DiMov records (Company/Debtor/Currency)   */
    /* ============================================================= */
    for each tqDInvoiceMovementForHistRebuild
        break by tqDInvoiceMovementForHistRebuild.tiCompany_ID
              by tqDInvoiceMovementForHistRebuild.tiDebtor_ID
              by tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID :
        
        /* ============================================================================ */
        /* if first-of then Create an output record with the indication 'unknown error' */
        /* Clear the tFcMessages to make sure we start clean                            */
        /* Stop the external instance                                                   */
        /* ============================================================================ */
        if first-of (tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID)
        then do :
            /* =========================================================== */
            /* Create an output record with the indication 'unknown error' */
            /* Clear the tFcMessages to make sure we start clean           */
            /* Stop the external instance and reset the counter and status */
            /* =========================================================== */
            empty temp-table tFcMessages.
            Create tDHistRebuildResult.
            assign tDHistRebuildResult.tcTopic     = tqDInvoiceMovementForHistRebuild.tcCompanyCode + "/":U + tqDInvoiceMovementForHistRebuild.tcDebtorCode + "/":U + tqDInvoiceMovementForHistRebuild.tcCurrencyCode + ": ":U + trim(#T-36'Building up new balance':255(69688)t-36#)
                   tDHistRebuildResult.tcResult    = trim(#T-34'Internal error: unknown error occurred.':255(65596)t-34#)
                   viDIMovementBlockReturnStatus   = 0
                   viQDInvoiceMovementNewRebuildSq = 0.
            if viBQDInvoiceMovementForDBalID <> 0 and
               viBQDInvoiceMovementForDBalID <> ?
            then do :
                <I-33 {bFcStopInstance
                     &CLASS           = "BQDInvoiceMovement"}>
                assign viBQDInvoiceMovementForDBalID = 0.
            end. /* if viBQDInvoiceMovementForDBalID <> 0 and */
            empty temp-table tQDInvoiceMovementNewRebuild.
        end. /* if first-of (tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID) */
                
        /* =================== */
        /* Create Q-tt-records */
        /* =================== */
        if viDIMovementBlockReturnStatus >= 0
        then do :
            create tQDInvoiceMovementNewRebuild.
            assign viQDInvoiceMovementNewRebuildSq                            = viQDInvoiceMovementNewRebuildSq + 1
                   tQDInvoiceMovementNewRebuild.tc_Rowid                      = string(viQDInvoiceMovementNewRebuildSq)
                   tQDInvoiceMovementNewRebuild.Company_ID                    = tqDInvoiceMovementForHistRebuild.tiCompany_ID
                   tQDInvoiceMovementNewRebuild.Debtor_ID                     = tqDInvoiceMovementForHistRebuild.tiDebtor_ID
                   tQDInvoiceMovementNewRebuild.Currency_ID                   = tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID
                   tQDInvoiceMovementNewRebuild.Period_ID                     = tqDInvoiceMovementForHistRebuild.tiPeriod_ID
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementDebitLC       = tqDInvoiceMovementForHistRebuild.tdPostingLineDebitLC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementCreditLC      = tqDInvoiceMovementForHistRebuild.tdPostingLineCreditLC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementDebitTC       = tqDInvoiceMovementForHistRebuild.tdPostingLineDebitTC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementCreditTC      = tqDInvoiceMovementForHistRebuild.tdPostingLineCreditTC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementDebitCC       = tqDInvoiceMovementForHistRebuild.tdPostingLineDebitCC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementCreditCC      = tqDInvoiceMovementForHistRebuild.tdPostingLineCreditCC
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementVatDebTC      = if tqDInvoiceMovementForHistRebuild.tcDInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} 
                                                                                then tqDInvoiceMovementForHistRebuild.tdDInvoiceVatDebitTC 
                                                                                else 0
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementVatCredTC     = if tqDInvoiceMovementForHistRebuild.tcDInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} 
                                                                                then tqDInvoiceMovementForHistRebuild.tdDInvoiceVatCreditTC 
                                                                                else 0
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementIsInvoice     = if tqDInvoiceMovementForHistRebuild.tcDInvoiceType = {&INVOICETYPE-INVOICE} or tqDInvoiceMovementForHistRebuild.tcDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or tqDInvoiceMovementForHistRebuild.tcDInvoiceType = {&INVOICETYPE-FINANCECHARGE}
                                                                                then true 
                                                                                else false
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementIsCreditN     = if tqDInvoiceMovementForHistRebuild.tcDInvoiceType = {&INVOICETYPE-CREDITNOTE} or tqDInvoiceMovementForHistRebuild.tcDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then true 
                                                                                else false
                   tQDInvoiceMovementNewRebuild.tdExchangeRate                = tqDInvoiceMovementForHistRebuild.tdDInvoiceExchangeRate
                   tQDInvoiceMovementNewRebuild.tdExchangeRateScale           = tqDInvoiceMovementForHistRebuild.tdDInvoiceRateScale 
                   tQDInvoiceMovementNewRebuild.ttValidityDate                = tqDInvoiceMovementForHistRebuild.ttDInvoiceTaxPointDate 
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementType          = tqDInvoiceMovementForHistRebuild.tcDInvoiceMovementType
                   tQDInvoiceMovementNewRebuild.DInvoiceMovementInfo          = trim(string(tqDInvoiceMovementForHistRebuild.tiDInvoiceMovementYearPeriod)) + '/':U + trim(tqDInvoiceMovementForHistRebuild.tcJournalCode) + '/':U + trim(string(tqDInvoiceMovementForHistRebuild.tiPostingVoucher))
                   tQDInvoiceMovementNewRebuild.GL_ID                         = tqDInvoiceMovementForHistRebuild.tiGL_ID
                   tQDInvoiceMovementNewRebuild.Division_ID                   = tqDInvoiceMovementForHistRebuild.tiDivision_ID
                   tQDInvoiceMovementNewRebuild.Project_ID                    = tqDInvoiceMovementForHistRebuild.tiProject_ID
                   tQDInvoiceMovementNewRebuild.Journal_ID                    = tqDInvoiceMovementForHistRebuild.tiJournal_ID
                   tQDInvoiceMovementNewRebuild.CostCentre_ID                 = tqDInvoiceMovementForHistRebuild.tiCostCentre_ID
                   tQDInvoiceMovementNewRebuild.MovementYearPeriod            = tqDInvoiceMovementForHistRebuild.tiDInvoiceMovementYearPeriod.
        end. /* if viDIMovementBlockReturnStatus >= 0 */
        
        /* ============================= */
        /* Actions for the last-of DIMov */
        /* ============================= */
        if last-of (tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID) 
        then do :
            /* ============================= */
            /* Call to BQDinvoiceMovement    */
            /* ============================= */
            if viDIMovementBlockReturnStatus >= 0
            then do :
                if can-find (first tQDInvoiceMovementNewRebuild)
                then do:
                    if viBQDInvoiceMovementForDBalID = 0 or
                       viBQDInvoiceMovementForDBalID = ?
                    then do :
                        <I-7 {bFcStartAndOpenInstance
                             &ADD-TO-TRANSACTION = "false"
                             &CLASS              = "BQDInvoiceMovement"}>
                    end. /* if viBQDInvoiceMovementForCBalID = 0 or */
                    else do :
                        <I-8 {bFcOpenInstance
                             &CLASS           = "BQDInvoiceMovement"}>
                    end. /* Not if viBQDInvoiceMovementForCBalID = 0 or */
                    <M-21 run ApiCreateQDInvoiceMovement
                       (input  tQDInvoiceMovementNewRebuild (tAPIQDInvoiceMovement), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BQDInvoiceMovement>
                    <I-37 {bFcCloseAndStopInstance
                         &CLASS           = "BQDInvoiceMovement"}>
                    if viExternalReturnStatus <> 0 
                    then assign viDIMovementBlockReturnStatus = viExternalReturnStatus.
                end. /* if can-find */
            end. /* if viDIMovementBlockReturnStatus >= 0 */
            /* ================================================================= */
            /* Error Handling: keep track and store any errors/warnings occured  */
            /* ================================================================= */
            if viDIMovementBlockReturnStatus = 0
            then assign tDHistRebuildResult.tcResult = trim(#T-16'Correctly processed':255(65598)T-16#).
            else do :
                /* Add general result to the result temp-table */
                if viDIMovementBlockReturnStatus > 0
                then assign tDHistRebuildResult.tcResult = trim(#T-17'Warning occurred:':255(65599)t-17#) + " ":U + string(viDIMovementBlockReturnStatus).
                else assign tDHistRebuildResult.tcResult = trim(#T-18'Error occurred.':255(65600)t-18#) + " ":U + string(viDIMovementBlockReturnStatus).
                /* Add the messages to the result temp-table */
                for each tFcMessages : 
                    assign tDHistRebuildResult.tcResult = tDHistRebuildResult.tcResult + chr(10) + 
                                                          tFcMessages.tcFcMsgNumber + "; ":U + tFcMessages.tcFcMessage.
                end. /* for each tFcMessages */
                /* Put the return of this single transaction to the return of the method */
                if viDIMovementBlockReturnStatus < viLocalReturnStatus
                then assign viLocalReturnStatus = viDIMovementBlockReturnStatus.
            end. /* if viDIMovementBlockReturnStatus <> 0 */
        end. /* if last-of (tqDInvoiceMovementForHistRebuild.tiDInvoiceCurrency_ID)  */
        
    end. /* for each tqDInvoiceMovementForHistRebuild */
    
/* ============= */
/* End Block     */
/* ============= */
END. /* MAJORBLOCK */

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.