mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
More chart optimisations.
This commit is contained in:
parent
ee6b72afa5
commit
50b72cf229
@ -39,12 +39,11 @@ interface BudgetChartGeneratorInterface
|
|||||||
public function frontpage(Collection $entries): array;
|
public function frontpage(Collection $entries): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $entries
|
* @param array $entries
|
||||||
* @param string $viewRange
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function period(Collection $entries, string $viewRange) : array;
|
public function period(array $entries) : array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
|
@ -101,15 +101,15 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $entries
|
* @param array $entries
|
||||||
* @param string $viewRange
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function period(Collection $entries, string $viewRange) : array
|
public function period(array $entries) : array
|
||||||
{
|
{
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'labels' => [],
|
'labels' => array_keys($entries),
|
||||||
'datasets' => [
|
'datasets' => [
|
||||||
0 => [
|
0 => [
|
||||||
'label' => trans('firefly.budgeted'),
|
'label' => trans('firefly.budgeted'),
|
||||||
@ -122,9 +122,8 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
|
|||||||
],
|
],
|
||||||
'count' => 2,
|
'count' => 2,
|
||||||
];
|
];
|
||||||
foreach ($entries as $entry) {
|
|
||||||
$label = Navigation::periodShow($entry['date'], $viewRange);
|
foreach ($entries as $label => $entry) {
|
||||||
$data['labels'][] = $label;
|
|
||||||
// data set 0 is budgeted
|
// data set 0 is budgeted
|
||||||
// data set 1 is spent:
|
// data set 1 is spent:
|
||||||
$data['datasets'][0]['data'][] = $entry['budgeted'];
|
$data['datasets'][0]['data'][] = $entry['budgeted'];
|
||||||
|
@ -15,15 +15,14 @@ namespace FireflyIII\Helpers\Report;
|
|||||||
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use DB;
|
|
||||||
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
||||||
use FireflyIII\Helpers\Collection\BudgetLine;
|
use FireflyIII\Helpers\Collection\BudgetLine;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use Illuminate\Database\Query\JoinClause;
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Navigation;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,8 +46,6 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO the query called here must be moved to a repository.
|
|
||||||
*
|
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
@ -57,50 +54,10 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
*/
|
*/
|
||||||
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
{
|
{
|
||||||
// get account ID's.
|
$budgets = $this->repository->getBudgets();
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
$queryResult = $this->repository->getBudgetPeriodReport($budgets, $accounts, $start, $end);
|
||||||
|
$data = [];
|
||||||
// define period to group on:
|
$periods = Navigation::listOfPeriods($start, $end);
|
||||||
$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);
|
|
||||||
}
|
|
||||||
$query->groupBy(['budget_transaction_journal.budget_id', 'period_marker']);
|
|
||||||
$queryResult = $query->get(
|
|
||||||
[
|
|
||||||
'budget_transaction_journal.budget_id',
|
|
||||||
DB::raw('DATE_FORMAT(transaction_journals.date,"' . $sqlDateFormat . '") AS period_marker'),
|
|
||||||
DB::raw('SUM(transactions.amount) as sum_of_period'),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$data = [];
|
|
||||||
$budgets = $this->repository->getBudgets();
|
|
||||||
$periods = $this->listOfPeriods($start, $end);
|
|
||||||
|
|
||||||
// do budget "zero"
|
// do budget "zero"
|
||||||
$emptyBudget = new Budget;
|
$emptyBudget = new Budget;
|
||||||
@ -108,12 +65,11 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
$emptyBudget->name = strval(trans('firefly.no_budget'));
|
$emptyBudget->name = strval(trans('firefly.no_budget'));
|
||||||
$budgets->push($emptyBudget);
|
$budgets->push($emptyBudget);
|
||||||
|
|
||||||
|
|
||||||
// get all budgets and years.
|
// get all budgets and years.
|
||||||
foreach ($budgets as $budget) {
|
foreach ($budgets as $budget) {
|
||||||
$data[$budget->id] = [
|
$data[$budget->id] = [
|
||||||
'name' => $budget->name,
|
'name' => $budget->name,
|
||||||
'entries' => $this->filterAllAmounts($queryResult, $budget->id, $periods),
|
'entries' => $this->repository->filterAmounts($queryResult, $budget->id, $periods),
|
||||||
'sum' => '0',
|
'sum' => '0',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -208,46 +164,6 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
return $set;
|
return $set;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listOfPeriods(Carbon $start, Carbon $end): array
|
|
||||||
{
|
|
||||||
// define period to increment
|
|
||||||
$increment = 'addDay';
|
|
||||||
$format = 'Y-m-d';
|
|
||||||
$displayFormat = strval(trans('config.month_and_day'));
|
|
||||||
// increment by month (for year)
|
|
||||||
if ($start->diffInMonths($end) > 1) {
|
|
||||||
$increment = 'addMonth';
|
|
||||||
$format = 'Y-m';
|
|
||||||
$displayFormat = strval(trans('config.month'));
|
|
||||||
}
|
|
||||||
|
|
||||||
// increment by year (for multi year)
|
|
||||||
if ($start->diffInMonths($end) > 12) {
|
|
||||||
$increment = 'addYear';
|
|
||||||
$format = 'Y';
|
|
||||||
$displayFormat = strval(trans('config.year'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$begin = clone $start;
|
|
||||||
$entries = [];
|
|
||||||
while ($begin < $end) {
|
|
||||||
$formatted = $begin->format($format);
|
|
||||||
$displayed = $begin->formatLocalized($displayFormat);
|
|
||||||
$entries[$formatted] = $displayed;
|
|
||||||
|
|
||||||
$begin->$increment();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $entries;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param LimitRepetition $repetition
|
* @param LimitRepetition $repetition
|
||||||
@ -268,37 +184,6 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters entries from the result set generated by getBudgetPeriodReport
|
|
||||||
*
|
|
||||||
* @param Collection $set
|
|
||||||
* @param int $budgetId
|
|
||||||
* @param array $periods
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function filterAllAmounts(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) {
|
|
||||||
return strval($object->period_marker) === $period && $budgetId === intval($object->budget_id);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
$amount = '0';
|
|
||||||
if (!is_null($result->first())) {
|
|
||||||
$amount = $result->first()->sum_of_period;
|
|
||||||
}
|
|
||||||
|
|
||||||
$arr[$period] = $amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters empty results from getBudgetPeriodReport
|
* Filters empty results from getBudgetPeriodReport
|
||||||
*
|
*
|
||||||
|
@ -53,12 +53,4 @@ interface BudgetReportHelperInterface
|
|||||||
*/
|
*/
|
||||||
public function getBudgetsWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection;
|
public function getBudgetsWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function listOfPeriods(Carbon $start, Carbon $end): array;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ use FireflyIII\Models\TransactionType;
|
|||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
|
||||||
use Navigation;
|
use Navigation;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Response;
|
use Response;
|
||||||
@ -147,7 +146,6 @@ class BudgetController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function frontpage(BudgetRepositoryInterface $repository)
|
public function frontpage(BudgetRepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
Log::debug('Hello');
|
|
||||||
$start = session('start', Carbon::now()->startOfMonth());
|
$start = session('start', Carbon::now()->startOfMonth());
|
||||||
$end = session('end', Carbon::now()->endOfMonth());
|
$end = session('end', Carbon::now()->endOfMonth());
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
@ -189,6 +187,7 @@ class BudgetController extends Controller
|
|||||||
*
|
*
|
||||||
* TODO use the NEW query that will be in the repository. Because that query will be shared between the budget period report (table for all budgets)
|
* TODO use the NEW query that will be in the repository. Because that query will be shared between the budget period report (table for all budgets)
|
||||||
* TODO and this chart (a single budget)
|
* TODO and this chart (a single budget)
|
||||||
|
*
|
||||||
* @param BudgetRepositoryInterface $repository
|
* @param BudgetRepositoryInterface $repository
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@ -199,9 +198,6 @@ class BudgetController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function period(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
|
public function period(BudgetRepositoryInterface $repository, Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// chart properties for cache:
|
// chart properties for cache:
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
@ -211,40 +207,60 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty('budget');
|
$cache->addProperty('budget');
|
||||||
$cache->addProperty('period');
|
$cache->addProperty('period');
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return Response::json($cache->get());
|
//return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
// loop over period, add by users range:
|
|
||||||
$current = clone $start;
|
// the expenses:
|
||||||
$viewRange = Preferences::get('viewRange', '1M')->data;
|
$periods = Navigation::listOfPeriods($start, $end);
|
||||||
$set = new Collection;
|
$result = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
|
||||||
|
$entries = $repository->filterAmounts($result, $budget->id, $periods);
|
||||||
|
$budgeted = [];
|
||||||
|
|
||||||
|
// the budget limits:
|
||||||
|
$range = '1D';
|
||||||
|
$key = 'Y-m-d';
|
||||||
|
if ($start->diffInMonths($end) > 1) {
|
||||||
|
$range = '1M';
|
||||||
|
$key = 'Y-m';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($start->diffInMonths($end) > 12) {
|
||||||
|
$range = '1Y';
|
||||||
|
$key = 'Y';
|
||||||
|
}
|
||||||
|
|
||||||
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
||||||
|
$current = clone $start;
|
||||||
|
|
||||||
while ($current < $end) {
|
while ($current < $end) {
|
||||||
$currentStart = clone $current;
|
$currentStart = Navigation::startOfPeriod($current, $range);
|
||||||
$currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
|
$currentEnd = Navigation::endOfPeriod($current, $range);
|
||||||
$reps = $repetitions->filter(
|
$reps = $repetitions->filter(
|
||||||
function (LimitRepetition $repetition) use ($budget, $currentStart) {
|
function (LimitRepetition $repetition) use ($budget, $currentStart, $currentEnd) {
|
||||||
if ($repetition->budget_id === $budget->id && $repetition->startdate == $currentStart) {
|
if ($repetition->budget_id === $budget->id && $repetition->startdate >= $currentStart && $repetition->enddate <= $currentEnd) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
$budgeted = $reps->sum('amount');
|
$index = $currentStart->format($key);
|
||||||
$spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
|
$budgeted[$index] = $reps->sum('amount');
|
||||||
$entry = [
|
|
||||||
'date' => clone $currentStart,
|
|
||||||
'budgeted' => $budgeted,
|
|
||||||
'spent' => $spent,
|
|
||||||
];
|
|
||||||
$set->push($entry);
|
|
||||||
$currentEnd->addDay();
|
$currentEnd->addDay();
|
||||||
$current = clone $currentEnd;
|
$current = clone $currentEnd;
|
||||||
|
|
||||||
}
|
}
|
||||||
$data = $this->generator->period($set, $viewRange);
|
|
||||||
|
// join them:
|
||||||
|
$result = [];
|
||||||
|
foreach (array_keys($periods) as $period) {
|
||||||
|
$nice = $periods[$period];
|
||||||
|
$result[$nice] = [
|
||||||
|
'spent' => isset($entries[$period]) ? $entries[$period] : '0',
|
||||||
|
'budgeted' => isset($entries[$period]) ? $budgeted[$period] : 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->generator->period($result);
|
||||||
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
@ -336,7 +352,7 @@ class BudgetController extends Controller
|
|||||||
{
|
{
|
||||||
// collector
|
// collector
|
||||||
$collector = new JournalCollector(auth()->user());
|
$collector = new JournalCollector(auth()->user());
|
||||||
$types = [TransactionType::WITHDRAWAL];
|
$types = [TransactionType::WITHDRAWAL];
|
||||||
$collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
|
$collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
|
||||||
$journals = $collector->getJournals();
|
$journals = $collector->getJournals();
|
||||||
$sum = '0';
|
$sum = '0';
|
||||||
|
@ -19,6 +19,7 @@ use FireflyIII\Helpers\Report\BudgetReportHelperInterface;
|
|||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Navigation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetController
|
* Class BudgetController
|
||||||
@ -45,10 +46,10 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty('budget-period-report');
|
$cache->addProperty('budget-period-report');
|
||||||
$cache->addProperty($accounts->pluck('id')->toArray());
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
//return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$periods = $helper->listOfPeriods($start, $end);
|
$periods = Navigation::listOfPeriods($start, $end);
|
||||||
$budgets = $helper->getBudgetPeriodReport($start, $end, $accounts);
|
$budgets = $helper->getBudgetPeriodReport($start, $end, $accounts);
|
||||||
$result = view('reports.partials.budget-period', compact('budgets', 'periods'))->render();
|
$result = view('reports.partials.budget-period', compact('budgets', 'periods'))->render();
|
||||||
$cache->store($result);
|
$cache->store($result);
|
||||||
|
@ -14,6 +14,7 @@ declare(strict_types = 1);
|
|||||||
namespace FireflyIII\Repositories\Budget;
|
namespace FireflyIII\Repositories\Budget;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
use FireflyIII\Events\StoredBudgetLimit;
|
use FireflyIII\Events\StoredBudgetLimit;
|
||||||
use FireflyIII\Events\UpdatedBudgetLimit;
|
use FireflyIII\Events\UpdatedBudgetLimit;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
@ -27,6 +28,7 @@ use Illuminate\Database\Eloquent\Builder;
|
|||||||
use Illuminate\Database\Query\JoinClause;
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetRepository
|
* Class BudgetRepository
|
||||||
@ -72,6 +74,39 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return true;
|
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.
|
* Find a budget.
|
||||||
*
|
*
|
||||||
@ -175,6 +210,73 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $set;
|
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
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
|
@ -38,6 +38,17 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function destroy(Budget $budget): bool;
|
public function destroy(Budget $budget): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a budget.
|
* Find a budget.
|
||||||
*
|
*
|
||||||
@ -79,6 +90,21 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection;
|
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.
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
|
@ -24,7 +24,6 @@ use FireflyIII\Exceptions\FireflyException;
|
|||||||
class Navigation
|
class Navigation
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Carbon\Carbon $theDate
|
* @param \Carbon\Carbon $theDate
|
||||||
* @param $repeatFreq
|
* @param $repeatFreq
|
||||||
@ -170,6 +169,46 @@ class Navigation
|
|||||||
return $currentEnd;
|
return $currentEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Carbon\Carbon $start
|
||||||
|
* @param \Carbon\Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function listOfPeriods(Carbon $start, Carbon $end): array
|
||||||
|
{
|
||||||
|
// define period to increment
|
||||||
|
$increment = 'addDay';
|
||||||
|
$format = 'Y-m-d';
|
||||||
|
$displayFormat = strval(trans('config.month_and_day'));
|
||||||
|
// increment by month (for year)
|
||||||
|
if ($start->diffInMonths($end) > 1) {
|
||||||
|
$increment = 'addMonth';
|
||||||
|
$format = 'Y-m';
|
||||||
|
$displayFormat = strval(trans('config.month'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// increment by year (for multi year)
|
||||||
|
if ($start->diffInMonths($end) > 12) {
|
||||||
|
$increment = 'addYear';
|
||||||
|
$format = 'Y';
|
||||||
|
$displayFormat = strval(trans('config.year'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$begin = clone $start;
|
||||||
|
$entries = [];
|
||||||
|
while ($begin < $end) {
|
||||||
|
$formatted = $begin->format($format);
|
||||||
|
$displayed = $begin->formatLocalized($displayFormat);
|
||||||
|
$entries[$formatted] = $displayed;
|
||||||
|
|
||||||
|
$begin->$increment();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entries;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Carbon\Carbon $date
|
* @param \Carbon\Carbon $date
|
||||||
* @param $repeatFrequency
|
* @param $repeatFrequency
|
||||||
|
Loading…
Reference in New Issue
Block a user