Some cleaning up and more charts.

This commit is contained in:
James Cole 2016-04-26 09:21:57 +02:00
parent 01cab599bb
commit d551333fa2
15 changed files with 150 additions and 18 deletions

View File

@ -42,8 +42,8 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface
$spent = $entry[2]; $spent = $entry[2];
$earned = $entry[3]; $earned = $entry[3];
$data['datasets'][0]['data'][] = bccomp($spent, '0') === 0 ? null : bcmul($spent, '-1'); $data['datasets'][0]['data'][] = bccomp($spent, '0') === 0 ? null : round(bcmul($spent, '-1'), 4);
$data['datasets'][1]['data'][] = bccomp($earned, '0') === 0 ? null : $earned; $data['datasets'][1]['data'][] = bccomp($earned, '0') === 0 ? null : round($earned, 4);
} }
return $data; return $data;

View File

@ -11,6 +11,7 @@ use FireflyIII\Helpers\Collection\Expense;
use FireflyIII\Helpers\Collection\Income; use FireflyIII\Helpers\Collection\Income;
use FireflyIII\Helpers\FiscalHelperInterface; use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag; use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
@ -311,4 +312,30 @@ class ReportHelper implements ReportHelperInterface
return $sum; 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;
}
} }

View File

@ -81,4 +81,14 @@ interface ReportHelperInterface
*/ */
public function tagReport(Carbon $start, Carbon $end, Collection $accounts): array; 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;
} }

View File

@ -313,6 +313,8 @@ class BudgetController extends Controller
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param Collection $accounts * @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/ */
public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts) public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{ {

View File

@ -10,6 +10,7 @@ use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI; use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface as SCRI; use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface as SCRI;
use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Navigation; use Navigation;
@ -442,4 +443,60 @@ class CategoryController extends Controller
return $data; 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);
}
} }

View File

@ -314,6 +314,9 @@ class ReportController extends Controller
// find the budgets we've spent money on this period with these accounts: // find the budgets we've spent money on this period with these accounts:
$budgets = $this->budgetHelper->getBudgetsWithExpenses($start, $end, $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('gaEventCategory', 'report');
Session::flash('gaEventAction', 'year'); Session::flash('gaEventAction', 'year');
Session::flash('gaEventLabel', $start->format('Y')); Session::flash('gaEventLabel', $start->format('Y'));
@ -330,7 +333,7 @@ class ReportController extends Controller
'reports.default.year', 'reports.default.year',
compact( compact(
'start', 'accountReport', 'incomes', 'reportType', 'accountIds', 'end', 'start', 'accountReport', 'incomes', 'reportType', 'accountIds', 'end',
'expenses', 'incomeTopLength', 'expenseTopLength', 'tags', 'budgets' 'expenses', 'incomeTopLength', 'expenseTopLength', 'tags', 'budgets', 'categories'
) )
); );
} }

View File

@ -207,6 +207,7 @@ Route::group(
// categories: // categories:
Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']); 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: // these three charts are for reports:
Route::get('/chart/category/earned-in-period/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@earnedInPeriod']); Route::get('/chart/category/earned-in-period/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@earnedInPeriod']);

View File

@ -33,7 +33,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
private $user; private $user;
/** /**
* BillRepository constructor. * BudgetRepository constructor.
* *
* @param User $user * @param User $user
*/ */

View File

@ -26,7 +26,7 @@ class CategoryRepository implements CategoryRepositoryInterface
private $user; private $user;
/** /**
* BillRepository constructor. * CategoryRepository constructor.
* *
* @param User $user * @param User $user
*/ */

View File

@ -23,7 +23,7 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
private $user; private $user;
/** /**
* BillRepository constructor. * SingleCategoryRepository constructor.
* *
* @param User $user * @param User $user
*/ */

View File

@ -26,7 +26,7 @@ class ExportJobRepository implements ExportJobRepositoryInterface
private $user; private $user;
/** /**
* BillRepository constructor. * ExportJobRepository constructor.
* *
* @param User $user * @param User $user
*/ */

View File

@ -30,7 +30,7 @@ class JournalRepository implements JournalRepositoryInterface
private $user; private $user;
/** /**
* BillRepository constructor. * JournalRepository constructor.
* *
* @param User $user * @param User $user
*/ */

View File

@ -18,6 +18,8 @@ interface RuleGroupRepositoryInterface
/** /**
*
*
* @return int * @return int
*/ */
public function count(): int; public function count(): int;

View File

@ -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/budget/year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'budgets');
stackedColumnChart('chart/category/spent-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-spent-in-period'); 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'); stackedColumnChart('chart/category/earned-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-earned-in-period');

View File

@ -61,6 +61,7 @@
</div> </div>
</div> </div>
<!--
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box"> <div class="box">
@ -73,7 +74,9 @@
</div> </div>
</div> </div>
</div> </div>
-->
<!--
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box"> <div class="box">
@ -86,20 +89,38 @@
</div> </div>
</div> </div>
</div> </div>
-->
{% for budget in budgets %} {% for category in categories %}
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box"> <div class="box">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ budget.name }}</h3> <h3 class="box-title">{{ 'category'|_ }} {{ category.name }}</h3>
</div> </div>
<div class="box-body"> <div class="box-body">
<canvas height="400" id="budgets_{{ budget.id }}" class="budget_year_chart" data-budget="{{ budget.id }}" style="width:100%;height:400px;"></canvas> <canvas height="400" id="categories_{{ category.id }}" class="category_year_chart" data-category="{{ category.id }}"
style="width:100%;height:400px;"></canvas>
</div>
</div>
</div>
</div>
{% endfor %}
{% for budget in budgets %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'budget'|_ }} {{ budget.name }}</h3>
</div>
<div class="box-body">
<canvas height="400" id="budgets_{{ budget.id }}" class="budget_year_chart" data-budget="{{ budget.id }}"
style="width:100%;height:400px;"></canvas>
</div>
</div> </div>
</div> </div>
</div> </div>
</div>
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}