From 8da0317b19acb5a6206622991532bc3e02411413 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 3 Mar 2016 09:03:55 +0100 Subject: [PATCH] Fixed tag repository and provider. --- app/Providers/FireflyServiceProvider.php | 1 - app/Providers/TagServiceProvider.php | 49 +++++++++++++++ app/Repositories/Tag/TagRepository.php | 78 ++++++++++++++---------- 3 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 app/Providers/TagServiceProvider.php diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 6219f09ed8..b70a7a7480 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -85,7 +85,6 @@ class FireflyServiceProvider extends ServiceProvider $this->app->bind('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface', 'FireflyIII\Repositories\Category\SingleCategoryRepository'); $this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository'); - $this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository'); $this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search'); // CSV import diff --git a/app/Providers/TagServiceProvider.php b/app/Providers/TagServiceProvider.php new file mode 100644 index 0000000000..227b27e73c --- /dev/null +++ b/app/Providers/TagServiceProvider.php @@ -0,0 +1,49 @@ +app->bind( + 'FireflyIII\Repositories\Tag\TagRepositoryInterface', + function (Application $app, array $arguments) { + if (!isset($arguments[0]) && Auth::check()) { + return app('FireflyIII\Repositories\Tag\TagRepository', [Auth::user()]); + } else { + if (!isset($arguments[0]) && !Auth::check()) { + throw new FireflyException('There is no user present.'); + } + } + + return app('FireflyIII\Repositories\Tag\TagRepository', $arguments); + } + ); + } +} diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 7f0bdd30b3..a0a6a3ab88 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -4,15 +4,16 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Tag; -use Auth; use Carbon\Carbon; use DB; use FireflyIII\Models\Account; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; +use FireflyIII\User; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; +use Log; /** * Class TagRepository @@ -22,6 +23,19 @@ use Illuminate\Support\Collection; class TagRepository implements TagRepositoryInterface { + /** @var User */ + private $user; + + /** + * BillRepository constructor. + * + * @param User $user + */ + public function __construct(User $user) + { + Log::debug('Constructed piggy bank repository for user #' . $user->id . ' (' . $user->email . ')'); + $this->user = $user; + } /** * @param Collection $accounts @@ -33,34 +47,34 @@ class TagRepository implements TagRepositoryInterface public function allCoveredByBalancingActs(Collection $accounts, Carbon $start, Carbon $end) { $ids = $accounts->pluck('id')->toArray(); - $set = Auth::user()->tags() - ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id') - ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id') - ->leftJoin( - 'transactions AS t_from', function (JoinClause $join) { - $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0); - } - ) - ->leftJoin( - 'transactions AS t_to', function (JoinClause $join) { - $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0); - } - ) - ->where('tags.tagMode', 'balancingAct') - ->where('transaction_types.type', TransactionType::TRANSFER) - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->whereNull('transaction_journals.deleted_at') - ->whereIn('t_from.account_id', $ids) - ->whereIn('t_to.account_id', $ids) - ->groupBy('t_to.account_id') - ->get( - [ - 't_to.account_id', - DB::raw('SUM(`t_to`.`amount`) as `sum`'), - ] - ); + $set = $this->user->tags() + ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id') + ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id') + ->leftJoin( + 'transactions AS t_from', function (JoinClause $join) { + $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0); + } + ) + ->leftJoin( + 'transactions AS t_to', function (JoinClause $join) { + $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0); + } + ) + ->where('tags.tagMode', 'balancingAct') + ->where('transaction_types.type', TransactionType::TRANSFER) + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->whereNull('transaction_journals.deleted_at') + ->whereIn('t_from.account_id', $ids) + ->whereIn('t_to.account_id', $ids) + ->groupBy('t_to.account_id') + ->get( + [ + 't_to.account_id', + DB::raw('SUM(`t_to`.`amount`) as `sum`'), + ] + ); return $set; } @@ -117,7 +131,7 @@ class TagRepository implements TagRepositoryInterface { // the quickest way to do this is by scanning all balancingAct tags // because there will be less of them any way. - $tags = Auth::user()->tags()->where('tagMode', 'balancingAct')->get(); + $tags = $this->user->tags()->where('tagMode', 'balancingAct')->get(); $amount = '0'; bcscale(2); @@ -157,7 +171,7 @@ class TagRepository implements TagRepositoryInterface public function get() { /** @var Collection $tags */ - $tags = Auth::user()->tags()->get(); + $tags = $this->user->tags()->get(); $tags = $tags->sortBy( function (Tag $tag) { return strtolower($tag->tag); @@ -182,7 +196,7 @@ class TagRepository implements TagRepositoryInterface $tag->longitude = $data['longitude']; $tag->zoomLevel = $data['zoomLevel']; $tag->tagMode = $data['tagMode']; - $tag->user()->associate(Auth::user()); + $tag->user()->associate($this->user); $tag->save(); return $tag;