mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-04 11:43:02 +00:00
Search related transfers, cleanup.
This commit is contained in:
@@ -1,16 +1,15 @@
|
|||||||
<?php namespace FireflyIII\Http\Controllers;
|
<?php namespace FireflyIII\Http\Controllers;
|
||||||
|
|
||||||
use Amount;
|
|
||||||
use Auth;
|
use Auth;
|
||||||
use FireflyIII\Http\Requests;
|
use FireflyIII\Http\Requests;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
|
||||||
use FireflyIII\Models\TransactionGroup;
|
use FireflyIII\Models\TransactionGroup;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Response;
|
|
||||||
use Input;
|
use Input;
|
||||||
use Redirect;
|
use Redirect;
|
||||||
|
use Response;
|
||||||
use URL;
|
use URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,10 +39,11 @@ class RelatedController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$unique = array_unique($ids);
|
$unique = array_unique($ids);
|
||||||
|
$journals = new Collection;
|
||||||
if (count($unique) > 0) {
|
if (count($unique) > 0) {
|
||||||
|
|
||||||
$set = Auth::user()->transactionjournals()->whereIn('id', $unique)->get();
|
$journals = Auth::user()->transactionjournals()->whereIn('id', $unique)->get();
|
||||||
$set->each(
|
$journals->each(
|
||||||
function (TransactionJournal $journal) {
|
function (TransactionJournal $journal) {
|
||||||
/** @var Transaction $t */
|
/** @var Transaction $t */
|
||||||
foreach ($journal->transactions()->get() as $t) {
|
foreach ($journal->transactions()->get() as $t) {
|
||||||
@@ -54,11 +54,38 @@ class RelatedController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return Response::json($set->toArray());
|
|
||||||
} else {
|
|
||||||
return Response::json((new Collection)->toArray());
|
|
||||||
}
|
}
|
||||||
|
$parent = $journal;
|
||||||
|
|
||||||
|
return view('related.alreadyRelated', compact('parent', 'journals'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
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
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
@@ -167,9 +166,11 @@ class RelatedController extends Controller
|
|||||||
{
|
{
|
||||||
|
|
||||||
$search = e(trim(Input::get('searchValue')));
|
$search = e(trim(Input::get('searchValue')));
|
||||||
|
$parent = $journal;
|
||||||
|
|
||||||
$journals = $repository->searchRelated($search, $journal);
|
$journals = $repository->searchRelated($search, $journal);
|
||||||
return view('related.searchResult',compact('journals'));
|
|
||||||
|
return view('related.searchResult', compact('journals', 'search', 'parent'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,23 +1,40 @@
|
|||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
$('.relateTransaction').click(relateTransaction);
|
$('.relateTransaction').click(relateTransactionDialog);
|
||||||
$('.unrelate-checkbox').click(unrelateTransaction);
|
//$('.unrelate-checkbox').click(unrelateTransaction);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function unrelateTransaction(e) {
|
function unrelateTransaction(e) {
|
||||||
var target = $(e.target);
|
var target = $(e.target);
|
||||||
var id = target.data('id');
|
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();
|
target.parent().parent().remove();
|
||||||
}).fail(function () {
|
}).fail(function () {
|
||||||
alert('Could not!');
|
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 target = $(e.target);
|
||||||
var ID = target.data('id');
|
var ID = target.data('id');
|
||||||
|
|
||||||
@@ -45,6 +62,8 @@ function searchRelatedTransactions(e, ID) {
|
|||||||
// post the results to some div.
|
// post the results to some div.
|
||||||
$('#relatedSearchResultsTitle').show();
|
$('#relatedSearchResultsTitle').show();
|
||||||
$('#relatedSearchResults').empty().html(data);
|
$('#relatedSearchResults').empty().html(data);
|
||||||
|
// remove any clicks.
|
||||||
|
$('.relate').unbind('click').on('click', doRelateNewTransaction);
|
||||||
|
|
||||||
}).fail(function () {
|
}).fail(function () {
|
||||||
alert('Could not search. Sorry.');
|
alert('Could not search. Sorry.');
|
||||||
@@ -58,38 +77,35 @@ function doRelateNewTransaction(e) {
|
|||||||
// remove the row from the table:
|
// remove the row from the table:
|
||||||
var target = $(e.target);
|
var target = $(e.target);
|
||||||
var id = target.data('id');
|
var id = target.data('id');
|
||||||
var relateToId = target.data('relateto');
|
var parent = target.data('parent');
|
||||||
if (!target.checked) {
|
|
||||||
var relateID = target.data('id');
|
if (typeof id == "undefined" && typeof parent == "undefined") {
|
||||||
$.post('related/relate/' + id + '/' + relateToId, {_token: token}).success(function (data) {
|
target = target.parent();
|
||||||
// success!
|
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();
|
target.parent().parent().remove();
|
||||||
getAlreadyRelatedTransactions(null, relateToId);
|
// get related stuff (again).
|
||||||
|
getAlreadyRelatedTransactions(null, parent);
|
||||||
}).fail(function () {
|
}).fail(function () {
|
||||||
// could not relate.
|
// could not relate.
|
||||||
alert('Error!');
|
alert('Could not relate this transaction to the intended target.');
|
||||||
});
|
});
|
||||||
|
return false;
|
||||||
|
|
||||||
} else {
|
|
||||||
alert('remove again!');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAlreadyRelatedTransactions(e, ID) {
|
function getAlreadyRelatedTransactions(e, ID) {
|
||||||
//#alreadyRelated
|
//#alreadyRelated
|
||||||
$.get('related/alreadyRelated/' + ID).success(function (data) {
|
$.get('related/alreadyRelated/' + ID).success(function (data) {
|
||||||
$('#alreadyRelated').empty();
|
$('#alreadyRelated').empty().html(data);
|
||||||
$.each(data, function (i, row) {
|
// some event triggers.
|
||||||
var tr = $('<tr>');
|
$('.unrelate').unbind('click').on('click', unrelateTransaction);
|
||||||
|
|
||||||
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));
|
|
||||||
});
|
|
||||||
}).fail(function () {
|
}).fail(function () {
|
||||||
alert('Cannot get related stuff.');
|
alert('Cannot get related stuff.');
|
||||||
});
|
});
|
||||||
|
62
resources/views/related/alreadyRelated.blade.php
Normal file
62
resources/views/related/alreadyRelated.blade.php
Normal file
@@ -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
|
@@ -19,8 +19,7 @@
|
|||||||
<div id="relatedSearchResults">
|
<div id="relatedSearchResults">
|
||||||
</div>
|
</div>
|
||||||
<h5>(Already) related transactions</h5>
|
<h5>(Already) related transactions</h5>
|
||||||
<div>
|
<div id="alreadyRelated">
|
||||||
<table id="alreadyRelated" class="table table-bordered table-striped"></table>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
|
<!-- search for: {{$search}} with {{$journals->count()}} results -->
|
||||||
|
@if($journals->count() > 0)
|
||||||
<table class="table table-bordered table-striped table-condensed">
|
<table class="table table-bordered table-striped table-condensed">
|
||||||
@foreach($journals as $journal)
|
@foreach($journals as $journal)
|
||||||
<tr>
|
<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>
|
<td>
|
||||||
@if($journal->transactiontype->type == 'Withdrawal')
|
@if($journal->transactiontype->type == 'Withdrawal')
|
||||||
<i class="fa fa-long-arrow-left fa-fw" title="Withdrawal"></i>
|
<i class="fa fa-long-arrow-left fa-fw" title="Withdrawal"></i>
|
||||||
@@ -30,29 +32,8 @@
|
|||||||
</td>
|
</td>
|
||||||
|
|
||||||
</tr>
|
</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
|
@endforeach
|
||||||
</table>
|
</table>
|
||||||
|
@else
|
||||||
|
<p><em>No results</em></p>
|
||||||
|
@endif
|
Reference in New Issue
Block a user