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

85 lines
2.6 KiB
PHP
Raw Normal View History

2015-05-16 02:41:14 -05:00
<?php
/**
* PiggyBankController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
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
/**
2017-11-15 05:25:49 -06:00
* Class PiggyBankController.
2015-05-16 02:41:14 -05:00
*/
class PiggyBankController extends Controller
{
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()) {
2018-03-10 13:30:09 -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) {
2018-04-02 08:10:40 -05:00
$label = $entry->date->formatLocalized((string)trans('config.month_and_day'));
2016-12-15 07:05:50 -06:00
$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
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
}