-
Notifications
You must be signed in to change notification settings - Fork 1
/
ing.php
147 lines (132 loc) · 5.44 KB
/
ing.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require_once 'conf/db.php';
require_once 'conf/auth.php';
function sendPush($message) {
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => $pushover_token,
"user" => $pushover_recipient,
"message" => $message,
),
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
));
curl_exec($ch);
curl_close($ch);
}
$now = new DateTime();
foreach (AFTERBANKS as $id => $account) {
$result = $con->query("SELECT MAX(fecha) ultimo FROM MOVIMIENTO WHERE cuenta = {$id}");
$movimiento = $result->fetch_assoc();
$from = $movimiento['ultimo'];
if ($from) {
$from = DateTime::createFromFormat('Y-m-d', $from);
$diff = $from->diff($now);
$from->sub(new DateInterval('P3D'));
}
$categorias = [];
$result = $con->query("SELECT * FROM CATEGORIA");
while ($categoria = $result->fetch_assoc()) {
$categorias['' . $categoria['id']] = $categoria['nombre'];
}
$page = 1;
$total = 0;
while (true) {
$ch = curl_init();
$url = 'www.afterbanks.com/apiapp/getTransactions/?advSelectedAccounts=' . $account . '&page=' . $page++;
if ($from) $url .= '&advDateFrom=' . $from->format('Y-m-d');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('O-AUTH-TOKEN:' . $afterbanks_token));
$transfers = json_decode(curl_exec($ch));
curl_close($ch);
if (!is_array($transfers)) {
sendPush('Algo fue mal: ' . serialize($transfers) . ' llamando: ' . $url);
break;
}
if (count($transfers) == 0) {
break;
} else {
foreach ($transfers as $index => $transfer) {
try {
if (!isset($categorias[$transfer->idType])) {
$con->query("INSERT INTO CATEGORIA (id) VALUES ({$transfer->idType})");
$categorias[$transfer->idType] = '';
}
$con->query("INSERT INTO MOVIMIENTO (hash, fecha, cuenta, categoria, importe, descripcion) VALUES ('{$transfer->md5}', '{$transfer->date}', {$id}, {$transfer->idType}, {$transfer->amount}, '{$transfer->description}')");
if ($con->affected_rows > 0) {
$total += $con->affected_rows;
$amount = number_format(abs(floatval($transfer->amount)), 2, ',', '.');
if (floatval($transfer->amount) < -1) {
sendPush('Adeudo de ' . $amount . ' € - ' . $transfer->description);
}
if (floatval($transfer->amount) > 1) {
sendPush('Abono de ' . $amount . ' € - ' . $transfer->description);
}
}
} catch (Exception $e) {
// Intentamos con los siguientes
}
}
}
}
// if ($total > 0) {
// sendPush($account . ': ' . $total . ' movimientos insertados (' . $now->format('Y-m-d H:i') . ')');
// } else {
// sendPush($account . ': No se registran movimientos (' . $now->format('Y-m-d H:i') . ')');
// // if (isset($diff)) {
// // $diff = intval($diff->format('%a'));
// // if ($diff > 2) { // Los fines de semana no hay actualizaciones
// // sendPush('No se registran movimientos en los últimos ' . $diff . ' días');
// // }
// // }
// }
}
$ch = curl_init();
$url = 'www.afterbanks.com/apiapp/getBanks';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('O-AUTH-TOKEN:' . $afterbanks_token));
$banks = json_decode(curl_exec($ch));
curl_close($ch);
foreach ($banks as $bank) {
foreach ($bank->products as $product) {
$con->query("UPDATE CUENTA SET codigo = '{$product->product}',
balance = {$product->balance},
color = CASE WHEN color IS NULL THEN '{$bank->color}' ELSE color END
WHERE descripcion = '{$product->description}'");
}
}
foreach ($categorias as $id => $nombre) {
if (strlen($nombre) == 0) {
actualizarCategorias();
break;
}
}
function actualizarCategorias() {
global $con;
$total = 0;
$ch = curl_init();
$url = 'www.afterbanks.com/apiapp/categories';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$categories = json_decode(curl_exec($ch));
curl_close($ch);
foreach ($categories as $id => $categorie) {
$color = substr($categorie->color, 1);
$con->query("UPDATE CATEGORIA SET nombre = '{$categorie->description}', color = '{$color}' WHERE id = {intval($id)}");
if ($con->affected_rows > 0) {
$total += $con->affected_rows;
}
}
echo $total . ' categorias actualizadas
';
}
?>