summaryrefslogtreecommitdiff
path: root/pages/api/money/account.php
blob: 1801b2811b27ad0f24976c663d329180b70708fa (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
<?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 = [];

if (!isset($_GET["id"])) {
    die(json_encode($obj, JSON_PRETTY_PRINT));
}

foreach ($accounts as $account) {
    if ($account["_name"] === $_GET["id"]) {
        $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"] = [];

        foreach ($account["transactions"] as $index => $transaction) {
            $member = getMemberWithoutSystem($transaction["author"]) ?? getMemberWithoutSystem("zdtsg");
            $trans = [
                "id" => $index,
                "author" => [
                    "avatar" => getAsset($member["_system"], $member["id"]),
                    "name" => $member["display_name"] ?? $member["name"]
                ],
                "type" => $transaction["amount"] < 0 ? "REMOVE" : "ADD",
                "amount" => round(abs($transaction["amount"]), 2),
                "date" => [
                    "ts" => strtotime($transaction["date"]),
                    "iso" => date('c', strtotime($transaction["date"])),
                    "relative" => timeAgo($transaction["date"])
                ],
                "description" => (isset($transaction["description"]) && trim($transaction["description"]) !== "") ? trim($transaction["description"]) : null
            ];

            $acc["transactions"][] = $trans;
        }

        $obj = $acc;
    }
}

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

die(json_encode($obj, JSON_PRETTY_PRINT));