diff options
author | RaindropsSys <contact@minteck.org> | 2023-05-16 22:17:15 +0200 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-05-16 22:17:15 +0200 |
commit | 98a84f3aa735f31b08e807c8a4f0549742cbd1b0 (patch) | |
tree | 9a510d41197d6af646f1ee0bdbf261437211f1bc /pages/money.inc | |
parent | 60f7da42c41b35b44fa397397a5e4bccd6039d45 (diff) | |
download | pluralconnect-98a84f3aa735f31b08e807c8a4f0549742cbd1b0.tar.gz pluralconnect-98a84f3aa735f31b08e807c8a4f0549742cbd1b0.tar.bz2 pluralconnect-98a84f3aa735f31b08e807c8a4f0549742cbd1b0.zip |
Updated 8 files (automated)
Diffstat (limited to 'pages/money.inc')
-rw-r--r-- | pages/money.inc | 124 |
1 files changed, 121 insertions, 3 deletions
diff --git a/pages/money.inc b/pages/money.inc index 4d93398..1338a3a 100644 --- a/pages/money.inc +++ b/pages/money.inc @@ -100,6 +100,76 @@ if ((isset($_GET["create"]) || isset($_GET["delete"])) && isset($parts[2])) { $rate = (float)trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/exchange.txt")); require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; +$transactionMonths = []; + +foreach ($accounts as $account) { + array_push($transactionMonths, ...array_map(function ($i) { return date('Y-m', strtotime($i["date"])); }, array_filter($account["transactions"], function ($i) { return !isset($i["initial"]) || !$i["initial"]; }))); +} + +$months = [ + ...[ + "2022-12", "2022-11", "2022-10", "2022-09", + "2022-08", "2022-07", "2022-06" + ], + ...array_values(array_filter(array_unique(array_reduce($transactionMonths, function ($a, $b) { + return [...$a, $b]; + }, [])), function ($i) { + return $i !== date('Y-m'); + })) +]; + +$monthlyCloudburst = []; +$monthlyRaindrops = []; + +$bits = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/bits.json"), true); +$projects = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/projects.json"), true); + +foreach ($bits["cloudburst"] as $month => $value) { + $monthlyCloudburst[$month] = $value; +} + +foreach ($bits["raindrops"] as $month => $value) { + $monthlyRaindrops[$month] = $value; +} + +foreach ($months as $month) { + $gainedCloudburst = $monthlyCloudburst[$month] ?? 0; + $gainedRaindrops = $monthlyRaindrops[$month] ?? 0; + + foreach ($accounts as $account) { + foreach ($account["transactions"] as $transaction) { + if (isset($transaction["initial"]) && $transaction["initial"]) continue; + if (date('Y-m', strtotime($transaction["date"])) !== $month) continue; + + if ($account["owner"] === "raindrops") { + $gainedRaindrops += $transaction["amount"]; + } else { + $gainedCloudburst += $transaction["amount"]; + } + } + } + + $monthlyCloudburst[$month] = $gainedCloudburst; + $monthlyRaindrops[$month] = $gainedRaindrops; +} + +uksort($monthlyCloudburst, function ($k1, $k2) { + return strtotime($k2) - strtotime($k1); +}); + +uksort($monthlyRaindrops, function ($k1, $k2) { + return strtotime($k2) - strtotime($k1); +}); + +function getMonthlyEarnings(): array { + global $monthlyRaindrops; + global $monthlyCloudburst; + + return [ + "cloudburst" => array_sum(array_values($monthlyCloudburst)) / count($monthlyCloudburst), + "raindrops" => array_sum(array_values($monthlyRaindrops)) / count($monthlyRaindrops) + ]; +} ?> @@ -267,7 +337,21 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; return $a + $b; }); - ?>£<?= number_format($allAccounts, 2, '.', ',') ?>)</h4> + ?>£<?= number_format($allAccounts, 2, '.', ',') ?>, earning £<?= number_format(getMonthlyEarnings()["cloudburst"], 2, '.', ',') ?>/month)</h4> + <div class="list-group"> + <details class="list-group-item list-group-item-action" style="margin-bottom: 10px;"> + <summary>Show monthly earnings</summary> + + <table style="margin-top: 10px;"> + <?php foreach ($monthlyCloudburst as $month => $value): ?> + <tr> + <td style="padding-right: 20px;"><b><?= date('F Y', strtotime($month)) ?>:</b></td> + <td class="text-<?= $value > 0 ? "success" : ($value === 0 ? "warning" : "danger") ?>">£<?= number_format($value, 2, '.', ',') ?></td> + </tr> + <?php endforeach; ?> + </table> + </details> + </div> <div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 20px;"> <?php foreach ($accounts as $index => $account): if ($account["owner"] === "cloudburst"): ?> <a style="color: white; text-decoration: none;" href="/-/money/<?= $account["_name"] ?>"> @@ -385,7 +469,21 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; return $a + $b; }); - ?>€<?= number_format($allAccounts, 2, '.', ',') ?>)</h4> + ?>€<?= number_format($allAccounts, 2, '.', ',') ?>, earning €<?= number_format(getMonthlyEarnings()["raindrops"], 2, '.', ',') ?>/month)</h4> + <div class="list-group"> + <details class="list-group-item list-group-item-action" style="margin-bottom: 10px;"> + <summary>Show monthly earnings</summary> + + <table style="margin-top: 10px;"> + <?php foreach ($monthlyRaindrops as $month => $value): ?> + <tr> + <td style="padding-right: 20px;"><b><?= date('F Y', strtotime($month)) ?>:</b></td> + <td class="text-<?= $value > 0 ? "success" : ($value === 0 ? "warning" : "danger") ?>">€<?= number_format($value, 2, '.', ',') ?></td> + </tr> + <?php endforeach; ?> + </table> + </details> + </div> <div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 20px;"> <?php foreach ($accounts as $index => $account): if ($account["owner"] === "raindrops"): ?> <a style="color: white; text-decoration: none;" href="/-/money/<?= $account["_name"] ?>"> @@ -423,6 +521,26 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; </div> <p style="text-align: center; margin-top: 5px;" class="<?= $allAccounts > $minimumRaindrops ? "" : "bold text-danger" ?>">€<?= number_format($difference, 2, '.', ',') ?><?php if ($allAccounts > $minimumRaindrops): ?> (<?= round($part2 * 100, 2) ?>%)<?php endif; ?> <?= $allAccounts > $minimumRaindrops ? "over" : "under" ?> the minimum</p> + <?php if (isset($projects["raindrops"]) && $projects["raindrops"]["enable"]): ?> + <div class="grid" style="margin-bottom: 10px; display: grid; grid-template-columns: 25% 1fr; grid-gap: 20px;"> + <div style="width: 100%; display: flex; align-items: center; justify-content: center;"> + <img src="<?= $projects["raindrops"]["image"] ?>" style="width: 100%;"> + </div> + <div style="display: flex; align-items: center;"> + <div style="width: 100%;"> + <h2><?= strip_tags($projects["raindrops"]["name"]) ?></h2> + <div class="progress" style="margin-top: 20px; margin-bottom: 20px; width: 100%; background-color: #222;"> + <div class="progress-bar bg-success" style="width: <?= ($allAccounts - $minimumRaindrops >= 0) ? (($allAccounts - $minimumRaindrops) / $projects["raindrops"]["cost"]) * 100 : 0 ?>%"></div> + </div> + <?php $missing = $projects["raindrops"]["cost"] - ($allAccounts - $minimumRaindrops); if ($missing > 0): $months = ceil($missing / getMonthlyEarnings()["raindrops"]); ?> + <div>Costs €<?= number_format($projects["raindrops"]["cost"], 2, '.', ',') ?> · Missing €<?= number_format($missing, 2, '.', ',') ?> · Can afford it in <?= date('F Y', time() + ($months * 2678400)) ?> (in <?= $months ?> month<?= $months > 1 ? "s" : "" ?>)</div> + <?php elseif ($missing === 0): ?><div>You can afford it now, this will take you to the minimum.</div> + <?php else: ?><div>You can afford it now, this will take you €<?= -$missing ?> above the minimum.</div><?php endif; ?> + </div> + </div> + </div> + <?php endif; ?> + <canvas id="history-raindrops" style="margin-top: 10px; width: 100%; height: 200px; max-height: 100%;"></canvas> <?php @@ -582,7 +700,7 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/components/header.inc'; <div class="list-group"> <a href="#" data-bs-toggle="modal" data-bs-target="#create" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: max-content 1fr;"><img src="/assets/icons/add.svg" style="filter: invert(1); margin-right: 10px; height: 32px; width: 32px;"><div style="display: flex; align-items: center;"><b>Add new transaction</b></div></a> <?php foreach ($account["transactions"] as $index => $transaction): $member = getMemberWithoutSystem($transaction["author"]) ?? getMemberWithoutSystem("zdtsg"); ?> - <a id="transaction-<?= $index ?>" onclick="deleteTransaction(<?= $index ?>)" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: max-content 1fr;cursor:pointer;"><div style="display: flex; align-items: center;"><img src="<?= getAsset($member["_system"], $member["id"]) ?>" style="border-radius: 999px; width: 32px; height: 32px; margin-right: 10px;"></div><div style="display: flex; align-items: center;"><div><b><?= $member["display_name"] ?? $member["name"] ?></b><?= $transaction["amount"] < 0 ? " removed" : " added" ?> <span class="<?= $transaction["amount"] < 0 ? "text-danger" : "text-success" ?>"><?= $account["currency"] === "gbp" ? "£" : "€" ?><?= number_format(abs($transaction["amount"]), 2, '.', ',') ?> </span><?= timeAgo($transaction["date"]) ?><?php if (isset($transaction["description"]) && trim($transaction["description"]) !== ""): ?>: <?= strip_tags(trim($transaction["description"])) ?><?php endif; ?></div></div></a> + <a id="transaction-<?= $index ?>" onclick="deleteTransaction(<?= $index ?>)" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: max-content 1fr;cursor:pointer;"><div style="display: flex; align-items: center;"><img src="<?= getAsset($member["_system"], $member["id"]) ?>" style="border-radius: 999px; width: 32px; height: 32px; margin-right: 10px;"></div><div style="display: flex; align-items: center;"><div><b><?= $member["display_name"] ?? $member["name"] ?></b><?= $transaction["amount"] < 0 ? " removed" : " added" ?> <span class="<?= $transaction["amount"] < 0 ? "text-danger" : "text-success" ?>"><?= $account["currency"] === "gbp" ? "£" : "€" ?><?= number_format(abs($transaction["amount"]), 2, '.', ',') ?> </span><?= timeAgo($transaction["date"]) ?><?php if (isset($transaction["description"]) && trim($transaction["description"]) !== "" && (!isset($transaction["initial"]) || !$transaction["initial"])): ?>: <?= strip_tags(trim($transaction["description"])) ?><?php endif; ?><?php if (isset($transaction["initial"]) && $transaction["initial"]): ?> <span class="badge bg-secondary rounded-pill">Initial amount</span><?php endif; ?></div></div></a> <?php endforeach; ?> </div> |