Hi all,
I'm not sure this topic can make your attention, but I made decision to post this.
One of my friend ask me how to convert any currency to another by real time
value. I've done this couple months ago but not in VFP language, I have made it
using PHP language.
After quick conversion from PHP to VFP, then I send this function to my friend
and he said thank you cause problem solved.
Okay.... here it is.
CLEAR ALL
CLEAR
? Currency_Conversion_ECB(1000, "USD", "GBP")
*!* Currency Conversion
*!*
*!* Using ECB (European Central Bank) online rates data
*!* Parameters:
*!* - tnAmount : Amount of money
*!* - tcFrom : Source currency
*!* - tcTo : Destination currency
*!* - tcClearCache : Clear download cache, set true if you want get realtime value
FUNCTION Currency_Conversion_ECB(tnAmount, tcFrom, tcTo, tlClearCache)
LOCAL lcCurrencies, lcURL, lcTarget
*!* just for sure
tcFrom = UPPER(m.tcFrom)
tcTo = UPPER(m.tcTo)
*!* check for valid currency
lcCurrencies = ;
"USD JPY BGN CZK DKK GBP HUF LTL LVL PLN RON SEK CHF NOK HRK RUB TRY AUD " + ;
"BRL CAD CNY HKD IDR ILS INR KRW MXN MYR NZD PHP SGD THB ZAR "
IF NOT (m.tcFrom $ m.lcCurrencies)
=MESSAGEBOX("Invalid source currency!")
RETURN m.tnAmount
ENDIF
IF NOT (m.tcTo $ m.lcCurrencies)
=MESSAGEBOX("Invalid destination currency!")
RETURN m.tnAmount
ENDIF
m.lcURL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
m.lcTarget = ADDBS(SYS(2023)) + SYS(2015) + ".XLS"
DECLARE LONG URLDownloadToFile IN urlmon.dll LONG, STRING, STRING, LONG, LONG
IF m.tlClearCache
DECLARE LONG DeleteUrlCacheEntry IN wininet.dll STRING
=DeleteUrlCacheEntry(m.lcURL)
ENDIF
IF URLDownloadToFile(0, m.lcURL, m.lcTarget, 0, 0) = 0
LOCAL lcData, lnFromRate, lnToRate
lcData = FILETOSTR(m.lcTarget)
ERASE (m.lcTarget)
*!* lookup for source rate
lnFromRate = VAL(STREXTRACT(m.lcData, "<Cube currency='" + m.tcFrom + "' rate='", "'"))
*!* lookup for destination rate
lnToRate = VAL(STREXTRACT(m.lcData, "<Cube currency='" + m.tcTo + "' rate='", "'"))
*!* now calculate and return new amount
RETURN ((m.tnAmount / m.lnFromRate) * m.lnToRate)
ELSE
=MESSAGEBOX("Cannot retrieve rates data from ECB!")
ENDIF
RETURN m.tnAmount
ENDFUNC
Maybe this function have weakness, but you can improve it by your self.
And for another option, you can use Google finance data to do the same purpose.
Finaly... enjoy it.
Regards,
Onytoo