project QadFinancials > class BExchangeRate > method ApiCalculateDerivedExchangeRates

Description

This method is used to calculate all necessary Exchange Rate records if two related currency had already been defined.


Parameters


itStartDateinputdateStart Date.
icExchangeRateTypeinputcharacterExchange Rate Type.
tApiDerivedExchangeRateoutputtemp-tableTemptable like ExchangeRate.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/bexchangerate.p)

oiReturnStatus = -98.

empty temp-table tApiDerivedExchangeRate.
empty temp-table tTempExchangeRate.

/* Retrieve currencies for companies with same exchange rate shared set */
<Q-1 run CompanySSByCompanyIDCodeSSType (all) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&SHAREDSETTYPECODE-EXCHANGERATE}, (SharedSetType)
    input ?, (CompanyCode)
    output dataset tqCompanySSByCompanyIDCodeSSType) in BCompany >
for each tqCompanySSByCompanyIDCodeSSType where
    tqCompanySSByCompanyIDCodeSSType.tcSharedSetTypeCode = {&SHAREDSETTYPECODE-EXCHANGERATE} and
    tqCompanySSByCompanyIDCodeSSType.tlSharedSetIsActive:
    viSharedSet_ID = tqCompanySSByCompanyIDCodeSSType.tiSharedSet_ID.

    <M-2 run GetSharedSetCompanyCurrencyIDs
       (input  viSharedSet_ID (iiSharedSetID), 
        output vcSharedSetCompanyCurrencyIDs (ocCurrencyIDList), 
        output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>

     <Q-3 run CompanyPropertyForAllData (all) (Read) (NoCache)
        (input viCompanyID, (CompanyId)
         output dataset tqCompanyPropertyForAllData) in BCompanyProperty >   
    for first tqCompanyPropertyForAllData:
        viCompanyCurrency_ID = tqCompanyPropertyForAllData.tiCurrency_ID.

        <Q-4 run ExchangeRateBySharedSet (all) (Read) (NoCache)
           (input viSharedSet_ID, (SharedSetId)
            output dataset tqExchangeRateBySharedSet) in BExchangeRate >
        for each tqExchangeRateBySharedSet where
            (tqExchangeRateBySharedSet.tiFromCurrency_ID = viCompanyCurrency_ID or
             tqExchangeRateBySharedSet.tiToCurrency_ID = viCompanyCurrency_ID) and
            tqExchangeRateBySharedSet.tcExchangeRateTypeCode =icExchangeRateType and
            ttExchangeRateValidDateTill > today:

            /* Only include exchange rates for base currencies of companies in same shared set */
            if((can-do(vcSharedSetCompanyCurrencyIDs,string(tqExchangeRateBySharedSet.tiFromCurrency_ID)) and
                tqExchangeRateBySharedSet.tiToCurrency_ID = viCompanyCurrency_ID) or
                (can-do(vcSharedSetCompanyCurrencyIDs,string(tqExchangeRateBySharedSet.tiToCurrency_ID)) and
                tqExchangeRateBySharedSet.tiFromCurrency_ID = viCompanyCurrency_ID)) then do:

                if(not can-do(vcCurrencyList,string(tqExchangeRateBySharedSet.tiFromCurrency_ID)) and
                   not can-do(vcCurrencyList,string(tqExchangeRateBySharedSet.tiToCurrency_ID))) then do:
                   
                   create tTempExchangeRate.
                   if(tqExchangeRateBySharedSet.tiFromCurrency_ID = viCompanyCurrency_ID) then do:
                       assign
                           tTempExchangeRate.tcToCurrencyCode=tqExchangeRateBySharedSet.tcCurrencyCode2
                           tTempExchangeRate.tiToCurrency_ID=tqExchangeRateBySharedSet.tiToCurrency_ID
                           tTempExchangeRate.tdExchangeRate=tqExchangeRateBySharedSet.tdExchangeRate * tqExchangeRateBySharedSet.tdExchangeRateScale
                           viCurrency_ID = tqExchangeRateBySharedSet.tiToCurrency_ID.
                   end.
                   else do:
                       assign
                           tTempExchangeRate.tcToCurrencyCode=tqExchangeRateBySharedSet.tcCurrencyCode
                           tTempExchangeRate.tiToCurrency_ID=tqExchangeRateBySharedSet.tiFromCurrency_ID
                           tTempExchangeRate.tdExchangeRate=1 / (tqExchangeRateBySharedSet.tdExchangeRate * tqExchangeRateBySharedSet.tdExchangeRateScale)
                           viCurrency_ID = tqExchangeRateBySharedSet.tiFromCurrency_ID.
                   end.

                   if(vcCurrencyList = ? or vcCurrencyList = "") then
                        vcCurrencyList = string(viCurrency_ID).
                    else
                        vcCurrencyList = vcCurrencyList + ',':U + string(viCurrency_ID) .

                  end. /* if(not can-do */
            end. /* if(can-do */
        end. /* for each tqExchangeRateBySharedSet */
    end. /* for first tqCompanyPropertyForAllData */
end. /* for first tqCompanySSByCompanyIDCodeSSType */
/*
CurrencyList will have the list of currencies which are a base currency for a company in the same shared set as 
this company and has an exchange rate (tTempExchangeRate record) to this currency.
*/
do viFcCount1 = 1 to num-entries(vcCurrencyList):
    vcCurrency1 = entry(viFcCount1,vcCurrencyList).
    find bCurrency1ER where 
        bCurrency1ER.tiToCurrency_ID=integer(vcCurrency1)
    no-error.
    if(available(bCurrency1ER)) then do:
        do viFcCount2 = viFcCount1 + 1 to num-entries(vcCurrencyList):
            vcCurrency2 = entry(viFcCount2,vcCurrencyList).    
            find bCurrency2ER where
                bCurrency2ER.tiToCurrency_ID=integer(vcCurrency2)
            no-error.
            if(available(bCurrency2ER)) then do:
                /* Determine if an exchange rate already exists */
                <Q-5 run ExchangeRateByCurrencies (all) (Read) (NoCache)
                   (input viCompanyID, (CompanyId)
                    input integer(vcCurrency1), (FromCurrency_ID)
                    input integer(vcCurrency2), (ToCurrency_ID)
                    output dataset tqExchangeRateByCurrencies) in BExchangeRate >
                find first tqExchangeRateByCurrencies where
                    tqExchangeRateByCurrencies.ttExchangeRateValidDateFrom >= itStartDate and
                    tqExchangeRateByCurrencies.tcExchangeRateTypeCode = icExchangeRateType
                no-error.
                if(not(available(tqExchangeRateByCurrencies))) then do:
                    create tApiDerivedExchangeRate.
                    assign
                        tApiDerivedExchangeRate.tiFromCurrency_ID=bCurrency1ER.tiToCurrency_ID
                        tApiDerivedExchangeRate.tcFromCurrencyCode=bCurrency1ER.tcToCurrencyCode
                        tApiDerivedExchangeRate.tiToCurrency_ID=bCurrency2ER.tiToCurrency_ID
                        tApiDerivedExchangeRate.tcToCurrencyCode=bCurrency2ER.tcToCurrencyCode
                        tApiDerivedExchangeRate.tdExchangeRate= if bCurrency1ER.tdExchangeRate > 0 then
                            (bCurrency2ER.tdExchangeRate / bCurrency1ER.tdExchangeRate) else 0                        
                        tApiDerivedExchangeRate.tdExchangeRateScale=1.0
                        tApiDerivedExchangeRate.tlIsDerived=true.
                end. /* if(not(available(tqExchangeRateByCurrencies)) */
            end. /* if(available(bCurrency2ER)) */
        end. /* do viFcCount2... */
    end. /* if(available(bCurrency1ER)) */
end. /* do viFcCount1... */

if(oiReturnStatus = -98) then oiReturnStatus = 0.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BExchangeRate".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiCalculateDerivedExchangeRates".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/bexchangerate.apicalculatederivedexchangerates.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::itStartDate = <parameter value>
       vhParameter::icExchangeRateType = <parameter value>.

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.