Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Added some correcting methods. [skip ci]
James Cole42b49d0e4b95
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.phpindex 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.phpindex 96a5899e13..6a0dc8a9d8 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -99,6 +99,7 @@ class BudgetRepository implements BudgetRepositoryInterfacepublic function getBudgets(){$budgets = Auth::user()->budgets()->get();+return $budgets;}@@ -287,6 +288,40 @@ class BudgetRepository implements BudgetRepositoryInterfacereturn $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.phpindex 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.phpindex e67a9db2a8..0de670911b 100644--- a/app/Repositories/Category/CategoryRepository.php+++ b/app/Repositories/Category/CategoryRepository.php@@ -205,6 +205,48 @@ class CategoryRepository implements CategoryRepositoryInterfacereturn $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 CategoryRepositoryInterfacereturn 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.phpindex 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.phpindex 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;}