Clean up code after changing routes.

This commit is contained in:
James Cole 2016-12-06 07:48:41 +01:00
parent 02257e3887
commit d8f291be6e
15 changed files with 138 additions and 92 deletions

View File

@ -245,7 +245,7 @@ class CategoryController extends Controller
*
* @return View
*/
public function showWithDate(Category $category, string $date)
public function showByDate(Category $category, string $date)
{
$carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data;
@ -263,7 +263,7 @@ class CategoryController extends Controller
$journals->setPath('categories/show/' . $category->id . '/' . $date);
return view('categories.show_with_date', compact('category', 'journals', 'hideCategory', 'subTitle', 'carbon'));
return view('categories.show-by-date', compact('category', 'journals', 'hideCategory', 'subTitle', 'carbon'));
}
/**

View File

@ -155,11 +155,13 @@ class CategoryController extends Controller
/**
* @param CRI $repository
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function reportPeriod(CRI $repository, Category $category, Carbon $start, Carbon $end, Collection $accounts)
public function reportPeriod(CRI $repository, Category $category, Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
@ -171,7 +173,8 @@ class CategoryController extends Controller
return $cache->get();
}
$report = $repository->getCategoryPeriodReport(new Collection([$category]), $accounts, $start, $end, true);
$expenses = $repository->periodExpenses(new Collection([$category]), $accounts, $start, $end);
$income = $repository->periodIncome(new Collection([$category]), $accounts, $start, $end);
$periods = Navigation::listOfPeriods($start, $end);
@ -180,8 +183,46 @@ class CategoryController extends Controller
foreach (array_keys($periods) as $period) {
$nice = $periods[$period];
$result[$nice] = [
'earned' => $report['income'][$category->id]['entries'][$period] ?? '0',
'spent' => $report['expense'][$category->id]['entries'][$period] ?? '0',
'earned' => $income[$category->id]['entries'][$period] ?? '0',
'spent' => $expenses[$category->id]['entries'][$period] ?? '0',
];
}
$data = $this->generator->reportPeriod($result);
return Response::json($data);
}
/**
* @param CRI $repository
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function reportPeriodNoCategory(CRI $repository, Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('no-category-period-chart');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get();
}
$expenses = $repository->periodExpensesNoCategory($accounts, $start, $end);
$income = $repository->periodIncomeNoCategory($accounts, $start, $end);
$periods = Navigation::listOfPeriods($start, $end);
// join them:
$result = [];
foreach (array_keys($periods) as $period) {
$nice = $periods[$period];
$result[$nice] = [
'earned' => $income['entries'][$period] ?? '0',
'spent' => $expenses['entries'][$period] ?? '0',
];
}
$data = $this->generator->reportPeriod($result);

View File

@ -60,14 +60,13 @@ class CategoryController extends Controller
}
/**
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return string
* @return mixed|string
*/
public function expenseReport(Carbon $start, Carbon $end, Collection $accounts)
public function expenses(Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
@ -100,7 +99,7 @@ class CategoryController extends Controller
*
* @return string
*/
public function incomeReport(Carbon $start, Carbon $end, Collection $accounts)
public function income(Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);

View File

@ -23,6 +23,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Log;
use Navigation;
/**
@ -258,6 +259,10 @@ class CategoryRepository implements CategoryRepositoryInterface
// loop transactions:
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
// if positive, skip:
if (bccomp($transaction->transaction_amount, '0') === 1) {
continue;
}
$categoryId = max(intval($transaction->transaction_journal_category_id), intval($transaction->transaction_category_id));
$date = $transaction->date->format($carbonFormat);
$data[$categoryId]['entries'][$date] = bcadd($data[$categoryId]['entries'][$date] ?? '0', $transaction->transaction_amount);
@ -278,7 +283,7 @@ class CategoryRepository implements CategoryRepositoryInterface
$carbonFormat = Navigation::preferredCarbonFormat($start, $end);
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setAccounts($accounts)->setRange($start, $end)->withOpposingAccount();
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])->enableInternalFilter();
$collector->withoutCategory();
$transactions = $collector->getJournals();
@ -289,6 +294,10 @@ class CategoryRepository implements CategoryRepositoryInterface
];
foreach ($transactions as $transaction) {
// if positive, skip:
if (bccomp($transaction->transaction_amount, '0') === 1) {
continue;
}
$date = $transaction->date->format($carbonFormat);
if (!isset($result['entries'][$date])) {
@ -334,6 +343,10 @@ class CategoryRepository implements CategoryRepositoryInterface
// loop transactions:
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
// if negative, skip:
if (bccomp($transaction->transaction_amount, '0') === -1) {
continue;
}
$categoryId = max(intval($transaction->transaction_journal_category_id), intval($transaction->transaction_category_id));
$date = $transaction->date->format($carbonFormat);
$data[$categoryId]['entries'][$date] = bcadd($data[$categoryId]['entries'][$date] ?? '0', $transaction->transaction_amount);
@ -351,10 +364,11 @@ class CategoryRepository implements CategoryRepositoryInterface
*/
public function periodIncomeNoCategory(Collection $accounts, Carbon $start, Carbon $end): array
{
Log::debug('Now in periodIncomeNoCategory()');
$carbonFormat = Navigation::preferredCarbonFormat($start, $end);
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setAccounts($accounts)->setRange($start, $end)->withOpposingAccount();
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])->enableInternalFilter();
$collector->withoutCategory();
$transactions = $collector->getJournals();
@ -363,8 +377,13 @@ class CategoryRepository implements CategoryRepositoryInterface
'name' => strval(trans('firefly.no_category')),
'sum' => '0',
];
Log::debug('Looping transactions..');
foreach ($transactions as $transaction) {
// if negative, skip:
if (bccomp($transaction->transaction_amount, '0') === -1) {
continue;
}
$date = $transaction->date->format($carbonFormat);
if (!isset($result['entries'][$date])) {
@ -372,6 +391,8 @@ class CategoryRepository implements CategoryRepositoryInterface
}
$result['entries'][$date] = bcadd($result['entries'][$date], $transaction->transaction_amount);
}
Log::debug('Done looping transactions..');
Log::debug('Finished periodIncomeNoCategory()');
return $result;
}

