project QadFinancials > class BDebtor > method CalculateBalanceDrafts

Description

Calculate the balances of the drafts


Parameters


iiDebtorIdinputinteger
icDebtorCodeinputcharacter
icCurrencyCodeinputcharacter
odBalanceoutputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtor.CalculateBalances


program code (program6/bdebtor.p)

if iiDebtorId     = ? then assign iiDebtorId     = 0.
if icDebtorCode   = ? then assign icDebtorCode   = "":U.
if icCurrencyCode = ? then assign icCurrencyCode = "":U.

if iiDebtorId   = 0 and
   icDebtorCode = "":U
then return.

/* error handling */
assign oiReturnStatus = -98
       viLocalReturn  = 0.

/* get debtor details */
if iiDebtorId     = 0 or
   icCurrencyCode = "":U
then do:
    <Q-10 run DebtorByDebtor (all) (Read) (NoCache)
       (input if iiDebtorId = 0 then viCompanyId else ?, (CompanyId)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        input if iiDebtorId = 0 then ? else iiDebtorId, (DebtorId)
        input if iiDebtorId = 0 then icDebtorCode else ?, (DebtorCode)
        output dataset tqDebtorByDebtor) in BDebtor >

    find first tqDebtorByDebtor where
               tqDebtorByDebtor.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE} and
               tqDebtorByDebtor.tiDebtor_ID       = (if iiDebtorId = 0
                                                     then tqDebtorByDebtor.tiDebtor_ID
                                                     else iiDebtorId)                   and
               tqDebtorByDebtor.tcDebtorCode      = (if iiDebtorId = 0
                                                     then icDebtorCode
                                                     else tqDebtorByDebtor.tcDebtorCode)
               no-error.

    if not available tqDebtorByDebtor
    then do:
        <M-26 run SetMessage
           (input  #T-26'Customer ($1) is not defined in the system.':200(31311)t-26# (icMessage), 
            input  icDebtorCode (icArguments), 
            input  'DebtorCode':U (icFieldName), 
            input  icDebtorCode (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-8597':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        assign oiReturnStatus = -1.
        return.
    end.

    assign iiDebtorId = tqDebtorByDebtor.tiDebtor_ID.

    if icCurrencyCode = "":U
    then assign icCurrencyCode = tqDebtorByDebtor.tcCurrencyCode.
end.

if iiDebtorId = 0
then do:
    assign oiReturnStatus = 0.
    return.
end.

<Q-13 run DDocumentByDebtorTypeOpen (all) (Read) (NoCache)
   (input ?, (CompanyId)
    input iiDebtorId, (DebtorId)
    input ?, (DebtorCode)
    input {&DOCUMENTTYPE-DRAFT}, (DocumentType)
    input true, (IsOpen)
    output dataset tqDDocumentByDebtorTypeOpen) in BDDocument >

MAIN_BLOCK:
for each tqDDocumentByDebtorTypeOpen where
         tqDDocumentByDebtorTypeOpen.tiDebtor_ID       = iiDebtorId and
         tqDDocumentByDebtorTypeOpen.tcDDocumentType   = {&DOCUMENTTYPE-DRAFT} and
         tqDDocumentByDebtorTypeOpen.tlDDocumentIsOpen = true                  and
         tqDDocumentByDebtorTypeOpen.tcDDocumentStatus <> {&DOCUMENTSTATUS-INIT}
    break by tqDDocumentByDebtorTypeOpen.tiCompany_ID
          by tqDDocumentByDebtorTypeOpen.tcCurrencyCode:

    if first-of(tqDDocumentByDebtorTypeOpen.tiCompany_ID)
    then do:
        if not vlStart1
        then do:
            <Q-19 run CompanyPropertyByCompany (Start) in BCompanyProperty >
            assign vlStart1 = true.
        end.

        <Q-20 run CompanyPropertyByCompany (all) (Read) (NoCache)
           (input tqDDocumentByDebtorTypeOpen.tiCompany_ID, (CompanyId)
            input ?, (CompanyCode)
            input ?, (CurrencyCode)
            output dataset tqCompanyPropertyByCompany) in BCompanyProperty >

        find first tqCompanyPropertyByCompany where
                   tqCompanyPropertyByCompany.tiCompany_ID = tqDDocumentByDebtorTypeOpen.tiCompany_ID
                   no-error.
        if not available tqCompanyPropertyByCompany
        then do:
             assign vcMessage = #T-22'The entity &1 is not defined in the system.':255(65672)t-22#.
             <M-21 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-8596':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtor>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end.
    end.
    
    if first-of(tqDDocumentByDebtorTypeOpen.tiCompany_ID) or
       first-of(tqDDocumentByDebtorTypeOpen.tcCurrencyCode)
    then do:
        if tqDDocumentByDebtorTypeOpen.tcCurrencyCode = icCurrencyCode
        then assign vdRate      = 1
                    vdRateScale = 1.
        else do:
            <M-74 run GetExchangeRate
               (input  tqDDocumentByDebtorTypeOpen.tiCompany_ID (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  tqDDocumentByDebtorTypeOpen.tcCurrencyCode (icFromCurrencyCode), 
                input  ? (iiToCurrencyID), 
                input  icCurrencyCode (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 = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave MAIN_BLOCK.
        end.
    end.
    
    /* Calculate open balance */
    assign vdBalanceTC = tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalDebitTC - tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalCreditTC
           vdBalanceLC = tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalDebitLC - tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalCreditLC.
    
    if tqDDocumentByDebtorTypeOpen.tcCurrencyCode = icCurrencyCode
    then assign odBalance = odBalance + vdBalanceTC.
    else assign odBalance = odBalance +
                            <M-17 RoundAmount
                               (input  vdBalanceTC * vdRate * vdRateScale (idUnroundedAmount), 
                                input  0 (iiCurrencyID), 
                                input  icCurrencyCode (icCurrencyCode)) in business>.
end. /* MAIN_BLOCK */

/* Stop queries and technical class */
if vlStart1
then do:
    <Q-18 run CompanyPropertyByCompany (Stop) in BCompanyProperty >
end.

/* Error handling */
assign oiReturnStatus = viLocalReturn.