Chart for budget report will also include split journals.

This commit is contained in:
James Cole 2016-11-26 07:18:20 +01:00
parent 28f655dba1
commit d6c7ff0ccb
4 changed files with 82 additions and 135 deletions

View File

@ -17,10 +17,8 @@ namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
@ -54,66 +52,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
{
$budgets = $this->repository->getBudgets();
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end);
$collector->setBudgets($budgets);
$transactions = $collector->getJournals();
// this is the date format we need:
// define period to group on:
$carbonFormat = 'Y-m-d';
// monthly report (for year)
if ($start->diffInMonths($end) > 1) {
$carbonFormat = 'Y-m';
}
// yearly report (for multi year)
if ($start->diffInMonths($end) > 12) {
$carbonFormat = 'Y';
}
// this is the set of transactions for this period
// in these budgets. Now they must be grouped (manually)
// id, period => amount
$data = [];
foreach ($transactions as $transaction) {
$budgetId = max(intval($transaction->transaction_journal_budget_id), intval($transaction->transaction_budget_id));
$date = $transaction->date->format($carbonFormat);
if (!isset($data[$budgetId])) {
$data[$budgetId]['name'] = $this->getBudgetName($budgetId, $budgets);
$data[$budgetId]['sum'] = '0';
$data[$budgetId]['entries'] = [];
}
if (!isset($data[$budgetId]['entries'][$date])) {
$data[$budgetId]['entries'][$date] = '0';
}
$data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date], $transaction->transaction_amount);
}
// and now the same for stuff without a budget:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end);
$collector->setTypes([TransactionType::WITHDRAWAL]);
$collector->withoutBudget();
$transactions = $collector->getJournals();
$data[0]['entries'] = [];
$data[0]['name'] = strval(trans('firefly.no_budget'));
$data[0]['sum'] = '0';
foreach ($transactions as $transaction) {
$date = $transaction->date->format($carbonFormat);
if (!isset($data[0]['entries'][$date])) {
$data[0]['entries'][$date] = '0';
}
$data[0]['entries'][$date] = bcadd($data[0]['entries'][$date], $transaction->transaction_amount);
}
$data = $this->filterBudgetPeriodReport($data);
$report = $this->repository->getBudgetPeriodReport($budgets, $accounts, $start, $end);
$data = $this->filterBudgetPeriodReport($report);
return $data;
}
@ -249,26 +189,4 @@ class BudgetReportHelper implements BudgetReportHelperInterface
return $data;
}
/**
* @param int $budgetId
* @param Collection $budgets
*
* @return string
*/
private function getBudgetName(int $budgetId, Collection $budgets): string
{
$first = $budgets->filter(
function (Budget $budget) use ($budgetId) {
return $budgetId === $budget->id;
}
);
if (!is_null($first->first())) {
return $first->first()->name;
}
return '(unknown)';
}
}

View File

