Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Moved code to relate transfers to another class. Still needs some work.
James Cole9a3aed80389f
diff --git a/app/controllers/RelatedController.php b/app/controllers/RelatedController.phpnew file mode 100644index 0000000000..1a65ae4c15--- /dev/null+++ b/app/controllers/RelatedController.php@@ -0,0 +1,139 @@+<?php+use FireflyIII\Helper\Related\RelatedInterface;+use Illuminate\Support\Collection;++/**+ * Class RelatedController+ */+class RelatedController extends BaseController+{++ protected $_repository;++ public function __construct(RelatedInterface $repository)+ {+ $this->_repository = $repository;++ }++ /**+ * @param TransactionJournal $journal+ *+ * @return \Illuminate\Http\JsonResponse+ */+ public function alreadyRelated(TransactionJournal $journal)+ {+ $ids = [];+ /** @var TransactionGroup $group */+ foreach ($journal->transactiongroups()->get() as $group) {+ /** @var TransactionJournal $loopJournal */+ foreach ($group->transactionjournals()->get() as $loopJournal) {+ if ($loopJournal->id != $journal->id) {+ $ids[] = $loopJournal->id;+ }+ }+ }+ $unique = array_unique($ids);+ if (count($unique) > 0) {++ $set = $this->_repository->getJournalsByIds($unique);+ $set->each(+ function (TransactionJournal $journal) {+ $journal->amount = Amount::format($journal->getAmount());+ }+ );++ return Response::json($set->toArray());+ } else {+ return Response::json((new Collection)->toArray());+ }+ }++ /**+ * @param TransactionJournal $parentJournal+ * @param TransactionJournal $childJournal+ *+ * @return \Illuminate\Http\JsonResponse+ */+ public function relate(TransactionJournal $parentJournal, TransactionJournal $childJournal)+ {+ $group = new TransactionGroup;+ $group->relation = 'balance';+ $group->user_id = $this->_repository->getUser()->id;+ $group->save();+ $group->transactionjournals()->save($parentJournal);+ $group->transactionjournals()->save($childJournal);++ return Response::json(true);++ }++ /**+ * @param TransactionJournal $journal+ *+ * @return \Illuminate\View\View+ */+ public function related(TransactionJournal $journal)+ {+ $groups = $journal->transactiongroups()->get();+ $members = new Collection;+ /** @var TransactionGroup $group */+ foreach ($groups as $group) {+ /** @var TransactionJournal $loopJournal */+ foreach ($group->transactionjournals()->get() as $loopJournal) {+ if ($loopJournal->id != $journal->id) {+ $members->push($loopJournal);+ }+ }+ }++ return View::make('related.relate', compact('journal', 'members'));+ }++ /**+ * @param TransactionJournal $parentJournal+ * @param TransactionJournal $childJournal+ *+ * @return \Illuminate\Http\JsonResponse+ * @throws Exception+ */+ public function removeRelation(TransactionJournal $parentJournal, TransactionJournal $childJournal)+ {+ $groups = $parentJournal->transactiongroups()->get();+ /** @var TransactionGroup $group */+ foreach ($groups as $group) {+ foreach ($group->transactionjournals()->get() as $loopJournal) {+ if ($loopJournal->id == $childJournal->id) {+ // remove from group:+ $group->transactionjournals()->detach($childJournal);+ }+ }+ if ($group->transactionjournals()->count() == 1) {+ $group->delete();+ }+ }++ return Response::json(true);+ }++ /**+ * @param TransactionJournal $journal+ *+ * @return \Illuminate\Http\JsonResponse+ */+ public function search(TransactionJournal $journal)+ {++ $search = e(trim(Input::get('searchValue')));++ $result = $this->_repository->search($search, $journal);+ $result->each(+ function (TransactionJournal $j) {+ $j->amount = Amount::format($j->getAmount());+ }+ );++ return Response::json($result->toArray());+ }++}\ No newline at end of filediff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.phpindex 167da8a6c0..d62fe5a3cb 100644--- a/app/controllers/TransactionController.php+++ b/app/controllers/TransactionController.php@@ -40,44 +40,6 @@ class TransactionController extends BaseControllerView::share('mainTitleIcon', 'fa-repeat');}- /**- *- * TODO this needs cleaning up and thinking over.- *- * @param TransactionJournal $journal- *- * @codeCoverageIgnore- *- * @return array|\Illuminate\Http\JsonResponse- */- public function alreadyRelated(TransactionJournal $journal)- {-- $ids = [];- /** @var TransactionGroup $group */- foreach ($journal->transactiongroups()->get() as $group) {- /** @var TransactionJournal $loopJournal */- foreach ($group->transactionjournals()->get() as $loopJournal) {- if ($loopJournal->id != $journal->id) {- $ids[] = $loopJournal->id;- }- }- }- $unique = array_unique($ids);- if (count($unique) > 0) {-- $set = $this->_repository->getByIds($unique);- $set->each(- function (TransactionJournal $journal) {- $journal->amount = Amount::format($journal->getAmount());- }- );-- return Response::json($set->toArray());- } else {- return (new Collection)->toArray();- }- }/*** Shows the view helping the user to create a new transaction journal.@@ -156,37 +118,6 @@ class TransactionController extends BaseControllerreturn Redirect::route('transactions.index', $return);}- /**- * TODO this needs cleaning up and thinking over.- *- * @return \Illuminate\Http\JsonResponse- * @codeCoverageIgnore- *- */- public function doRelate()- {- $brother = intval(Input::get('id'));- $sister = intval(Input::get('relateTo'));-- $journal = $this->_repository->find($brother);- $sis = $this->_repository->find($sister);-- if ($journal && $sis) {- $group = new TransactionGroup;- $group->relation = 'balance';- $group->user_id = $this->_repository->getUser()->id;- $group->save();- $group->transactionjournals()->save($journal);- $group->transactionjournals()->save($sis);-- return Response::json(true);- }-- return Response::json(false);--- }-/*** Shows the view to edit a transaction.*@@ -266,54 +197,7 @@ class TransactionController extends BaseController}- /**- * TODO refactor relate stuff into another controller.- *- * @param TransactionJournal $journal- *- * @return \Illuminate\View\View- * @codeCoverageIgnore- *- */- public function relate(TransactionJournal $journal)- {- $groups = $journal->transactiongroups()->get();- $members = new Collection;- /** @var TransactionGroup $group */- foreach ($groups as $group) {- /** @var TransactionJournal $loopJournal */- foreach ($group->transactionjournals()->get() as $loopJournal) {- if ($loopJournal->id != $journal->id) {- $members->push($loopJournal);- }- }- }-- return View::make('transactions.relate', compact('journal', 'members'));- }-- /**- * TODO this needs cleaning up and thinking over.- *- * @param TransactionJournal $journal- *- * @codeCoverageIgnore- *- * @return \Illuminate\Http\JsonResponse- */- public function relatedSearch(TransactionJournal $journal)- {- $search = e(trim(Input::get('searchValue')));- $result = $this->_repository->searchRelated($search, $journal);- $result->each(- function (TransactionJournal $j) {- $j->amount = Amount::format($j->getAmount());- }- );-- return Response::json($result->toArray());- }/*** @param TransactionJournal $journal@@ -401,35 +285,6 @@ class TransactionController extends BaseControllerreturn Redirect::route('transactions.create', $data['what'])->withInput();}- /**- * TODO this needs cleaning up and thinking over.- *- * @param TransactionJournal $journal- *- * @codeCoverageIgnore- * @return \Illuminate\Http\JsonResponse- * @throws Exception- */- public function unrelate(TransactionJournal $journal)- {- $groups = $journal->transactiongroups()->get();- $relatedTo = intval(Input::get('relation'));- /** @var TransactionGroup $group */- foreach ($groups as $group) {- foreach ($group->transactionjournals()->get() as $loopJournal) {- if ($loopJournal->id == $relatedTo) {- // remove from group:- $group->transactionjournals()->detach($relatedTo);- }- }- if ($group->transactionjournals()->count() == 1) {- $group->delete();- }- }-- return Response::json(true);-- }/*** @param TransactionJournal $journaldiff --git a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.phpindex 73bc4e3877..ff7cafa8e2 100644--- a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php+++ b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php@@ -571,37 +571,4 @@ class TransactionJournal implements TransactionJournalInterface, CUDInterface, Creturn \Paginator::make($items, $count, $limit);}-- /**- * @param string $query- * @param \TransactionJournal $journal- *- * @return Collection- */- public function searchRelated($query, \TransactionJournal $journal)- {- $start = clone $journal->date;- $end = clone $journal->date;- $start->startOfMonth();- $end->endOfMonth();-- // get already related transactions:- $exclude = [$journal->id];- foreach ($journal->transactiongroups()->get() as $group) {- foreach ($group->transactionjournals() as $current) {- $exclude[] = $current->id;- }- }- $exclude = array_unique($exclude);-- $query = $this->getUser()->transactionjournals()- ->withRelevantData()- ->before($end)- ->after($start)- ->whereNotIn('id', $exclude)- ->where('description', 'LIKE', '%' . $query . '%')- ->get();-- return $query;- }}diff --git a/app/lib/FireflyIII/FF3ServiceProvider.php b/app/lib/FireflyIII/FF3ServiceProvider.phpindex 6495f65f51..879313a2d2 100644--- a/app/lib/FireflyIII/FF3ServiceProvider.php+++ b/app/lib/FireflyIII/FF3ServiceProvider.php@@ -102,6 +102,9 @@ class FF3ServiceProvider extends ServiceProvider$this->app->bind('FireflyIII\Report\ReportInterface', 'FireflyIII\Report\Report');$this->app->bind('FireflyIII\Report\ReportQueryInterface', 'FireflyIII\Report\ReportQuery');$this->app->bind('FireflyIII\Report\ReportHelperInterface', 'FireflyIII\Report\ReportHelper');++ $this->app->bind('FireflyIII\Helper\Related\RelatedInterface', 'FireflyIII\Helper\Related\Related');+$this->app->bind('FireflyIII\Helper\TransactionJournal\HelperInterface', 'FireflyIII\Helper\TransactionJournal\Helper');// chartdiff --git a/app/lib/FireflyIII/Helper/Related/Related.php b/app/lib/FireflyIII/Helper/Related/Related.phpnew file mode 100644index 0000000000..2c54bd6451--- /dev/null+++ b/app/lib/FireflyIII/Helper/Related/Related.php@@ -0,0 +1,69 @@+<?php+namespace FireflyIII\Helper\Related;++use FireflyIII\Database\SwitchUser;+use Illuminate\Support\Collection;++/**+ * Class Related+ *+ * @package FireflyIII\Helper\Related+ */+class Related implements RelatedInterface+{+ use SwitchUser;++ /**+ *+ */+ public function __construct()+ {+ $this->setUser(\Auth::user());+ }++ /**+ * @param array $objectIds+ *+ * @return Collection+ */+ public function getJournalsByIds(array $objectIds)+ {+ /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */+ $repository = \App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');++ return $repository->getByIds($objectIds);+ }++ /**+ * @param string $query+ * @param \TransactionJournal $journal+ *+ * @return Collection+ */+ public function search($query, \TransactionJournal $journal)+ {+ $start = clone $journal->date;+ $end = clone $journal->date;+ $start->startOfMonth();+ $end->endOfMonth();++ // get already related transactions:+ $exclude = [$journal->id];+ foreach ($journal->transactiongroups()->get() as $group) {+ foreach ($group->transactionjournals() as $current) {+ $exclude[] = $current->id;+ }+ }+ $exclude = array_unique($exclude);++ $query = $this->getUser()->transactionjournals()+ ->withRelevantData()+ ->before($end)+ ->after($start)+ ->whereNotIn('id', $exclude)+ ->where('description', 'LIKE', '%' . $query . '%')+ ->get();++ return $query;+ }+}\ No newline at end of filediff --git a/app/lib/FireflyIII/Helper/Related/RelatedInterface.php b/app/lib/FireflyIII/Helper/Related/RelatedInterface.phpnew file mode 100644index 0000000000..cde8e7e54a--- /dev/null+++ b/app/lib/FireflyIII/Helper/Related/RelatedInterface.php@@ -0,0 +1,29 @@+<?php++namespace FireflyIII\Helper\Related;+use Illuminate\Support\Collection;++/**+ * Interface RelatedInterface+ *+ * @package FireflyIII\Helper\Related+ */+interface RelatedInterface+{++ /**+ * @param string $query+ * @param \TransactionJournal $journal+ *+ * @return Collection+ */+ public function search($query, \TransactionJournal $journal);++ /**+ * @param array $objectIds+ *+ * @return Collection+ */+ public function getJournalsByIds(array $objectIds);++}\ No newline at end of filediff --git a/app/routes.php b/app/routes.phpindex 0aa5b61cd5..4e37cce682 100644--- a/app/routes.php+++ b/app/routes.php@@ -97,6 +97,16 @@ Route::bind(return null;});+Route::bind(+ 'tjSecond', function ($value, $route) {+ if (Auth::check()) {+ return TransactionJournal::+ where('id', $value)->where('user_id', Auth::user()->id)->first();+ }++ return null;+}+);Route::bind('currency', function ($value, $route) {@@ -247,6 +257,13 @@ Route::group(Route::get('/profile', ['uses' => 'ProfileController@index', 'as' => 'profile']);Route::get('/profile/change-password', ['uses' => 'ProfileController@changePassword', 'as' => 'change-password']);+ // related controller:+ Route::get('/related/removeRelation/{tj}/{tjSecond}', ['uses' => 'RelatedController@removeRelation','as' => 'related.removeRelation']);+ Route::get('/related/related/{tj}', ['uses' => 'RelatedController@related','as' => 'related.related']);+ Route::post('/related/search/{tj}', ['uses' => 'RelatedController@search','as' => 'related.search']);+ Route::post('/related/relate/{tj}/{tjSecond}', ['uses' => 'RelatedController@relate','as' => 'related.relate']);+ Route::get('/related/alreadyRelated/{tj}', ['uses' => 'RelatedController@alreadyRelated','as' => 'related.alreadyRelated']);+// bills controllerRoute::get('/bills', ['uses' => 'BillController@index', 'as' => 'bills.index']);Route::get('/bills/rescan/{bill}', ['uses' => 'BillController@rescan', 'as' => 'bills.rescan']); # rescan for matching.diff --git a/app/views/transactions/relate.blade.php b/app/views/related/relate.blade.phpsimilarity index 100%rename from app/views/transactions/relate.blade.phprename to app/views/related/relate.blade.phpdiff --git a/public/assets/javascript/firefly/related-manager.js b/public/assets/javascript/firefly/related-manager.jsindex dc4171512a..923afb5147 100644--- a/public/assets/javascript/firefly/related-manager.js+++ b/public/assets/javascript/firefly/related-manager.js@@ -9,7 +9,7 @@ function unrelateTransaction(e) {var id = target.data('id');var relatedTo = target.data('relatedto');- $.post('transactions/unrelate/' + relatedTo, {relation: id}).success(function (data) {+ $.post('related/removeRelation/' + id + '/' + relatedTo).success(function (data) {target.parent().parent().remove();}).fail(function () {alert('Could not!');@@ -23,7 +23,7 @@ function relateTransaction(e) {console.log($('#searchRelated').length);- $('#relationModal').empty().load('transaction/relate/' + ID, function () {+ $('#relationModal').empty().load('related/related/' + ID, function () {$('#relationModal').modal('show');getAlreadyRelatedTransactions(e, ID);@@ -42,7 +42,7 @@ function relateTransaction(e) {function searchRelatedTransactions(e, ID) {var searchValue = $('#relatedSearchValue').val();if (searchValue != '') {- $.post('transactions/relatedSearch/' + ID, {searchValue: searchValue}).success(function (data) {+ $.post('related/search/' + ID, {searchValue: searchValue}).success(function (data) {// post each result to some div.$('#relatedSearchResults').empty();// TODO this is the worst.@@ -74,7 +74,7 @@ function doRelateNewTransaction(e) {var relateToId = target.data('relateto');if (!target.checked) {var relateID = target.data('id');- $.post('transactions/doRelate', {relateTo: relateToId, id: id}).success(function (data) {+ $.post('related/relate/' + id + '/' + relateToId).success(function (data) {// success!target.parent().parent().remove();getAlreadyRelatedTransactions(null, relateToId);@@ -91,7 +91,7 @@ function doRelateNewTransaction(e) {function getAlreadyRelatedTransactions(e, ID) {//#alreadyRelated- $.post('transactions/alreadyRelated/' + ID).success(function (data) {+ $.get('related/alreadyRelated/' + ID).success(function (data) {$('#alreadyRelated').empty();$.each(data, function (i, row) {var tr = $('<tr>');