mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
More chart optimisations.
This commit is contained in:
@@ -14,6 +14,7 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Repositories\Budget;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use FireflyIII\Events\StoredBudgetLimit;
|
||||
use FireflyIII\Events\UpdatedBudgetLimit;
|
||||
use FireflyIII\Models\Budget;
|
||||
@@ -27,6 +28,7 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class BudgetRepository
|
||||
@@ -72,6 +74,39 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters entries from the result set generated by getBudgetPeriodReport
|
||||
*
|
||||
* @param Collection $set
|
||||
* @param int $budgetId
|
||||
* @param array $periods
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function filterAmounts(Collection $set, int $budgetId, array $periods): array
|
||||
{
|
||||
$arr = [];
|
||||
$keys = array_keys($periods);
|
||||
foreach ($keys as $period) {
|
||||
/** @var stdClass $object */
|
||||
$result = $set->filter(
|
||||
function (TransactionJournal $object) use ($budgetId, $period) {
|
||||
$result = strval($object->period_marker) === strval($period) && $budgetId === intval($object->budget_id);
|
||||
|
||||
return $result;
|
||||
}
|
||||
);
|
||||
$amount = '0';
|
||||
if (!is_null($result->first())) {
|
||||
$amount = $result->first()->sum_of_period;
|
||||
}
|
||||
|
||||
$arr[$period] = $amount;
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a budget.
|
||||
*
|
||||
@@ -175,6 +210,73 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
// get account ID's.
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
|
||||
// get budget ID's.
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
// define period to group on:
|
||||
$sqlDateFormat = '%Y-%m-%d';
|
||||
// monthly report (for year)
|
||||
if ($start->diffInMonths($end) > 1) {
|
||||
$sqlDateFormat = '%Y-%m';
|
||||
}
|
||||
|
||||
// yearly report (for multi year)
|
||||
if ($start->diffInMonths($end) > 12) {
|
||||
$sqlDateFormat = '%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);
|
||||
|
||||
if (count($accountIds) > 0) {
|
||||
$query->whereIn('transactions.account_id', $accountIds);
|
||||
}
|
||||
|
||||
if (count($budgetIds) > 0) {
|
||||
$query->whereIn('budget_transaction_journal.budget_id', $budgetIds);
|
||||
}
|
||||
|
||||
$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 Collection
|
||||
*/
|
||||
|
Reference in New Issue
Block a user