2017-10-22 18:15:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FrontpageController.php
|
2019-10-02 06:37:26 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
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;
|
|
|
|
|
|
|
|
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;
|
2018-08-04 17:30:06 +02:00
|
|
|
use Log;
|
|
|
|
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.
|
|
|
|
*
|
2017-11-18 05:46:19 +01:00
|
|
|
* @param PiggyBankRepositoryInterface $repository
|
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
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!
|
|
|
|
$pct = round(($amount / $piggyBank->targetamount) * 100);
|
|
|
|
|
|
|
|
$entry = [
|
|
|
|
'id' => $piggyBank->id,
|
|
|
|
'name' => $piggyBank->name,
|
|
|
|
'amount' => $amount,
|
|
|
|
'target' => $piggyBank->targetamount,
|
|
|
|
'percentage' => $pct,
|
|
|
|
];
|
|
|
|
|
|
|
|
$info[] = $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$html = '';
|
2019-05-30 12:31:19 +02:00
|
|
|
if (count($info) > 0) {
|
2018-08-04 17:30:06 +02:00
|
|
|
try {
|
|
|
|
$html = view('json.piggy-banks', compact('info'))->render();
|
2018-08-31 21:12:53 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-08-04 17:30:06 +02:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
Log::error(sprintf('Cannot render json.piggy-banks: %s', $e->getMessage()));
|
|
|
|
$html = 'Could not render view.';
|
|
|
|
}
|
2018-08-31 21:12:53 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
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
|
|
|
}
|