From 003177fa498050df9a8acf22727cb6b1b449e7ce Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 20 May 2016 11:09:02 +0200 Subject: [PATCH] Move stuff around. --- .../Transaction/MassController.php | 190 ++++++++++++++++++ .../Controllers/TransactionController.php | 148 -------------- 2 files changed, 190 insertions(+), 148 deletions(-) create mode 100644 app/Http/Controllers/Transaction/MassController.php diff --git a/app/Http/Controllers/Transaction/MassController.php b/app/Http/Controllers/Transaction/MassController.php new file mode 100644 index 0000000000..aa80d2cead --- /dev/null +++ b/app/Http/Controllers/Transaction/MassController.php @@ -0,0 +1,190 @@ +get('confirm_mass_delete'); + $set = new Collection; + if (is_array($ids)) { + /** @var int $journalId */ + foreach ($ids as $journalId) { + /** @var TransactionJournal $journal */ + $journal = $repository->find($journalId); + if (!is_null($journal->id) && $journalId == $journal->id) { + $set->push($journal); + } + } + } + unset($journal); + $count = 0; + + /** @var TransactionJournal $journal */ + foreach ($set as $journal) { + $repository->delete($journal); + $count++; + } + + Preferences::mark(); + Session::flash('success', trans('firefly.mass_deleted_transactions_success', ['amount' => $count])); + + // redirect to previous URL: + return redirect(session('transactions.mass-delete.url')); + + } + + + /** + * @param Collection $journals + * + * @return View + */ + public function massEdit(Collection $journals) + { + $subTitle = trans('firefly.mass_edit_journals'); + $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $accountList = ExpandedForm::makeSelectList($crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])); + + // put previous url in session + Session::put('transactions.mass-edit.url', URL::previous()); + Session::flash('gaEventCategory', 'transactions'); + Session::flash('gaEventAction', 'mass-edit'); + + return view('transactions.mass-edit', compact('journals', 'subTitle', 'accountList')); + } + + /** + * @param MassEditJournalRequest $request + * @param JournalRepositoryInterface $repository + * + * @return mixed + */ + public function massUpdate(MassEditJournalRequest $request, JournalRepositoryInterface $repository) + { + $journalIds = $request->get('journals'); + $count = 0; + if (is_array($journalIds)) { + foreach ($journalIds as $journalId) { + $journal = $repository->find(intval($journalId)); + if ($journal) { + // get optional fields: + $what = strtolower(TransactionJournal::transactionTypeStr($journal)); + $sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0; + $destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0; + $expenseAccount = $request->get('expense_account')[$journal->id] ?? ''; + $revenueAccount = $request->get('revenue_account')[$journal->id] ?? ''; + $budgetId = $journal->budgets->first() ? $journal->budgets->first()->id : 0; + $category = $journal->categories->first() ? $journal->categories->first()->name : ''; + $tags = $journal->tags->pluck('tag')->toArray(); + + // for a deposit, the 'account_id' is the account the money is deposited on. + // needs a better way of handling. + // more uniform source/destination field names + $accountId = $sourceAccountId; + if ($what == 'deposit') { + $accountId = $destAccountId; + } + + // build data array + $data = [ + 'id' => $journal->id, + 'what' => $what, + 'description' => $request->get('description')[$journal->id], + 'account_id' => intval($accountId), + 'account_from_id' => intval($sourceAccountId), + 'account_to_id' => intval($destAccountId), + 'expense_account' => $expenseAccount, + 'revenue_account' => $revenueAccount, + 'amount' => round($request->get('amount')[$journal->id], 4), + 'user' => Auth::user()->id, + 'amount_currency_id_amount' => intval($request->get('amount_currency_id_amount_' . $journal->id)), + 'date' => new Carbon($request->get('date')[$journal->id]), + 'interest_date' => $journal->interest_date, + 'book_date' => $journal->book_date, + 'process_date' => $journal->process_date, + 'budget_id' => $budgetId, + 'category' => $category, + 'tags' => $tags, + + ]; + // call repository update function. + $repository->update($journal, $data); + + $count++; + } + } + } + Preferences::mark(); + Session::flash('success', trans('firefly.mass_edited_transactions_success', ['amount' => $count])); + + // redirect to previous URL: + return redirect(session('transactions.mass-edit.url')); + + } +} \ No newline at end of file diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index e041e23bed..7a6edd4ef2 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -12,21 +12,17 @@ declare(strict_types = 1); namespace FireflyIII\Http\Controllers; -use Auth; use Carbon\Carbon; use ExpandedForm; use FireflyIII\Events\TransactionJournalStored; use FireflyIII\Events\TransactionJournalUpdated; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Requests\JournalFormRequest; -use FireflyIII\Http\Requests\MassDeleteJournalRequest; -use FireflyIII\Http\Requests\MassEditJournalRequest; use FireflyIII\Models\AccountType; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Http\Request; -use Illuminate\Support\Collection; use Preferences; use Response; use Session; @@ -215,150 +211,6 @@ class TransactionController extends Controller } - /** - * @param Collection $journals - * - * @return View - */ - public function massDelete(Collection $journals) - { - $subTitle = trans('firefly.mass_delete_journals'); - - // put previous url in session - Session::put('transactions.mass-delete.url', URL::previous()); - Session::flash('gaEventCategory', 'transactions'); - Session::flash('gaEventAction', 'mass-delete'); - - return view('transactions.mass-delete', compact('journals', 'subTitle')); - - } - - /** - * @param MassDeleteJournalRequest $request - * @param JournalRepositoryInterface $repository - * - * @return mixed - */ - public function massDestroy(MassDeleteJournalRequest $request, JournalRepositoryInterface $repository) - { - $ids = $request->get('confirm_mass_delete'); - $set = new Collection; - if (is_array($ids)) { - /** @var int $journalId */ - foreach ($ids as $journalId) { - /** @var TransactionJournal $journal */ - $journal = $repository->find($journalId); - if (!is_null($journal->id) && $journalId == $journal->id) { - $set->push($journal); - } - } - } - unset($journal); - $count = 0; - - /** @var TransactionJournal $journal */ - foreach ($set as $journal) { - $repository->delete($journal); - $count++; - } - - Preferences::mark(); - Session::flash('success', trans('firefly.mass_deleted_transactions_success', ['amount' => $count])); - - // redirect to previous URL: - return redirect(session('transactions.mass-delete.url')); - - } - - - /** - * @param Collection $journals - * - * @return View - */ - public function massEdit(Collection $journals) - { - $subTitle = trans('firefly.mass_edit_journals'); - $accountRepository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); - $accountList = ExpandedForm::makeSelectList($accountRepository->getAccountsByType(['Default account', 'Asset account'])); - - // put previous url in session - Session::put('transactions.mass-edit.url', URL::previous()); - Session::flash('gaEventCategory', 'transactions'); - Session::flash('gaEventAction', 'mass-edit'); - - return view('transactions.mass-edit', compact('journals', 'subTitle', 'accountList')); - } - - /** - * @param MassEditJournalRequest $request - * @param JournalRepositoryInterface $repository - * - * @return mixed - */ - public function massUpdate(MassEditJournalRequest $request, JournalRepositoryInterface $repository) - { - $journalIds = $request->get('journals'); - $count = 0; - if (is_array($journalIds)) { - foreach ($journalIds as $journalId) { - $journal = $repository->find(intval($journalId)); - if ($journal) { - // get optional fields: - $what = strtolower(TransactionJournal::transactionTypeStr($journal)); - $sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0; - $destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0; - $expenseAccount = $request->get('expense_account')[$journal->id] ?? ''; - $revenueAccount = $request->get('revenue_account')[$journal->id] ?? ''; - $budgetId = $journal->budgets->first() ? $journal->budgets->first()->id : 0; - $category = $journal->categories->first() ? $journal->categories->first()->name : ''; - $tags = $journal->tags->pluck('tag')->toArray(); - - // for a deposit, the 'account_id' is the account the money is deposited on. - // needs a better way of handling. - // more uniform source/destination field names - $accountId = $sourceAccountId; - if ($what == 'deposit') { - $accountId = $destAccountId; - } - - // build data array - $data = [ - 'id' => $journal->id, - 'what' => $what, - 'description' => $request->get('description')[$journal->id], - 'account_id' => intval($accountId), - 'account_from_id' => intval($sourceAccountId), - 'account_to_id' => intval($destAccountId), - 'expense_account' => $expenseAccount, - 'revenue_account' => $revenueAccount, - 'amount' => round($request->get('amount')[$journal->id], 4), - 'user' => Auth::user()->id, - 'amount_currency_id_amount' => intval($request->get('amount_currency_id_amount_' . $journal->id)), - 'date' => new Carbon($request->get('date')[$journal->id]), - 'interest_date' => $journal->interest_date, - 'book_date' => $journal->book_date, - 'process_date' => $journal->process_date, - 'budget_id' => $budgetId, - 'category' => $category, - 'tags' => $tags, - - ]; - // call repository update function. - $repository->update($journal, $data); - - $count++; - } - } - } - Preferences::mark(); - Session::flash('success', trans('firefly.mass_edited_transactions_success', ['amount' => $count])); - - // redirect to previous URL: - return redirect(session('transactions.mass-edit.url')); - - } - /** * @param Request $request * @param JournalRepositoryInterface $repository