mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Month report optimised.
This commit is contained in:
parent
54ede8aa18
commit
59731878f6
@ -26,9 +26,6 @@ class BalanceLine
|
|||||||
/** @var BudgetModel */
|
/** @var BudgetModel */
|
||||||
protected $budget;
|
protected $budget;
|
||||||
|
|
||||||
/** @var LimitRepetition */
|
|
||||||
protected $repetition;
|
|
||||||
|
|
||||||
protected $role = self::ROLE_DEFAULTROLE;
|
protected $role = self::ROLE_DEFAULTROLE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,7 +107,7 @@ class BalanceLine
|
|||||||
*/
|
*/
|
||||||
public function leftOfRepetition()
|
public function leftOfRepetition()
|
||||||
{
|
{
|
||||||
$start = $this->getRepetition() ? $this->getRepetition()->amount : 0;
|
$start = isset($this->budget->amount) ? $this->budget->amount : 0;
|
||||||
/** @var BalanceEntry $balanceEntry */
|
/** @var BalanceEntry $balanceEntry */
|
||||||
foreach ($this->getBalanceEntries() as $balanceEntry) {
|
foreach ($this->getBalanceEntries() as $balanceEntry) {
|
||||||
$start += $balanceEntry->getSpent();
|
$start += $balanceEntry->getSpent();
|
||||||
@ -119,22 +116,6 @@ class BalanceLine
|
|||||||
return $start;
|
return $start;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return LimitRepetition
|
|
||||||
*/
|
|
||||||
public function getRepetition()
|
|
||||||
{
|
|
||||||
return $this->repetition;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param LimitRepetition $repetition
|
|
||||||
*/
|
|
||||||
public function setRepetition($repetition)
|
|
||||||
{
|
|
||||||
$this->repetition = $repetition;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
|
@ -20,6 +20,8 @@ use FireflyIII\Models\Account;
|
|||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Budget as BudgetModel;
|
use FireflyIII\Models\Budget as BudgetModel;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
|
use FireflyIII\Models\Tag;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,13 +64,8 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
|
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
|
||||||
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
|
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
|
||||||
|
|
||||||
/** @var \FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface $singleRepository */
|
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
|
||||||
$singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
|
|
||||||
|
|
||||||
$set = $repository->listCategories();
|
|
||||||
foreach ($set as $category) {
|
foreach ($set as $category) {
|
||||||
$spent = $singleRepository->balanceInPeriod($category, $start, $end, $accounts);
|
|
||||||
$category->spent = $spent;
|
|
||||||
$object->addCategory($category);
|
$object->addCategory($category);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,13 +329,18 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
*/
|
*/
|
||||||
public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts)
|
public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts)
|
||||||
{
|
{
|
||||||
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
|
||||||
|
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
||||||
|
|
||||||
|
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
||||||
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
||||||
$balance = new Balance;
|
|
||||||
|
$balance = new Balance;
|
||||||
|
|
||||||
// build a balance header:
|
// build a balance header:
|
||||||
$header = new BalanceHeader;
|
$header = new BalanceHeader;
|
||||||
$budgets = $repository->getBudgets();
|
$budgets = $repository->getBudgetsAndLimitsInRange($start, $end);
|
||||||
|
$spentData = $repository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end);
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$header->addAccount($account);
|
$header->addAccount($account);
|
||||||
}
|
}
|
||||||
@ -348,18 +350,22 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
$line = new BalanceLine;
|
$line = new BalanceLine;
|
||||||
$line->setBudget($budget);
|
$line->setBudget($budget);
|
||||||
|
|
||||||
// get budget amount for current period:
|
|
||||||
$rep = $repository->getCurrentRepetition($budget, $start, $end);
|
|
||||||
// could be null?
|
|
||||||
$line->setRepetition($rep);
|
|
||||||
|
|
||||||
// loop accounts:
|
// loop accounts:
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$balanceEntry = new BalanceEntry;
|
$balanceEntry = new BalanceEntry;
|
||||||
$balanceEntry->setAccount($account);
|
$balanceEntry->setAccount($account);
|
||||||
|
|
||||||
// get spent:
|
// get spent:
|
||||||
$spent = $this->query->spentInBudget($account, $budget, $start, $end); // I think shared is irrelevant.
|
$entry = $spentData->filter(
|
||||||
|
function (TransactionJournal $model) use ($budget, $account) {
|
||||||
|
return $model->account_id == $account->id && $model->budget_id == $budget->id;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$spent = 0;
|
||||||
|
if (!is_null($entry->first())) {
|
||||||
|
$spent = $entry->first()->spent;
|
||||||
|
}
|
||||||
|
//$spent = $this->query->spentInBudget($account, $budget, $start, $end); // I think shared is irrelevant.
|
||||||
|
|
||||||
$balanceEntry->setSpent($spent);
|
$balanceEntry->setSpent($spent);
|
||||||
$line->addBalanceEntry($balanceEntry);
|
$line->addBalanceEntry($balanceEntry);
|
||||||
@ -374,13 +380,31 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
$empty = new BalanceLine;
|
$empty = new BalanceLine;
|
||||||
$tags = new BalanceLine;
|
$tags = new BalanceLine;
|
||||||
$diffLine = new BalanceLine;
|
$diffLine = new BalanceLine;
|
||||||
|
$tagsLeft = $tagRepository->allCoveredByBalancingActs($accounts, $start, $end);
|
||||||
|
|
||||||
$tags->setRole(BalanceLine::ROLE_TAGROLE);
|
$tags->setRole(BalanceLine::ROLE_TAGROLE);
|
||||||
$diffLine->setRole(BalanceLine::ROLE_DIFFROLE);
|
$diffLine->setRole(BalanceLine::ROLE_DIFFROLE);
|
||||||
|
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$spent = $this->query->spentNoBudget($account, $start, $end);
|
//$spent = $this->query->spentNoBudget($account, $start, $end);
|
||||||
$left = $tagRepository->coveredByBalancingActs($account, $start, $end);
|
$entry = $spentData->filter(
|
||||||
|
function (TransactionJournal $model) use ($budget, $account) {
|
||||||
|
return $model->account_id == $account->id && is_null($model->budget_id);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$spent = 0;
|
||||||
|
if (!is_null($entry->first())) {
|
||||||
|
$spent = $entry->first()->spent;
|
||||||
|
}
|
||||||
|
$leftEntry = $tagsLeft->filter(
|
||||||
|
function (Tag $tag) use ($account) {
|
||||||
|
return $tag->account_id == $account->id;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$left = 0;
|
||||||
|
if (!is_null($leftEntry->first())) {
|
||||||
|
$left = $leftEntry->first()->sum;
|
||||||
|
}
|
||||||
bcscale(2);
|
bcscale(2);
|
||||||
$diff = bcadd($spent, $left);
|
$diff = bcadd($spent, $left);
|
||||||
|
|
||||||
|
@ -125,21 +125,18 @@ class ReportController extends Controller
|
|||||||
$expenseTopLength = 8;
|
$expenseTopLength = 8;
|
||||||
|
|
||||||
// get report stuff!
|
// get report stuff!
|
||||||
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done
|
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done (+2)
|
||||||
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done
|
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done (+3)
|
||||||
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done
|
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done (+1)
|
||||||
$budgets = $this->helper->getBudgetReport($start, $end, $accounts);
|
$budgets = $this->helper->getBudgetReport($start, $end, $accounts); // done (+5)
|
||||||
$categories = $this->helper->getCategoryReport($start, $end, $accounts);
|
$categories = $this->helper->getCategoryReport($start, $end, $accounts); // done (+1) (20)
|
||||||
$balance = $this->helper->getBalanceReport($start, $end, $accounts);
|
$balance = $this->helper->getBalanceReport($start, $end, $accounts); // +566
|
||||||
$bills = $this->helper->getBillReport($start, $end, $accounts);
|
// $bills = $this->helper->getBillReport($start, $end, $accounts);
|
||||||
|
|
||||||
|
$bills = [];
|
||||||
|
|
||||||
// and some id's, joined:
|
// and some id's, joined:
|
||||||
$accountIds = [];
|
$accountIds = join(',', $accounts->pluck('id')->toArray());
|
||||||
/** @var Account $account */
|
|
||||||
foreach ($accounts as $account) {
|
|
||||||
$accountIds[] = $account->id;
|
|
||||||
}
|
|
||||||
$accountIds = join(',', $accountIds);
|
|
||||||
|
|
||||||
// continue!
|
// continue!
|
||||||
return view(
|
return view(
|
||||||
|
@ -817,4 +817,56 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of expenses (in the field "spent", grouped per budget per account.
|
||||||
|
*
|
||||||
|
* @param Collection $budgets
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
|
||||||
|
$accountIds = $accounts->pluck('id')->toArray();
|
||||||
|
$budgetIds = $budgets->pluck('id')->toArray();
|
||||||
|
$set = Auth::user()->transactionjournals()
|
||||||
|
->leftJoin(
|
||||||
|
'transactions AS t_from', function (JoinClause $join) {
|
||||||
|
$join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->leftJoin(
|
||||||
|
'transactions AS t_to', function (JoinClause $join) {
|
||||||
|
$join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||||
|
->whereIn('t_from.account_id', $accountIds)
|
||||||
|
->whereNotIn('t_to.account_id', $accountIds)
|
||||||
|
->where(
|
||||||
|
function (Builder $q) use ($budgetIds) {
|
||||||
|
$q->whereIn('budget_transaction_journal.budget_id', $budgetIds);
|
||||||
|
$q->orWhereNull('budget_transaction_journal.budget_id');
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->after($start)
|
||||||
|
->before($end)
|
||||||
|
->groupBy('t_from.account_id')
|
||||||
|
->groupBy('budget_transaction_journal.budget_id')
|
||||||
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE])
|
||||||
|
->get(
|
||||||
|
[
|
||||||
|
't_from.account_id',
|
||||||
|
'budget_transaction_journal.budget_id',
|
||||||
|
DB::Raw('SUM(`t_from`.`amount`) AS `spent`')
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface BudgetRepositoryInterface
|
interface BudgetRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@ -51,6 +55,19 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end);
|
public function getExpensesPerMonth(Budget $budget, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of expenses (in the field "spent", grouped per budget per account.
|
||||||
|
*
|
||||||
|
* @param Collection $budgets
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array with the following key:value pairs:
|
* Returns an array with the following key:value pairs:
|
||||||
*
|
*
|
||||||
@ -84,6 +101,8 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentPerDayForAccounts(Budget $budget, Collection $accounts, Carbon $start, Carbon $end);
|
public function spentPerDayForAccounts(Budget $budget, Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array with the following key:value pairs:
|
* Returns an array with the following key:value pairs:
|
||||||
*
|
*
|
||||||
|
@ -5,11 +5,13 @@ namespace FireflyIII\Repositories\Tag;
|
|||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\Tag;
|
use FireflyIII\Models\Tag;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
|
use Illuminate\Database\Query\JoinClause;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,6 +96,49 @@ class TagRepository implements TagRepositoryInterface
|
|||||||
return $amount;
|
return $amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function allCoveredByBalancingActs(Collection $accounts, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
|
$set = Auth::user()->tags()
|
||||||
|
->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id')
|
||||||
|
->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
|
||||||
|
->leftJoin(
|
||||||
|
'transactions AS t_from', function (JoinClause $join) {
|
||||||
|
$join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->leftJoin(
|
||||||
|
'transactions AS t_to', function (JoinClause $join) {
|
||||||
|
$join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->where('tags.tagMode', 'balancingAct')
|
||||||
|
->where('transaction_types.type', TransactionType::TRANSFER)
|
||||||
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||||
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||||
|
->whereNull('transaction_journals.deleted_at')
|
||||||
|
->whereIn('t_from.account_id', $ids)
|
||||||
|
->whereIn('t_to.account_id', $ids)
|
||||||
|
->groupBy('t_to.account_id')
|
||||||
|
->get(
|
||||||
|
[
|
||||||
|
't_to.account_id',
|
||||||
|
DB::Raw('SUM(`t_to`.`amount`) as `sum`')
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $set;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Tag $tag
|
* @param Tag $tag
|
||||||
*
|
*
|
||||||
|
@ -16,7 +16,14 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface TagRepositoryInterface
|
interface TagRepositoryInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function allCoveredByBalancingActs(Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
|
Loading…
Reference in New Issue
Block a user