View File

@ -0,0 +1,4 @@
$(function () {
"use strict";
columnChart(specific, 'period-specific-period');
});

View File

@ -1,19 +1,5 @@
/* globals $, categoryID, columnChart, categoryDate */
$(function () {
"use strict";
if (typeof categoryID !== 'undefined') {
// more splits:
if ($('#all').length > 0) {
columnChart('chart/category/' + categoryID + '/all', 'all');
}
if ($('#period').length > 0) {
columnChart('chart/category/' + categoryID + '/period', 'period');
}
}
if (typeof categoryID !== 'undefined' && typeof categoryDate !== 'undefined') {
columnChart('chart/category/' + categoryID + '/period/' + categoryDate, 'period-specific-period');
}
columnChart(all, 'all');
columnChart(current, 'period');
});

View File

@ -1,19 +0,0 @@
/* globals $, categoryID, columnChart, categoryDate */
$(function () {
"use strict";
if (typeof categoryID !== 'undefined') {
// more splits:
if ($('#all').length > 0) {
columnChart('chart/category/' + categoryID + '/all', 'all');
}
if ($('#period').length > 0) {
columnChart('chart/category/' + categoryID + '/period', 'period');
}
}
if (typeof categoryID !== 'undefined' && typeof categoryDate !== undefined) {
columnChart('chart/category/' + categoryID + '/period/' + categoryDate, 'period-specific-period');
}
});

View File

