mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Piggy banks and #452
This commit is contained in:
@@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ChartJsPiggyBankChartGenerator.php
|
|
||||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
||||||
*
|
|
||||||
* This software may be modified and distributed under the terms of the
|
|
||||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
||||||
*
|
|
||||||
* See the LICENSE file for details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
namespace FireflyIII\Generator\Chart\PiggyBank;
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ChartJsPiggyBankChartGenerator
|
|
||||||
*
|
|
||||||
* @package FireflyIII\Generator\Chart\PiggyBank
|
|
||||||
*/
|
|
||||||
class ChartJsPiggyBankChartGenerator implements PiggyBankChartGeneratorInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $set
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function history(Collection $set): array
|
|
||||||
{
|
|
||||||
|
|
||||||
// language:
|
|
||||||
$format = (string)trans('config.month_and_day');
|
|
||||||
|
|
||||||
$data = [
|
|
||||||
'count' => 1,
|
|
||||||
'labels' => [],
|
|
||||||
'datasets' => [
|
|
||||||
[
|
|
||||||
'label' => 'Diff',
|
|
||||||
'data' => [],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
$sum = '0';
|
|
||||||
foreach ($set as $key => $value) {
|
|
||||||
$date = new Carbon($key);
|
|
||||||
$sum = bcadd($sum, $value);
|
|
||||||
$data['labels'][] = $date->formatLocalized($format);
|
|
||||||
$data['datasets'][0]['data'][] = round($sum, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* PiggyBankChartGeneratorInterface.php
|
|
||||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
||||||
*
|
|
||||||
* This software may be modified and distributed under the terms of the
|
|
||||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
||||||
*
|
|
||||||
* See the LICENSE file for details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
|
|
||||||
namespace FireflyIII\Generator\Chart\PiggyBank;
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface PiggyBankChartGeneratorInterface
|
|
||||||
*
|
|
||||||
* @package FireflyIII\Generator\Chart\PiggyBank
|
|
||||||
*/
|
|
||||||
interface PiggyBankChartGeneratorInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param Collection $set
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function history(Collection $set): array;
|
|
||||||
}
|
|
@@ -13,13 +13,12 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace FireflyIII\Http\Controllers\Chart;
|
namespace FireflyIII\Http\Controllers\Chart;
|
||||||
|
|
||||||
use FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGeneratorInterface;
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
use FireflyIII\Models\PiggyBankEvent;
|
use FireflyIII\Models\PiggyBankEvent;
|
||||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Response;
|
use Response;
|
||||||
|
|
||||||
|
|
||||||
@@ -31,7 +30,7 @@ use Response;
|
|||||||
class PiggyBankController extends Controller
|
class PiggyBankController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var PiggyBankChartGeneratorInterface */
|
/** @var GeneratorInterface */
|
||||||
protected $generator;
|
protected $generator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,7 +40,7 @@ class PiggyBankController extends Controller
|
|||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
// create chart generator:
|
// create chart generator:
|
||||||
$this->generator = app(PiggyBankChartGeneratorInterface::class);
|
$this->generator = app(GeneratorInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,26 +55,24 @@ class PiggyBankController extends Controller
|
|||||||
{
|
{
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty('piggy-history');
|
$cache->addProperty('chart.piggy-bank.history');
|
||||||
$cache->addProperty($piggyBank->id);
|
$cache->addProperty($piggyBank->id);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get());
|
return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
$set = $repository->getEvents($piggyBank);
|
$set = $repository->getEvents($piggyBank);
|
||||||
$set = $set->reverse();
|
$set = $set->reverse();
|
||||||
$collection = [];
|
$chartData = [];
|
||||||
|
$sum = '0';
|
||||||
/** @var PiggyBankEvent $entry */
|
/** @var PiggyBankEvent $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
$date = $entry->date->format('Y-m-d');
|
$label = $entry->date->formatLocalized(strval(trans('config.month_and_day')));
|
||||||
$amount = $entry->amount;
|
$sum = bcadd($sum, $entry->amount);
|
||||||
if (isset($collection[$date])) {
|
$chartData[$label] = $sum;
|
||||||
$amount = bcadd($amount, $collection[$date]);
|
|
||||||
}
|
|
||||||
$collection[$date] = $amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->generator->history(new Collection($collection));
|
$data = $this->generator->singleSet($piggyBank->name, $chartData);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
|
Reference in New Issue
Block a user