project QadFinancials > class BDebtorBalance > method UpdateDBalanceAndDHistDebtorHighCredit

Description

UpdateDBalanceAndDHistDebtorHighCredit: calculate the overall credit of the debtor and update DebtorHighCredit when applicable


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtorBalance.UpdateDBalanceAndDHist


program code (program6/bdebtorbalance.p)

    
    /* ====================================================================== */
    /* UpdateDBalanceAndDHistDebtorHighCredit: calculate the overall credit   */
    /* of the debtor and update DebtorHighCredit when applicable              */
    /* This method is running in the same segemnt as the parent-method and    */
    /* the parent-method its input table is also accessible from here         */
    /* Query DBalanceByCyDebtorCurrID is already started by the calling class */
    /* ====================================================================== */
    
    /* ========================================================================================== */
    /* This method will simply create tDebtorHighCreditWriteDirect: this temp-table will be       */
    /* filled in this method and that will be used in method PreSave as input to call WriteDirect */
    /* ========================================================================================== */
                                                  
    /* ======================================== */
    /* Empty temp-table that is used in PreSave */
    /* ======================================== */
    empty temp-table tDebtorHighCreditWriteDirect.
    
    /* ================== */
    /* Exception hanlding */
    /* ================== */
    assign oiReturnStatus = -98.
    
    /* ============= */
    /* Start queries */
    /* Start a block */
    /* ============= */
    <Q-63 run DebtorByIDsHighCreditLastInfo
       (Start) in BDebtor >
    MAJORLOOPBLOCK : DO :
        
        /* ==================================================== */
        /* Go through all records of the parent its input table */
        /* Break by the debtor                                  */
        /* ==================================================== */
        for each tDebtBalQDInvoiceMovement where 
                 tDebtBalQDInvoiceMovement.Debtor_ID <> 0 and 
                 tDebtBalQDInvoiceMovement.Debtor_ID <> ?
                 no-lock
                 break by tDebtBalQDInvoiceMovement.Debtor_ID :
            
            /* ========================================================= */
            /* Only continue in case it is the first record for a debtor */
            /* ========================================================= */
            if not first-of (tDebtBalQDInvoiceMovement.Debtor_ID)
            then next.
            
            /* ============================================== */
            /* Read the debtor to know its current HighCredit */
            /* Reset vdBalanceDebtorCurrency                  */
            /* ============================================== */
            <Q-62 run DebtorByIDsHighCreditLastInfo (all) (Read) (NoCache)
               (input ?, (CompanyID)
                input tDebtBalQDInvoiceMovement.Debtor_ID, (DebtorID)
                output dataset tqDebtorByIDsHighCreditLastInfo) in BDebtor >
            find tqDebtorByIDsHighCreditLastInfo
                 no-lock no-error.
            if not available tqDebtorByIDsHighCreditLastInfo
            then do :
                assign vcDebtorBalanceMsg  = trim(substitute(#T-60'Internal error. The system is unable to find the customer record based on its ID (&1).':213(59504)t-60#,tDebtBalQDInvoiceMovement.Debtor_ID))
                       viBlockReturnStatus = -3.
                <M-61 run SetMessage
                   (input  vcDebtorBalanceMsg (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  string(tDebtBalQDInvoiceMovement.Debtor_ID) (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-6023':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                Leave MAJORLOOPBLOCK.
            end. /* not av tqDebtorByIDsHighCreditLastInfo */
            assign vdBalanceDebtorCurrency = 0.
            
            /* =============================================== */
            /* Read all DBalance-records of the debtor from db */
            /* =============================================== */
            <Q-65 run DBalanceByCyDebtorCurrID (all) (Read) (Cache)
               (input ?, (CompanyId)
                input ?, (DBalance_ID)
                input tDebtBalQDInvoiceMovement.Debtor_ID, (Debtor_ID)
                input ?, (Currency_ID)
                output dataset tqDBalanceByCyDebtorCurrID) in BDebtorBalance >
            for each tqDBalanceByCyDebtorCurrID
                     no-lock 
                     Break by tqDBalanceByCyDebtorCurrID.tiCompany_ID : 
                
                /* ============================== */
                /* Reset vdBalanceLC per company  */
                /* ============================== */
                if first-of (tqDBalanceByCyDebtorCurrID.tiCompany_ID)
                then assign vdBalanceLC = 0.
                
                /* ================================================================================ */
                /* Sommate the LC-balance                                                           */
                /* If the value also exists in the instance then take the info from there           */
                /* As ithe record exists in the db, the record in the instance has to have status=C */
                /* ================================================================================ */
                find tDBalance where 
                     tDBalance.DBalance_ID = tqDBalanceByCyDebtorCurrID.tiDBalance_ID and
                     tDBalance.tc_Status   = "C":U
                     no-lock no-error.
                assign vdBalanceLC = vdBalanceLC + (if available tDBalance 
                                                    then tDBalance.DBalanceLC 
                                                    else tqDBalanceByCyDebtorCurrID.tdDBalanceLC).
                
                /* =========================================================== */
                /* Transfer the balance per company to the balance per debtor  */
                /* After it has been converted to the debtor its currency      */
                /* =========================================================== */
                if last-of (tqDBalanceByCyDebtorCurrID.tiCompany_ID)
                then do :
                    /* ===================================== */
                    /* Get the local currency of the company */
                    /* ===================================== */
                    <Q-66 run CompanyPropertyByCompanyIDCurr (all) (Read) (NoCache)
                       (input tqDBalanceByCyDebtorCurrID.tiCompany_ID, (CompanyId)
                        input ?, (CompanyPropertyID)
                        input ?, (CurrencyID)
                        output dataset tqCompanyPropertyByCompanyIDCurr) in BCompanyProperty >
                    find first tqCompanyPropertyByCompanyIDCurr no-lock no-error.
                    if not available tqCompanyPropertyByCompanyIDCurr
                    then do :
                        assign vcDebtorBalanceMsg  = trim(substitute(#T-68'Internal error. The system is unable to find the entity based on its ID (&1).':213(59517)t-68#,tqDBalanceByCyDebtorCurrID.tiCompany_ID))
                               viBlockReturnStatus = -3.
                        <M-67 run SetMessage
                           (input  vcDebtorBalanceMsg (icMessage), 
                            input  '':U (icArguments), 
                            input  '':U (icFieldName), 
                            input  string(tqDBalanceByCyDebtorCurrID.tiCompany_ID) (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  '':U (icRowid), 
                            input  'QadFin-6026':U (icFcMsgNumber), 
                            input  '' (icFcExplanation), 
                            input  '' (icFcIdentification), 
                            input  '' (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                        Leave MAJORLOOPBLOCK.
                    end. /* if not available tqCompanyPropertyByCompanyIDCurr */
                    /* ========================================== */
                    /* Add vdBalanceLC to vdBalanceDebtorCurrency */                  
                    /* ========================================== */
                    if tqDebtorByIDsHighCreditLastInfo.tiCurrency_ID = tqCompanyPropertyByCompanyIDCurr.tiCurrency_ID
                    then assign vdBalanceDebtorCurrency = vdBalanceDebtorCurrency + vdBalanceLC.
                    else do :
                        <M-35 run GetExchangeRate
                           (input  ? (iiCompanyID), 
                            input  tqCompanyPropertyByCompanyIDCurr.tiCurrency_ID (iiFromCurrencyID), 
                            input  ? (icFromCurrencyCode), 
                            input  tqDebtorByIDsHighCreditLastInfo.tiCurrency_ID (iiToCurrencyID), 
                            input  ? (icToCurrencyCode), 
                            input  ? (iiExchangeRateTypeID), 
                            input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                            input  today (itValidityDate), 
                            output vdRate (odExchangeRate), 
                            output vdRateScale (odExchangeScaleFactor), 
                            output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                        if viFcReturnSuper <> 0
                        then assign viBlockReturnStatus = viFcReturnSuper.
                        if viFcReturnSuper < 0
                        then Leave MAJORLOOPBLOCK.
                        assign vdBalanceDebtorCurrency = vdBalanceDebtorCurrency + (vdBalanceLC * vdRate * vdRateScale).
                    end. /* if tqDebtorByIDsHighCreditLastInfo = tqCompanyPropertyByCompanyIDCurr */
                end. /* if last-of (tqDBalanceByCyDebtorCurrID.tiCompany_ID) */
                
            end. /* for each tqDBalanceByCyDebtorCurrID for a single debtor */
            
            /* =============================================================================================== */
            /* Raise the current sum of the with the values in tDBalance that were not yet taken into account  */
            /* This can happen if the new tDBalance of the debtor was for a new company or currency            */
            /* We are not grouping the tDBlance-records (per company) as we assume the number of these records */ 
            /* is limited; we thus just take the TC-amount and convert it to the debtor its currency           */
            /* =============================================================================================== */
            for each tDBalance where
                     tDBalance.tc_Status = "N":U and 
                     tDBalance.Debtor_ID = tqDebtorByIDsHighCreditLastInfo.tiDebtor_ID : 
                
                /* ====================================================================== */
                /* Add the balance fo the new tDBalance-record to vdBalanceDebtorCurrency */                  
                /* ====================================================================== */
                if tDBalance.Currency_ID = tqDebtorByIDsHighCreditLastInfo.tiCurrency_ID 
                then assign vdBalanceDebtorCurrency = vdBalanceDebtorCurrency + tDBalance.DBalanceTC.
                else do :
                    <M-16 run GetExchangeRate
                       (input  ? (iiCompanyID), 
                        input  tDBalance.Currency_ID (iiFromCurrencyID), 
                        input  ? (icFromCurrencyCode), 
                        input  tqDebtorByIDsHighCreditLastInfo.tiCurrency_ID (iiToCurrencyID), 
                        input  ? (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        input  today (itValidityDate), 
                        output vdRate (odExchangeRate), 
                        output vdRateScale (odExchangeScaleFactor), 
                        output viFcReturnSuper (oiReturnStatus)) in BDebtorBalance>
                    if viFcReturnSuper <> 0
                    then assign viBlockReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0
                    then Leave MAJORLOOPBLOCK.
                    assign vdBalanceDebtorCurrency = vdBalanceDebtorCurrency + (tDBalance.DBalanceTC * vdRate * vdRateScale).
                end. /* if tqDebtorByIDsHighCreditLastInfo = tqCompanyPropertyByCompanyIDCurr */
                
            end. /* for each tDBalance where */
                       
            /* ================================================================ */
            /* Compare vdBalanceDebtorCurrency with the current HighCreditLimit */
            /* ================================================================ */
            if tqDebtorByIDsHighCreditLastInfo.tdDebtorHighCredit < vdBalanceDebtorCurrency or
               tqDebtorByIDsHighCreditLastInfo.tdDebtorHighCredit = ?
            then do :    
                /* Use the date of the 'some' (the current) tDebtBalQDInvoiceMovement of the debtor as date for the HighCredit */
                /* Create the record that will be used in PreSave */
                create tDebtorHighCreditWriteDirect.
                assign tDebtorHighCreditWriteDirect.tiDebtorID             = tqDebtorByIDsHighCreditLastInfo.tiDebtor_ID
                       tDebtorHighCreditWriteDirect.ttDebtorHighCreditDate = (if tDebtBalQDInvoiceMovement.ttValidityDate = ? then today else tDebtBalQDInvoiceMovement.ttValidityDate)
                       tDebtorHighCreditWriteDirect.tdDebtorHighCredit     = <M-72 RoundAmount
          (input  vdBalanceDebtorCurrency (idUnroundedAmount), 
           input  tqDebtorByIDsHighCreditLastInfo.tiCurrency_ID (iiCurrencyID), 
           input  '':U (icCurrencyCode)) in business> .
            end. /* if vdBalanceDebtorCurrency > tqDebtorByIDsHighCreditLastInfo.tdDebtorHighCredit */
            
        end. /* for each tDebtBalQDInvoiceMovement no-lock: + QDINVOICEBLOCK */
    
    END. /* MAJORLOOPBLOCK */
    
    /* ============ */
    /* Stop queries */
    /* ============ */
    <Q-64 run DebtorByIDsHighCreditLastInfo
       (Stop) in BDebtor >
    
    /* ================== */
    /* Exception handling */
    /* ================== */
    if oiReturnStatus = -98
    then assign oiReturnStatus = 0.