@ -8,31 +8,25 @@
* See the LICENSE file for details.
*/
// it's hard coded, but what you're gonna do?
var catInUri = 'chart/category/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate + '/OTHERS/income';
var catOutUri = 'chart/category/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate + '/OTHERS/expense';
var accInUri = 'chart/account/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate + '/OTHERS/income';
var accOutUri = 'chart/account/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate + '/OTHERS/expense';
$(function () {
"use strict";
drawChart();
$('#categories-in-pie-chart-checked').on('change', function () {
redrawPieChart('categories-in-pie-chart', catInUri);
redrawPieChart('categories-in-pie-chart', categoryIncomeUri);
});
$('#categories-out-pie-chart-checked').on('change', function () {
redrawPieChart('categories-out-pie-chart', catOutUri);
redrawPieChart('categories-out-pie-chart', categoryExpenseUri);
});
$('#accounts-in-pie-chart-checked').on('change', function () {
redrawPieChart('accounts-in-pie-chart', accInUri);
redrawPieChart('accounts-in-pie-chart', accountIncomeUri);
});
$('#accounts-out-pie-chart-checked').on('change', function () {
redrawPieChart('accounts-out-pie-chart', accOutUri);
redrawPieChart('accounts-out-pie-chart', accountExpenseUri);
});
});
@ -42,13 +36,13 @@ function drawChart() {
"use strict";
// month view:
stackedColumnChart('chart/category-report-in-out/' + accountIds + '/' + categoryIds + '/' + startDate + '/' + endDate, 'in-out-chart');
stackedColumnChart(mainUri, 'in-out-chart');
// draw pie chart of income, depending on "show other transactions too":
redrawPieChart('categories-in-pie-chart', catInUri);
redrawPieChart('categories-out-pie-chart', catOutUri);
redrawPieChart('accounts-in-pie-chart', accInUri);
redrawPieChart('accounts-out-pie-chart', accOutUri);
redrawPieChart('categories-in-pie-chart', categoryIncomeUri);
redrawPieChart('categories-out-pie-chart', categoryExpenseUri);
redrawPieChart('accounts-in-pie-chart', accountIncomeUri);
redrawPieChart('accounts-out-pie-chart', accountExpenseUri);
}

View File

@ -108,8 +108,7 @@ function clickCategoryChart(e) {
var link = $(e.target);
var categoryId = link.data('category');
// this url is different from the one below. this is something that must be fixed
var URL = 'chart/category/' + categoryId + '/report-period/' + startDate + '/' + endDate + '/' + accountIds;
var URL = 'chart/category/report-period/' + categoryId + '/' + accountIds + '/' + startDate + '/' + endDate;
var container = 'category_chart';
columnChart(URL, container);
return false;

View File

@ -5,7 +5,6 @@ $(function () {
drawChart();
loadAjaxPartial('budgetPeriodReport', budgetPeriodReportUri);
loadAjaxPartial('categoryExpense', categoryExpenseUri);
loadAjaxPartial('categoryIncome', categoryIncomeUri);
});

View File

@ -77,7 +77,3 @@
</div>
{{ Form.close|raw }}
{% endblock %}
{% block scripts %}
<script type="text/javascript" src="js/ff/accounts/create-edit.js"></script>
{% endblock %}

View File

@ -40,12 +40,11 @@
{% endblock %}
{% block scripts %}
<script type="text/javascript">
var categoryID = {{ category.id }};
var categoryDate = "{{ carbon.format('Y-m-d') }}";
var specific = '{{ route('chart.category.specific', [category.id, carbon.format('Ymd')]) }}';
</script>
<script type="text/javascript" src="js/lib/Chart.bundle.min.js"></script>
<script type="text/javascript" src="js/ff/charts.defaults.js"></script>
<script type="text/javascript" src="js/ff/charts.js"></script>
<script type="text/javascript" src="js/ff/categories/show_with_date.js"></script>
<script type="text/javascript" src="js/ff/categories/show-by-date.js"></script>
<script type="text/javascript" src="js/ff/transactions/list.js"></script>
{% endblock %}

View File

@ -73,7 +73,8 @@
{% endblock %}
{% block scripts %}
<script type="text/javascript">
var categoryID = {{ category.id }};
var current = '{{ route('chart.category.current', [category.id]) }}';
var all = '{{ route('chart.category.all', [category.id]) }}';
</script>
<script type="text/javascript" src="js/lib/Chart.bundle.min.js"></script>
<script type="text/javascript" src="js/ff/charts.defaults.js"></script>

View File

@ -369,6 +369,13 @@
var endDate = '{{ end.format('Ymd') }}';
var accountIds = '{{ accountIds }}';
var categoryIds = '{{ categoryIds }}';
// chart uri's
var categoryIncomeUri = '{{ route('chart.category.category-income', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';
var categoryExpenseUri = '{{ route('chart.category.category-expense', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';
var accountIncomeUri = '{{ route('chart.category.account-income', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';
var accountExpenseUri = '{{ route('chart.category.account-expense', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd'),'OTHERS']) }}';
var mainUri = '{{ route('chart.category.main', [accountIds, categoryIds, start.format('Ymd'), end.format('Ymd')]) }}';
</script>

