mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-22 12:04:00 +00:00
Expand link view and more features #616
This commit is contained in:
@@ -13,13 +13,80 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Http\Controllers\Transaction;
|
||||
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use FireflyIII\Http\Requests\JournalLinkRequest;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
|
||||
use Log;
|
||||
use Session;
|
||||
|
||||
class LinkController
|
||||
{
|
||||
|
||||
public function store(Request $request) {
|
||||
var_dump($request->all());
|
||||
/**
|
||||
* @param JournalLinkRequest $request
|
||||
* @param LinkTypeRepositoryInterface $repository
|
||||
* @param JournalRepositoryInterface $journalRepository
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function store(
|
||||
JournalLinkRequest $request, LinkTypeRepositoryInterface $repository, JournalRepositoryInterface $journalRepository, TransactionJournal $journal
|
||||
) {
|
||||
$linkType = $request->get('link_type');
|
||||
$parts = explode('_', $linkType);
|
||||
if (count($parts) !== 2) {
|
||||
Session::flash('error', trans('firefly.invalid_link_data'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
if (!in_array($parts[1], ['inward', 'outward'])) {
|
||||
Session::flash('error', trans('firefly.invalid_link_data'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
$linkTypeId = intval($parts[0]);
|
||||
$linkType = $repository->find($linkTypeId);
|
||||
if ($linkType->id !== $linkTypeId) {
|
||||
Session::flash('error', trans('firefly.invalid_link_data'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
Log::debug('Will link using linktype', $linkType->toArray());
|
||||
$linkJournalId = intval($request->get('link_journal_id'));
|
||||
|
||||
if ($linkJournalId === 0 && ctype_digit($request->string('link_other'))) {
|
||||
$linkJournalId = intval($request->string('link_other'));
|
||||
}
|
||||
|
||||
$opposing = $journalRepository->find($linkJournalId);
|
||||
$result = $repository->findLink($journal, $opposing);
|
||||
if ($result) {
|
||||
Session::flash('error', trans('firefly.journals_error_linked'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
Log::debug(sprintf('Journal is %d, opposing is %d', $journal->id, $opposing->id));
|
||||
|
||||
$journalLink = new TransactionJournalLink;
|
||||
$journalLink->linkType()->associate($linkType);
|
||||
if ($parts[1] === 'inward') {
|
||||
Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->inward, $opposing->id, $journal->id));
|
||||
$journalLink->source()->associate($opposing);
|
||||
$journalLink->destination()->associate($journal);
|
||||
}
|
||||
if ($parts[1] === 'outward') {
|
||||
Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->outward, $journal->id, $opposing->id));
|
||||
$journalLink->source()->associate($journal);
|
||||
$journalLink->destination()->associate($opposing);
|
||||
}
|
||||
$journalLink->comment = strlen($request->string('comments')) > 0 ? $request->string('comments') : null;
|
||||
$journalLink->save();
|
||||
Session::flash('success', trans('firefly.journals_linked'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
|
||||
}
|
@@ -161,13 +161,14 @@ class TransactionController extends Controller
|
||||
if ($this->isOpeningBalance($journal)) {
|
||||
return $this->redirectToAccount($journal);
|
||||
}
|
||||
$linkTypes = $linkTypeRepository->get();
|
||||
$linkTypes = $linkTypeRepository->get();
|
||||
$links = $linkTypeRepository->getLinks($journal);
|
||||
$events = $tasker->getPiggyBankEvents($journal);
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
||||
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
||||
|
||||
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes'));
|
||||
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes','links'));
|
||||
|
||||
|
||||
}
|
||||
|
40
app/Http/Requests/JournalLinkRequest.php
Normal file
40
app/Http/Requests/JournalLinkRequest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalLinkRequest.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Http\Requests;
|
||||
|
||||
/**
|
||||
* Class JournalLink
|
||||
*
|
||||
*
|
||||
* @package FireflyIII\Http\Requests
|
||||
*/
|
||||
class JournalLinkRequest extends Request
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
// Only allow logged in users
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -13,7 +13,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Models;
|
||||
|
||||
|
||||
use Crypt;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Class TransactionJournalLink
|
||||
@@ -22,31 +24,58 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class TransactionJournalLink extends Model
|
||||
{
|
||||
protected $table = 'journal_links';
|
||||
|
||||
protected $table = 'journal_links';
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function linkType()
|
||||
public function destination()
|
||||
{
|
||||
return $this->belongsTo(TransactionJournal::class, 'destination_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function getCommentAttribute($value): ?string
|
||||
{
|
||||
if (!is_null($value)) {
|
||||
return Crypt::decrypt($value);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function linkType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(LinkType::class);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $value
|
||||
*/
|
||||
public function setCommentAttribute($value): void
|
||||
{
|
||||
if (!is_null($value) && strlen($value) > 0) {
|
||||
$this->attributes['comment'] = Crypt::encrypt($value);
|
||||
|
||||
return;
|
||||
}
|
||||
$this->attributes['comment'] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function source()
|
||||
{
|
||||
return $this->belongsTo(TransactionJournal::class,'source_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function destination()
|
||||
{
|
||||
return $this->belongsTo(TransactionJournal::class,'destination_id');
|
||||
return $this->belongsTo(TransactionJournal::class, 'source_id');
|
||||
}
|
||||
|
||||
|
||||
|
@@ -13,6 +13,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Repositories\LinkType;
|
||||
|
||||
use FireflyIII\Models\LinkType;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalLink;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -49,6 +50,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
|
||||
TransactionJournalLink::where('link_type_id', $linkType->id)->update(['link_type_id' => $moveTo->id]);
|
||||
}
|
||||
$linkType->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -67,6 +69,24 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
|
||||
return $linkType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if link exists between journals.
|
||||
*
|
||||
* @param TransactionJournal $one
|
||||
* @param TransactionJournal $two
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function findLink(TransactionJournal $one, TransactionJournal $two): bool
|
||||
{
|
||||
$count = TransactionJournalLink::whereDestinationId($one->id)->whereSourceId($two->id)->count();
|
||||
$opposingCount = TransactionJournalLink::whereDestinationId($two->id)->whereSourceId($one->id)->count();
|
||||
|
||||
return ($count + $opposingCount > 0);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -75,6 +95,21 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
|
||||
return LinkType::orderBy('name', 'ASC')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return list of existing connections.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getLinks(TransactionJournal $journal): Collection
|
||||
{
|
||||
$outward = TransactionJournalLink::whereSourceId($journal->id)->get();
|
||||
$inward = TransactionJournalLink::whereDestinationId($journal->id)->get();
|
||||
|
||||
return $outward->merge($inward);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
|
@@ -13,6 +13,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Repositories\LinkType;
|
||||
|
||||
use FireflyIII\Models\LinkType;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
@@ -22,18 +23,6 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface LinkTypeRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return LinkType
|
||||
*/
|
||||
public function find(int $id): LinkType;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function get(): Collection;
|
||||
|
||||
/**
|
||||
* @param LinkType $linkType
|
||||
*
|
||||
@@ -49,6 +38,37 @@ interface LinkTypeRepositoryInterface
|
||||
*/
|
||||
public function destroy(LinkType $linkType, LinkType $moveTo): bool;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return LinkType
|
||||
*/
|
||||
public function find(int $id): LinkType;
|
||||
|
||||
/**
|
||||
* Check if link exists between journals.
|
||||
*
|
||||
* @param TransactionJournal $one
|
||||
* @param TransactionJournal $two
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function findLink(TransactionJournal $one, TransactionJournal $two): bool;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function get(): Collection;
|
||||
|
||||
/**
|
||||
* Return list of existing connections.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getLinks(TransactionJournal $journal): Collection;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
|
@@ -40,8 +40,7 @@ class ChangesForV470 extends Migration
|
||||
$table->string('inward');
|
||||
$table->boolean('editable');
|
||||
|
||||
$table->unique(['name']);
|
||||
$table->unique(['outward','inward']);
|
||||
$table->unique(['name', 'outward','inward']);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -54,8 +53,7 @@ class ChangesForV470 extends Migration
|
||||
$table->integer('link_type_id', false, true);
|
||||
$table->integer('source_id', false, true);
|
||||
$table->integer('destination_id', false, true);
|
||||
$table->text('comment');
|
||||
$table->integer('sequence', false, true);
|
||||
$table->text('comment')->nullable();
|
||||
|
||||
$table->foreign('link_type_id')->references('id')->on('link_types')->onDelete('cascade');
|
||||
$table->foreign('source_id')->references('id')->on('transaction_journals')->onDelete('cascade');
|
||||
|
@@ -27,28 +27,28 @@ class LinkTypeSeeder extends Seeder
|
||||
$link = new LinkType;
|
||||
$link->name = 'Related';
|
||||
$link->inward = 'relates to';
|
||||
$link->outward = 'is related to';
|
||||
$link->outward = 'relates to';
|
||||
$link->editable = false;
|
||||
$link->save();
|
||||
|
||||
$link = new LinkType;
|
||||
$link->name = 'Refund';
|
||||
$link->inward = '(partially) refunds';
|
||||
$link->outward = 'is (partially) refunded by';
|
||||
$link->inward = 'is (partially) refunded by';
|
||||
$link->outward = '(partially) refunds';
|
||||
$link->editable = false;
|
||||
$link->save();
|
||||
|
||||
$link = new LinkType;
|
||||
$link->name = 'Paid';
|
||||
$link->inward = '(partially) pays for';
|
||||
$link->outward = 'is (partially) paid for by';
|
||||
$link->inward = 'is (partially) paid for by';
|
||||
$link->outward = '(partially) pays for';
|
||||
$link->editable = false;
|
||||
$link->save();
|
||||
|
||||
$link = new LinkType;
|
||||
$link->name = 'Reimbursement';
|
||||
$link->inward = '(partially) reimburses';
|
||||
$link->outward = 'is (partially) reimbursed by';
|
||||
$link->inward = 'is (partially) reimbursed by';
|
||||
$link->outward = '(partially) reimburses';
|
||||
$link->editable = false;
|
||||
$link->save();
|
||||
|
||||
|
@@ -957,7 +957,10 @@ return [
|
||||
'this_transaction' => 'This transaction',
|
||||
'transaction' => 'Transaction',
|
||||
'comments' => 'Comments',
|
||||
|
||||
'to_link_not_found' => 'If the transaction you want to link to is not listed, simply enter its ID.',
|
||||
'invalid_link_data' => 'Invalid link type selected. Cannot link transaction.',
|
||||
'journals_linked' => 'Transactions are linked.',
|
||||
'journals_error_linked' => 'These journals are already linked.',
|
||||
|
||||
// split a transaction:
|
||||
'transaction_meta_data' => 'Transaction meta-data',
|
||||
|
@@ -54,7 +54,8 @@
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#" data-toggle="modal" data-target="#linkJournalModal"><i class="fa fa-fw fa-link"></i> {{ 'link_transaction'|_ }}</a>
|
||||
<a href="#" data-toggle="modal" data-target="#linkJournalModal"><i class="fa fa-fw fa-link"></i> {{ 'link_transaction'|_ }}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -141,7 +142,8 @@
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#" data-toggle="modal" data-target="#linkJournalModal"><i class="fa fa-fw fa-link"></i> {{ 'link_transaction'|_ }}</a>
|
||||
<a href="#" data-toggle="modal" data-target="#linkJournalModal"><i class="fa fa-fw fa-link"></i> {{ 'link_transaction'|_ }}
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<a href="{{ route('transactions.delete',journal.id) }}" class="btn btn-danger"><i class="fa fa-trash fa-fw"></i> {{ 'delete'|_ }}
|
||||
@@ -309,12 +311,52 @@
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if links.count > 0 %}
|
||||
<div class="box">
|
||||
<div class="box-header with-border">
|
||||
<h3 class="box-title">{{ 'journal_links'|_ }}</h3>
|
||||
</div>
|
||||
<div class="box-body table-responsive no-padding">
|
||||
<table class="table table-hover">
|
||||
{% for link in links %}
|
||||
<tr>
|
||||
<td>
|
||||
<div class="btn-group btn-group-xs">
|
||||
<a href="{{ route('attachments.delete', att.id) }}" class="btn btn-danger"><i class="fa fa-trash"></i></a>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
{{ 'this_transaction'|_ }}
|
||||
(<a href="{{ route('transactions.show',journal.id) }}">#{{ journal.id }}</a>)
|
||||
{% if link.source.id == journal.id %}
|
||||
{{ link.linkType.outward }}
|
||||
<a href="{{ route('transactions.show',link.destination.id) }}">#{{ link.destination.id }}</a>:
|
||||
<a href="{{ route('transactions.show',link.destination.id) }}">{{ link.destination.description }}</a>
|
||||
({{ journalAmount(link.destination) }})
|
||||
{% else %}
|
||||
{{ link.linkType.inward }}
|
||||
<a href="{{ route('transactions.show',link.source.id) }}">#{{ link.source.id }}</a>:
|
||||
<a href="{{ route('transactions.show',link.source.id) }}">{{ link.source.description }}</a>
|
||||
({{ journalAmount(link.source) }})
|
||||
{% endif %}
|
||||
{% if link.comment != "" %}
|
||||
<br/><em>{{ link.comment }}</em>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<p>
|
||||
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-12 col-md-12 col-sm-12">
|
||||
|
||||
<div class="box">
|
||||
<div class="box-header with-border">
|
||||
<h3 class="box-title">{{ 'transactions'|_ }}</h3>
|
||||
@@ -398,7 +440,7 @@
|
||||
<select id="link_type" class="form-control" name="link_type">
|
||||
{% for linkType in linkTypes %}
|
||||
<option label="{{ linkType.inward }}" value="{{ linkType.id }}_inward">{{ linkType.inward }}</option>
|
||||
<option label="{{ linkType.outward }}" value="{{ linkType.id }}_inward">{{ linkType.outward }}</option>
|
||||
<option label="{{ linkType.outward }}" value="{{ linkType.id }}_outward">{{ linkType.outward }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
@@ -407,6 +449,7 @@
|
||||
<label for="link_other" class="col-sm-2 control-label">{{ 'transaction'|_ }}</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" name="link_other" autocomplete="off" id="link_other" value="" class="form-control">
|
||||
<p class="help-block">{{ 'to_link_not_found'|_ }}</p>
|
||||
<input type="hidden" name="link_journal_id" value="0">
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user