firefly-iii/app/Http/Controllers/Chart/PiggyBankController.php

121 lines
4.2 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
/**
* PiggyBankController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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-21 01:40:00 -05: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/>.
*/
declare(strict_types=1);
2015-05-16 02:41:14 -05:00
namespace FireflyIII\Http\Controllers\Chart;
2018-04-15 12:20:04 -05:00
use Carbon\Carbon;
2016-12-15 07:05:50 -06:00
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
2015-05-16 02:41:14 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\PiggyBank;
2016-05-01 00:09:58 -05:00
use FireflyIII\Models\PiggyBankEvent;
2015-05-16 02:41:14 -05:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2015-06-27 04:44:18 -05:00
use FireflyIII\Support\CacheProperties;
2018-07-14 16:22:08 -05:00
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
2018-04-15 12:20:04 -05:00
use Illuminate\Support\Collection;
2015-05-16 02:41:14 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class PiggyBankController.
2015-05-16 02:41:14 -05:00
*/
class PiggyBankController extends Controller
{
2018-07-14 16:22:08 -05:00
use DateCalculation;
2018-07-21 01:06:24 -05:00
/** @var GeneratorInterface Chart generation methods. */
2015-06-27 04:44:18 -05:00
protected $generator;
/**
2018-07-21 01:06:24 -05:00
* PiggyBankController constructor.
2019-06-29 01:14:28 -05:00
* @codeCoverageIgnore
2015-06-27 04:44:18 -05:00
*/
public function __construct()
{
parent::__construct();
// create chart generator:
2016-12-15 07:05:50 -06:00
$this->generator = app(GeneratorInterface::class);
2015-06-27 04:44:18 -05:00
}
2015-05-16 02:41:14 -05:00
/**
* Shows the piggy bank history.
*
* TODO this chart is not multi-currency aware.
*
2015-05-16 02:41:14 -05:00
* @param PiggyBankRepositoryInterface $repository
* @param PiggyBank $piggyBank
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2018-07-17 15:21:03 -05:00
*
2015-05-16 02:41:14 -05:00
*/
2018-07-08 05:28:42 -05:00
public function history(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank): JsonResponse
2015-05-16 02:41:14 -05:00
{
2015-06-27 04:44:18 -05:00
// chart properties for cache:
$cache = new CacheProperties;
2016-12-15 07:05:50 -06:00
$cache->addProperty('chart.piggy-bank.history');
2015-06-27 04:44:18 -05:00
$cache->addProperty($piggyBank->id);
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2015-05-16 02:41:14 -05:00
}
2018-04-15 12:20:04 -05:00
$set = $repository->getEvents($piggyBank);
$set = $set->reverse();
// get first event or start date of piggy bank or today
$startDate = $piggyBank->start_date ?? new Carbon;
/** @var PiggyBankEvent $first */
$firstEvent = $set->first();
$firstDate = null === $firstEvent ? new Carbon : $firstEvent->date;
// which ever is older:
$oldest = $startDate->lt($firstDate) ? $startDate : $firstDate;
$today = new Carbon;
// depending on diff, do something with range of chart.
2018-07-14 16:22:08 -05:00
$step = $this->calculateStep($oldest, $today);
2015-05-16 02:41:14 -05:00
2016-12-15 07:05:50 -06:00
$chartData = [];
2018-04-15 12:20:04 -05:00
while ($oldest <= $today) {
/** @var Collection $filtered */
$filtered = $set->filter(
function (PiggyBankEvent $event) use ($oldest) {
return $event->date->lte($oldest);
}
);
$currentSum = $filtered->sum('amount');
$label = $oldest->formatLocalized((string)trans('config.month_and_day'));
$chartData[$label] = $currentSum;
$oldest = app('navigation')->addPeriod($oldest, $step, 0);
2016-05-01 00:09:58 -05:00
}
2018-07-08 00:59:58 -05:00
/** @var Collection $finalFiltered */
2018-04-15 12:24:20 -05:00
$finalFiltered = $set->filter(
function (PiggyBankEvent $event) use ($today) {
return $event->date->lte($today);
}
);
2018-07-08 00:59:58 -05:00
$finalSum = $finalFiltered->sum('amount');
2018-04-15 12:24:20 -05:00
$finalLabel = $today->formatLocalized((string)trans('config.month_and_day'));
$chartData[$finalLabel] = $finalSum;
2016-05-01 00:09:58 -05:00
2016-12-15 07:05:50 -06:00
$data = $this->generator->singleSet($piggyBank->name, $chartData);
2015-06-27 04:44:18 -05:00
$cache->store($data);
2015-05-16 02:41:14 -05:00
2018-03-10 13:30:09 -06:00
return response()->json($data);
2015-05-16 02:41:14 -05:00
}
2015-05-20 12:56:14 -05:00
}