mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-23 04:18:50 +00:00
Refactored the bulk edit controller.
This commit is contained in:
@@ -23,11 +23,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@@ -35,73 +33,46 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
*/
|
||||
class SimpleJournalList implements BinderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): array
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$list = array_unique(array_map('\intval', explode(',', $value)));
|
||||
if (0 === count($list)) {
|
||||
throw new NotFoundHttpException; // @codeCoverageIgnore
|
||||
$list = self::parseList($value);
|
||||
|
||||
// get the journals by using the collector.
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]);
|
||||
$collector->withCategoryInformation()->withBudgetInformation()->withTagInformation();
|
||||
$collector->setJournalIds($list);
|
||||
$result = $collector->getExtractedJournals();
|
||||
if (0 === count($result)) {
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
// 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[] = (string)trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($destinations->count() > 1) {
|
||||
$messages[] = (string)trans('firefly.cannot_edit_multiple_dest', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
}
|
||||
if (TransactionType::OPENING_BALANCE === $repository->getTransactionType($journal)) {
|
||||
$messages[] = (string)trans('firefly.cannot_edit_opening_balance');
|
||||
continue;
|
||||
}
|
||||
|
||||
// cannot edit reconciled transactions / journals:
|
||||
if ($repository->isJournalReconciled($journal)) {
|
||||
$messages[] = (string)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;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return array
|
||||
*/
|
||||
protected static function parseList(string $value): array
|
||||
{
|
||||
$list = array_unique(array_map('\intval', explode(',', $value)));
|
||||
if (0 === count($list)) {
|
||||
throw new NotFoundHttpException; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user