mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user