View File

@ -269,20 +269,36 @@ Route::group(
* Chart\Category Controller
*/
Route::group(
['middleware' => 'user-full-auth', 'namespace' => 'Chart', 'prefix' => 'chart/category'], function () {
['middleware' => 'user-full-auth', 'namespace' => 'Chart', 'prefix' => 'chart/category', 'as' => 'chart.category.'], function () {
Route::get('frontpage', ['uses' => 'CategoryController@frontpage']);
Route::get('period/{category}', ['uses' => 'CategoryController@currentPeriod']);
Route::get('period/{category}/{date}', ['uses' => 'CategoryController@specificPeriod']);
Route::get('all/{category}', ['uses' => 'CategoryController@all']);
Route::get('period/{category}', ['uses' => 'CategoryController@currentPeriod', 'as' => 'current']);
Route::get('period/{category}/{date}', ['uses' => 'CategoryController@specificPeriod', 'as' => 'specific']);
Route::get('all/{category}', ['uses' => 'CategoryController@all', 'as' => 'all']);
Route::get('report-period/0/{accountList}/{start_date}/{end_date}', ['uses' => 'CategoryController@reportPeriodNoCategory']);
Route::get('report-period/{category}/{accountList}/{start_date}/{end_date}', ['uses' => 'CategoryController@reportPeriod']);
// these charts are used in reports (category reports):
Route::get('category/income/{accountList}/{categoryList}/{start_date}/{end_date}/{others}', ['uses' => 'CategoryReportController@categoryIncome']);
Route::get('category/expense/{accountList}/{categoryList}/{start_date}/{end_date}/{others}', ['uses' => 'CategoryReportController@categoryExpense']);
Route::get('account/income/{accountList}/{categoryList}/{start_date}/{end_date}/{others}', ['uses' => 'CategoryReportController@accountIncome']);
Route::get('account/income/{accountList}/{categoryList}/{start_date}/{end_date}/{others}', ['uses' => 'CategoryReportController@accountExpense']);
Route::get('report-in-out/{accountList}/{categoryList}/{start_date}/{end_date}', ['uses' => 'CategoryReportController@mainChart']);
Route::get(
'category/income/{accountList}/{categoryList}/{start_date}/{end_date}/{others}',
['uses' => 'CategoryReportController@categoryIncome', 'as' => 'category-income']
);
Route::get(
'category/expense/{accountList}/{categoryList}/{start_date}/{end_date}/{others}',
['uses' => 'CategoryReportController@categoryExpense', 'as' => 'category-expense']
);
Route::get(
'account/income/{accountList}/{categoryList}/{start_date}/{end_date}/{others}',
['uses' => 'CategoryReportController@accountIncome', 'as' => 'account-income']
);
Route::get(
'account/expense/{accountList}/{categoryList}/{start_date}/{end_date}/{others}',
['uses' => 'CategoryReportController@accountExpense', 'as' => 'account-expense']
);
Route::get('report-in-out/{accountList}/{categoryList}/{start_date}/{end_date}',
['uses' => 'CategoryReportController@mainChart', 'as' => 'main']);
}
);
@ -292,6 +308,7 @@ Route::group(
*/
Route::group(
['middleware' => 'user-full-auth', 'namespace' => 'Chart', 'prefix' => 'chart/piggy-bank'], function () {
// continue here.
Route::get('{piggyBank}', ['uses' => 'PiggyBankController@history']);
}
);
@ -473,6 +490,8 @@ Route::group(
*/
Route::group(
['middleware' => 'user-full-auth', 'namespace' => 'Report', 'prefix' => 'report-data/category', 'as' => 'report-data.category.'], function () {
Route::get('operations/{accountList}/{start_date}/{end_date}', ['uses' => 'CategoryController@operations', 'as' => 'operations']);
Route::get('income/{accountList}/{start_date}/{end_date}', ['uses' => 'CategoryController@income', 'as' => 'income']);
Route::get('expenses/{accountList}/{start_date}/{end_date}', ['uses' => 'CategoryController@expenses', 'as' => 'expenses']);