mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-23 20:38:46 +00:00
New budget table for multi year report.
Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
@@ -14,6 +14,7 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Helpers\Report;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use FireflyIII\Helpers\Collection\Bill as BillCollection;
|
||||
use FireflyIII\Helpers\Collection\BillLine;
|
||||
use FireflyIII\Helpers\Collection\Category as CategoryCollection;
|
||||
@@ -21,6 +22,7 @@ use FireflyIII\Helpers\Collection\Expense;
|
||||
use FireflyIII\Helpers\Collection\Income;
|
||||
use FireflyIII\Helpers\FiscalHelperInterface;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
@@ -111,6 +113,67 @@ class ReportHelper implements ReportHelperInterface
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array
|
||||
{
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$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', 'the_year']);
|
||||
$queryResult = $query->get(
|
||||
[
|
||||
'budget_transaction_journal.budget_id',
|
||||
DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
|
||||
DB::raw('SUM(transactions.amount) as sum_of_period'),
|
||||
]
|
||||
);
|
||||
|
||||
$data = [];
|
||||
$budgets = $this->budgetRepository->getBudgets();
|
||||
$years = $this->listOfYears($start, $end);
|
||||
|
||||
// do budget "zero"
|
||||
$emptyBudget = new Budget;
|
||||
$emptyBudget->id = 0;
|
||||
$emptyBudget->name = strval(trans('firefly.no_budget'));
|
||||
$budgets->push($emptyBudget);
|
||||
|
||||
|
||||
// get all budgets and years.
|
||||
foreach ($budgets as $budget) {
|
||||
$data[$budget->id] = [
|
||||
'name' => $budget->name,
|
||||
'entries' => [],
|
||||
];
|
||||
foreach ($years as $year) {
|
||||
// filter query result here!
|
||||
$data[$budget->id]['entries'][$year] = $this->filterAmount($queryResult, $budget->id, $year);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -231,6 +294,24 @@ class ReportHelper implements ReportHelperInterface
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listOfYears(Carbon $start, Carbon $end): array
|
||||
{
|
||||
$begin = clone $start;
|
||||
$years = [];
|
||||
while ($begin < $end) {
|
||||
$years[] = $begin->year;
|
||||
$begin->addYear();
|
||||
}
|
||||
|
||||
return $years;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of tags and their comparitive size with amounts bla bla.
|
||||
*
|
||||
@@ -305,6 +386,30 @@ class ReportHelper implements ReportHelperInterface
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $set
|
||||
* @param int $budgetId
|
||||
* @param int $year
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function filterAmount(Collection $set, int $budgetId, int $year): string
|
||||
{
|
||||
/** @var stdClass $object */
|
||||
$result = $set->filter(
|
||||
function (TransactionJournal $object) use ($budgetId, $year) {
|
||||
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
|
||||
}
|
||||
);
|
||||
$amount = '0';
|
||||
if (!is_null($result->first())) {
|
||||
$amount = $result->first()->sum_of_period;
|
||||
}
|
||||
|
||||
return $amount;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
|
||||
* and sum up everything in the array in the given range.
|
||||
@@ -331,4 +436,6 @@ class ReportHelper implements ReportHelperInterface
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -28,6 +28,16 @@ use Illuminate\Support\Collection;
|
||||
interface ReportHelperInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array;
|
||||
|
||||
|
||||
/**
|
||||
* This method generates a full report for the given period on all
|
||||
* the users bills and their payments.
|
||||
@@ -80,6 +90,14 @@ interface ReportHelperInterface
|
||||
*/
|
||||
public function listOfMonths(Carbon $date): array;
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listOfYears(Carbon $start, Carbon $end): array;
|
||||
|
||||
/**
|
||||
* Returns an array of tags and their comparitive size with amounts bla bla.
|
||||
*
|
||||
|
Reference in New Issue
Block a user