mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-17 01:42:19 +00:00
Use new methods from category repository
This commit is contained in:
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Http\Controllers\Chart;
|
namespace FireflyIII\Http\Controllers\Chart;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
@@ -32,6 +33,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|||||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
|
use FireflyIII\Support\Chart\Category\WholePeriodChartGenerator;
|
||||||
use FireflyIII\Support\Http\Controllers\AugumentData;
|
use FireflyIII\Support\Http\Controllers\AugumentData;
|
||||||
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
||||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||||
@@ -66,89 +68,32 @@ class CategoryController extends Controller
|
|||||||
* TODO this chart is not multi-currency aware.
|
* TODO this chart is not multi-currency aware.
|
||||||
*
|
*
|
||||||
* @param CategoryRepositoryInterface $repository
|
* @param CategoryRepositoryInterface $repository
|
||||||
* @param AccountRepositoryInterface $accountRepository
|
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
*
|
*
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*
|
*
|
||||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||||
*/
|
*/
|
||||||
public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category): JsonResponse
|
public function all(CategoryRepositoryInterface $repository, Category $category): JsonResponse
|
||||||
{
|
{
|
||||||
|
// cache results:
|
||||||
$cache = new CacheProperties;
|
$cache = new CacheProperties;
|
||||||
$cache->addProperty('chart.category.all');
|
$cache->addProperty('chart.category.all');
|
||||||
$cache->addProperty($category->id);
|
$cache->addProperty($category->id);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
//return response()->json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$start = $repository->firstUseDate($category);
|
$start = $repository->firstUseDate($category) ?? $this->getDate();
|
||||||
$start = $start ?? new Carbon;
|
|
||||||
$range = app('preferences')->get('viewRange', '1M')->data;
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
||||||
$start = app('navigation')->startOfPeriod($start, $range);
|
$start = app('navigation')->startOfPeriod($start, $range);
|
||||||
$end = new Carbon;
|
$end = $this->getDate();
|
||||||
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
|
||||||
|
|
||||||
Log::debug(sprintf('Full range is %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
Log::debug(sprintf('Full range is %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
||||||
|
|
||||||
$chartData = [
|
/** @var WholePeriodChartGenerator $generator */
|
||||||
[
|
$generator = app(WholePeriodChartGenerator::class);
|
||||||
'label' => (string)trans('firefly.spent'),
|
$chartData = $generator->generate($category, $start, $end);
|
||||||
'entries' => [], 'type' => 'bar',
|
$data = $this->generator->multiSet($chartData);
|
||||||
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'label' => (string)trans('firefly.earned'),
|
|
||||||
'entries' => [], 'type' => 'bar',
|
|
||||||
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'label' => (string)trans('firefly.sum'),
|
|
||||||
'entries' => [], 'type' => 'line', 'fill' => false,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
$step = $this->calculateStep($start, $end);
|
|
||||||
/** @var Carbon $current */
|
|
||||||
$current = clone $start;
|
|
||||||
|
|
||||||
Log::debug(sprintf('abc Step is %s', $step));
|
|
||||||
|
|
||||||
switch ($step) {
|
|
||||||
case '1D':
|
|
||||||
while ($current <= $end) {
|
|
||||||
//Log::debug(sprintf('Current day is %s', $current->format('Y-m-d')));
|
|
||||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $current, $current);
|
|
||||||
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $current, $current);
|
|
||||||
$sum = bcadd($spent, $earned);
|
|
||||||
$label = app('navigation')->periodShow($current, $step);
|
|
||||||
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
|
|
||||||
$chartData[1]['entries'][$label] = round($earned, 12);
|
|
||||||
$chartData[2]['entries'][$label] = round($sum, 12);
|
|
||||||
$current->addDay();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
// for some reason it doesn't pick up on these case entries.
|
|
||||||
case '1W':
|
|
||||||
case '1M':
|
|
||||||
case '1Y':
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
while ($current <= $end) {
|
|
||||||
$currentEnd = app('navigation')->endOfPeriod($current, $step);
|
|
||||||
//Log::debug(sprintf('abc Range is %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
|
|
||||||
|
|
||||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $current, $currentEnd);
|
|
||||||
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $current, $currentEnd);
|
|
||||||
$sum = bcadd($spent, $earned);
|
|
||||||
$label = app('navigation')->periodShow($current, $step);
|
|
||||||
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
|
|
||||||
$chartData[1]['entries'][$label] = round($earned, 12);
|
|
||||||
$chartData[2]['entries'][$label] = round($sum, 12);
|
|
||||||
$current = app('navigation')->addPeriod($current, $step, 0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = $this->generator->multiSet($chartData);
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return response()->json($data);
|
return response()->json($data);
|
||||||
@@ -383,8 +328,6 @@ class CategoryController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Chart for a specific period.
|
* Chart for a specific period.
|
||||||
*
|
*
|
||||||
* TODO this chart is not multi-currency aware.
|
|
||||||
*
|
|
||||||
* @param Category $category
|
* @param Category $category
|
||||||
* @param $date
|
* @param $date
|
||||||
*
|
*
|
||||||
@@ -399,8 +342,42 @@ class CategoryController extends Controller
|
|||||||
[$end, $start] = [$start, $end]; // @codeCoverageIgnore
|
[$end, $start] = [$start, $end]; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->makePeriodChart($category, $start, $end);
|
$cache = new CacheProperties;
|
||||||
|
$cache->addProperty($start);
|
||||||
|
$cache->addProperty($end);
|
||||||
|
$cache->addProperty($category->id);
|
||||||
|
$cache->addProperty('chart.category.period-chart');
|
||||||
|
|
||||||
|
|
||||||
|
if ($cache->has()) {
|
||||||
|
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var GeneratorInterface $generator */
|
||||||
|
$generator = app(GeneratorInterface::class);
|
||||||
|
|
||||||
|
/** @var WholePeriodChartGenerator $chartGenerator */
|
||||||
|
$chartGenerator = app(WholePeriodChartGenerator::class);
|
||||||
|
$chartData = $chartGenerator->generate($category, $start, $end);
|
||||||
|
$data = $generator->multiSet($chartData);
|
||||||
|
|
||||||
|
$cache->store($data);
|
||||||
|
|
||||||
return response()->json($data);
|
return response()->json($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Carbon
|
||||||
|
*/
|
||||||
|
private function getDate(): Carbon
|
||||||
|
{
|
||||||
|
$carbon = null;
|
||||||
|
try {
|
||||||
|
$carbon = new Carbon;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$e->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $carbon;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -165,7 +165,7 @@ class CategoryController extends Controller
|
|||||||
$cache->addProperty('category-report');
|
$cache->addProperty('category-report');
|
||||||
$cache->addProperty($accounts->pluck('id')->toArray());
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get(); // @codeCoverageIgnore
|
//return $cache->get(); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var CategoryRepositoryInterface $repository */
|
/** @var CategoryRepositoryInterface $repository */
|
||||||
@@ -174,17 +174,29 @@ class CategoryController extends Controller
|
|||||||
$report = [];
|
$report = [];
|
||||||
/** @var Category $category */
|
/** @var Category $category */
|
||||||
foreach ($categories as $category) {
|
foreach ($categories as $category) {
|
||||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
|
$spent = $repository->spentInPeriod($category, $accounts, $start, $end);
|
||||||
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $start, $end);
|
$earned = $repository->earnedInPeriod($category, $accounts, $start, $end);
|
||||||
if (0 !== bccomp($spent, '0') || 0 !== bccomp($earned, '0')) {
|
$currencies = array_keys($spent) + array_keys($earned);
|
||||||
$report[$category->id] = ['name' => $category->name, 'spent' => $spent, 'earned' => $earned, 'id' => $category->id];
|
foreach ($currencies as $code) {
|
||||||
|
$currencyInfo = $spent[$code] ?? $earned[$code];
|
||||||
|
$report[$category->id] = [
|
||||||
|
'name' => sprintf('%s (%s)', $category->name, $code),
|
||||||
|
'spent' => round($spent[$code]['spent'] ?? '0', $currencyInfo['currency_decimal_places']),
|
||||||
|
'earned' => round($earned[$code]['earned'] ?? '0', $currencyInfo['currency_decimal_places']),
|
||||||
|
'id' => $category->id,
|
||||||
|
'currency_id' => $currencyInfo['currency_id'],
|
||||||
|
'currency_code' => $currencyInfo['currency_code'],
|
||||||
|
'currency_symbol' => $currencyInfo['currency_symbol'],
|
||||||
|
'cyrrency_decimal_places' => $currencyInfo['currency_decimal_places'],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sum = [];
|
$sum = [];
|
||||||
foreach ($report as $categoryId => $row) {
|
foreach ($report as $categoryId => $row) {
|
||||||
$sum[$categoryId] = (float)$row['spent'];
|
$sum[$categoryId] = (float)$row['spent'];
|
||||||
}
|
}
|
||||||
array_multisort($sum, SORT_ASC, $report);
|
array_multisort($sum, SORT_ASC, $report);
|
||||||
|
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
try {
|
try {
|
||||||
$result = view('reports.partials.categories', compact('report'))->render();
|
$result = view('reports.partials.categories', compact('report'))->render();
|
||||||
@@ -199,5 +211,19 @@ class CategoryController extends Controller
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $array
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function noAmountInArray(array $array): bool
|
||||||
|
{
|
||||||
|
if (0 === count($array)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user