hedera-web/rest/misc/exchange-rate.php

52 lines
1.1 KiB
PHP
Raw Normal View History

<?php
/**
* Ejemplo:
* <Cube><Cube time="2010-12-10"><Cube currency="USD" rate="1.3244"/>
2016-12-20 09:32:17 +00:00
*/
class ExchangeRate extends Vn\Lib\Method
{
2016-09-23 22:47:34 +00:00
function run ($db)
{
2016-08-31 11:53:46 +00:00
$db->selectDb ('vn2008');
// Indica la URL del archivo
$xml = new SimpleXMLElement (
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml', 0, TRUE);
$date = $db->getValue ("SELECT MAX(date) fecha FROM reference_rate");
$maxDate = $date ? DateTime::createFromFormat ('Y-m-d', $date) : NULL;
foreach ($xml->Cube[0]->Cube as $cube)
{
$xmlDate = new DateTime ($cube['time']);
2016-12-20 09:32:17 +00:00
// Si existen datos m<>s recientes de la m<>xima fecha los a<>ade
if ($maxDate < $xmlDate)
foreach ($cube->Cube as $subCube)
if ($subCube['currency'] == 'USD')
{
$params = [
'date' => $xmlDate,
'rate' => $subCube['rate']
];
$db->query (
'REPLACE INTO reference_rate (moneda_id, date, rate)
VALUES (2, #date, #rate)',
$params
);
$db->query (
'REPLACE INTO reference_rate (moneda_id, date, rate)
VALUES (2, TIMESTAMPADD(DAY, 1, #date), #rate)',
$params
);
}
}
$db->queryFromFile (__DIR__.'/exrate-add');
}
}