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
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-22 18:15:12 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-22 18:15:12 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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;
|
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
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;
|
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 = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2017-10-22 18:15:12 +02:00
|
|
|
/** @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 {
|
2022-01-29 14:11:12 +01:00
|
|
|
$html = view('json.piggy-banks', compact('info'))->render();
|
2023-12-20 19:35:52 +01:00
|
|
|
} 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
|
|
|
|
2022-11-02 06:25:37 +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
|
|
|
}
|