@ -207,13 +207,12 @@ class BudgetController extends Controller
$cache->addProperty('budget');
$cache->addProperty('period');
if ($cache->has()) {
return Response::json($cache->get());
// return Response::json($cache->get());
}
// the expenses:
$periods = Navigation::listOfPeriods($start, $end);
$result = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
$entries = $repository->filterAmounts($result, $budget->id, $periods);
$entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
$budgeted = [];
// the budget limits:
@ -229,6 +228,7 @@ class BudgetController extends Controller
$key = 'Y';
}
// get budgeted:
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$current = clone $start;
while ($current < $end) {
@ -254,7 +254,7 @@ class BudgetController extends Controller
foreach (array_keys($periods) as $period) {
$nice = $periods[$period];
$result[$nice] = [
'spent' => isset($entries[$period]) ? $entries[$period] : '0',
'spent' => isset($entries[$budget->id]['entries'][$period]) ? $entries[$budget->id]['entries'][$period] : '0',
'budgeted' => isset($entries[$period]) ? $budgeted[$period] : 0,
];
}

View File

@ -14,9 +14,9 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use DB;
use FireflyIII\Events\StoredBudgetLimit;
use FireflyIII\Events\UpdatedBudgetLimit;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
@ -221,59 +221,70 @@ class BudgetRepository implements BudgetRepositoryInterface
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
* @return array
*/
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
{
// get account ID's.
$accountIds = $accounts->pluck('id')->toArray();
// get budget ID's.
$budgetIds = $budgets->pluck('id')->toArray();
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end);
$collector->setBudgets($budgets);
$transactions = $collector->getJournals();
// this is the date format we need:
// define period to group on:
$sqlDateFormat = '%Y-%m-%d';
$carbonFormat = 'Y-m-d';
// monthly report (for year)
if ($start->diffInMonths($end) > 1) {
$sqlDateFormat = '%Y-%m';
$carbonFormat = 'Y-m';
}
// yearly report (for multi year)
if ($start->diffInMonths($end) > 12) {
$sqlDateFormat = '%Y';
$carbonFormat = 'Y';
}
// build query.
$query = TransactionJournal
::leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', 'Withdrawal')
->where('transaction_journals.user_id', auth()->user()->id);
// this is the set of transactions for this period
// in these budgets. Now they must be grouped (manually)
// id, period => amount
$data = [];
foreach ($transactions as $transaction) {
$budgetId = max(intval($transaction->transaction_journal_budget_id), intval($transaction->transaction_budget_id));
$date = $transaction->date->format($carbonFormat);
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
if (!isset($data[$budgetId])) {
$data[$budgetId]['name'] = $this->getBudgetName($budgetId, $budgets);
$data[$budgetId]['sum'] = '0';
$data[$budgetId]['entries'] = [];
}
if (count($budgetIds) > 0) {
$query->whereIn('budget_transaction_journal.budget_id', $budgetIds);
if (!isset($data[$budgetId]['entries'][$date])) {
$data[$budgetId]['entries'][$date] = '0';
}
$data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date], $transaction->transaction_amount);
}
// and now the same for stuff without a budget:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end);
$collector->setTypes([TransactionType::WITHDRAWAL]);
$collector->withoutBudget();
$transactions = $collector->getJournals();
$data[0]['entries'] = [];
$data[0]['name'] = strval(trans('firefly.no_budget'));
$data[0]['sum'] = '0';
foreach ($transactions as $transaction) {
$date = $transaction->date->format($carbonFormat);
if (!isset($data[0]['entries'][$date])) {
$data[0]['entries'][$date] = '0';
}
$data[0]['entries'][$date] = bcadd($data[0]['entries'][$date], $transaction->transaction_amount);
}
$query->groupBy(['budget_transaction_journal.budget_id', 'period_marker']);
return $query->get(
[
'budget_transaction_journal.budget_id',
DB::raw(sprintf('DATE_FORMAT(transaction_journals.date,"%s") AS period_marker', $sqlDateFormat)),
DB::raw('SUM(transactions.amount) as sum_of_period'),
]
);
return $data;
}
@ -547,4 +558,25 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit;
}
/**
* @param int $budgetId
* @param Collection $budgets
*
* @return string
*/
private function getBudgetName(int $budgetId, Collection $budgets): string
{
$first = $budgets->filter(
function (Budget $budget) use ($budgetId) {
return $budgetId === $budget->id;
}
);
if (!is_null($first->first())) {
return $first->first()->name;
}
return '(unknown)';
}
}

View File

@ -91,19 +91,16 @@ interface BudgetRepositoryInterface
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection;
/**
* This method is being used to generate the budget overview in the year/multi-year report. More specifically, this
* method runs the query and returns the result that is used for this report.
*
* The query is used in both the year/multi-year budget overview AND in the accompanying chart.
*
* todo always collects without budget info
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
* @return array
*/
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection;
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @return Collection