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
|
|
|
|
* 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;
|
|
|
|
|
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;
|
2015-05-16 02:41:14 -05:00
|
|
|
use Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PiggyBankController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Chart
|
|
|
|
*/
|
|
|
|
class PiggyBankController extends Controller
|
|
|
|
{
|
2015-06-27 04:44:18 -05:00
|
|
|
|
2016-12-15 07:05:50 -06:00
|
|
|
/** @var GeneratorInterface */
|
2015-06-27 04:44:18 -05:00
|
|
|
protected $generator;
|
|
|
|
|
|
|
|
/**
|
2016-02-04 00:28:39 -06:00
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* @param PiggyBankRepositoryInterface $repository
|
|
|
|
* @param PiggyBank $piggyBank
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2015-06-27 04:44:18 -05:00
|
|
|
public function history(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
|
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()) {
|
2017-03-04 04:19:44 -06:00
|
|
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
|
|
|
|
2016-12-15 07:05:50 -06:00
|
|
|
$set = $repository->getEvents($piggyBank);
|
|
|
|
$set = $set->reverse();
|
|
|
|
$chartData = [];
|
|
|
|
$sum = '0';
|
2016-05-01 00:09:58 -05:00
|
|
|
/** @var PiggyBankEvent $entry */
|
|
|
|
foreach ($set as $entry) {
|
2016-12-15 07:05:50 -06:00
|
|
|
$label = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
|
|
|
|
$sum = bcadd($sum, $entry->amount);
|
|
|
|
$chartData[$label] = $sum;
|
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
|
|
|
|
2015-06-27 04:44:18 -05:00
|
|
|
return Response::json($data);
|
2015-05-16 02:41:14 -05:00
|
|
|
}
|
2015-05-20 12:56:14 -05:00
|
|
|
}
|