project QadFinancials > class BCreditorBalance > method RebuildHistory

Description

This method will remove (part of) the history and raise new daemon-requests for the entered selection criteria.


Parameters


icCompanyCodeinputcharacterCompany wherefor you want to do the rebuild (Optional)
icCreditorCodeinputcharacterSupplier wherefor you want to do the rebuild (Optional) - note that in case icCompanyCode was filled, the supplier shoudl exist for that company.
icCurrencyCodeinputcharacterCurrency wherefor you want to do the rebuild (Optional)
iiFromPeriodYearinputintegerPeriod-year from where you want to do the rebuild (Optional) - note that in case icCompanyCode was filled, the period should exist for that company.
iiFromPeriodPeriodinputintegerPeriod-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
tCHistRebuildResultoutputtemp-tableTemp table with the rebuild result
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program8/bcreditorbalance.p)

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

/* ================================================================================================= */
/* Validate: no transaction should be active at this moment because this method needs to do several  */
/* commits itself as every loop on tqCBalanceByCompanyCreditorCurr needs to have its own transaction */
/* ================================================================================================= */
if viTransactionID <> 0 and
   viTransactionID <> ?
then do :
    assign oiReturnStatus = -1
           vcMessage      = trim(substitute(#T-5'Internal error: the rebuild cannot be started when a transaction is already active.':255(65597)t-5#)).
    <M-4 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-7203':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
    Return.
end. /* if viTransactionID <> 0 and */

/* ============= */
/* Start Block   */
/* ============= */
MAJORBLOCK: DO :
    
    /* ========================================================================================= */
    /* Input parameter validations:                                                              */
    /* - Company and currency should exist when filled                                           */
    /* - Supplier wherefor you want to do the rebuild (Optional)                                 */
    /*  (note that in case icCompanyCode was filled, the supplier shoudl 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-1 run RebuildHistoryValidateInputCheckDaemon
       (input-output icCompanyCode (bcCompanyCode), 
        input-output icCreditorCode (bcCreditorCode), 
        input-output icCurrencyCode (bcCurrencyCode), 
        input-output iiFromPeriodYear (biFromPeriodYear), 
        input-output iiFromPeriodPeriod (biFromPeriodPeriod), 
        output viResolvedCompanyID (oiResolvedCompanyID), 
        output viResolvedCreditorID (oiResolvedCreditorID), 
        output viResolvedCurrencyID (oiResolvedCurrencyID), 
        output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
    if viFcReturnSuper <> 0 
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then Leave MAJORBLOCK.
    
    
    /* =========================================================== */
    /* Query call to read the CBal-records that need to be removed */
    /* =========================================================== */
    <Q-2 run CBalanceByCompanyCreditorCurr (all) (Read) (NoCache)
       (input viResolvedCompanyID, (CompanyID)
        input viResolvedCreditorID, (CreditorID)
        input viResolvedCurrencyID, (CurrencyID)
        input ?, (CBalanceID)
        input icCompanyCode, (CompanyCode)
        input icCreditorCode, (CreditorCode)
        input icCurrencyCode, (CurrencyCode)
        output dataset tqCBalanceByCompanyCreditorCurr) in BCreditorBalance >
    
    /* ============================================================ */
    /* Go through all read CBal records (Company/Creditor/Currency) */
    /* ============================================================ */
    for each tqCBalanceByCompanyCreditorCurr : 
        
        /* ============= */
        /* Start Block   */
        /* ============= */
        MINORONEBLOCK: DO :
            
            /* =========================================================== */
            /* Create an output record with the indication 'unknown error' */
            /* Clear the tFcMessages to make sure we start clean           */
            /* =========================================================== */
            empty temp-table tFcMessages.
            Create tCHistRebuildResult.
            assign tCHistRebuildResult.tcTopic  = tqCBalanceByCompanyCreditorCurr.tcCompanyCode + "/":U + tqCBalanceByCompanyCreditorCurr.tcCreditorCode + "/":U + tqCBalanceByCompanyCreditorCurr.tcCurrencyCode + ": ":U + trim(#T-32'Remove old balance':255(69685)T-32#)
                   tCHistRebuildResult.tcResult = trim(#T-3'Internal error: unknown error occurred.':255(65596)t-3#)
                   viMinorBlockOneReturnStatus     = 0.
            
            /* ================= */
            /* Load the instance */
            /* ================= */
            <M-6 run DataLoad
               (input  ? (icRowids), 
                input  string(tqCBalanceByCompanyCreditorCurr.ticbalance_ID) (icPkeys), 
                input  ? (icObjectIds), 
                input  ? (icFreeform), 
                input  false (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockOneReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORONEBLOCK.
            find tCBalance where 
                 tCBalance.CBalance_ID = tqCBalanceByCompanyCreditorCurr.tiCBalance_ID
                 no-error.
            if not available tCBalance
            then do :
                assign viMinorBlockOneReturnStatus = -3
                       vcMessage                = trim(substitute(#T-24'Internal error. The rebuild failed because the system cannot find the balance record based on its ID (&1).':255(69669)t-24#,string(tqCBalanceByCompanyCreditorCurr.ticbalance_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-8246':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
                Leave MINORONEBLOCK.
            end. /* if not available tCBalance */
                
            /* =========================================================================== */
            /* 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 CHist and CBlance record   */
            /* =========================================================================== */
            if iiFromPeriodYear = 0
            then assign tCBalance.tc_Status = "D":U.
            else do :
                /* Mark the later CHists as deleted */
                for each tCHist where
                         tCHist.tc_ParentRowid       = tCBalance.tc_Rowid and 
                         tCHist.CHistYearPeriodFrom >= iiFromPeriodYear * 100 + iiFromPeriodPeriod and 
                         tCHist.tc_Status           <> "D":U :
                    assign tCHist.tc_Status = "D":U.
                end. /* for each tCHist where */
                assign tCBalance.CBalanceTC       = 0
                       tCBalance.CBalanceLC       = 0
                       tCBalance.CBalanceCC       = 0
                       tCBalance.tc_Status        = "C":U.

                for each tCHist where 
                         tCHist.tc_ParentRowid       = tCBalance.tc_Rowid and 
                         tCHist.tc_Status           <> "D":U 
                         break by tCHist.Company_ID
                               by tCHist.Creditor_ID
                               by tCHist.Currency_ID
                               by tCHist.Journal_ID
                               by tCHist.GL_ID
                               by tCHist.Division_ID
                               by tCHist.Project_ID
                               by tCHist.CostCentre_ID
                               by tCHist.CHistYearperiodFrom desc:
                    if first-of(tCHist.CostCentre_ID)
                    then do:
                        assign tCHist.CHistYearPeriodTill = 999999
                               tCHist.tc_Status           = "C":U
                               tCBalance.CBalanceTC       = tCBalance.CBalanceTC + tCHist.CHistBalanceTC
                               tCBalance.CBalanceLC       = tCBalance.Cbalancelc + tCHist.CHistBalanceLC
                               tCBalance.CBalanceCC       = tCBalance.CBalanceCC + tCHist.CHistBalanceCC.
                               tCBalance.tc_Status        = "C":U.
                    end. /* first-of */
                end. /* for each tCHist where */
            end. /* Not if iiFromPeriodYear = 0 */
                            
            /* ============================================ */
            /* ValidateBC, AddUpd and DataSave (commit trx) */
            /* ============================================ */
            <M-26 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockOneReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORONEBLOCK.
            <M-27 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockOneReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORONEBLOCK.
            <M-28 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BCreditorBalance>
            if viFcReturnSuper <> 0 
            then assign viMinorBlockOneReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave MINORONEBLOCK.
            
        /* ============= */
        /* End Block     */
        /* ============= */
        END. /* MINORONEBLOCK */
        
        /* ================================================================= */
        /* Error Handling: keep track and store any errors/warnings occured  */
        /* ================================================================= */
        if viMinorBlockOneReturnStatus = 0
        then assign tCHistRebuildResult.tcResult = trim(#T-29'Correctly processed':255(65598)T-29#).
        else do :
            /* Add general result to the result temp-table */
            if viMinorBlockOneReturnStatus > 0
            then assign tCHistRebuildResult.tcResult = trim(#T-30'Warning occurred:':255(65599)t-30#) + " ":U + string(viMinorBlockOneReturnStatus).
            else assign tCHistRebuildResult.tcResult = trim(#T-31'Error occurred.':255(65600)t-31#) + " ":U + string(viMinorBlockOneReturnStatus).
            /* Add the messages to the result temp-table */
            for each tFcMessages : 
                assign tCHistRebuildResult.tcResult = tCHistRebuildResult.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 viMinorBlockOneReturnStatus < viLocalReturnStatus
            then assign viLocalReturnStatus = viMinorBlockOneReturnStatus.
        end. /* if viMinorBlockOneReturnStatus <> 0 */
        
    end. /* for each tqCBalanceByCompanyCreditorCurr */
    
    
    /* ===================== */
    /* Read the CI-movements */
    /* ===================== */
    <Q-10 run CInvoiceMovementForHistRebuild (all) (Read) (NoCache)
       (input viResolvedCompanyID, (CompanyId)
        input viResolvedCreditorID, (CreditorID)
        input iiFromPeriodYear * 100 + iiFromPeriodPeriod, (FromPeriodYearPeriod)
        input viResolvedCurrencyID, (CurrencyID)
        input icCompanyCode, (CompanyCode)
        input icCreditorCode, (CreditorCode)
        input icCurrencyCode, (CurrencyCode)
        output dataset tqCInvoiceMovementForHistRebuild) in BCInvoice >
    
    /* ============================================================= */
    /* Go through all read CiMov records (Company/Creditor/Currency) */
    /* ============================================================= */
    for each tqCInvoiceMovementForHistRebuild
        break by tqCInvoiceMovementForHistRebuild.tiCompany_ID
              by tqCInvoiceMovementForHistRebuild.tiCreditor_ID
              by tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_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 (tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_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 tCHistRebuildResult.
            assign tCHistRebuildResult.tcTopic     = tqCInvoiceMovementForHistRebuild.tcCompanyCode + "/":U + tqCInvoiceMovementForHistRebuild.tcCreditorCode + "/":U + tqCInvoiceMovementForHistRebuild.tcCurrencyCode + ": ":U + trim(#T-35'Building up new balance.':255(69686)t-35#)
                   tCHistRebuildResult.tcResult    = trim(#T-36'Internal error: unknown error occurred.':255(65596)t-36#)
                   viCIMovementBlockReturnStatus   = 0
                   viQCInvoiceMovementNewRebuildSq = 0.
            if viBQCInvoiceMovementForCBalID <> 0 and
               viBQCInvoiceMovementForCBalID <> ?
            then do :
                <I-34 {bFcStopInstance
                     &CLASS           = "BQCInvoiceMovement"}>
                assign viBQCInvoiceMovementForCBalID = 0.
            end. /* if viBQCInvoiceMovementForCBalID <> 0 and */
            empty temp-table tQCInvoiceMovementNewRebuild.
        end. /* if first-of (tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_ID) */
        
        /* =================== */
        /* Create Q-tt-records */
        /* =================== */
        if viCIMovementBlockReturnStatus >= 0
        then do :
            create tQCInvoiceMovementNewRebuild.
            assign viQCInvoiceMovementNewRebuildSq                            = viQCInvoiceMovementNewRebuildSq + 1
                   tQCInvoiceMovementNewRebuild.tc_Rowid                      = string(viQCInvoiceMovementNewRebuildSq)
                   tQCInvoiceMovementNewRebuild.Company_ID                    = tqCInvoiceMovementForHistRebuild.tiCompany_ID
                   tQCInvoiceMovementNewRebuild.Creditor_ID                   = tqCInvoiceMovementForHistRebuild.tiCreditor_ID
                   tQCInvoiceMovementNewRebuild.Currency_ID                   = tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_ID
                   tQCInvoiceMovementNewRebuild.Period_ID                     = tqCInvoiceMovementForHistRebuild.tiPeriod_ID
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementDebitLC       = tqCInvoiceMovementForHistRebuild.tdPostingLineDebitLC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementCreditLC      = tqCInvoiceMovementForHistRebuild.tdPostingLineCreditLC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementDebitTC       = tqCInvoiceMovementForHistRebuild.tdPostingLineDebitTC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementCreditTC      = tqCInvoiceMovementForHistRebuild.tdPostingLineCreditTC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementDebitCC       = tqCInvoiceMovementForHistRebuild.tdPostingLineDebitCC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementCreditCC      = tqCInvoiceMovementForHistRebuild.tdPostingLineCreditCC
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementVatDebTC      = if tqCInvoiceMovementForHistRebuild.tcCInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} 
                                                                                then tqCInvoiceMovementForHistRebuild.tdCInvoiceVatDebitTC 
                                                                                else 0
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementVatCredTC     = if tqCInvoiceMovementForHistRebuild.tcCInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} 
                                                                                then tqCInvoiceMovementForHistRebuild.tdCInvoiceVatCreditTC 
                                                                                else 0
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementIsInvoice     = if tqCInvoiceMovementForHistRebuild.tcCInvoiceType = {&INVOICETYPE-INVOICE} or tqCInvoiceMovementForHistRebuild.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                                                                then true 
                                                                                else false
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementIsCreditN     = if tqCInvoiceMovementForHistRebuild.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} or tqCInvoiceMovementForHistRebuild.tcCInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                                                                then true 
                                                                                else false
                   tQCInvoiceMovementNewRebuild.tdExchangeRate                = tqCInvoiceMovementForHistRebuild.tdCInvoiceExchangeRate
                   tQCInvoiceMovementNewRebuild.tdExchangeRateScale           = tqCInvoiceMovementForHistRebuild.tdCinvoiceRateScale 
                   tQCInvoiceMovementNewRebuild.ttValidityDate                = tqCInvoiceMovementForHistRebuild.ttCInvoiceTaxPointDate 
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementType          = tqCInvoiceMovementForHistRebuild.tcCInvoiceMovementType
                   tQCInvoiceMovementNewRebuild.CInvoiceMovementInfo          = trim(string(tqCInvoiceMovementForHistRebuild.tiCInvoiceMovementYearPeriod)) + '/':U + 
                                                                                trim(tqCInvoiceMovementForHistRebuild.tcJournalCode) + '/':U + 
                                                                                trim(string(tqCInvoiceMovementForHistRebuild.tiPostingVoucher))                   
                   tQCInvoiceMovementNewRebuild.GL_ID                         = tqCInvoiceMovementForHistRebuild.tiGL_ID
                   tQCInvoiceMovementNewRebuild.Division_ID                   = tqCInvoiceMovementForHistRebuild.tiDivision_ID
                   tQCInvoiceMovementNewRebuild.Project_ID                    = tqCInvoiceMovementForHistRebuild.tiProject_ID
                   tQCInvoiceMovementNewRebuild.Journal_ID                    = tqCInvoiceMovementForHistRebuild.tiJournal_ID
                   tQCInvoiceMovementNewRebuild.CostCentre_ID                 = tqCInvoiceMovementForHistRebuild.tiCostCentre_ID
                   tQCInvoiceMovementNewRebuild.MovementYearPeriod            = tqCInvoiceMovementForHistRebuild.tiCInvoiceMovementYearPeriod.
        end. /* if viCIMovementBlockReturnStatus >= 0 */
        
        /* ============================= */
        /* Actions for the last-of CIMov */
        /* ============================= */
        if last-of (tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_ID) 
        then do :
            /* ============================= */
            /* Call to BQCinvoiceMovement    */
            /* ============================= */
            if viCIMovementBlockReturnStatus >= 0
            then do : 
                if can-find (first tQCInvoiceMovementNewRebuild)
                then do:
                    if viBQCInvoiceMovementForCBalID = 0 or
                       viBQCInvoiceMovementForCBalID = ?
                    then do :
                        <I-11 {bFcStartAndOpenInstance
                             &ADD-TO-TRANSACTION = "false"
                             &CLASS              = "BQCInvoiceMovement"}>
                    end. /* if viBQCInvoiceMovementForCBalID = 0 or */
                    else do :
                        <I-12 {bFcOpenInstance
                             &CLASS           = "BQCInvoiceMovement"}>
                    end. /* Not if viBQCInvoiceMovementForCBalID = 0 or */
                    <M-13 run ApiCreateQCInvoiceMovement
                       (input  tQCInvoiceMovementNewRebuild (tAPIQCInvoiceMovement), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        output viExternalReturnStatus (oiReturnStatus)) in BQCInvoiceMovement>
                    <I-37 {bFcCloseAndStopInstance
                         &CLASS           = "BQCInvoiceMovement"}>
                    if viExternalReturnStatus <> 0 
                    then assign viCIMovementBlockReturnStatus = viExternalReturnStatus.
                end. /* if can-find */
            end. /* if viMinorBlockTwoReturnStatus >= 0 */
            /* ================================================================= */
            /* Error Handling: keep track and store any errors/warnings occured  */
            /* ================================================================= */
            if viCIMovementBlockReturnStatus = 0
            then assign tCHistRebuildResult.tcResult = trim(#T-7'Correctly processed':255(65598)T-7#).
            else do :
                /* Add general result to the result temp-table */
                if viCIMovementBlockReturnStatus > 0
                then assign tCHistRebuildResult.tcResult = trim(#T-8'Warning occurred:':255(65599)t-8#) + " ":U + string(viCIMovementBlockReturnStatus).
                else assign tCHistRebuildResult.tcResult = trim(#T-9'Error occurred.':255(65600)t-9#) + " ":U + string(viCIMovementBlockReturnStatus).
                /* Add the messages to the result temp-table */
                for each tFcMessages : 
                    assign tCHistRebuildResult.tcResult = tCHistRebuildResult.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 viCIMovementBlockReturnStatus < viLocalReturnStatus
                then assign viLocalReturnStatus = viCIMovementBlockReturnStatus.
            end. /* if viCIMovementBlockReturnStatus <> 0 */
        end. /* if last-of (tqCInvoiceMovementForHistRebuild.tiCInvoiceCurrency_ID) */
        
    end. /* for each tqCInvoiceMovementForHistRebuild */
    
/* ============= */
/* End Block     */
/* ============= */
END. /* MAJORBLOCK */

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