mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 14:48:11 +00:00
Expanded reports
This commit is contained in:
@@ -6,7 +6,9 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Database\Account\Account as AccountRepository;
|
||||
use FireflyIII\Database\SwitchUser;
|
||||
use FireflyIII\Database\TransactionJournal\TransactionJournal as JournalRepository;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use stdClass;
|
||||
|
||||
// todo add methods to itnerface
|
||||
|
||||
@@ -80,6 +82,273 @@ class Report implements ReportInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountsForMonth(Carbon $date)
|
||||
{
|
||||
$start = clone $date;
|
||||
$start->startOfMonth();
|
||||
$end = clone $date;
|
||||
$end->endOfMonth();
|
||||
$list = \Auth::user()->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', "accountRole");
|
||||
}
|
||||
)
|
||||
->whereIn('account_types.type', ['Default account', 'Cash account', 'Asset account'])
|
||||
->where('active', 1)
|
||||
->where(
|
||||
function ($query) {
|
||||
$query->where('account_meta.data', '!=', '"sharedExpense"');
|
||||
$query->orWhereNull('account_meta.data');
|
||||
}
|
||||
)
|
||||
->get(['accounts.*']);
|
||||
$list->each(
|
||||
function (\Account $account) use ($start, $end) {
|
||||
$account->startBalance = \Steam::balance($account, $start);
|
||||
$account->endBalance = \Steam::balance($account, $end);
|
||||
$account->difference = $account->endBalance - $account->startBalance;
|
||||
}
|
||||
);
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetsForMonth(Carbon $date)
|
||||
{
|
||||
$start = clone $date;
|
||||
$start->startOfMonth();
|
||||
$end = clone $date;
|
||||
$end->endOfMonth();
|
||||
// all budgets
|
||||
/** @var Collection $budgets */
|
||||
$budgets = \Auth::user()->budgets()
|
||||
->leftJoin(
|
||||
'budget_limits', function (JoinClause $join) use ($date) {
|
||||
$join->on('budget_limits.budget_id', '=', 'budgets.id')->where('budget_limits.startdate', '=', $date->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->get(['budgets.*', 'budget_limits.amount as budget_amount']);
|
||||
$amounts = \Auth::user()->transactionjournals()
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budgets', 'budget_transaction_journal.budget_id', '=', 'budgets.id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->where('account_meta.data', '!=', '"sharedExpense"')
|
||||
->where('transaction_types.type', 'Withdrawal')
|
||||
->groupBy('budgets.id')
|
||||
->orderBy('name','ASC')
|
||||
->get(['budgets.id', 'budgets.name', \DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
|
||||
|
||||
$spentNoBudget = 0;
|
||||
foreach ($budgets as $budget) {
|
||||
$budget->spent = 0;
|
||||
foreach ($amounts as $amount) {
|
||||
if (intval($budget->id) == intval($amount->id)) {
|
||||
$budget->spent = floatval($amount->sum) * -1;
|
||||
}
|
||||
if (is_null($amount->id)) {
|
||||
$spentNoBudget = floatval($amount->sum) * -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$noBudget = new stdClass;
|
||||
$noBudget->id = 0;
|
||||
$noBudget->name = '(no budget)';
|
||||
$noBudget->budget_amount = 0;
|
||||
$noBudget->spent = $spentNoBudget;
|
||||
|
||||
// also get transfers to expense accounts (which are without a budget, and grouped).
|
||||
$transfers = $this->getTransfersToSharedAccounts($date);
|
||||
foreach($transfers as $transfer) {
|
||||
$noBudget->spent += floatval($transfer->sum) * -1;
|
||||
}
|
||||
|
||||
|
||||
$budgets->push($noBudget);
|
||||
|
||||
return $budgets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getCategoriesForMonth(Carbon $date, $limit = 15)
|
||||
{
|
||||
$start = clone $date;
|
||||
$start->startOfMonth();
|
||||
$end = clone $date;
|
||||
$end->endOfMonth();
|
||||
// all categories.
|
||||
$amounts = \Auth::user()->transactionjournals()
|
||||
->leftJoin(
|
||||
'category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)
|
||||
->leftJoin('categories', 'category_transaction_journal.category_id', '=', 'categories.id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->where('account_meta.data', '!=', '"sharedExpense"')
|
||||
->where('transaction_types.type', 'Withdrawal')
|
||||
->groupBy('categories.id')
|
||||
->orderBy('sum')
|
||||
->get(['categories.id', 'categories.name', \DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
$spentNoCategory = 0;
|
||||
foreach ($amounts as $amount) {
|
||||
if (is_null($amount->id)) {
|
||||
$spentNoCategory = floatval($amount->sum) * -1;
|
||||
}
|
||||
}
|
||||
$noCategory = new stdClass;
|
||||
$noCategory->id = 0;
|
||||
$noCategory->name = '(no category)';
|
||||
$noCategory->sum = $spentNoCategory;
|
||||
$amounts->push($noCategory);
|
||||
|
||||
$return = new Collection;
|
||||
$bottom = new stdClass();
|
||||
$bottom->name = 'Others';
|
||||
$bottom->id = 0;
|
||||
$bottom->sum = 0;
|
||||
|
||||
foreach ($amounts as $index => $entry) {
|
||||
if ($index < $limit) {
|
||||
$return->push($entry);
|
||||
} else {
|
||||
$bottom->sum += floatval($entry->sum);
|
||||
}
|
||||
}
|
||||
$return->push($bottom);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getExpenseGroupedForMonth(Carbon $date, $limit = 15)
|
||||
{
|
||||
$start = clone $date;
|
||||
$start->startOfMonth();
|
||||
$end = clone $date;
|
||||
$end->endOfMonth();
|
||||
$userId = $this->_accounts->getUser()->id;
|
||||
|
||||
$set = \TransactionJournal::
|
||||
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
|
||||
);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin(
|
||||
'transactions AS otherTransactions', function (JoinClause $join) {
|
||||
$join->on('transaction_journals.id', '=', 'otherTransactions.transaction_journal_id')->where(
|
||||
'otherTransactions.amount', '<', 0
|
||||
);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts as otherAccounts', 'otherAccounts.id', '=', 'otherTransactions.account_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('otherAccounts.id', '=', 'account_meta.account_id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->where('account_meta.data', '!=', '"sharedExpense"')
|
||||
->where('transaction_types.type', 'Withdrawal')
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
->where('transaction_journals.user_id', $userId)
|
||||
->groupBy('account_id')
|
||||
->orderBy('sum', 'ASC')
|
||||
->get(
|
||||
[
|
||||
'transactions.account_id',
|
||||
'accounts.name',
|
||||
\DB::Raw('SUM(`transactions`.`amount`) * -1 AS `sum`')
|
||||
]
|
||||
);
|
||||
$transfers = $this->getTransfersToSharedAccounts($date);
|
||||
// merge $transfers into $set
|
||||
foreach ($transfers as $transfer) {
|
||||
if (!is_null($transfer->account_id)) {
|
||||
$set->push($transfer);
|
||||
}
|
||||
}
|
||||
// sort the list.
|
||||
$set = $set->sortBy(
|
||||
function ($entry) {
|
||||
return floatval($entry->sum);
|
||||
}
|
||||
);
|
||||
$return = new Collection;
|
||||
$bottom = new stdClass();
|
||||
$bottom->name = 'Others';
|
||||
$bottom->account_id = 0;
|
||||
$bottom->sum = 0;
|
||||
|
||||
$count = 0;
|
||||
foreach ($set as $entry) {
|
||||
if ($count < $limit) {
|
||||
$return->push($entry);
|
||||
} else {
|
||||
$bottom->sum += floatval($entry->sum);
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
|
||||
$return->push($bottom);
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
* @param bool $shared
|
||||
@@ -95,15 +364,87 @@ class Report implements ReportInterface
|
||||
$userId = $this->_accounts->getUser()->id;
|
||||
|
||||
$list = \TransactionJournal::withRelevantData()
|
||||
->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->transactionTypes(['Deposit'])
|
||||
->where('user_id', $userId)
|
||||
->orderBy('date','DESC')
|
||||
->where('transaction_journals.user_id', $userId)
|
||||
->where('transactions.amount', '>', 0)
|
||||
->where('account_meta.data', '!=', '"sharedExpense"')
|
||||
->orderBy('date', 'ASC')
|
||||
->before($end)->after($start)->get(['transaction_journals.*']);
|
||||
|
||||
// incoming from a shared account: it's profit (income):
|
||||
$transfers = \TransactionJournal::withRelevantData()
|
||||
->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->transactionTypes(['Transfer'])
|
||||
->where('transaction_journals.user_id', $userId)
|
||||
->where('transactions.amount', '<', 0)
|
||||
->where('account_meta.data', '=', '"sharedExpense"')
|
||||
->orderBy('date', 'ASC')
|
||||
->before($end)->after($start)->get(['transaction_journals.*']);
|
||||
|
||||
$list = $list->merge($transfers);
|
||||
$list->sort(
|
||||
function (\TransactionJournal $journal) {
|
||||
return $journal->date->format('U');
|
||||
}
|
||||
);
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getTransfersToSharedAccounts(Carbon $date)
|
||||
{
|
||||
$start = clone $date;
|
||||
$start->startOfMonth();
|
||||
$end = clone $date;
|
||||
$end->endOfMonth();
|
||||
|
||||
return \TransactionJournal::
|
||||
leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where(
|
||||
'transactions.amount', '>', 0
|
||||
);
|
||||
}
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
||||
}
|
||||
)
|
||||
->where('account_meta.data', '"sharedExpense"')
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->where('transaction_types.type', 'Transfer')
|
||||
->where('transaction_journals.user_id', \Auth::user()->id)
|
||||
->get(
|
||||
[
|
||||
'transactions.account_id',
|
||||
'accounts.name',
|
||||
\DB::Raw('SUM(`transactions`.`amount`) * -1 AS `sum`')
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user