Moved code to relate transfers to another class. Still needs some work.

This commit is contained in:
James Cole 2015-01-02 10:53:18 +01:00
parent fb58bf1bf5
commit 9a3aed8038
9 changed files with 262 additions and 183 deletions

View File

@ -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());
}
}

View File

@ -40,44 +40,6 @@ class TransactionController extends BaseController
View::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 BaseController
return 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 BaseController
return 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 $journal

View File

@ -571,37 +571,4 @@ class TransactionJournal implements TransactionJournalInterface, CUDInterface, C
return \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;
}
}

View File

@ -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');
// chart

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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 controller
Route::get('/bills', ['uses' => 'BillController@index', 'as' => 'bills.index']);
Route::get('/bills/rescan/{bill}', ['uses' => 'BillController@rescan', 'as' => 'bills.rescan']); # rescan for matching.

View File

@ -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>');