diff --git a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php index e87a4543be..94a1820fd7 100644 --- a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php +++ b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php @@ -42,8 +42,8 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface $spent = $entry[2]; $earned = $entry[3]; - $data['datasets'][0]['data'][] = bccomp($spent, '0') === 0 ? null : bcmul($spent, '-1'); - $data['datasets'][1]['data'][] = bccomp($earned, '0') === 0 ? null : $earned; + $data['datasets'][0]['data'][] = bccomp($spent, '0') === 0 ? null : round(bcmul($spent, '-1'), 4); + $data['datasets'][1]['data'][] = bccomp($earned, '0') === 0 ? null : round($earned, 4); } return $data; diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index d1f70d7826..8fbadc3bd4 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -11,6 +11,7 @@ use FireflyIII\Helpers\Collection\Expense; use FireflyIII\Helpers\Collection\Income; use FireflyIII\Helpers\FiscalHelperInterface; use FireflyIII\Models\Bill; +use FireflyIII\Models\Category; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; @@ -311,4 +312,30 @@ class ReportHelper implements ReportHelperInterface return $sum; } + + /** + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Collection + */ + public function getCategoriesWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection + { + /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $collection = $repository->earnedForAccountsPerMonth($accounts, $start, $end); + $second = $repository->spentForAccountsPerMonth($accounts, $start, $end); + $collection = $collection->merge($second); + $array = []; + /** @var Category $category */ + foreach ($collection as $category) { + $id = $category->id; + $array[$id] = $category; + + } + $set = new Collection($array); + + return $set; + } } diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index 33be9fe4e8..78ee81e5c1 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -81,4 +81,14 @@ interface ReportHelperInterface */ public function tagReport(Carbon $start, Carbon $end, Collection $accounts): array; + + /** + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return Collection + */ + public function getCategoriesWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection; + } diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index d611d0e9b3..07c0728fee 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -313,6 +313,8 @@ class BudgetController extends Controller * @param Carbon $start * @param Carbon $end * @param Collection $accounts + * + * @return \Illuminate\Http\JsonResponse */ public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 3eb80cf958..cdb10237d6 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -10,6 +10,7 @@ use FireflyIII\Models\Category; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI; use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface as SCRI; +use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Navigation; @@ -442,4 +443,60 @@ class CategoryController extends Controller return $data; } + /** + * @param Category $category + * @param string $reportType + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return \Illuminate\Http\JsonResponse + */ + public function period(Category $category, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + { + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty($reportType); + $cache->addProperty($accounts); + $cache->addProperty($category->id); + $cache->addProperty('category'); + $cache->addProperty('period'); + if ($cache->has()) { + return Response::json($cache->get()); + } + + /** @var SingleCategoryRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); + // loop over period, add by users range: + $current = clone $start; + $viewRange = Preferences::get('viewRange', '1M')->data; + $format = strval(trans('config.month')); + $set = new Collection; + while ($current < $end) { + $currentStart = clone $current; + $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); + + $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd))); + $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd))); + + $entry = [ + $category->name, + $currentStart->formatLocalized($format), + $spent, + $earned, + + ]; + $set->push($entry); + $currentEnd->addDay(); + $current = clone $currentEnd; + } + $data = $this->generator->period($set); + $cache->store($data); + + return Response::json($data); + + } + } diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index ac269636e6..4354a815af 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -314,6 +314,9 @@ class ReportController extends Controller // find the budgets we've spent money on this period with these accounts: $budgets = $this->budgetHelper->getBudgetsWithExpenses($start, $end, $accounts); + // find the categories we've spent money on this period with these accounts: + $categories = $this->helper->getCategoriesWithExpenses($start, $end, $accounts); + Session::flash('gaEventCategory', 'report'); Session::flash('gaEventAction', 'year'); Session::flash('gaEventLabel', $start->format('Y')); @@ -330,7 +333,7 @@ class ReportController extends Controller 'reports.default.year', compact( 'start', 'accountReport', 'incomes', 'reportType', 'accountIds', 'end', - 'expenses', 'incomeTopLength', 'expenseTopLength', 'tags', 'budgets' + 'expenses', 'incomeTopLength', 'expenseTopLength', 'tags', 'budgets', 'categories' ) ); } diff --git a/app/Http/routes.php b/app/Http/routes.php index c8ab30ac4d..2c36250347 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -207,6 +207,7 @@ Route::group( // categories: Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']); + Route::get('/chart/category/period/{category}/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@period']); // these three charts are for reports: Route::get('/chart/category/earned-in-period/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@earnedInPeriod']); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index abeae76236..da1b34abed 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -33,7 +33,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn private $user; /** - * BillRepository constructor. + * BudgetRepository constructor. * * @param User $user */ diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index 14f5495fc4..0575a2b665 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -26,7 +26,7 @@ class CategoryRepository implements CategoryRepositoryInterface private $user; /** - * BillRepository constructor. + * CategoryRepository constructor. * * @param User $user */ diff --git a/app/Repositories/Category/SingleCategoryRepository.php b/app/Repositories/Category/SingleCategoryRepository.php index 57509641c9..5fa7ffe2c2 100644 --- a/app/Repositories/Category/SingleCategoryRepository.php +++ b/app/Repositories/Category/SingleCategoryRepository.php @@ -23,7 +23,7 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate private $user; /** - * BillRepository constructor. + * SingleCategoryRepository constructor. * * @param User $user */ diff --git a/app/Repositories/ExportJob/ExportJobRepository.php b/app/Repositories/ExportJob/ExportJobRepository.php index ad8bc3301e..c84486bfdb 100644 --- a/app/Repositories/ExportJob/ExportJobRepository.php +++ b/app/Repositories/ExportJob/ExportJobRepository.php @@ -26,7 +26,7 @@ class ExportJobRepository implements ExportJobRepositoryInterface private $user; /** - * BillRepository constructor. + * ExportJobRepository constructor. * * @param User $user */ diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 71e7beb15d..8c72b8c490 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -30,7 +30,7 @@ class JournalRepository implements JournalRepositoryInterface private $user; /** - * BillRepository constructor. + * JournalRepository constructor. * * @param User $user */ diff --git a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php index 8e91189457..86d881b232 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php +++ b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php @@ -18,6 +18,8 @@ interface RuleGroupRepositoryInterface /** + * + * * @return int */ public function count(): int; diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js index 30c8efaa84..521989587f 100644 --- a/public/js/ff/reports/default/year.js +++ b/public/js/ff/reports/default/year.js @@ -28,6 +28,15 @@ function drawChart() { }); + // and another loop + $.each($('.category_year_chart'), function (i, v) { + var holder = $(v); + var id = holder.attr('id'); + var categoryId = holder.data('category'); + columnChart('chart/category/period/' + categoryId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, id); + + }); + //stackedColumnChart('chart/budget/year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'budgets'); stackedColumnChart('chart/category/spent-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-spent-in-period'); stackedColumnChart('chart/category/earned-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-earned-in-period'); diff --git a/resources/views/reports/default/year.twig b/resources/views/reports/default/year.twig index 83e189a86f..f8d4040337 100644 --- a/resources/views/reports/default/year.twig +++ b/resources/views/reports/default/year.twig @@ -61,6 +61,7 @@ + + - {% for budget in budgets %} -