project QadFinancials > class BDebtorBalance > method UpdateDBalanceAndDHist

Description

Update DBalance And DHist


Parameters


tDebtBalQDInvoiceMovementinputtemp-tabletemp-table that stores the deleted QDInvoiceMovement Records that need to be processed.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BQDInvoiceMovement.AdditionalUpdates


program code (program6/bdebtorbalance.p)

/* ======================================================================== */
/* This method will update the Debtor Balance and the Debtor History.       */
/* These modifications will be based on the temp-table that will be passed. */
/* This temp-table holds the QDInvoiceMovement records that need to be      */
/* processed. (Status = 'D')                                                */
/* ======================================================================== */

/* Exception Handling */
assign oiReturnStatus = -98.

/* Start queries */
<Q-4 run DBalanceByCyDebtorCurrID  (Start) in BDebtorBalance >


QDINVOICEBLOCK: DO :
  
  for each tDebtBalQDInvoiceMovement:

    /* Replace any unknown value by 0 - although striclty spoken here might be no ? here */
    if tDebtBalQDInvoiceMovement.Company_ID = ?
    then assign tDebtBalQDInvoiceMovement.Company_ID = 0.
    if tDebtBalQDInvoiceMovement.CostCentre_ID = ?
    then assign tDebtBalQDInvoiceMovement.CostCentre_ID = 0.
    if tDebtBalQDInvoiceMovement.Currency_ID = ?
    then assign tDebtBalQDInvoiceMovement.Currency_ID = 0.
    if tDebtBalQDInvoiceMovement.Debtor_ID = ?
    then assign tDebtBalQDInvoiceMovement.Debtor_ID = 0.
    if tDebtBalQDInvoiceMovement.Division_ID = ?
    then assign tDebtBalQDInvoiceMovement.Division_ID = 0.
    if tDebtBalQDInvoiceMovement.GL_ID = ?
    then assign tDebtBalQDInvoiceMovement.GL_ID = 0.
    if tDebtBalQDInvoiceMovement.Journal_ID = ?
    then assign tDebtBalQDInvoiceMovement.Journal_ID = 0.
    if tDebtBalQDInvoiceMovement.Period_ID = ?
    then assign tDebtBalQDInvoiceMovement.Period_ID = 0.
    if tDebtBalQDInvoiceMovement.Project_ID = ?
    then assign tDebtBalQDInvoiceMovement.Project_ID = 0.    
    
    /* ============== */
    /* DEBTOR BALANCE */
    /* ============== */
    /* ================================================================== */
    /* Check if a DBalance-record exists for this company/Debtor/currency */
    /* Load when it exists; if it does not exist, create it.              */
    /* ================================================================== */
    Find tDBalance where 
         tDBalance.Company_ID  = tDebtBalQDInvoiceMovement.Company_ID  and
         tDBalance.Debtor_ID   = tDebtBalQDInvoiceMovement.Debtor_ID   and
         tDBalance.Currency_ID = tDebtBalQDInvoiceMovement.Currency_ID
         no-error.
    if not available tDBalance
    then do :
        <Q-19 run DBalanceByCyDebtorCurrID (all) (Read) (Cache)
           (input tDebtBalQDInvoiceMovement.Company_ID, (CompanyId)
            input ?, (DBalance_ID)
            input tDebtBalQDInvoiceMovement.Debtor_ID, (Debtor_ID)
            input tDebtBalQDInvoiceMovement.Currency_ID, (Currency_ID)
            output dataset tqDBalanceByCyDebtorCurrID) in BDebtorBalance >
        find tqDBalanceByCyDebtorCurrID where
             tqDBalanceByCyDebtorCurrID.tiCompany_ID  = tDebtBalQDInvoiceMovement.Company_ID  and
             tqDBalanceByCyDebtorCurrID.tiDebtor_ID   = tDebtBalQDInvoiceMovement.Debtor_ID   and
             tqDBalanceByCyDebtorCurrID.tiCurrency_ID = tDebtBalQDInvoiceMovement.Currency_ID
             no-lock no-error.
        if not available tqDBalanceByCyDebtorCurrID
        then do:
            <M-20 run AddDetailLine
               (input  'DBalance':U (icTable), 
                input  ? (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave QDINVOICEBLOCK.
            assign tDBalance.Company_ID  = tDebtBalQDInvoiceMovement.Company_ID
                   tDBalance.Debtor_ID   = tDebtBalQDInvoiceMovement.Debtor_ID
                   tDBalance.Currency_ID = tDebtBalQDInvoiceMovement.Currency_ID.
        end. /* DBalance does not exist yet */
        else do:
            <M-21 run DataLoad
               (input  ? (icRowids), 
                input  string(tqDBalanceByCyDebtorCurrID.tiDBalance_ID) (icPkeys), 
                input  ? (icObjectIds), 
                input  '' (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave QDINVOICEBLOCK.
            find tDBalance where
                 tDBalance.DBalance_ID = tqDBalanceByCyDebtorCurrID.tiDBalance_ID
                 no-lock no-error.
            if not available tDBalance
            then do:
                assign viBlockReturnStatus = -3
                       vcDebtorBalanceMsg = trim(#T-31'Customer balance could not be loaded.':255(1690)T-31#) + chr(10) +
                                            trim(substitute(#T-32'Customer balance ID: &1.':150(1691)T-32#, trim(string(tqDBalanceByCyDebtorCurrID.tiDBalance_ID)) )).
                <M-22 run SetMessage
                   (input  vcDebtorBalanceMsg (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  string(tqDBalanceByCyDebtorCurrID.tiDBalance_ID) (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QADFIN-1327':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                leave QDINVOICEBLOCK.
            end.
            assign tDBalance.tc_Status = 'C':U.
        end. /* DBalance does already exist */
    end. /* if not available tDBalance */
    /* =========================== */
    /* assign the DBalance-amounts */
    /* =========================== */
    assign tDBalance.DBalanceLC = tDBalance.DBalanceLC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
           tDBalance.DBalanceTC = tDBalance.DBalanceTC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
           tDBalance.DBalanceCC = tDBalance.DBalanceCC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC.
    

    /* ============== */
    /* DEBTOR HISTORY */
    /* ============== */
    /* =================== */
    /* Get the Year/Period */
    /* =================== */
    if tDebtBalQDInvoiceMovement.MovementYearPeriod = 0 or 
       tDebtBalQDInvoiceMovement.MovementYearPeriod = ?
    then do :
        if vlStartedPeriodPrim = false
        then do :
            <Q-35 run PeriodPrim  (Start) in BPeriod >
            assign vlStartedPeriodPrim = true.
        end. /* if vlStartedPeriodPrim = false */
        <Q-12 run PeriodPrim (all) (Read) (Cache)
          (input tDebtBalQDInvoiceMovement.Company_ID, (CompanyId)
           input tDebtBalQDInvoiceMovement.Period_ID, (PeriodId)
           input ?, (PeriodYear)
           input ?, (PeriodPeriod)
           output dataset tqPeriodPrim) in BPeriod >
        find tqPeriodPrim where
             tqPeriodPrim.tiCompany_ID = tDebtBalQDInvoiceMovement.Company_ID and
             tqPeriodPrim.tiPeriod_ID  = tdebtBalQDInvoiceMovement.Period_ID
             no-lock no-error.
        if not available tqPeriodPrim
        then do:
            assign viBlockReturnStatus = -3
                   vcDebtorBalanceMsg  = trim(#T-33'The GL period specified is not defined in the system.':255(1456)t-33#) + chr(10) +
                                         trim(substitute(#T-34'GL period ID: &1.':150(1457)T-34#, trim(string(tDebtBalQDInvoiceMovement.Period_ID)) )).
            <M-23 run SetMessage
               (input  vcDebtorBalanceMsg (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  string(tdebtbalQDInvoiceMovement.period_ID) (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QADFIN-1328':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            leave QDINVOICEBLOCK.
        end.
        assign viYearPeriod = tqPeriodPrim.tiPeriodYear * 100 + tqPeriodPrim.tiPeriodPeriod.
    end. /* if tDebtBalQDInvoiceMovement.MovementYearPeriod = 0 or */
    else assign viYearPeriod = tDebtBalQDInvoiceMovement.MovementYearPeriod.
    /* Calculate the VAT amount */
    assign vdVatAmountTC = tDebtBalQDInvoiceMovement.DInvoiceMovementVatDebTC - tDebtBalQDInvoiceMovement.DInvoiceMovementVatCredTC
           vdVatAmountLC = tDebtBalQDInvoiceMovement.DInvoiceMovementVatDebLC - tDebtBalQDInvoiceMovement.DInvoiceMovementVatCredLC
           vdVatAmountCC = tDebtBalQDInvoiceMovement.DInvoiceMovementVatDebCC - tDebtBalQDInvoiceMovement.DInvoiceMovementVatCredCC.
    /* =================================================================== */
    /* Look for the DHist-records for this Debtor for this period or later */
    /* =================================================================== */
    find tDHist where
         tDHist.Company_ID           = tDebtBalQDInvoiceMovement.Company_ID    and
         tDHist.Debtor_ID            = tDebtBalQDInvoiceMovement.Debtor_ID     and
         tDHist.Currency_ID          = tDebtBalQDInvoiceMovement.Currency_ID   and
         tDHist.GL_ID                = tDebtBalQDInvoiceMovement.GL_ID         and
         tDHist.Project_ID           = tDebtBalQDInvoiceMovement.Project_ID    and
         tDHist.CostCentre_ID        = tDebtBalQDInvoiceMovement.CostCentre_ID and
         tDHist.Journal_ID           = tDebtBalQDInvoiceMovement.Journal_ID    and
         tDHist.Division_ID          = tDebtBalQDInvoiceMovement.Division_ID   and
         tDHist.DHistYearPeriodFrom <= viYearPeriod                            and
         tDHist.DHistYearPeriodTill >= viYearPeriod                            and
         tDHist.tc_Status           <> 'D':U
         no-error.
    if not available tDHist /* No current history record found */
    then do:
        /* ===================== */
        /* Create History Record */
        /* ===================== */
        <M-24 run AddDetailLine
           (input  'DHist':U (icTable), 
            input  tDBalance.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
        if viFcReturnSuper <> 0
        then assign viBlockReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then Leave QDINVOICEBLOCK.
        ASSIGN viPrevYearPeriod = 0.
        FOR EACH bDhist where
                 bDHist.Company_ID           = tDebtBalQDInvoiceMovement.Company_ID    and
                 bDHist.Debtor_ID            = tDebtBalQDInvoiceMovement.Debtor_ID     and
                 bDHist.Currency_ID          = tDebtBalQDInvoiceMovement.Currency_ID   and
                 bDHist.GL_ID                = tDebtBalQDInvoiceMovement.GL_ID         and
                 bDHist.Project_ID           = tDebtBalQDInvoiceMovement.Project_ID    and
                 bDHist.CostCentre_ID        = tDebtBalQDInvoiceMovement.CostCentre_ID and
                 bDHist.Journal_ID           = tDebtBalQDInvoiceMovement.Journal_ID    and
                 bDHist.Division_ID          = tDebtBalQDInvoiceMovement.Division_ID   and
                 bDHist.DHistYearPeriodFrom  > viYearPeriod                            and
                 bDHist.tc_Status           <> 'D':U
                 NO-LOCK 
                 by bDHist.DHistYearPeriodFrom DESCENDING :                    
              ASSIGN viPrevYearPeriod = bDHist.DHistYearPeriodFrom.
        END.
        if viPrevYearPeriod <> 0 /* History record found with later starting period */
        then assign viPrevYearPeriod = viPrevYearPeriod - 1.
        else assign viPrevYearPeriod = 999999.
        assign tDHist.DBalance_ID           = tDBalance.DBalance_ID
               tDHist.Company_ID            = tDebtBalQDInvoiceMovement.Company_ID
               tDHist.Debtor_ID             = tDebtBalQDInvoiceMovement.Debtor_ID
               tDHist.Currency_ID           = tDebtBalQDInvoiceMovement.Currency_ID
               tDHist.GL_ID                 = tDebtBalQDInvoiceMovement.GL_ID
               tDHist.Project_ID            = tDebtBalQDInvoiceMovement.Project_ID
               tDHist.CostCentre_ID         = tDebtBalQDInvoiceMovement.CostCentre_ID
               tDHist.Journal_ID            = tDebtBalQDInvoiceMovement.Journal_ID
               tDHist.Division_ID           = tDebtBalQDInvoiceMovement.Division_ID
               tDHist.DHistYearPeriodFrom   = viYearPeriod
               tDHist.DHistYearPeriodTill   = viPrevYearPeriod
               tDHist.DHistBalanceLC        = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
               tDHist.DHistBalanceTC        = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
               tDHist.DHistBalanceCC        = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
               tDHist.DHistMovementCreditLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
               tDHist.DHistMovementCreditTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
               tDHist.DHistMovementCreditCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
               tDHist.DHistMovementDebitLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
               tDHist.DHistMovementDebitTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
               tDHist.DHistMovementDebitCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.             
        /* vdVatAmountTC is a credit amount and therefore < 0 */               
        if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsInvoice        
        then assign tDHist.DHistNettoInvoiceLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC + vdVatAmountLC
                    tDHist.DHistNettoInvoiceTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC + vdVatAmountTC
                    tDHist.DHistNettoInvoiceCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC + vdVatAmountCC
                    tDHist.DHistGrossInvoiceLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
                    tDHist.DHistGrossInvoiceTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
                    tDHist.DHistGrossInvoiceCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.
        /* vdVatAmountTC is a debit amount and therefore > 0 */
        if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsCreditN
        then assign tDHist.DHistNettoCredNoteLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC - vdVatAmountLC
                    tDHist.DHistNettoCredNoteTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC - vdVatAmountTC
                    tDHist.DHistNettoCredNoteCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC - vdVatAmountCC
                    tDHist.DHistGrossCredNoteLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                    tDHist.DHistGrossCredNoteTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                    tDHist.DHistGrossCredNoteCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC.
        <M-25 run UpdateDBalanceAndDHistDHistory
           (input  viYearPeriod (iiYearPeriod), 
            input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC (idDHistBalanceLC), 
            input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC (idDHistBalanceTC), 
            input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC (idDHistBalanceCC), 
            input  tDHist.GL_ID (iiGL), 
            input  tDHist.Project_ID (iiProject), 
            input  tDHist.CostCentre_ID (iiCostCentre), 
            input  tDHist.Division_ID (iiDivision), 
            input  tDHist.Journal_ID (iiJournal), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
        if viFcReturnSuper <> 0
        then assign viBlockReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then Leave QDINVOICEBLOCK.
    end. /* No current history record found */ 
    else do:
        assign tDHist.tc_Status = 'C':U.
        if tDHist.DHistYearPeriodFrom = viYearPeriod
        then do:
            assign tDHist.DHistBalanceLC        = tDHist.DHistBalanceLC        + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                   tDHist.DHistBalanceTC        = tDHist.DHistBalanceTC        + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                   tDHist.DHistBalanceCC        = tDHist.DHistBalanceCC        + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
                   tDHist.DHistMovementCreditLC = tDHist.DHistMovementCreditLC + tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                   tDHist.DHistMovementCreditTC = tDHist.DHistMovementCreditTC + tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                   tDHist.DHistMovementCreditCC = tDHist.DHistMovementCreditCC + tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
                   tDHist.DHistMovementDebitLC  = tDHist.DHistMovementDebitLC  + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
                   tDHist.DHistMovementDebitTC  = tDHist.DHistMovementDebitTC  + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
                   tDHist.DHistMovementDebitCC  = tDHist.DHistMovementDebitCC  + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.             
            /* vdVatAmountTC is a credit amount and therefore < 0 */               
            if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsInvoice        
            then assign tDHist.DHistNettoInvoiceLC  = tDHist.DHistNettoInvoiceLC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC + vdVatAmountLC
                        tDHist.DHistNettoInvoiceTC  = tDHist.DHistNettoInvoiceTC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC + vdVatAmountTC
                        tDHist.DHistNettoInvoiceCC  = tDHist.DHistNettoInvoiceCC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC + vdVatAmountCC
                        tDHist.DHistGrossInvoiceLC  = tDHist.DHistGrossInvoiceLC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
                        tDHist.DHistGrossInvoiceTC  = tDHist.DHistGrossInvoiceTC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
                        tDHist.DHistGrossInvoiceCC  = tDHist.DHistGrossInvoiceCC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.
            /* vdVatAmountTC is a debit amount and therefore > 0 */
            if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsCreditN
            then assign tDHist.DHistNettoCredNoteLC = tDHist.DHistNettoCredNoteLC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC - vdVatAmountLC
                        tDHist.DHistNettoCredNoteTC = tDHist.DHistNettoCredNoteTC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC - vdVatAmountTC
                        tDHist.DHistNettoCredNoteCC = tDHist.DHistNettoCredNoteCC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC - vdVatAmountCC
                        tDHist.DHistGrossCredNoteLC = tDHist.DHistGrossCredNoteLC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                        tDHist.DHistGrossCredNoteTC = tDHist.DHistGrossCredNoteTC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                        tDHist.DHistGrossCredNoteCC = tDHist.DHistGrossCredNoteCC + 
                                                      tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC.
            <M-26 run UpdateDBalanceAndDHistDHistory
               (input  viYearPeriod (iiYearPeriod), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditLC (idDHistBalanceLC), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditTC (idDHistBalanceTC), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditCC (idDHistBalanceCC), 
                input  tDHist.GL_ID (iiGL), 
                input  tDHist.Project_ID (iiProject), 
                input  tDHist.CostCentre_ID (iiCostCentre), 
                input  tDHist.Division_ID (iiDivision), 
                input  tDHist.Journal_ID (iiJournal), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave QDINVOICEBLOCK.
        end.
        else do:
            assign viPrevYearPeriod            = tDHist.DHistYearPeriodTill
                   vdDHistBalanceLC            = tDHist.DHistBalanceLC
                   vdDHistBalanceTC            = tDHist.DHistBalanceTC
                   vdDHistBalanceCC            = tDHist.DHistBalanceCC
                   tDHist.DHistYearPeriodTill  = viYearPeriod - 1.
            <M-27 run AddDetailLine
               (input  'DHist':U (icTable), 
                input  tDBalance.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave QDINVOICEBLOCK.
            assign tDHist.DBalance_ID           = tDBalance.DBalance_ID
                   tDHist.Company_ID            = tDebtBalQDInvoiceMovement.Company_ID
                   tDHist.Debtor_ID             = tDebtBalQDInvoiceMovement.Debtor_ID
                   tDHist.Currency_ID           = tDebtBalQDInvoiceMovement.Currency_ID
                   tDHist.GL_ID                 = tDebtBalQDInvoiceMovement.GL_ID
                   tDHist.Project_ID            = tDebtBalQDInvoiceMovement.Project_ID
                   tDHist.CostCentre_ID         = tDebtBalQDInvoiceMovement.CostCentre_ID
                   tDHist.Journal_ID            = tDebtBalQDInvoiceMovement.Journal_ID
                   tDHist.Division_ID           = tDebtBalQDInvoiceMovement.Division_ID
                   tDHist.DHistYearPeriodFrom   = viYearPeriod
                   tDHist.DHistYearPeriodTill   = viPrevYearPeriod
                   tDHist.DHistBalanceLC        = vdDHistBalanceLC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                   tDHist.DHistBalanceTC        = vdDHistBalanceTC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                   tDHist.DHistBalanceCC        = vdDHistBalanceCC + tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
                   tDHist.DHistMovementCreditLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                   tDHist.DHistMovementCreditTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                   tDHist.DHistMovementCreditCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC
                   tDHist.DHistMovementDebitLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
                   tDHist.DHistMovementDebitTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
                   tDHist.DHistMovementDebitCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.             
            /* vdVatAmountTC is a credit amount and therefore < 0 */               
            if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsInvoice        
            then assign tDHist.DHistNettoInvoiceLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC + vdVatAmountLC
                        tDHist.DHistNettoInvoiceTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC + vdVatAmountTC
                        tDHist.DHistNettoInvoiceCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC + vdVatAmountCC
                        tDHist.DHistGrossInvoiceLC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC
                        tDHist.DHistGrossInvoiceTC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC
                        tDHist.DHistGrossInvoiceCC  = tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC.
            /* vdVatAmountTC is a debit amount and therefore > 0 */
            if tDebtBalQDInvoiceMovement.DInvoiceMovementType = 'INITIAL':U and tDebtBalQDInvoiceMovement.DInvoiceMovementIsCreditN
            then assign tDHist.DHistNettoCredNoteLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC - vdVatAmountLC
                        tDHist.DHistNettoCredNoteTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC - vdVatAmountTC
                        tDHist.DHistNettoCredNoteCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC - vdVatAmountCC
                        tDHist.DHistGrossCredNoteLC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditLC
                        tDHist.DHistGrossCredNoteTC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditTC
                        tDHist.DHistGrossCredNoteCC = tDebtBalQDInvoiceMovement.DInvoiceMovementCreditCC.
            <M-28 run UpdateDBalanceAndDHistDHistory
               (input  viYearPeriod (iiYearPeriod), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitLC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditLC (idDHistBalanceLC), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitTC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditTC (idDHistBalanceTC), 
                input  tDebtBalQDInvoiceMovement.DInvoiceMovementDebitCC - tdebtBalQDInvoiceMovement.DInvoiceMovementCreditCC (idDHistBalanceCC), 
                input  tDHist.GL_ID (iiGL), 
                input  tDHist.Project_ID (iiProject), 
                input  tDHist.CostCentre_ID (iiCostCentre), 
                input  tDHist.Division_ID (iiDivision), 
                input  tDHist.Journal_ID (iiJournal), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
            if viFcReturnSuper <> 0
            then assign viBlockReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then Leave QDINVOICEBLOCK.
        end. /* Note: if tDHist.DHistYearPeriodFrom = viYearPeriod */
    end. /* History record found for this period */ 

  end. /* for each tDebtBalQDInvoiceMovement  */

  /* ====================================================================================================================== */
  /* DEBTORHIGHCREDITLIMIT: Do this only once outside the for-each loop as there can be 2 records in it for the same debtor */
  /* ====================================================================================================================== */
  <M-36 run UpdateDBalanceAndDHistDebtorHighCredit
     (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
  if viFcReturnSuper <> 0 
  then assign viBlockReturnStatus = viFcReturnSuper.
  if viFcReturnSuper < 0
  then Leave QDINVOICEBLOCK.

END. /* QDINVOICEBLOCK */

/* Stop queries */
<Q-6 run DBalanceByCyDebtorCurrID  (Stop) in BDebtorBalance >
if vlStartedPeriodPrim
then do :
    <Q-7 run PeriodPrim (Stop) in BPeriod >
end. /* if vlStartedPeriodPrim */

/* Check for errors in the block */
if viBlockReturnStatus <> 0 then assign oiReturnStatus = viBlockReturnStatus.
if viBlockReturnStatus < 0 then Return.

/* Validation */
<M-29 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.

/* Additional updates */
<M-37 run AdditionalUpdates
   (output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.

/* Exception Handling */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.