mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Chart for budget report will also include split journals.
This commit is contained in:
parent
28f655dba1
commit
d6c7ff0ccb
@ -17,10 +17,8 @@ namespace FireflyIII\Helpers\Report;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
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\Helpers\Collector\JournalCollectorInterface;
|
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
use FireflyIII\Models\TransactionType;
|
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
@ -54,66 +52,8 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
{
|
{
|
||||||
$budgets = $this->repository->getBudgets();
|
$budgets = $this->repository->getBudgets();
|
||||||
/** @var JournalCollectorInterface $collector */
|
$report = $this->repository->getBudgetPeriodReport($budgets, $accounts, $start, $end);
|
||||||
$collector = app(JournalCollectorInterface::class);
|
$data = $this->filterBudgetPeriodReport($report);
|
||||||
$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);
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@ -249,26 +189,4 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
|
|
||||||
return $data;
|
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)';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -207,13 +207,12 @@ 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// the expenses:
|
// the expenses:
|
||||||
$periods = Navigation::listOfPeriods($start, $end);
|
$periods = Navigation::listOfPeriods($start, $end);
|
||||||
$result = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
|
$entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
|
||||||
$entries = $repository->filterAmounts($result, $budget->id, $periods);
|
|
||||||
$budgeted = [];
|
$budgeted = [];
|
||||||
|
|
||||||
// the budget limits:
|
// the budget limits:
|
||||||
@ -229,6 +228,7 @@ class BudgetController extends Controller
|
|||||||
$key = 'Y';
|
$key = 'Y';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get budgeted:
|
||||||
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
||||||
$current = clone $start;
|
$current = clone $start;
|
||||||
while ($current < $end) {
|
while ($current < $end) {
|
||||||
@ -254,7 +254,7 @@ class BudgetController extends Controller
|
|||||||
foreach (array_keys($periods) as $period) {
|
foreach (array_keys($periods) as $period) {
|
||||||
$nice = $periods[$period];
|
$nice = $periods[$period];
|
||||||
$result[$nice] = [
|
$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,
|
'budgeted' => isset($entries[$period]) ? $budgeted[$period] : 0,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -348,7 +348,7 @@ class BudgetController extends Controller
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function spentInPeriodWithout(Carbon $start, Carbon $end):array
|
private function spentInPeriodWithout(Carbon $start, Carbon $end): array
|
||||||
{
|
{
|
||||||
// collector
|
// collector
|
||||||
$collector = new JournalCollector(auth()->user());
|
$collector = new JournalCollector(auth()->user());
|
||||||
|
@ -14,9 +14,9 @@ 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\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
@ -221,59 +221,70 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @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.
|
/** @var JournalCollectorInterface $collector */
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
$collector = app(JournalCollectorInterface::class);
|
||||||
|
$collector->setAllAssetAccounts()->setRange($start, $end);
|
||||||
// get budget ID's.
|
$collector->setBudgets($budgets);
|
||||||
$budgetIds = $budgets->pluck('id')->toArray();
|
$transactions = $collector->getJournals();
|
||||||
|
|
||||||
|
// this is the date format we need:
|
||||||
// define period to group on:
|
// define period to group on:
|
||||||
$sqlDateFormat = '%Y-%m-%d';
|
$carbonFormat = 'Y-m-d';
|
||||||
// monthly report (for year)
|
// monthly report (for year)
|
||||||
if ($start->diffInMonths($end) > 1) {
|
if ($start->diffInMonths($end) > 1) {
|
||||||
$sqlDateFormat = '%Y-%m';
|
$carbonFormat = 'Y-m';
|
||||||
}
|
}
|
||||||
|
|
||||||
// yearly report (for multi year)
|
// yearly report (for multi year)
|
||||||
if ($start->diffInMonths($end) > 12) {
|
if ($start->diffInMonths($end) > 12) {
|
||||||
$sqlDateFormat = '%Y';
|
$carbonFormat = 'Y';
|
||||||
}
|
}
|
||||||
|
|
||||||
// build query.
|
// this is the set of transactions for this period
|
||||||
$query = TransactionJournal
|
// in these budgets. Now they must be grouped (manually)
|
||||||
::leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
// id, period => amount
|
||||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
$data = [];
|
||||||
->leftJoin(
|
foreach ($transactions as $transaction) {
|
||||||
'transactions', function (JoinClause $join) {
|
$budgetId = max(intval($transaction->transaction_journal_budget_id), intval($transaction->transaction_budget_id));
|
||||||
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
|
$date = $transaction->date->format($carbonFormat);
|
||||||
|
|
||||||
|
if (!isset($data[$budgetId])) {
|
||||||
|
$data[$budgetId]['name'] = $this->getBudgetName($budgetId, $budgets);
|
||||||
|
$data[$budgetId]['sum'] = '0';
|
||||||
|
$data[$budgetId]['entries'] = [];
|
||||||
}
|
}
|
||||||
)
|
|
||||||
->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) {
|
if (!isset($data[$budgetId]['entries'][$date])) {
|
||||||
$query->whereIn('transactions.account_id', $accountIds);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($budgetIds) > 0) {
|
return $data;
|
||||||
$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'),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,7 +330,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) : string
|
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
|
||||||
{
|
{
|
||||||
// collect amount of transaction journals, which is easy:
|
// collect amount of transaction journals, which is easy:
|
||||||
$budgetIds = $budgets->pluck('id')->toArray();
|
$budgetIds = $budgets->pluck('id')->toArray();
|
||||||
@ -504,7 +515,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return BudgetLimit
|
* @return BudgetLimit
|
||||||
*/
|
*/
|
||||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, int $amount) : BudgetLimit
|
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, int $amount): BudgetLimit
|
||||||
{
|
{
|
||||||
// there might be a budget limit for this startdate:
|
// there might be a budget limit for this startdate:
|
||||||
$repeatFreq = config('firefly.range_to_repeat_freq.' . $range);
|
$repeatFreq = config('firefly.range_to_repeat_freq.' . $range);
|
||||||
@ -547,4 +558,25 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
|
|
||||||
return $limit;
|
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)';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,19 +91,16 @@ 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.
|
|
||||||
*
|
*
|
||||||
|
* todo always collects without budget info
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @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
|
* @return Collection
|
||||||
|
Loading…
Reference in New Issue
Block a user