2015-05-16 02:41:14 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* PiggyBankController.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
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.
|
|
|
|
*
|
2018-08-27 11:59:30 -05:00
|
|
|
* 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
|
|
|
}
|