From 42b49d0e4b9532242109d3f1e408a2ff95a78c99 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 20 May 2015 06:50:15 +0200 Subject: [PATCH] Added some correcting methods. [skip ci] --- app/Models/TransactionJournal.php | 9 +++- app/Repositories/Budget/BudgetRepository.php | 47 ++++++++++++++++ .../Budget/BudgetRepositoryInterface.php | 30 +++++++++-- .../Category/CategoryRepository.php | 54 +++++++++++++++++++ .../Category/CategoryRepositoryInterface.php | 23 ++++++++ app/Support/Twig/Budget.php | 15 ++++++ 6 files changed, 174 insertions(+), 4 deletions(-) diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 9bbe977e46..80cc0b0c26 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -99,15 +99,22 @@ class TransactionJournal extends Model /** @var Tag $tag */ $tag = $this->tags()->where('tagMode', 'advancePayment')->first(); if ($tag && $this->transactionType->type == 'Withdrawal') { + // loop other deposits, remove from our amount. $others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get(); foreach ($others as $other) { - $amount -= $other->amount; + $amount -= $other->actualAmount; } return $amount; } + // if this journal is part of an advancePayment AND the journal is a deposit, + // then the journal amount is correcting a withdrawal, and the amount is zero: + if ($tag && $this->transactionType->type == 'Deposit') { + return 0; + } + return $amount; } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 96a5899e13..6a0dc8a9d8 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -99,6 +99,7 @@ class BudgetRepository implements BudgetRepositoryInterface public function getBudgets() { $budgets = Auth::user()->budgets()->get(); + return $budgets; } @@ -287,6 +288,40 @@ class BudgetRepository implements BudgetRepositoryInterface return $sum; } + /** + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param bool $shared + * + * @return float + */ + public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true) + { + if ($shared === true) { + // get everything: + $sum = floatval($budget->transactionjournals()->before($end)->after($start)->lessThan(0)->get(['transaction_journals.*'])->sum('amount')); + } else { + // get all journals in this month where the asset account is NOT shared. + $sum = $budget->transactionjournals() + ->before($end) + ->after($start) + ->lessThan(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', '!=', '"sharedAsset"') + ->get(['transaction_journals.*']) + ->sum('amount'); + $sum = floatval($sum); + } + + return $sum; + } + /** * @param array $data * @@ -372,4 +407,16 @@ class BudgetRepository implements BudgetRepositoryInterface } + + /** + * @param Budget $budget + * @param Carbon $date + * + * @return float + */ + public function expensesOnDayCorrected(Budget $budget, Carbon $date) + { + $sum = floatval($budget->transactionjournals()->transactionTypes(['Withdrawal'])->onDate($date)->get(['transaction_journals.*'])->sum('amount')); + return $sum * -1; + } } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index fc65660310..384893acef 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -34,6 +34,16 @@ interface BudgetRepositoryInterface */ public function expensesOnDay(Budget $budget, Carbon $date); + /** + * Takes tags into account. + * + * @param Budget $budget + * @param Carbon $date + * + * @return float + */ + public function expensesOnDayCorrected(Budget $budget, Carbon $date); + /** * @return Collection */ @@ -122,16 +132,30 @@ interface BudgetRepositoryInterface */ public function getWithoutBudgetSum(Carbon $start, Carbon $end); + /** - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end * @param boolean $shared * * @return float */ public function spentInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true); + /** + * + * Same as ::spentInPeriod but corrects journals for their amount (tags). + * + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param boolean $shared + * + * @return float + */ + public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true); + /** * @param array $data * diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index e67a9db2a8..0de670911b 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -205,6 +205,48 @@ class CategoryRepository implements CategoryRepositoryInterface return $sum; } + /** + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * + * @param bool $shared + * + * @return float + */ + public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false) + { + if ($shared === true) { + // shared is true. + // always ignore transfers between accounts! + $sum = floatval( + $category->transactionjournals() + ->transactionTypes(['Withdrawal']) + ->before($end)->after($start)->get(['transaction_journals.*'])->sum('amount') + ); + + } else { + // do something else, SEE budgets. + // get all journals in this month where the asset account is NOT shared. + $sum = $category->transactionjournals() + ->before($end) + ->after($start) + ->transactionTypes(['Withdrawal']) + ->lessThan(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', '!=', '"sharedAsset"') + ->get(['transaction_journals.*'])->sum('amount'); + $sum = floatval($sum); + } + + return $sum; + } + /** * @param Category $category * @param Carbon $date @@ -216,6 +258,18 @@ class CategoryRepository implements CategoryRepositoryInterface return floatval($category->transactionjournals()->onDate($date)->lessThan(0)->sum('amount')) * -1; } + /** + * Corrected for tags + * @param Category $category + * @param Carbon $date + * + * @return float + */ + public function spentOnDaySumCorrected(Category $category, Carbon $date) + { + return floatval($category->transactionjournals()->onDate($date)->get(['transaction_journals.*'])->sum('amount')); + } + /** * @param array $data * diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index 2c1355c057..d5883dd51f 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -81,6 +81,19 @@ interface CategoryRepositoryInterface */ public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false); + /** + * Corrected for tags. + * + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @param bool $shared + * + * @return float + */ + public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false); + /** * @param Category $category * @param Carbon $date @@ -89,6 +102,16 @@ interface CategoryRepositoryInterface */ public function spentOnDaySum(Category $category, Carbon $date); + /** + * + * Corrected for tags. + * @param Category $category + * @param Carbon $date + * + * @return float + */ + public function spentOnDaySumCorrected(Category $category, Carbon $date); + /** * @param array $data * diff --git a/app/Support/Twig/Budget.php b/app/Support/Twig/Budget.php index 046e741446..51f449a3bb 100644 --- a/app/Support/Twig/Budget.php +++ b/app/Support/Twig/Budget.php @@ -39,6 +39,21 @@ class Budget extends Twig_Extension } ); + $functions[] = new Twig_SimpleFunction( + 'spentInRepetitionCorrected', function (LimitRepetition $repetition) { + $sum = + Auth::user()->transactionjournals() + ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id') + ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') + ->before($repetition->enddate) + ->after($repetition->startdate) + ->where('limit_repetitions.id', '=', $repetition->id) + ->get(['transaction_journals.*'])->sum('amount'); + return floatval($sum); + } + ); + return $functions; }