mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Update form for linking.
This commit is contained in:
parent
86aa27d92c
commit
dba3d89027
@ -94,60 +94,32 @@ class LinkController extends Controller
|
||||
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);
|
||||
if (is_null($opposing->id)) {
|
||||
Session::flash('error', trans('firefly.invalid_link_data'));
|
||||
|
||||
return redirect(route('transactions.show', $journal->id));
|
||||
}
|
||||
$result = $repository->findLink($journal, $opposing);
|
||||
if ($result) {
|
||||
$linkInfo = $request->getLinkInfo();
|
||||
$linkType = $repository->find($linkInfo['link_type_id']);
|
||||
$other = $journalRepository->find($linkInfo['transaction_journal_id']);
|
||||
$alreadyLinked = $repository->findLink($journal, $other);
|
||||
if ($alreadyLinked) {
|
||||
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));
|
||||
Log::debug(sprintf('Journal is %d, opposing is %d', $journal->id, $other->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);
|
||||
if ($linkInfo['direction'] === 'inward') {
|
||||
Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->inward, $other->id, $journal->id));
|
||||
$journalLink->source()->associate($other);
|
||||
$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));
|
||||
|
||||
if ($linkInfo['direction'] === 'outward') {
|
||||
Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->outward, $journal->id, $other->id));
|
||||
$journalLink->source()->associate($journal);
|
||||
$journalLink->destination()->associate($opposing);
|
||||
$journalLink->destination()->associate($other);
|
||||
}
|
||||
$journalLink->comment = strlen($request->string('comments')) > 0 ? $request->string('comments') : null;
|
||||
|
||||
$journalLink->comment = $linkInfo['comments'];
|
||||
$journalLink->save();
|
||||
Session::flash('success', trans('firefly.journals_linked'));
|
||||
|
||||
|
@ -12,6 +12,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Http\Requests;
|
||||
|
||||
use FireflyIII\Models\LinkType;
|
||||
|
||||
/**
|
||||
* Class JournalLink
|
||||
*
|
||||
@ -29,12 +31,44 @@ class JournalLinkRequest extends Request
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLinkInfo(): array
|
||||
{
|
||||
$return = [];
|
||||
$linkType = $this->get('link_type');
|
||||
$parts = explode('_', $linkType);
|
||||
$return['link_type_id'] = intval($parts[0]);
|
||||
$return['transaction_journal_id'] = $this->integer('link_journal_id');
|
||||
$return['comments'] = strlen($this->string('comments')) > 0 ? $this->string('comments') : null;
|
||||
$return['direction'] = $parts[1];
|
||||
if ($return['transaction_journal_id'] === 0 && ctype_digit($this->string('link_other'))) {
|
||||
$return['transaction_journal_id'] = $this->integer('link_other');
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [];
|
||||
// all possible combinations of link types and inward / outward:
|
||||
$combinations = [];
|
||||
$linkTypes = LinkType::get(['id']);
|
||||
/** @var LinkType $type */
|
||||
foreach ($linkTypes as $type) {
|
||||
$combinations[] = sprintf('%d_inward', $type->id);
|
||||
$combinations[] = sprintf('%d_outward', $type->id);
|
||||
}
|
||||
$string = join(',', $combinations);
|
||||
|
||||
return [
|
||||
'link_type' => sprintf('required|in:%s', $string),
|
||||
'link_other' => 'belongsToUser:transaction_journals',
|
||||
'link_journal_id' => 'belongsToUser:transaction_journals',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user