Month report optimised.

This commit is contained in:
James Cole 2016-01-01 19:46:12 +01:00
parent 54ede8aa18
commit 59731878f6
7 changed files with 177 additions and 52 deletions

View File

@ -26,9 +26,6 @@ class BalanceLine
/** @var BudgetModel */
protected $budget;
/** @var LimitRepetition */
protected $repetition;
protected $role = self::ROLE_DEFAULTROLE;
/**
@ -110,7 +107,7 @@ class BalanceLine
*/
public function leftOfRepetition()
{
$start = $this->getRepetition() ? $this->getRepetition()->amount : 0;
$start = isset($this->budget->amount) ? $this->budget->amount : 0;
/** @var BalanceEntry $balanceEntry */
foreach ($this->getBalanceEntries() as $balanceEntry) {
$start += $balanceEntry->getSpent();
@ -119,22 +116,6 @@ class BalanceLine
return $start;
}
/**
* @return LimitRepetition
*/
public function getRepetition()
{
return $this->repetition;
}
/**
* @param LimitRepetition $repetition
*/
public function setRepetition($repetition)
{
$this->repetition = $repetition;
}
/**
* @return Collection
*/

View File

@ -20,6 +20,8 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget as BudgetModel;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
/**
@ -62,13 +64,8 @@ class ReportHelper implements ReportHelperInterface
/** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
/** @var \FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface $singleRepository */
$singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface');
$set = $repository->listCategories();
$set = $repository->spentForAccountsPerMonth($accounts, $start, $end);
foreach ($set as $category) {
$spent = $singleRepository->balanceInPeriod($category, $start, $end, $accounts);
$category->spent = $spent;
$object->addCategory($category);
}
@ -332,13 +329,18 @@ class ReportHelper implements ReportHelperInterface
*/
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');
$balance = new Balance;
$balance = new Balance;
// build a balance header:
$header = new BalanceHeader;
$budgets = $repository->getBudgets();
$header = new BalanceHeader;
$budgets = $repository->getBudgetsAndLimitsInRange($start, $end);
$spentData = $repository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end);
foreach ($accounts as $account) {
$header->addAccount($account);
}
@ -348,18 +350,22 @@ class ReportHelper implements ReportHelperInterface
$line = new BalanceLine;
$line->setBudget($budget);
// get budget amount for current period:
$rep = $repository->getCurrentRepetition($budget, $start, $end);
// could be null?
$line->setRepetition($rep);
// loop accounts:
foreach ($accounts as $account) {
$balanceEntry = new BalanceEntry;
$balanceEntry->setAccount($account);
// 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);
$line->addBalanceEntry($balanceEntry);
@ -374,13 +380,31 @@ class ReportHelper implements ReportHelperInterface
$empty = new BalanceLine;
$tags = new BalanceLine;
$diffLine = new BalanceLine;
$tagsLeft = $tagRepository->allCoveredByBalancingActs($accounts, $start, $end);
$tags->setRole(BalanceLine::ROLE_TAGROLE);
$diffLine->setRole(BalanceLine::ROLE_DIFFROLE);
foreach ($accounts as $account) {
$spent = $this->query->spentNoBudget($account, $start, $end);
$left = $tagRepository->coveredByBalancingActs($account, $start, $end);
//$spent = $this->query->spentNoBudget($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);
$diff = bcadd($spent, $left);

View File

@ -125,21 +125,18 @@ class ReportController extends Controller
$expenseTopLength = 8;
// get report stuff!
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done
$budgets = $this->helper->getBudgetReport($start, $end, $accounts);
$categories = $this->helper->getCategoryReport($start, $end, $accounts);
$balance = $this->helper->getBalanceReport($start, $end, $accounts);
$bills = $this->helper->getBillReport($start, $end, $accounts);
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done (+2)
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done (+3)
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done (+1)
$budgets = $this->helper->getBudgetReport($start, $end, $accounts); // done (+5)
$categories = $this->helper->getCategoryReport($start, $end, $accounts); // done (+1) (20)
$balance = $this->helper->getBalanceReport($start, $end, $accounts); // +566
// $bills = $this->helper->getBillReport($start, $end, $accounts);
$bills = [];
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
$accountIds = join(',', $accountIds);
$accountIds = join(',', $accounts->pluck('id')->toArray());
// continue!
return view(

View File

@ -817,4 +817,56 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
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;
}
}

View File

@ -15,6 +15,10 @@ use Illuminate\Support\Collection;
*/
interface BudgetRepositoryInterface
{
/**
* @return void
*/
@ -51,6 +55,19 @@ interface BudgetRepositoryInterface
*/
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:
*
@ -84,6 +101,8 @@ interface BudgetRepositoryInterface
*/
public function spentPerDayForAccounts(Budget $budget, Collection $accounts, Carbon $start, Carbon $end);
/**
* Returns an array with the following key:value pairs:
*

View File

@ -5,11 +5,13 @@ namespace FireflyIII\Repositories\Tag;
use Auth;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
/**
@ -94,6 +96,49 @@ class TagRepository implements TagRepositoryInterface
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
*

View File

@ -16,7 +16,14 @@ use Illuminate\Support\Collection;
*/
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