project QadFinancials > class BDebtor > method CalculateCreditOnTurnover


Parameters


iiDebtorIdinputinteger
icDebtorCodeinputcharacter
icCurrencyCodeinputcharacter
idPercentageOfTurnoverinputdecimal
odCreditOnTurnoveroutputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtor.ApiGetTurnoverCreditLimit
method BDebtor.CalculateBalances
method BDebtorReport.CustomerCreditOverview
method BDebtorReport.DebtorAgeingAnalysisBackwards
method BDebtorReport.DebtorAgeingAnalysisCurrent
method BDebtorReport.DebtorAgingHistAcroDomain


program code (program6/bdebtor.p)

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

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

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

if iiDebtorId     = 0 or
   icCurrencyCode = "":U
then do:
    <Q-1 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-7 run SetMessage
           (input  #T-8'Customer ($1) is not defined in the system.':200(31311)t-8# (icMessage), 
            input  icDebtorCode (icArguments), 
            input  'DebtorCode':U (icFieldName), 
            input  icDebtorCode (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-8598':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        assign oiReturnStatus = -1.
        return.
    end.

    if iiDebtorId = 0
    then 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-2 run PeriodByStartEndDate (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input today, (Date)
    output dataset tqPeriodByStartEndDate) in BPeriod >

find first tqPeriodByStartEndDate where
           tqPeriodByStartEndDate.ttPeriodStartDate <= today and
           tqPeriodByStartEndDate.ttPeriodEndDate   >= today
           no-error.

if not available tqPeriodByStartEndDate
then do:
    assign oiReturnStatus = 0.
    return.
end.


assign viPeriodFrom = (tqPeriodByStartEndDate.tiPeriodYear - 1) * 100 + tqPeriodByStartEndDate.tiPeriodPeriod
       viPeriodTill = tqPeriodByStartEndDate.tiPeriodYear * 100 + tqPeriodByStartEndDate.tiPeriodPeriod.

<Q-3 run DHistByDebtorFromTill (all) (Read) (NoCache)
   (input ?, (CompanyId)
    input iiDebtorId, (DebtorId)
    input viPeriodFrom, (DHistYearPeriodFrom)
    input viPeriodTill, (DHistYearPeriodTill)
    output dataset tqDHistByDebtorFromTill) in BDebtorBalance >

MAIN_BLOCK:
for each tqDHistByDebtorFromTill where
         tqDHistByDebtorFromTill.tiDebtor_ID            = iiDebtorId   and
         tqDHistByDebtorFromTill.tiDHistYearPeriodFrom >= viPeriodFrom and
         tqDHistByDebtorFromTill.tiDHistYearPeriodFrom  < viPeriodTill
    break by tqDHistByDebtorFromTill.tcCurrencyCode:

    if first-of(tqDHistByDebtorFromTill.tcCurrencyCode)
    then do:
        if tqDHistByDebtorFromTill.tcCurrencyCode = icCurrencyCode
        then assign vdRate      = 1
                    vdRateScale = 1.
        else do:
             <M-21 run GetExchangeRate
                (input  viCompanyID (iiCompanyID), 
                 input  ? (iiFromCurrencyID), 
                 input  tqDHistByDebtorFromTill.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 balance */
    assign vdCreditOnTurnover = tqDHistByDebtorFromTill.tdDHistGrossInvoiceTC - tqDHistByDebtorFromTill.tdDHistGrossCredNoteTC.

    if tqDHistByDebtorFromTill.tcCurrencyCode = icCurrencyCode
    then assign odCreditOnTurnover = odCreditOnTurnover + vdCreditOnTurnover.
    else assign odCreditOnTurnover = odCreditOnTurnover +
                                    <M-5 RoundAmount
                                       (input  vdCreditOnTurnover * vdRate * vdRateScale (idUnroundedAmount), 
                                        input  0 (iiCurrencyID), 
                                        input  icCurrencyCode (icCurrencyCode)) in business>.
end.

assign odCreditOnTurnover = <M-6 RoundAmount
                               (input  odCreditOnTurnover * idPercentageOfTurnover / 100 (idUnroundedAmount), 
                                input  0 (iiCurrencyID), 
                                input  icCurrencyCode (icCurrencyCode)) in business>.

/* Stop queries and technical class */
if valid-handle(vhTExchangeRateInst)
then do :
    run gipr_DeleteProcedure in vhTExchangeRateInst no-error.
    delete procedure vhTExchangeRateInst.
end.

/* Error handling */
assign oiReturnStatus = viLocalReturn.