-
Notifications
You must be signed in to change notification settings - Fork 3
/
cron.php
63 lines (55 loc) · 1.84 KB
/
cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
* @author Carlos García Gómez [email protected]
* @copyright 2016, Carlos García Gómez. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_model('divisa.php');
/**
* Description of cron_google_divisas
*
* @author carlos
*/
class cron_google_divisas
{
public function __construct()
{
$fsvar = new fs_var();
if ($fsvar->simple_get('google_divisas_cron')) {
$divisa = new divisa();
foreach ($divisa->all() as $div) {
if ($div->coddivisa != 'EUR') {
$div->tasaconv_compra = $div->tasaconv = $this->convert_currency(1, 'EUR', $div->coddivisa);
$div->save();
echo '.';
}
}
} else {
echo 'Cron desactivado.';
}
}
private function convert_currency($amount, $from, $to)
{
$url = "https://api.exchangerate-api.com/v4/latest/" . $from;
$data = fs_file_get_contents($url);
$json = json_decode($data, true);
$tasa = 0;
if (isset($json['rates'][$to])) {
$tasa = (float) $json['rates'][$to];
}
return $amount * $tasa;
}
}
new cron_google_divisas();