From b812881cdb5f689c88910215f428c1e0ff1248e7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 May 2015 15:43:58 +0200 Subject: [PATCH] More hip report stuff. --- app/Helpers/Collection/Balance.php | 10 ++ app/Helpers/Collection/BalanceLine.php | 36 ++++ app/Helpers/Report/ReportHelper.php | 15 +- app/Helpers/Report/ReportQuery.php | 73 +++++--- app/Helpers/Report/ReportQueryInterface.php | 12 ++ app/Http/Controllers/ReportController.php | 161 ------------------ ...{budgets-vs-accounts.twig => balance.twig} | 18 ++ resources/twig/reports/month.twig | 2 +- 8 files changed, 140 insertions(+), 187 deletions(-) rename resources/twig/partials/reports/{budgets-vs-accounts.twig => balance.twig} (87%) diff --git a/app/Helpers/Collection/Balance.php b/app/Helpers/Collection/Balance.php index d6eabf1192..84f361b804 100644 --- a/app/Helpers/Collection/Balance.php +++ b/app/Helpers/Collection/Balance.php @@ -49,5 +49,15 @@ class Balance $this->balanceHeader = $balanceHeader; } + /** + * @return \Illuminate\Support\Collection + */ + public function getBalanceLines() + { + return $this->balanceLines; + } + + + } \ No newline at end of file diff --git a/app/Helpers/Collection/BalanceLine.php b/app/Helpers/Collection/BalanceLine.php index f4473513e2..ff618118f0 100644 --- a/app/Helpers/Collection/BalanceLine.php +++ b/app/Helpers/Collection/BalanceLine.php @@ -15,9 +15,13 @@ class BalanceLine /** @var Collection */ protected $balanceEntries; + /** @var BudgetModel */ protected $budget; + /** @var float */ + protected $budgetAmount = 0.0; + /** * */ @@ -26,6 +30,9 @@ class BalanceLine $this->balanceEntries = new Collection; } + /** + * @param BalanceEntry $balanceEntry + */ public function addBalanceEntry(BalanceEntry $balanceEntry) { $this->balanceEntries->push($balanceEntry); @@ -63,5 +70,34 @@ class BalanceLine $this->budget = $budget; } + /** + * @return float + */ + public function getBudgetAmount() + { + return $this->budgetAmount; + } + + /** + * @param float $budgetAmount + */ + public function setBudgetAmount($budgetAmount) + { + $this->budgetAmount = $budgetAmount; + } + + /** + * @return float + */ + public function left() { + $start = $this->getBudgetAmount(); + /** @var BalanceEntry $balanceEntry */ + foreach($this->getBalanceEntries() as $balanceEntry) { + $start += $balanceEntry->getSpent(); + } + return $start; + } + + } \ No newline at end of file diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index fbc2464e11..90efd2b0c4 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -109,21 +109,34 @@ class ReportHelper implements ReportHelperInterface $header->addAccount($account); } + /** @var BudgetModel $budget */ foreach ($budgets as $budget) { $line = new BalanceLine; $line->setBudget($budget); + // get budget amount for current period: + $rep = $budget->limitrepetitions()->where('budget_limits.startdate',$start->format('Y-m-d 00:00:00'))->first(); + if($rep) { + $line->setBudgetAmount($rep->amount); + } + // loop accounts: foreach ($accounts as $account) { $balanceEntry = new BalanceEntry; $balanceEntry->setAccount($account); - $balanceEntry->setSpent(rand(1, 100)); + + // get spent: + $spent = $this->query->spentInBudget($account, $budget, $start, $end, $shared); // I think shared is irrelevant. + + $balanceEntry->setSpent($spent); $line->addBalanceEntry($balanceEntry); } // add line to balance: $balance->addBalanceLine($line); } + // then a new line for without budget. + $balance->setBalanceHeader($header); return $balance; diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index a4b673f410..13ad59df6f 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Crypt; use DB; use FireflyIII\Models\Account; +use FireflyIII\Models\Budget; use FireflyIII\Models\TransactionJournal; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -522,36 +523,28 @@ class ReportQuery implements ReportQueryInterface } /** - * - * This query will get all transaction journals and budget information for a specified account - * in a certain date range, where the transaction journal does not have a budget. - * There is no get() specified, this is up to the method itself. - * * @param Account $account + * @param Budget $budget * @param Carbon $start * @param Carbon $end + * @param bool $shared * - * @return Builder + * @return float */ - protected function queryJournalsNoBudget(Account $account, Carbon $start, Carbon $end) + public function spentInBudget(Account $account, Budget $budget, Carbon $start, Carbon $end, $shared = false) { - return TransactionJournal:: - leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('budgets', 'budgets.id', '=', 'budget_transaction_journal.budget_id') - ->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') - ->before($end) - ->after($start) - ->where('accounts.id', $account->id) - ->where('transaction_journals.user_id', Auth::user()->id) - ->where('transaction_types.type', 'Withdrawal') - ->groupBy('budgets.id') - ->orderBy('budgets.name', 'ASC'); + + return floatval( + Auth::user()->transactionjournals() + ->leftJoin('transactions' , 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transactions.amount', '<', 0) + ->where('transactions.account_id', $account->id) + ->before($end) + ->after($start) + ->where('budget_transaction_journal.budget_id', $budget->id) + ->sum('transactions.amount') + ); } /** @@ -591,4 +584,36 @@ class ReportQuery implements ReportQueryInterface return $query; } + /** + * + * This query will get all transaction journals and budget information for a specified account + * in a certain date range, where the transaction journal does not have a budget. + * There is no get() specified, this is up to the method itself. + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + * + * @return Builder + */ + protected function queryJournalsNoBudget(Account $account, Carbon $start, Carbon $end) + { + return TransactionJournal:: + leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->leftJoin('budgets', 'budgets.id', '=', 'budget_transaction_journal.budget_id') + ->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') + ->before($end) + ->after($start) + ->where('accounts.id', $account->id) + ->where('transaction_journals.user_id', Auth::user()->id) + ->where('transaction_types.type', 'Withdrawal') + ->groupBy('budgets.id') + ->orderBy('budgets.name', 'ASC'); + } } diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.php index 7ffcb0b6ad..67f42ef115 100644 --- a/app/Helpers/Report/ReportQueryInterface.php +++ b/app/Helpers/Report/ReportQueryInterface.php @@ -4,6 +4,7 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; use FireflyIII\Models\Account; +use FireflyIII\Models\Budget; use Illuminate\Support\Collection; /** @@ -123,6 +124,17 @@ interface ReportQueryInterface */ public function journalsByCategory(Carbon $start, Carbon $end, $includeShared = false); + /** + * @param Account $account + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param bool $shared + * + * @return float + */ + public function spentInBudget(Account $account, Budget $budget, Carbon $start, Carbon $end, $shared = false); // I think shared is irrelevant. + /** * Gets a list of expense accounts and the expenses therein, grouped by that expense account. diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 42a8033f69..9f65632b27 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -162,70 +162,6 @@ class ReportController extends Controller $categories = $this->helper->getCategoryReport($start, $end, $shared); $balance = $this->helper->getBalanceReport($start, $end, $shared); - // /** - // * DO BUDGETS. - // */ - // /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */ - // $repository = App::make('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); - // $set = $repository->getBudgets(); - // $budgets = new Collection; - // $budgetSums = ['budgeted' => 0, 'spent' => 0, 'left' => 0, 'overspent' => 0]; - // /** @var Budget $budget */ - // foreach ($set as $budget) { - // $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end); - // if ($repetitions->count() == 0) { - // $exp = $repository->spentInPeriod($budget, $start, $end, $shared); - // $budgets->push([$budget, null, 0, 0, $exp]); - // $budgetSums['overspent'] += $exp; - // continue; - // } - // /** @var LimitRepetition $repetition */ - // foreach ($repetitions as $repetition) { - // $exp = $repository->spentInPeriod($budget, $repetition->startdate, $repetition->enddate, $shared); - // $left = $exp < floatval($repetition->amount) ? floatval($repetition->amount) - $exp : 0; - // $spent = $exp > floatval($repetition->amount) ? 0 : $exp; - // $overspent = $exp > floatval($repetition->amount) ? $exp - floatval($repetition->amount) : 0; - // - // $budgetSums['budgeted'] += floatval($repetition->amount); - // $budgetSums['spent'] += $spent; - // $budgetSums['left'] += $left; - // $budgetSums['overspent'] += $overspent; - // - // $budgets->push([$budget, $repetition, $left, $spent, $overspent]); - // } - // } - // - // $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end); - // $budgets->push([null, null, 0, 0, $noBudgetExpenses]); - // $budgetSums['overspent'] += $noBudgetExpenses; - // unset($noBudgetExpenses, $repository, $set, $repetition, $repetitions, $exp); - - // /** - // * GET CATEGORIES: - // */ - // /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ - // $repository = App::make('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); - // $set = $repository->getCategories(); - // $categories = []; - // $categorySum = 0; - // foreach ($set as $category) { - // $spent = $repository->spentInPeriod($category, $start, $end, $shared); - // $categories[] = [$category, $spent]; - // $categorySum += $spent; - // } - // // no category: - // - // // sort with callback: - // uasort( - // $categories, function ($a, $b) { - // if ($a[1] == $b[1]) { - // return 0; - // } - // - // return ($a[1] < $b[1]) ? 1 : -1; - // } - // ); - // unset($set, $repository, $spent); return view( 'reports.month', @@ -240,103 +176,6 @@ class ReportController extends Controller ) ); - - // get all income and expenses. it's OK. - // $income = $this->query->incomeInPeriod($start, $end, $shared); - // $expensesSet = $this->query->journalsByExpenseAccount($start, $end, $shared); - // - // /** - // * INCLUDE ORIGINAL BUDGET REPORT HERE: - // */ - // // should show shared reports? - // /** @var Preference $pref */ - // $accountAmounts = []; // array with sums of spent amounts on each account. - // $accounts = $this->query->getAllAccounts($start, $end, $shared); // all accounts and some data. - // - // foreach ($accounts as $account) { - // - // $budgets = $this->query->getBudgetSummary($account, $start, $end);// get budget summary for this account: - // $balancedAmount = $this->query->balancedTransactionsSum($account, $start, $end); - // $accountAmounts[$account->id] = $balancedAmount; - // // balance out the transactions (see transaction groups & tags) ^^ - // - // // array with budget information for each account: - // $array = []; - // // should always hide account - // $hide = true; - // // loop all budgets - // /** @var \FireflyIII\Models\Budget $budget */ - // foreach ($budgets as $budget) { - // $id = intval($budget->id); - // $data = $budget->toArray(); - // $array[$id] = $data; - // - // // no longer hide account if any budget has money in it. - // if (floatval($data['queryAmount']) != 0) { - // $hide = false; - // } - // $accountAmounts[$account->id] += $data['queryAmount']; - // } - // $account->hide = $hide; - // $account->budgetInformation = $array; - // $account->balancedAmount = $balancedAmount; - // - // } - // /** - // * END ORIGINAL BUDGET REPORT - // */ - // - // /** - // * Start getBudgetsForMonth DONE - // */ - // $budgets = $this->helper->getBudgetsForMonth($date, $shared); - // - // /** - // * End getBudgetsForMonth DONE - // */ - // /** - // * Start getCategoriesForMonth DONE - // */ - // // all categories. - // $result = $this->query->journalsByCategory($start, $end); - // $categories = Steam::makeArray($result); - // - // - // // all transfers - // if ($shared === false) { - // $result = $this->query->sharedExpensesByCategory($start, $end); - // $transfers = Steam::makeArray($result); - // $merged = Steam::mergeArrays($categories, $transfers); - // } else { - // $merged = $categories; - // } - // - // - // // sort. - // $sorted = Steam::sortNegativeArray($merged); - // - // // limit to $limit: - // $categories = Steam::limitArray($sorted, 10); - // - // /** - // * End getCategoriesForMonth DONE - // */ - // - // - // // clean up and sort expenses: - // $expenses = Steam::makeArray($expensesSet); - // $expenses = Steam::sortArray($expenses); - // $expenses = Steam::limitArray($expenses, 10); - - // - // - // return view( - // 'reports.month', - // compact( - // 'income', 'expenses', 'budgets', 'accounts', 'categories', 'shared', - // 'date', 'subTitle', 'displaySum', 'subTitleIcon' - // ) - // ); } /** diff --git a/resources/twig/partials/reports/budgets-vs-accounts.twig b/resources/twig/partials/reports/balance.twig similarity index 87% rename from resources/twig/partials/reports/budgets-vs-accounts.twig rename to resources/twig/partials/reports/balance.twig index 471c8b001d..7456fbb6ad 100644 --- a/resources/twig/partials/reports/budgets-vs-accounts.twig +++ b/resources/twig/partials/reports/balance.twig @@ -15,6 +15,24 @@ + + {% for balanceLine in balance.getBalanceLines %} + + {{ balanceLine.getBudget.name }} + {{ balanceLine.getBudgetAmount|formatAmount }} + {% for balanceEntry in balanceLine.getBalanceEntries %} + + {% if balanceEntry.getSpent != 0 %} + {{ (balanceEntry.getSpent*-1)|formatAmountPlain }} + {% endif %} + + {% endfor %} + + {{ balanceLine.left|formatAmount }} + + + {% endfor %} +