summaryrefslogtreecommitdiff
path: root/pages/api/money/accounts.php
blob: 9f853236c47e6b59deb96064936fdf059d34b5f6 (plain)
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
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/session.inc"; global $isLoggedIn; global $_PROFILE;
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/functions.inc";
if (!$isLoggedIn) header("Location: /-/login") and die();
header("Content-Type: application/json");

$accounts = array_map(function ($i) {
    $name = substr($i, 0, -5);
    $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/money/" . $i), true);
    $data["_name"] = $name;
    return $data;
}, array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/money"), function ($i) { return !str_starts_with($i, "."); })));
$rate = (float)trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/exchange.txt"));
$obj = [
    "total" => 0,
    "exchange_rate" => $rate,
    "users" => [
        "cloudburst" => [
            "total" => 0,
            "accounts" => []
        ],
        "raindrops" => [
            "total" => 0,
            "accounts" => []
        ]
    ]
];

$allAccounts = array_reduce(array_map(function ($i) {
    return calculateFullAmount($i, true, true);
}, $accounts), function ($a, $b) {
    return $a + $b;
});
$obj["total"] = [
    "gbp" => round($allAccounts, 2),
    "eur" => round($allAccounts * (1 / $rate), 2)
];

/* ------------------- */

$allAccounts = array_reduce(array_map(function ($i) {
    return calculateFullAmount($i, true, true);
}, array_values(array_filter($accounts, function ($i) { return $i["owner"] === "cloudburst"; }))), function ($a, $b) {
    return $a + $b;
});

$obj["users"]["cloudburst"]["total"] = $allAccounts;

foreach ($accounts as $index => $account) {
    if ($account["owner"] === "cloudburst") {
        $acc = [];

        $acc["id"] = $account["_name"];
        $acc["name"] = $account["name"];
        $acc["currency"] = $account["currency"];
        $acc["default"] = $account["default"];
        $acc["total"] = round(calculateFullAmount($account, true), 2);
        $acc["interests"] = $account["interests"];
        $acc["max"] = $account["max"];
        $acc["used_percentage"] = isset($account["max"]) ? round((calculateFullAmount($account, true) / $account["max"]) * 100, 2) : null;

        $obj["users"]["cloudburst"]["accounts"][] = $acc;
    }
}
/* ------------------- */

$allAccounts = array_reduce(array_map(function ($i) {
    return calculateFullAmount($i, true, true);
}, array_values(array_filter($accounts, function ($i) { return $i["owner"] === "raindrops"; }))), function ($a, $b) {
    return $a + $b;
});

$obj["users"]["raindrops"]["total"] = $allAccounts;

foreach ($accounts as $index => $account) {
    if ($account["owner"] === "raindrops") {
        $acc = [];

        $acc["id"] = $account["_name"];
        $acc["name"] = $account["name"];
        $acc["currency"] = $account["currency"];
        $acc["default"] = $account["default"];
        $acc["total"] = round(calculateFullAmount($account, true), 2);
        $acc["interests"] = $account["interests"];
        $acc["max"] = $account["max"];
        $acc["used_percentage"] = isset($account["max"]) ? round((calculateFullAmount($account, true) / $account["max"]) * 100, 2) : null;
        $acc["transactions"] = null;

        $obj["users"]["raindrops"]["accounts"][] = $acc;
    }
}

/* ------------------- */

die(json_encode($obj, JSON_PRETTY_PRINT));