diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index a2ea1b41a7..c62d06a799 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -137,7 +137,8 @@ class BudgetController extends Controller $budgets->each( function (Budget $budget) use ($repository) { $date = Session::get('start', Carbon::now()->startOfMonth()); - $budget->spent = $repository->spentInMonth($budget, $date); + $end = Session::get('end', Carbon::now()->endOfMonth()); + $budget->spent = $repository->spentInPeriod($budget, $date, $end); $budget->currentRep = $repository->getCurrentRepetition($budget, $date); } ); diff --git a/app/Http/Controllers/GoogleChartController.php b/app/Http/Controllers/GoogleChartController.php index 45220a5714..a4cf4b4baa 100644 --- a/app/Http/Controllers/GoogleChartController.php +++ b/app/Http/Controllers/GoogleChartController.php @@ -104,41 +104,6 @@ class GoogleChartController extends Controller } - /** - * @param GChart $chart - * @param BudgetRepositoryInterface $repository - * @param $year - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function allBudgetsAndSpending(GChart $chart, BudgetRepositoryInterface $repository, $year, $shared = false) - { - $budgets = $repository->getBudgets(); - $chart->addColumn(trans('firefly.month'), 'date'); - foreach ($budgets as $budget) { - $chart->addColumn($budget->name, 'number'); - } - - $start = Carbon::createFromDate(intval($year), 1, 1); - $end = clone $start; - $end->endOfYear(); - - while ($start <= $end) { - $row = [clone $start]; - foreach ($budgets as $budget) { - $spent = $repository->spentInMonth($budget, $start, $shared); - $row[] = $spent; - } - $chart->addRowArray($row); - $start->addMonth(); - } - - $chart->generate(); - - return Response::json($chart->getData()); - - } - /** * @param GChart $chart * @param BudgetRepositoryInterface $repository @@ -370,43 +335,6 @@ class GoogleChartController extends Controller } - /** - * @param GChart $chart - * @param BudgetRepositoryInterface $repository - * @param Budget $budget - * @param int $year - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function budgetsAndSpending(GChart $chart, BudgetRepositoryInterface $repository, Budget $budget, $year = 0) - { - $chart->addColumn(trans('firefly.month'), 'date'); - $chart->addColumn(trans('firefly.budgeted'), 'number'); - $chart->addColumn(trans('firefly.spent'), 'number'); - - if ($year == 0) { - $start = $repository->getFirstBudgetLimitDate($budget); - $end = $repository->getLastBudgetLimitDate($budget); - } else { - $start = Carbon::createFromDate(intval($year), 1, 1); - $end = clone $start; - $end->endOfYear(); - } - - while ($start <= $end) { - $spent = $repository->spentInMonth($budget, $start); - $budgeted = $repository->getLimitAmountOnDate($budget, $start); - $chart->addRow(clone $start, $budgeted, $spent); - $start->addMonth(); - } - - $chart->generate(); - - return Response::json($chart->getData()); - - - } - /** * @param GChart $chart * @param CategoryRepositoryInterface $repository @@ -498,95 +426,4 @@ class GoogleChartController extends Controller return Response::json($chart->getData()); } - - /** - * @param GChart $chart - * @param ReportQueryInterface $query - * @param $year - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function yearInExp(GChart $chart, ReportQueryInterface $query, $year, $shared = false) - { - $start = new Carbon('01-01-' . $year); - $chart->addColumn(trans('firefly.month'), 'date'); - $chart->addColumn(trans('firefly.income'), 'number'); - $chart->addColumn(trans('firefly.expenses'), 'number'); - - if ($shared == 'shared') { - $shared = true; - } - - // get report query interface. - - $end = clone $start; - $end->endOfYear(); - while ($start < $end) { - $currentEnd = clone $start; - $currentEnd->endOfMonth(); - // total income && total expenses: - $incomeSum = floatval($query->incomeInPeriod($start, $currentEnd, $shared)->sum('queryAmount')); - $expenseSum = floatval($query->journalsByExpenseAccount($start, $currentEnd, $shared)->sum('queryAmount')); - - $chart->addRow(clone $start, $incomeSum, $expenseSum); - $start->addMonth(); - } - - - $chart->generate(); - - return Response::json($chart->getData()); - - } - - /** - * @param GChart $chart - * @param ReportQueryInterface $query - * @param $year - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function yearInExpSum(GChart $chart, ReportQueryInterface $query, $year, $shared = false) - { - $start = new Carbon('01-01-' . $year); - $chart->addColumn(trans('firefly.summary'), 'string'); - $chart->addColumn(trans('firefly.income'), 'number'); - $chart->addColumn(trans('firefly.expenses'), 'number'); - - if ($shared == 'shared') { - $shared = true; - } - - $income = 0; - $expense = 0; - $count = 0; - - $end = clone $start; - $end->endOfYear(); - while ($start < $end) { - $currentEnd = clone $start; - $currentEnd->endOfMonth(); - // total income: - $incomeSum = floatval($query->incomeInPeriod($start, $currentEnd, $shared)->sum('queryAmount')); - // total expenses: - $expenseSum = floatval($query->journalsByExpenseAccount($start, $currentEnd, $shared)->sum('queryAmount')); - - $income += $incomeSum; - $expense += $expenseSum; - $count++; - $start->addMonth(); - } - - - $chart->addRow(trans('firefly.sum'), $income, $expense); - $count = $count > 0 ? $count : 1; - $chart->addRow(trans('firefly.average'), ($income / $count), ($expense / $count)); - - $chart->generate(); - - return Response::json($chart->getData()); - - } - - } diff --git a/app/Http/Controllers/ReportChartController.php b/app/Http/Controllers/ReportChartController.php new file mode 100644 index 0000000000..49d4e29f84 --- /dev/null +++ b/app/Http/Controllers/ReportChartController.php @@ -0,0 +1,146 @@ +getBudgets(); + + // add columns: + $chart->addColumn(trans('firefly.month'), 'date'); + foreach ($budgets as $budget) { + $chart->addColumn($budget->name, 'number'); + } + + while ($start < $end) { + // month is the current end of the period: + $month = clone $start; + $month->endOfMonth(); + // make a row: + $row = [clone $start]; + + // each budget, fill the row: + foreach ($budgets as $budget) { + $spent = $repository->spentInPeriod($budget, $start, $month, $shared); + $row[] = $spent; + } + $chart->addRowArray($row); + + $start->addMonth(); + } + + $chart->generate(); + + return Response::json($chart->getData()); + } + + public function yearCategories(GChart $chart, $year, $shared = false) + { + $start = new Carbon($year . '-01-01'); + $end = new Carbon($year . '-12-31'); + $shared = $shared == 'shared' ? true : false; + + $chart->generate(); + + return Response::json($chart->getData()); + } + + /** + * Summarizes all income and expenses, per month, for a given year. + * + * @param GChart $chart + * @param ReportQueryInterface $query + * @param $year + * @param bool $shared + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function yearInOut(GChart $chart, ReportQueryInterface $query, $year, $shared = false) + { + // get start and end of year + $start = new Carbon($year . '-01-01'); + $end = new Carbon($year . '-12-31'); + $shared = $shared == 'shared' ? true : false; + + $chart->addColumn(trans('firefly.month'), 'date'); + $chart->addColumn(trans('firefly.income'), 'number'); + $chart->addColumn(trans('firefly.expenses'), 'number'); + + while ($start < $end) { + $month = clone $start; + $month->endOfMonth(); + // total income and total expenses: + $incomeSum = floatval($query->incomeInPeriod($start, $month, $shared)->sum('queryAmount')); + $expenseSum = floatval($query->expenseInPeriod($start, $month, $shared)->sum('queryAmount')) * -1; + + $chart->addRow(clone $start, $incomeSum, $expenseSum); + $start->addMonth(); + } + $chart->generate(); + + return Response::json($chart->getData()); + + } + + /** + * Summarizes all income and expenses for a given year. Gives a total and an average. + * + * @param GChart $chart + * @param ReportQueryInterface $query + * @param $year + * @param bool $shared + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function yearInOutSummarized(GChart $chart, ReportQueryInterface $query, $year, $shared = false) + { + $start = new Carbon($year . '-01-01'); + $end = new Carbon($year . '-12-31'); + $shared = $shared == 'shared' ? true : false; + $income = 0; + $expense = 0; + $count = 0; + + $chart->addColumn(trans('firefly.summary'), 'string'); + $chart->addColumn(trans('firefly.income'), 'number'); + $chart->addColumn(trans('firefly.expenses'), 'number'); + + while ($start < $end) { + $month = clone $start; + $month->endOfMonth(); + // total income and total expenses: + $income += floatval($query->incomeInPeriod($start, $month, $shared)->sum('queryAmount')); + $expense += floatval($query->expenseInPeriod($start, $month, $shared)->sum('queryAmount')) * -1; + $count++; + $start->addMonth(); + } + + // add total + average: + $chart->addRow(trans('firefly.sum'), $income, $expense); + $count = $count > 0 ? $count : 1; + $chart->addRow(trans('firefly.average'), ($income / $count), ($expense / $count)); + + $chart->generate(); + + return Response::json($chart->getData()); + + } +} \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 31eef34c21..83f109d08f 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -280,8 +280,6 @@ Route::group( Route::get('/chart/home/categories', ['uses' => 'GoogleChartController@allCategoriesHomeChart']); Route::get('/chart/home/bills', ['uses' => 'GoogleChartController@billsOverview']); Route::get('/chart/account/{account}/{view?}', ['uses' => 'GoogleChartController@accountBalanceChart']); - Route::get('/chart/budget/{budget}/spending/{year?}', ['uses' => 'GoogleChartController@budgetsAndSpending']); - Route::get('/chart/budgets/spending/{year}/{shared?}', ['uses' => 'GoogleChartController@allBudgetsAndSpending'])->where(['year' => '[0-9]{4}','shared' => 'shared']); Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'GoogleChartController@budgetLimitSpending']); Route::get('/chart/reports/income-expenses/{year}/{shared?}', ['uses' => 'GoogleChartController@yearInExp'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); Route::get('/chart/reports/income-expenses-sum/{year}/{shared?}', ['uses' => 'GoogleChartController@yearInExpSum'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); @@ -361,6 +359,15 @@ Route::group( Route::get('/reports/modal/{account}/{year}/{month}/balanced-transfers', ['uses' => 'ReportController@modalBalancedTransfers', 'as' => 'reports.balanced-transfers']); Route::get('/reports/modal/{account}/{year}/{month}/left-unbalanced', ['uses' => 'ReportController@modalLeftUnbalanced', 'as' => 'reports.left-unbalanced']); + /** + * Report Chart Controller: + */ + Route::get('/report/chart/in-out/{year}/{shared?}', ['uses' => 'ReportChartController@yearInOut'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); + Route::get('/report/chart/in-out-sum/{year}/{shared?}', ['uses' => 'ReportChartController@yearInOutSummarized'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); + + Route::get('/report/chart/budgets/{year}/{shared?}', ['uses' => 'ReportChartController@yearBudgets'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); + Route::get('/report/chart/categories/{year}/{shared?}', ['uses' => 'ReportChartController@yearCategories'])->where(['year' => '[0-9]{4}','shared'=> 'shared']); + /** * Search Controller */ diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index fbd0c5c743..495697f5db 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -257,26 +257,22 @@ class BudgetRepository implements BudgetRepositoryInterface } /** - * @param Budget $budget - * @param Carbon $date - * @param boolean $shared + * @param Budget $budget + * @param Carbon $date + * @param bool $shared * * @return float */ - public function spentInMonth(Budget $budget, Carbon $date, $shared = true) + public function spentInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true) { - $end = clone $date; - $date->startOfMonth(); - $end->endOfMonth(); - if ($shared === true) { // get everything: - $sum = floatval($budget->transactionjournals()->before($end)->after($date)->lessThan(0)->sum('amount')) * -1; + $sum = floatval($budget->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1; } else { // get all journals in this month where the asset account is NOT shared. $sum = $budget->transactionjournals() ->before($end) - ->after($date) + ->after($start) ->lessThan(0) ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') ->leftJoin( diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 1e20b93474..fc65660310 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -124,12 +124,13 @@ interface BudgetRepositoryInterface /** * @param Budget $budget - * @param Carbon $date + * @param Carbon $start + * @param Carbon $end * @param boolean $shared * * @return float */ - public function spentInMonth(Budget $budget, Carbon $date, $shared = true); + public function spentInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true); /** * @param array $data diff --git a/public/js/reports.js b/public/js/reports.js index c2bc2eaf94..88e7e8ffd8 100644 --- a/public/js/reports.js +++ b/public/js/reports.js @@ -4,10 +4,10 @@ if (typeof(google) != 'undefined') { function drawChart() { - googleColumnChart('chart/reports/income-expenses/' + year + shared, 'income-expenses-chart'); - googleColumnChart('chart/reports/income-expenses-sum/' + year + shared, 'income-expenses-sum-chart') + googleColumnChart('report/chart/in-out/' + year + shared, 'income-expenses-chart'); + googleColumnChart('report/chart/in-out-sum/' + year + shared, 'income-expenses-sum-chart') - googleStackedColumnChart('chart/budgets/spending/' + year + shared, 'budgets'); + googleStackedColumnChart('report/chart/budgets/' + year + shared, 'budgets'); } $(function () {