getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); $budgets[0] = trans('firefly.no_budget'); $piggyBanks = Auth::user()->piggyBanks()->orderBy('order', 'ASC')->get(); /** @var PiggyBank $piggy */ foreach ($piggyBanks as $piggy) { $piggy->name = $piggy->name . ' (' . Amount::format($piggy->currentRelevantRep()->currentamount, false) . ')'; } $piggies = ExpandedForm::makeSelectList($piggyBanks); $piggies[0] = trans('form.noPiggybank'); $preFilled = Session::has('preFilled') ? session('preFilled') : []; $respondTo = ['account_id', 'account_from_id']; $subTitle = trans('form.add_new_' . $what); foreach ($respondTo as $r) { $preFilled[$r] = Input::get($r); } Session::put('preFilled', $preFilled); // put previous url in session if not redirect from store (not "create another"). if (session('transactions.create.fromStore') !== true) { Session::put('transactions.create.url', URL::previous()); } Session::forget('transactions.create.fromStore'); Session::flash('gaEventCategory', 'transactions'); Session::flash('gaEventAction', 'create-' . $what); asort($piggies); return view('transactions.create', compact('accounts', 'uploadSize', 'budgets', 'what', 'piggies', 'subTitle')); } /** * Shows the form that allows a user to delete a transaction journal. * * @param TransactionJournal $journal * * @return \Illuminate\View\View */ public function delete(TransactionJournal $journal) { $what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); $subTitle = trans('firefly.delete_' . $what, ['description' => $journal->description]); // put previous url in session Session::put('transactions.delete.url', URL::previous()); Session::flash('gaEventCategory', 'transactions'); Session::flash('gaEventAction', 'delete-' . $what); return view('transactions.delete', compact('journal', 'subTitle', 'what')); } /** * @param JournalRepositoryInterface $repository * @param TransactionJournal $transactionJournal * * @return \Illuminate\Http\RedirectResponse */ public function destroy(JournalRepositoryInterface $repository, TransactionJournal $transactionJournal) { $type = strtolower($transactionJournal->transaction_type_type ?? TransactionJournal::transactionTypeStr($transactionJournal)); Session::flash('success', strval(trans('firefly.deleted_' . $type, ['description' => e($transactionJournal->description)]))); $repository->delete($transactionJournal); Preferences::mark(); // redirect to previous URL: return redirect(session('transactions.delete.url')); } /** * @param TransactionJournal $journal * * @return mixed */ public function edit(TransactionJournal $journal) { /** @var ARI $accountRepository */ $accountRepository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); /** @var BudgetRepositoryInterface $budgetRepository */ $budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); /** @var PiggyBankRepositoryInterface $piggyRepository */ $piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); $accountList = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); $budgetList = ExpandedForm::makeSelectList($budgetRepository->getActiveBudgets()); $piggyBankList = ExpandedForm::makeSelectList($piggyRepository->getPiggyBanks()); $budgetList[0] = trans('firefly.no_budget'); $piggyBankList[0] = trans('form.noPiggybank'); $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize')); $maxPostSize = Steam::phpBytes(ini_get('post_max_size')); $uploadSize = min($maxFileSize, $maxPostSize); $what = strtolower(TransactionJournal::transactionTypeStr($journal)); $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); $preFilled = [ 'date' => TransactionJournal::dateAsString($journal), 'interest_date' => TransactionJournal::dateAsString($journal, 'interest_date'), 'book_date' => TransactionJournal::dateAsString($journal, 'book_date'), 'process_date' => TransactionJournal::dateAsString($journal, 'process_date'), 'category' => TransactionJournal::categoryAsString($journal), 'budget_id' => TransactionJournal::budgetId($journal), 'piggy_bank_id' => TransactionJournal::piggyBankId($journal), 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), 'account_from_id' => TransactionJournal::sourceAccount($journal)->id, 'account_to_id' => TransactionJournal::destinationAccount($journal)->id, 'amount' => TransactionJournal::amountPositive($journal), ]; if ($journal->isWithdrawal()) { $preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id; if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') { $preFilled['expense_account'] = TransactionJournal::destinationAccount($journal)->name; } } else { $preFilled['account_id'] = TransactionJournal::destinationAccount($journal)->id; if (TransactionJournal::sourceAccountTypeStr($journal) != 'Cash account') { $preFilled['revenue_account'] = TransactionJournal::sourceAccount($journal)->name; } } Session::flash('preFilled', $preFilled); Session::flash('gaEventCategory', 'transactions'); Session::flash('gaEventAction', 'edit-' . $what); // put previous url in session if not redirect from store (not "return_to_edit"). if (session('transactions.edit.fromUpdate') !== true) { Session::put('transactions.edit.url', URL::previous()); } Session::forget('transactions.edit.fromUpdate'); return view('transactions.edit', compact('journal', 'uploadSize', 'accountList', 'what', 'budgetList', 'piggyBankList', 'subTitle'))->with( 'data', $preFilled ); } /** * @param JournalRepositoryInterface $repository * @param $what * * @return \Illuminate\View\View */ public function index(JournalRepositoryInterface $repository, string $what) { $pageSize = Preferences::get('transactionPageSize', 50)->data; $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what); $types = config('firefly.transactionTypesByWhat.' . $what); $subTitle = trans('firefly.title_' . $what); $page = intval(Input::get('page')); $journals = $repository->getJournalsOfTypes($types, $page, $pageSize); $journals->setPath('transactions/' . $what); return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals')); } /** * @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 */ public function massEdit(Collection $journals) { $subTitle = trans('firefly.mass_edit_journals'); /** @var ARI $accountRepository */ $accountRepository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); $accountList = ExpandedForm::makeSelectList($accountRepository->getAccounts(['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')); } /** * */ public function massUpdate(MassEditJournalRequest $request, JournalRepositoryInterface $repository) { $journalIds = Input::get('journals'); $count = 0; if (is_array($journalIds)) { foreach ($journalIds as $journalId) { $journal = $repository->find(intval($journalId)); if ($journal) { // do update. // 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 JournalRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ public function reorder(JournalRepositoryInterface $repository) { $ids = Input::get('items'); $date = new Carbon(Input::get('date')); if (count($ids) > 0) { $order = 0; foreach ($ids as $id) { $journal = $repository->getWithDate($id, $date); if ($journal) { $journal->order = $order; $order++; $journal->save(); } } } Preferences::mark(); return Response::json([true]); } /** * @param JournalRepositoryInterface $repository * @param TransactionJournal $journal * * @return \Illuminate\View\View */ public function show(JournalRepositoryInterface $repository, TransactionJournal $journal) { /** @var Collection $set */ $events = $journal->piggyBankEvents()->get(); $events->each( function (PiggyBankEvent $event) { $event->piggyBank = $event->piggyBank()->withTrashed()->first(); } ); $journal->transactions->each( function (Transaction $t) use ($journal, $repository) { $t->before = $repository->getAmountBefore($journal, $t); $t->after = bcadd($t->before, $t->amount); } ); $what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); $subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"'; return view('transactions.show', compact('journal', 'events', 'subTitle', 'what')); } /** * @param JournalFormRequest $request * @param JournalRepositoryInterface $repository * * @param AttachmentHelperInterface $att * * @return \Illuminate\Http\RedirectResponse */ public function store(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att) { $journalData = $request->getJournalData(); // if not withdrawal, unset budgetid. if ($journalData['what'] != strtolower(TransactionType::WITHDRAWAL)) { $journalData['budget_id'] = 0; } $journal = $repository->store($journalData); $att->saveAttachmentsForModel($journal); // flash errors if (count($att->getErrors()->get('attachments')) > 0) { Session::flash('error', $att->getErrors()->get('attachments')); } // flash messages if (count($att->getMessages()->get('attachments')) > 0) { Session::flash('info', $att->getMessages()->get('attachments')); } Log::debug('Triggered TransactionJournalStored with transaction journal #' . $journal->id . ' and piggy #' . intval($request->get('piggy_bank_id'))); event(new TransactionJournalStored($journal, intval($request->get('piggy_bank_id')))); Session::flash('success', strval(trans('firefly.stored_journal', ['description' => e($journal->description)]))); Preferences::mark(); if (intval(Input::get('create_another')) === 1) { // set value so create routine will not overwrite URL: Session::put('transactions.create.fromStore', true); return redirect(route('transactions.create', [$request->input('what')]))->withInput(); } // redirect to previous URL. return redirect(session('transactions.create.url')); } /** * @param JournalFormRequest $request * @param JournalRepositoryInterface $repository * @param AttachmentHelperInterface $att * @param TransactionJournal $journal * * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att, TransactionJournal $journal) { $journalData = $request->getJournalData(); $repository->update($journal, $journalData); // save attachments: $att->saveAttachmentsForModel($journal); // flash errors if (count($att->getErrors()->get('attachments')) > 0) { Session::flash('error', $att->getErrors()->get('attachments')); } // flash messages if (count($att->getMessages()->get('attachments')) > 0) { Session::flash('info', $att->getMessages()->get('attachments')); } event(new TransactionJournalUpdated($journal)); // update, get events by date and sort DESC $type = strtolower($journal->transaction_type_type ?? TransactionJournal::transactionTypeStr($journal)); Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($journalData['description'])]))); Preferences::mark(); if (intval(Input::get('return_to_edit')) === 1) { // set value so edit routine will not overwrite URL: Session::put('transactions.edit.fromUpdate', true); return redirect(route('transactions.edit', [$journal->id]))->withInput(['return_to_edit' => 1]); } // redirect to previous URL. return redirect(session('transactions.edit.url')); } }