Skip to content

Firefly III: a personal finances manager

Search related transfers, cleanup.

James Cole83b169c6ef1e
diff --git a/app/Http/Controllers/RelatedController.php b/app/Http/Controllers/RelatedController.php
index 59f112b2d7..6e5102bf52 100644
--- a/app/Http/Controllers/RelatedController.php
+++ b/app/Http/Controllers/RelatedController.php
@@ -1,16 +1,15 @@
<?php namespace FireflyIII\Http\Controllers;
-use Amount;
use Auth;
use FireflyIII\Http\Requests;
use FireflyIII\Models\Transaction;
-use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionGroup;
+use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
-use Response;
use Input;
use Redirect;
+use Response;
use URL;
/**
@@ -39,11 +38,12 @@ class RelatedController extends Controller
}
}
}
- $unique = array_unique($ids);
+ $unique = array_unique($ids);
+ $journals = new Collection;
if (count($unique) > 0) {
- $set = Auth::user()->transactionjournals()->whereIn('id', $unique)->get();
- $set->each(
+ $journals = Auth::user()->transactionjournals()->whereIn('id', $unique)->get();
+ $journals->each(
function (TransactionJournal $journal) {
/** @var Transaction $t */
foreach ($journal->transactions()->get() as $t) {
@@ -54,11 +54,38 @@ class RelatedController extends Controller
}
);
+ }
+ $parent = $journal;
+
+ return view('related.alreadyRelated', compact('parent', 'journals'));
+ }
- return Response::json($set->toArray());
- } else {
- return Response::json((new Collection)->toArray());
+ /**
+ * @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
+ *
+ * @param TransactionJournal $parentJournal
+ * @param TransactionJournal $childJournal
+ *
+ * @return \Illuminate\Http\JsonResponse
+ * @throws Exception
+ */
+ public function getRemoveRelation(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 Redirect::to(URL::previous());
}
/**
@@ -130,34 +157,6 @@ class RelatedController extends Controller
return Response::json(true);
}
- /**
- * @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
- *
- * @param TransactionJournal $parentJournal
- * @param TransactionJournal $childJournal
- *
- * @return \Illuminate\Http\JsonResponse
- * @throws Exception
- */
- public function getRemoveRelation(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 Redirect::to(URL::previous());
- }
-
/**
* @param TransactionJournal $journal
*
@@ -167,9 +166,11 @@ class RelatedController extends Controller
{
$search = e(trim(Input::get('searchValue')));
+ $parent = $journal;
$journals = $repository->searchRelated($search, $journal);
- return view('related.searchResult',compact('journals'));
+
+ return view('related.searchResult', compact('journals', 'search', 'parent'));
}
diff --git a/public/js/related-manager.js b/public/js/related-manager.js
index 8ed80bbfaa..276532d77f 100644
--- a/public/js/related-manager.js
+++ b/public/js/related-manager.js
@@ -1,23 +1,40 @@
$(document).ready(function () {
- $('.relateTransaction').click(relateTransaction);
- $('.unrelate-checkbox').click(unrelateTransaction);
+ $('.relateTransaction').click(relateTransactionDialog);
+ //$('.unrelate-checkbox').click(unrelateTransaction);
});
function unrelateTransaction(e) {
var target = $(e.target);
var id = target.data('id');
- var relatedTo = target.data('relatedto');
+ var parent = target.data('parent');
- $.post('related/removeRelation/' + id + '/' + relatedTo, {_token: token}).success(function (data) {
+ if(typeof id == "undefined" && typeof parent == "undefined") {
+ target = target.parent();
+ id = target.data('id');
+ parent = target.data('parent');
+ }
+ console.log('unlink ' + id + ' from ' + parent);
+
+ $.post('related/removeRelation/' + id + '/' + parent, {_token: token}).success(function (data) {
target.parent().parent().remove();
}).fail(function () {
alert('Could not!');
});
+
+ return false;
+
+
+ //$.post('related/removeRelation/' + id + '/' + relatedTo, {_token: token}).success(function (data) {
+ // target.parent().parent().remove();
+ //}).fail(function () {
+ // alert('Could not!');
+ //});
+
}
-function relateTransaction(e) {
+function relateTransactionDialog(e) {
var target = $(e.target);
var ID = target.data('id');
@@ -45,6 +62,8 @@ function searchRelatedTransactions(e, ID) {
// post the results to some div.
$('#relatedSearchResultsTitle').show();
$('#relatedSearchResults').empty().html(data);
+ // remove any clicks.
+ $('.relate').unbind('click').on('click', doRelateNewTransaction);
}).fail(function () {
alert('Could not search. Sorry.');
@@ -58,38 +77,35 @@ function doRelateNewTransaction(e) {
// remove the row from the table:
var target = $(e.target);
var id = target.data('id');
- var relateToId = target.data('relateto');
- if (!target.checked) {
- var relateID = target.data('id');
- $.post('related/relate/' + id + '/' + relateToId, {_token: token}).success(function (data) {
- // success!
- target.parent().parent().remove();
- getAlreadyRelatedTransactions(null, relateToId);
- }).fail(function () {
- // could not relate.
- alert('Error!');
- });
+ var parent = target.data('parent');
-
- } else {
- alert('remove again!');
+ if (typeof id == "undefined" && typeof parent == "undefined") {
+ target = target.parent();
+ console.log(target);
+ id = target.data('id');
+ parent = target.data('parent');
}
+
+ console.log('Relate ' + id + ' to ' + parent);
+ $.post('related/relate/' + parent + '/' + id, {_token: token}).success(function (data) {
+ // success! remove entry:
+ target.parent().parent().remove();
+ // get related stuff (again).
+ getAlreadyRelatedTransactions(null, parent);
+ }).fail(function () {
+ // could not relate.
+ alert('Could not relate this transaction to the intended target.');
+ });
+ return false;
}
function getAlreadyRelatedTransactions(e, ID) {
//#alreadyRelated
$.get('related/alreadyRelated/' + ID).success(function (data) {
- $('#alreadyRelated').empty();
- $.each(data, function (i, row) {
- var tr = $('<tr>');
-
- var checkBox = $('<td>').append($('<input>').attr('type', 'checkbox').data('relateto', ID).data('id', row.id).click(doRelateNewTransaction));
- var description = $('<td>').text(row.description);
- var amount = $('<td>').html(row.amount);
- tr.append(checkBox).append(description).append(amount);
- $('#alreadyRelated').append(tr);
- //$('#relatedSearchResults').append($('<div>').text(row.id));
- });
+ $('#alreadyRelated').empty().html(data);
+ // some event triggers.
+ $('.unrelate').unbind('click').on('click', unrelateTransaction);
+
}).fail(function () {
alert('Cannot get related stuff.');
});
diff --git a/resources/views/related/alreadyRelated.blade.php b/resources/views/related/alreadyRelated.blade.php
new file mode 100644
index 0000000000..619d785dc2
--- /dev/null
+++ b/resources/views/related/alreadyRelated.blade.php
@@ -0,0 +1,62 @@
+@if($journals->count() > 0)
+<table class="table table-bordered table-striped table-condensed">
+ @foreach($journals as $journal)
+ <tr>
+ <td><a title="Unlink" data-id="{{$journal->id}}" data-parent="{{$parent->id}}" class="btn unrelate btn-xs btn-default" href="#"><span class="glyphicon glyphicon-resize-full"></span></a></td>
+ <td>
+ @if($journal->transactiontype->type == 'Withdrawal')
+ <i class="fa fa-long-arrow-left fa-fw" title="Withdrawal"></i>
+ @endif
+ @if($journal->transactiontype->type == 'Deposit')
+ <i class="fa fa-long-arrow-right fa-fw" title="Deposit"></i>
+ @endif
+ @if($journal->transactiontype->type == 'Transfer')
+ <i class="fa fa-arrows-h fa-fw" title="Transfer"></i>
+ @endif
+ </td>
+ <td>{{$journal->date->format('jS M Y')}}</td>
+ <td>
+ <a title="{{$journal->date->format('jS M Y')}}" href="{{route('transactions.show',$journal->id)}}">{{{$journal->description}}}</a>
+ </td>
+ <td>
+ @if($journal->transactiontype->type == 'Withdrawal')
+ <span class="text-danger">{{Amount::formatTransaction($journal->transactions[0],false)}}</span>
+ @endif
+ @if($journal->transactiontype->type == 'Deposit')
+ <span class="text-success">{{Amount::formatTransaction($journal->transactions[1],false)}}</span>
+ @endif
+ @if($journal->transactiontype->type == 'Transfer')
+ <span class="text-info">{{Amount::formatTransaction($journal->transactions[1],false)}}</span>
+ @endif
+ </td>
+
+ </tr>
+{{--
+
+
+
+
+
+ <span class="pull-right small">
+@if(isset($account))
+ @foreach($journal->transactions as $index => $t)
+ @if($t->account_id == $account->id)
+ {!! Amount::formatTransaction($t) !!}
+ @endif
+ @endforeach
+ @else
+ @foreach($journal->transactions as $index => $t)
+ @if($index == 0)
+ {!! Amount::formatTransaction($t) !!}
+ @endif
+ @endforeach
+ @endif
+</span>
+
+ </a>
+ --}}
+ @endforeach
+</table>
+@else
+ <p><em>No related transactions</em></p>
+@endif
\ No newline at end of file
diff --git a/resources/views/related/relate.blade.php b/resources/views/related/relate.blade.php
index 7d8ad029d8..194b4d74a8 100644
--- a/resources/views/related/relate.blade.php
+++ b/resources/views/related/relate.blade.php
@@ -19,8 +19,7 @@
<div id="relatedSearchResults">
</div>
<h5>(Already) related transactions</h5>
- <div>
- <table id="alreadyRelated" class="table table-bordered table-striped"></table>
+ <div id="alreadyRelated">
</div>
</div>
<div class="modal-footer">
diff --git a/resources/views/related/searchResult.blade.php b/resources/views/related/searchResult.blade.php
index 6a958f36a0..4ae8411c48 100644
--- a/resources/views/related/searchResult.blade.php
+++ b/resources/views/related/searchResult.blade.php
@@ -1,7 +1,9 @@
+<!-- search for: {{$search}} with {{$journals->count()}} results -->
+@if($journals->count() > 0)
<table class="table table-bordered table-striped table-condensed">
@foreach($journals as $journal)
<tr>
- <td><a href="#">link</a></td>
+ <td><a title="Link" data-id="{{$journal->id}}" data-parent="{{$parent->id}}" class="btn relate btn-xs btn-default" href="#"><span class="glyphicon glyphicon-resize-small"></span></a></td>
<td>
@if($journal->transactiontype->type == 'Withdrawal')
<i class="fa fa-long-arrow-left fa-fw" title="Withdrawal"></i>
@@ -30,29 +32,8 @@
</td>
</tr>
-{{--
-
-
-
-
-
- <span class="pull-right small">
-@if(isset($account))
- @foreach($journal->transactions as $index => $t)
- @if($t->account_id == $account->id)
- {!! Amount::formatTransaction($t) !!}
- @endif
- @endforeach
- @else
- @foreach($journal->transactions as $index => $t)
- @if($index == 0)
- {!! Amount::formatTransaction($t) !!}
- @endif
- @endforeach
- @endif
-</span>
-
- </a>
- --}}
@endforeach
-</table>
\ No newline at end of file
+</table>
+@else
+ <p><em>No results</em></p>
+@endif
\ No newline at end of file