Files
firefly-iii/app/Http/Controllers/Json/FrontpageController.php

104 lines
3.8 KiB
PHP
Raw Normal View History

2017-10-22 18:15:12 +02:00
<?php
2022-12-24 05:06:39 +01:00
2017-10-22 18:15:12 +02:00
/**
* FrontpageController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-10-22 18:15:12 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-22 18:15:12 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-22 18:15:12 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-22 18:15:12 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-22 18:15:12 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-10-22 18:15:12 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Exceptions\FireflyException;
2017-10-22 18:15:12 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2017-10-22 18:15:12 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class FrontpageController.
2017-10-22 18:15:12 +02:00
*/
class FrontpageController extends Controller
{
/**
2018-07-21 08:06:24 +02:00
* Piggy bank pie chart.
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2017-10-22 18:15:12 +02:00
*/
2018-07-08 12:28:42 +02:00
public function piggyBanks(PiggyBankRepositoryInterface $repository): JsonResponse
2017-10-22 18:15:12 +02:00
{
$set = $repository->getPiggyBanks();
$info = [];
$native = Amount::getNativeCurrency();
$convertToNative = Amount::convertToNative();
2023-12-20 19:35:52 +01:00
2017-10-22 18:15:12 +02:00
/** @var PiggyBank $piggyBank */
foreach ($set as $piggyBank) {
$amount = $repository->getCurrentAmount($piggyBank);
$nativeAmount = $repository->getCurrentNativeAmount($piggyBank);
2018-02-17 10:47:32 +01:00
if (1 === bccomp($amount, '0')) {
2017-10-22 18:15:12 +02:00
// percentage!
$pct = 0;
2025-05-04 13:47:00 +02:00
if (0 !== bccomp((string) $piggyBank->target_amount, '0')) {
$pct = (int) bcmul(bcdiv($amount, (string) $piggyBank->target_amount), '100');
2022-03-27 20:24:13 +02:00
}
2017-10-22 18:15:12 +02:00
$entry = [
'id' => $piggyBank->id,
'name' => $piggyBank->name,
'amount' => $amount,
'native_amount' => $nativeAmount,
'target' => $piggyBank->target_amount,
'native_target' => $piggyBank->native_target_amount,
'percentage' => $pct,
// currency:
'currency_symbol' => $piggyBank->transactionCurrency->symbol,
'currency_decimal_places' => $piggyBank->transactionCurrency->decimal_places,
'native_currency_symbol' => $native->symbol,
'native_currency_decimal_places' => $native->decimal_places,
2017-10-22 18:15:12 +02:00
];
$info[] = $entry;
}
}
2024-11-03 08:56:19 +01:00
// sort by current percentage (lowest at the top)
uasort(
$info,
static fn (array $a, array $b) => $a['percentage'] <=> $b['percentage']
2024-11-03 08:56:19 +01:00
);
$html = '';
2022-11-04 05:11:05 +01:00
if (0 !== count($info)) {
2018-08-04 17:30:06 +02:00
try {
$html = view('json.piggy-banks', compact('info', 'convertToNative', 'native'))->render();
} catch (\Throwable $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Cannot render json.piggy-banks: %s', $e->getMessage()));
app('log')->error($e->getTraceAsString());
2018-08-04 17:30:06 +02:00
$html = 'Could not render view.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
2018-08-04 17:30:06 +02:00
}
2017-10-22 18:15:12 +02:00
}
2018-03-10 20:30:09 +01:00
return response()->json(['html' => $html]);
2017-10-22 18:15:12 +02:00
}
2017-11-08 09:05:10 +01:00
}