mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 04:34:00 +00:00
Some code cleanup for #1272
This commit is contained in:
@@ -28,7 +28,6 @@ use ExpandedForm;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Http\Requests\BulkEditJournalRequest;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Update\JournalUpdateService;
|
||||
@@ -66,70 +65,25 @@ class BulkController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Collection $journals
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
public function edit(Request $request, Collection $journals)
|
||||
public function edit(Collection $journals)
|
||||
{
|
||||
|
||||
$subTitle = trans('firefly.mass_bulk_journals');
|
||||
|
||||
// skip transactions that have multiple destinations, multiple sources or are an opening balance.
|
||||
$filtered = new Collection;
|
||||
$messages = [];
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$sources = $this->repository->getJournalSourceAccounts($journal);
|
||||
$destinations = $this->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 === $this->repository->getTransactionType($journal)) {
|
||||
$messages[] = trans('firefly.cannot_edit_opening_balance');
|
||||
continue;
|
||||
}
|
||||
|
||||
// cannot edit reconciled transactions / journals:
|
||||
if ($this->repository->isJournalReconciled($journal)) {
|
||||
$messages[] = trans('firefly.cannot_edit_reconciled', ['description' => $journal->description, 'id' => $journal->id]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$filtered->push($journal);
|
||||
}
|
||||
|
||||
if (count($messages) > 0) {
|
||||
$request->session()->flash('info', $messages);
|
||||
}
|
||||
|
||||
// put previous url in session
|
||||
$this->rememberPreviousUri('transactions.bulk-edit.uri');
|
||||
|
||||
// get list of budgets:
|
||||
/** @var BudgetRepositoryInterface $repository */
|
||||
$repository = app(BudgetRepositoryInterface::class);
|
||||
$budgetList = ExpandedForm::makeSelectListWithEmpty($repository->getActiveBudgets());
|
||||
// collect some useful meta data for the mass edit:
|
||||
$filtered->each(
|
||||
$journals->each(
|
||||
function (TransactionJournal $journal) {
|
||||
$journal->transaction_count = $journal->transactions()->count();
|
||||
}
|
||||
);
|
||||
|
||||
if (0 === $filtered->count()) {
|
||||
$request->session()->flash('error', trans('firefly.no_edit_multiple_left'));
|
||||
}
|
||||
|
||||
$journals = $filtered;
|
||||
|
||||
return view('transactions.bulk.edit', compact('journals', 'subTitle', 'budgetList'));
|
||||
}
|
||||
|
||||
@@ -142,36 +96,32 @@ class BulkController extends Controller
|
||||
*/
|
||||
public function update(BulkEditJournalRequest $request, JournalRepositoryInterface $repository)
|
||||
{
|
||||
/** @var JournalUpdateService $service */
|
||||
$service = app(JournalUpdateService::class);
|
||||
$journalIds = $request->get('journals');
|
||||
$ignoreCategory = (int)$request->get('ignore_category') === 1;
|
||||
$ignoreBudget = (int)$request->get('ignore_budget') === 1;
|
||||
$ignoreTags = (int)$request->get('ignore_tags') === 1;
|
||||
$count = 0;
|
||||
|
||||
if (is_array($journalIds)) {
|
||||
if (\is_array($journalIds)) {
|
||||
foreach ($journalIds as $journalId) {
|
||||
$journal = $repository->find((int)$journalId);
|
||||
if (null !== $journal) {
|
||||
$count++;
|
||||
Log::debug(sprintf('Found journal #%d', $journal->id));
|
||||
// update category if not told to ignore
|
||||
if ($ignoreCategory === false) {
|
||||
Log::debug(sprintf('Set category to %s', $request->string('category')));
|
||||
$count++;
|
||||
Log::debug(sprintf('Found journal #%d', $journal->id));
|
||||
|
||||
$repository->updateCategory($journal, $request->string('category'));
|
||||
}
|
||||
// update budget if not told to ignore (and is withdrawal)
|
||||
if ($ignoreBudget === false) {
|
||||
Log::debug(sprintf('Set budget to %d', $request->integer('budget_id')));
|
||||
$repository->updateBudget($journal, $request->integer('budget_id'));
|
||||
}
|
||||
if ($ignoreTags === false) {
|
||||
Log::debug(sprintf('Set tags to %s', $request->string('budget_id')));
|
||||
$repository->updateTags($journal, ['tags' => explode(',', $request->string('tags'))]);
|
||||
}
|
||||
// update tags if not told to ignore (and is withdrawal)
|
||||
// update category if not told to ignore
|
||||
if ($ignoreCategory === false) {
|
||||
Log::debug(sprintf('Set category to %s', $request->string('category')));
|
||||
|
||||
$repository->updateCategory($journal, $request->string('category'));
|
||||
}
|
||||
// update budget if not told to ignore (and is withdrawal)
|
||||
if ($ignoreBudget === false) {
|
||||
Log::debug(sprintf('Set budget to %d', $request->integer('budget_id')));
|
||||
$repository->updateBudget($journal, $request->integer('budget_id'));
|
||||
}
|
||||
if ($ignoreTags === false) {
|
||||
Log::debug(sprintf('Set tags to %s', $request->string('budget_id')));
|
||||
$repository->updateTags($journal, ['tags' => explode(',', $request->string('tags'))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user