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

87 lines
2.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;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2018-08-04 17:30:06 +02:00
use Throwable;
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-06-21 12:34:58 +02:00
* @param PiggyBankRepositoryInterface $repository
2017-11-18 05:46:19 +01:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
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 = [];
/** @var PiggyBank $piggyBank */
foreach ($set as $piggyBank) {
2018-02-17 10:47:32 +01:00
$amount = $repository->getCurrentAmount($piggyBank);
if (1 === bccomp($amount, '0')) {
2017-10-22 18:15:12 +02:00
// percentage!
2022-03-27 20:24:13 +02:00
$pct = 0;
2022-12-24 05:06:39 +01:00
if (0 !== bccomp($piggyBank->targetamount, '0')) {
2022-12-31 06:57:05 +01:00
$pct = (int)bcmul(bcdiv($amount, $piggyBank->targetamount), '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,
'target' => $piggyBank->targetamount,
'percentage' => $pct,
];
$info[] = $entry;
}
}
$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'))->render();
2022-10-31 05:53:36 +01:00
} catch (Throwable $e) {
2018-08-04 17:30:06 +02:00
Log::error(sprintf('Cannot render json.piggy-banks: %s', $e->getMessage()));
2023-08-30 11:58:44 +02:00
Log::error($e->getTraceAsString());
2018-08-04 17:30:06 +02:00
$html = 'Could not render view.';
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
}