project QadFinancials > class BDebtor > method CalculateBalanceOpenItems


Parameters


iiDebtorIdinputinteger
icDebtorCodeinputcharacter
icCurrencyCodeinputcharacter
odBalanceoutputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtor.CalculateBalances


program code (program9/bdebtor.p)

assign oiReturnStatus = -98
       viLocalReturn  = 0.

/* Normalize input values */
if iiDebtorId     = 0    then assign iiDebtorId     = ?.
if icDebtorCode   = "":U then assign icDebtorCode   = ?.
if icCurrencyCode = "":U then assign icCurrencyCode = ?.

/* Validate input parameters */
if iiDebtorId   = ? and
   icDebtorCode = ?
then do:
    assign oiReturnStatus = 0.
    return.
end.

MAIN_BLOCK:
do:
    /* Default values */
    assign viEndCurrencyId         = ?
           vlStarttqCompPropByComp = false.
    
    /* Get debtor's ID and Currency if needed */
    if iiDebtorId     = ? or
       icCurrencyCode = ?
    then do:
        <Q-13 run DebtorByDebtor (all) (Read) (NoCache)
           (input if iiDebtorId = ? then viCompanyId else ?, (CompanyId)
            input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
            input iiDebtorId, (DebtorId)
            input icDebtorCode, (DebtorCode)
            output dataset tqDebtorByDebtor) in BDebtor >
    
        find first tqDebtorByDebtor where
                   tqDebtorByDebtor.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE} and
                   tqDebtorByDebtor.tiDebtor_ID       = (if iiDebtorId = ?
                                                         then tqDebtorByDebtor.tiDebtor_ID
                                                         else iiDebtorId)                   and
                   tqDebtorByDebtor.tcDebtorCode      = (if icDebtorCode = ?
                                                         then icDebtorCode
                                                         else tqDebtorByDebtor.tcDebtorCode)
                   no-error.
    
        if not available tqDebtorByDebtor   
        then do:
            assign vcMessage = trim(#T-24'Invalid customer code':255(733714905)T-24#).            
            <M-14 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'DebtorCode':U (icFieldName), 
                input  icDebtorCode (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-8567':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtor>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end.
        
        assign iiDebtorId = tqDebtorByDebtor.tiDebtor_ID.
        if icCurrencyCode = ?
        then assign icCurrencyCode  = tqDebtorByDebtor.tcCurrencyCode
                    viEndCurrencyId = tqDebtorByDebtor.tiCurrency_ID.
    end.
    
    /* Get currency ID */
    if viEndCurrencyId = ? and
       icCurrencyCode <> ?
    then do:
        <Q-10 run CurrencyPrim (all) (Read) (NoCache)
           (input icCurrencyCode, (CurrencyCode)
            input ?, (Currency_ID)
            output dataset tqCurrencyPrim) in BCurrency >
        find first tqCurrencyPrim where
                   tqCurrencyPrim.tcCurrencyCode = icCurrencyCode
                   no-error.
        if not available tqCurrencyPrim
        then do:
            assign vcMessage = trim(#T-25'Invalid currency code.':200(16214)T-25#).
            <M-27 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'CurrencyCode':U (icFieldName), 
                input  icCurrencyCode (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-9467':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtor>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end.
        
        assign viEndCurrencyId = tqCurrencyPrim.tiCurrency_ID.
    end. /* if viEndCurrencyId = ? and */
    
    if iiDebtorId      = ? or 
       viEndCurrencyId = ?
    then do:
         assign vcMessage = trim(#T-26'Invalid currency code.':200(16214)T-26#).
         <M-17 run SetMessage
            (input  vcMessage (icMessage), 
             input  '':U (icArguments), 
             input  'CurrencyCode':U (icFieldName), 
             input  icCurrencyCode (icFieldValue), 
             input  'S':U (icType), 
             input  3 (iiSeverity), 
             input  '':U (icRowid), 
             input  'QadFin-8568':U (icFcMsgNumber), 
             input  '':U (icFcExplanation), 
             input  '':U (icFcIdentification), 
             input  '':U (icFcContext), 
             output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK.
    end. 
    
    
    /* =================================================================================== */
    /* Get all open invoices of the debtor                                                 */
    /* =================================================================================== */
    <Q-8 run DInvoiceByDebtorIsOpenSimple (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input iiDebtorId, (DebtorId)
        input true, (DInvoiceIsOpen)
        input ?, (DInvoiceType)
        input ?, (CurrencyId)
        output dataset tqDInvoiceByDebtorIsOpenSimple) in BDInvoice>
    
    for each tqDInvoiceByDebtorIsOpenSimple where
             tqDInvoiceByDebtorIsOpenSimple.tiDebtor_ID      = iiDebtorId and
             tqDInvoiceByDebtorIsOpenSimple.tlDInvoiceIsOpen = true
        break by tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID
              by tqDInvoiceByDebtorIsOpenSimple.tiDInvoiceCurrency_ID:
    
        
        /* Get Local Currency of the company */
        if first-of(tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID)
        then do:
            assign viCompanyCurrencyLCId = ?.
            
            if vlStarttqCompPropByComp = false
            then do:
                <Q-21 run CompanyPropertyByCompany  (Start) in BCompanyProperty >
                assign vlStarttqCompPropByComp = true.
            end. 
            
            <Q-19 run CompanyPropertyByCompany (all) (Read) (NoCache)
               (input tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID, (CompanyId)
                input ?, (CompanyCode)
                input ?, (CurrencyCode)
                output dataset tqCompanyPropertyByCompany) in BCompanyProperty >
        
            find first tqCompanyPropertyByCompany where
                       tqCompanyPropertyByCompany.tiCompany_ID = tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID
                       no-error.
            if available tqCompanyPropertyByCompany
            then assign viCompanyCurrencyLCId = tqCompanyPropertyByCompany.tiCurrency_ID.
        end.
    
        /* Get exchange rate between transactions currency of invoice and required currency */
        if first-of(tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID) or
           first-of(tqDInvoiceByDebtorIsOpenSimple.tiDInvoiceCurrency_ID)
        then do:
            if viEndCurrencyId = viCompanyCurrencyLCId
            then assign vdRate      = ?
                        vdRateScale = ?.
            else if tqDInvoiceByDebtorIsOpenSimple.tiDInvoiceCurrency_ID = viEndCurrencyId
            then assign vdRate      = 1
                        vdRateScale = 1.
            else do :
                <M-87 run GetExchangeRate
                   (input  viCompanyID (iiCompanyID), 
                    input  tqDInvoiceByDebtorIsOpenSimple.tiDInvoiceCurrency_ID (iiFromCurrencyID), 
                    input  ? (icFromCurrencyCode), 
                    input  viEndCurrencyId (iiToCurrencyID), 
                    input  ? (icToCurrencyCode), 
                    input  ? (iiExchangeRateTypeID), 
                    input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                    input  today (itValidityDate), 
                    output vdRate (odExchangeRate), 
                    output vdRateScale (odExchangeScaleFactor), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtor>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0
                then assign viLocalReturn = -1.
                if viFcReturnSuper < 0 then leave MAIN_BLOCK.
            end.
        end. /* if first-of(tqDInvoiceByDebtorIsOpenSimple.tiCompany_ID) or */
    
        /* Calculate balance of current invoice */
        assign vdBalanceTC = tqDInvoiceByDebtorIsOpenSimple.tdDInvoiceBalanceDebitTC - tqDInvoiceByDebtorIsOpenSimple.tdDInvoiceBalanceCreditTC
               vdBalanceLC = tqDInvoiceByDebtorIsOpenSimple.tdDInvoiceBalanceDebitLC - tqDInvoiceByDebtorIsOpenSimple.tdDInvoiceBalanceCreditLC.
    
        /* Calculate overall balance of all open invoices */
        if viEndCurrencyId = viCompanyCurrencyLCId
        then assign odBalance = odBalance + vdBalanceLC.
        else if tqDInvoiceByDebtorIsOpenSimple.tiDInvoiceCurrency_ID = viEndCurrencyId
        then assign odBalance = odBalance + vdBalanceTC.
        else assign odBalance = odBalance + <M-20 RoundAmount
                                               (input  vdBalanceTC * vdRate * vdRateScale (idUnroundedAmount), 
                                                input  viEndCurrencyId (iiCurrencyID), 
                                                input  ? (icCurrencyCode)) in business>.
    end. /* for each tqDInvoiceByDebtorIsOpenSimple where */
end. /* MAIN_BLOCK */

/* Stop queries */
if vlStarttqCompPropByComp
then do:
    <Q-22 run CompanyPropertyByCompany  (Stop) in BCompanyProperty >
    assign vlStarttqCompPropByComp = false.
end. 

/* Error handling */
assign oiReturnStatus = viLocalReturn.