. */ declare(strict_types=1); namespace FireflyIII\Support\Binder; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Routing\Route; use Illuminate\Support\Collection; use Session; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class SimpleJournalList */ class SimpleJournalList implements BinderInterface { /** * @param string $value * @param Route $route * * @return mixed * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public static function routeBinder(string $value, Route $route): Collection { if (auth()->check()) { $list = []; $incoming = explode(',', $value); foreach ($incoming as $entry) { $list[] = (int)$entry; } $list = array_unique($list); if (\count($list) === 0) { throw new NotFoundHttpException; // @codeCoverageIgnore } // prep some vars $messages = []; $final = new Collection; /** @var JournalRepositoryInterface $repository */ $repository = app(JournalRepositoryInterface::class); // get all journals: /** @var \Illuminate\Support\Collection $collection */ $collection = auth()->user()->transactionJournals() ->whereIn('transaction_journals.id', $list) ->where('transaction_journals.completed', 1) ->get(['transaction_journals.*']); // filter the list! Yay! /** @var TransactionJournal $journal */ foreach ($collection as $journal) { $sources = $repository->getJournalSourceAccounts($journal); $destinations = $repository->getJournalDestinationAccounts($journal); if ($sources->count() > 1) { $messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]); continue; } if ($destinations->count() > 1) { $messages[] = trans('firefly.cannot_edit_multiple_dest', ['description' => $journal->description, 'id' => $journal->id]); continue; } if (TransactionType::OPENING_BALANCE === $repository->getTransactionType($journal)) { $messages[] = trans('firefly.cannot_edit_opening_balance'); continue; } // cannot edit reconciled transactions / journals: if ($repository->isJournalReconciled($journal)) { $messages[] = trans('firefly.cannot_edit_reconciled', ['description' => $journal->description, 'id' => $journal->id]); continue; } $final->push($journal); } if ($final->count() > 0) { if (\count($messages) > 0) { session()->flash('info', $messages); } return $final; } } throw new NotFoundHttpException; } }