Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Removed duplicate code.
James Cole4b7e1ae1c6f6
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.phpindex bed615d4ad..86fcab135c 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -7,6 +7,7 @@ use Carbon\Carbon;use FireflyIII\Models\Budget;use FireflyIII\Models\BudgetLimit;use FireflyIII\Models\LimitRepetition;+use FireflyIII\Repositories\Shared\ComponentRepository;use Illuminate\Database\Query\Builder as QueryBuilder;use Illuminate\Database\Query\JoinClause;use Illuminate\Pagination\LengthAwarePaginator;@@ -18,7 +19,7 @@ use Input;** @package FireflyIII\Repositories\Budget*/-class BudgetRepository implements BudgetRepositoryInterface+class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface{/**@@ -262,32 +263,11 @@ class BudgetRepository implements BudgetRepositoryInterface* @param Carbon $end* @param bool $shared*- * @return float+ * @return string*/public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true){- if ($shared === true) {- // get everything:- $sum = floatval($budget->transactionjournals()->before($end)->after($start)->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)- ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')- ->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;+ return $this->spentInPeriod($budget, $start, $end, $shared);}/**diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.phpindex 0a035bea35..12d3d01b93 100644--- a/app/Repositories/Budget/BudgetRepositoryInterface.php+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php@@ -134,7 +134,7 @@ interface BudgetRepositoryInterface* @param Carbon $end* @param boolean $shared*- * @return float+ * @return string*/public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true);diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.phpindex 19e633be6b..c401bca5e3 100644--- a/app/Repositories/Category/CategoryRepository.php+++ b/app/Repositories/Category/CategoryRepository.php@@ -9,13 +9,13 @@ use FireflyIII\Models\Category;use FireflyIII\Models\TransactionJournal;use Illuminate\Database\Query\JoinClause;use Illuminate\Support\Collection;-+use FireflyIII\Repositories\Shared\ComponentRepository;/*** Class CategoryRepository** @package FireflyIII\Repositories\Category*/-class CategoryRepository implements CategoryRepositoryInterface+class CategoryRepository extends ComponentRepository implements CategoryRepositoryInterface{/**@@ -181,39 +181,11 @@ class CategoryRepository implements CategoryRepositoryInterface** @param bool $shared*- * @return float+ * @return string*/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'])- ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')- ->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;+ return $this->spentInPeriod($category, $start, $end, $shared);}/**diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.phpindex cfe3f93c50..a24dfb6efe 100644--- a/app/Repositories/Category/CategoryRepositoryInterface.php+++ b/app/Repositories/Category/CategoryRepositoryInterface.php@@ -81,7 +81,7 @@ interface CategoryRepositoryInterface** @param bool $shared*- * @return float+ * @return string*/public function spentInPeriodCorrected(Category $category, Carbon $start, Carbon $end, $shared = false);diff --git a/app/Repositories/Shared/ComponentRepository.php b/app/Repositories/Shared/ComponentRepository.phpnew file mode 100644index 0000000000..db0d5054a0--- /dev/null+++ b/app/Repositories/Shared/ComponentRepository.php@@ -0,0 +1,56 @@+<?php++namespace FireflyIII\Repositories\Shared;++use Carbon\Carbon;+use Illuminate\Database\Query\JoinClause;++/**+ * Class ComponentRepository+ *+ * @package FireflyIII\Repositories\Shared+ */+class ComponentRepository+{+++ /**+ * @param $object+ * @param Carbon $start+ * @param Carbon $end+ *+ * @param bool $shared+ *+ * @return string+ */+ protected function spentInPeriod($object, Carbon $start, Carbon $end, $shared = false)+ {+ if ($shared === true) {+ // shared is true.+ // always ignore transfers between accounts!+ $sum+ = $object->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 = $object->transactionjournals()+ ->before($end)+ ->after($start)+ ->transactionTypes(['Withdrawal'])+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')+ ->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');+ }++ return $sum;+ }+}\ No newline at end of filediff --git a/tests/repositories/BudgetRepositoryTest.php b/tests/repositories/BudgetRepositoryTest.phpindex 8008d78f11..d52716c89f 100644--- a/tests/repositories/BudgetRepositoryTest.php+++ b/tests/repositories/BudgetRepositoryTest.php@@ -290,6 +290,7 @@ class BudgetRepositoryTest extends TestCase/*** @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected+ * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod*/public function testSpentInPeriodCorrected(){@@ -301,6 +302,7 @@ class BudgetRepositoryTest extends TestCase/*** @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected+ * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod*/public function testSpentInPeriodCorrectedShared(){diff --git a/tests/repositories/CategoryRepositoryTest.php b/tests/repositories/CategoryRepositoryTest.phpindex d8fcf3ed76..07f553fbc6 100644--- a/tests/repositories/CategoryRepositoryTest.php+++ b/tests/repositories/CategoryRepositoryTest.php@@ -208,6 +208,7 @@ class CategoryRepositoryTest extends TestCase/*** @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected+ * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod*/public function testSpentInPeriodSumCorrected(){@@ -221,6 +222,7 @@ class CategoryRepositoryTest extends TestCase/*** @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected+ * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod*/public function testSpentInPeriodSumCorrectedShared(){