From a3d2a9e00b9109b3ce66eabbf15f4ad2b8f3f3e9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 30 Dec 2015 08:15:04 +0100 Subject: [PATCH] Some cleaning up, and I hope simplification. --- .../Controllers/Chart/CategoryController.php | 9 +- .../Category/CategoryRepository.php | 143 ++++++++++-------- .../Category/CategoryRepositoryInterface.php | 29 ++-- app/Sql/Query.php | 17 +++ 4 files changed, 114 insertions(+), 84 deletions(-) create mode 100644 app/Sql/Query.php diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 6a96bc5bdc..41a28b36b6 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -129,7 +129,6 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository * @param $reportType * @param Carbon $start * @param Carbon $end @@ -140,9 +139,9 @@ class CategoryController extends Controller */ public function multiYear($reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories) { - /** @var CategoryRepositoryInterface $repository */ + /** @var CRI $repository */ $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); - /** @var SingleCategoryRepositoryInterface $singleRepository */ + /** @var SCRI $singleRepository */ $singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); // chart properties for cache: @@ -182,8 +181,8 @@ class CategoryController extends Controller // get data: if (is_null($category->id)) { $name = trans('firefly.noCategory'); - $spent = $repository->spentNoCategoryForAccounts($accounts, $currentStart, $currentEnd); - $earned = $repository->earnedNoCategoryForAccounts($accounts, $currentStart, $currentEnd); + $spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd); + $earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd); } else { $name = $category->name; $spent = $singleRepository->spentInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd); diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index 8ccc840780..269255cba6 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use DB; use FireflyIII\Models\Category; use FireflyIII\Models\TransactionType; +use FireflyIII\Sql\Query; use FireflyIII\Support\CacheProperties; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; @@ -44,72 +45,6 @@ class CategoryRepository implements CategoryRepositoryInterface return $set; } - /** - * Returns the amount earned without category by accounts in period. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end) - { - - $accountIds = []; - foreach ($accounts as $account) { - $accountIds[] = $account->id; - } - - // is deposit AND account_from is in the list of $accounts - // not from any of the accounts in the list? - - return Auth::user() - ->transactionjournals() - ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->whereNull('category_transaction_journal.id') - ->before($end) - ->after($start) - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->whereIn('transactions.account_id', $accountIds) - ->transactionTypes([TransactionType::DEPOSIT]) - ->get(['transaction_journals.*'])->sum('amount'); - } - - - /** - * Returns the amount spent without category by accounts in period. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end) - { - - $accountIds = []; - foreach ($accounts as $account) { - $accountIds[] = $account->id; - } - - // is withdrawal or transfer AND account_from is in the list of $accounts - - - return Auth::user() - ->transactionjournals() - ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->whereNull('category_transaction_journal.id') - ->before($end) - ->after($start) - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->whereIn('transactions.account_id', $accountIds) - ->transactionTypes([TransactionType::WITHDRAWAL]) - ->get(['transaction_journals.*'])->sum('amount'); - } - - /** * @param Carbon $start * @param Carbon $end @@ -147,6 +82,7 @@ class CategoryRepository implements CategoryRepositoryInterface } // without category: + // TODO REMOVE ME $single = Auth::user()->transactionjournals() ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') @@ -392,4 +328,79 @@ class CategoryRepository implements CategoryRepositoryInterface return $collection; } + + /** + * Returns the total amount of money related to transactions without any category connected to + * it. Returns either the spent amount. + * + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end) + { + return $this->sumNoCategory($accounts, $start, $end, Query::SPENT); + } + + /** + * Returns the total amount of money related to transactions without any category connected to + * it. Returns either the earned amount. + * + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end) + { + return $this->sumNoCategory($accounts, $start, $end, Query::EARNED); + } + + /** + * Returns the total amount of money related to transactions without any category connected to + * it. Returns either the earned or the spent amount. + * + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * @param int $group + * + * @return string + */ + protected function sumNoCategory(Collection $accounts, Carbon $start, Carbon $end, $group = Query::EARNED) + { + $accountIds = []; + foreach ($accounts as $account) { + $accountIds[] = $account->id; + } + if ($group == Query::EARNED) { + $types = [TransactionType::DEPOSIT]; + } else { + $types = [TransactionType::WITHDRAWAL]; + } + + // is withdrawal or transfer AND account_from is in the list of $accounts + $single = Auth::user() + ->transactionjournals() + ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->whereNull('category_transaction_journal.id') + ->before($end) + ->after($start) + ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->whereIn('transactions.account_id', $accountIds) + ->transactionTypes($types) + ->first( + [ + DB::Raw('SUM(`transactions`.`amount`) as `sum`)')] + ); + if (!is_null($single)) { + return $single->sum; + } + + return '0'; + + } } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index 32b9892cd5..fd44e95db5 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -3,6 +3,7 @@ namespace FireflyIII\Repositories\Category; use Carbon\Carbon; +use FireflyIII\Sql\Query; use Illuminate\Support\Collection; /** @@ -39,17 +40,6 @@ interface CategoryRepositoryInterface */ public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end); - /** - * Returns the amount earned without category by accounts in period. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end); - /** * @return Collection */ @@ -100,7 +90,8 @@ interface CategoryRepositoryInterface public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end); /** - * Returns the amount spent without category by accounts in period. + * Returns the total amount of money related to transactions without any category connected to + * it. Returns either the spent amount. * * @param Collection $accounts * @param Carbon $start @@ -108,6 +99,18 @@ interface CategoryRepositoryInterface * * @return string */ - public function spentNoCategoryForAccounts(Collection $accounts, Carbon $start, Carbon $end); + public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end); + + /** + * Returns the total amount of money related to transactions without any category connected to + * it. Returns either the earned amount. + * + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end); } diff --git a/app/Sql/Query.php b/app/Sql/Query.php new file mode 100644 index 0000000000..9f31ad83fe --- /dev/null +++ b/app/Sql/Query.php @@ -0,0 +1,17 @@ +