/** * @param TransactionJournal $journal * * @return bool */ public function act(TransactionJournal $journal) : bool { $name = $this->action->action_value; $category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => $journal->user->id]); $journal->categories()->sync([$category->id]); Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to budget #%d ("%s").', $journal->id, $category->id, $category->name)); return true; }
/** * Shows the view to edit a transaction. * * @param ARI $repository * @param TransactionJournal $journal * * @return $this */ public function edit(ARI $repository, TransactionJournal $journal) { // cannot edit opening balance if ($journal->isOpeningBalance()) { return view('error')->with('message', 'Cannot edit this transaction. Edit the account instead!'); } $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize')); $maxPostSize = Steam::phpBytes(ini_get('post_max_size')); $uploadSize = min($maxFileSize, $maxPostSize); $what = strtolower($journal->getTransactionType()); $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); $budgets[0] = trans('form.noBudget'); $piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get()); $piggies[0] = trans('form.noPiggybank'); $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); $preFilled = ['date' => $journal->date->format('Y-m-d'), 'category' => '', 'budget_id' => 0, 'piggy_bank_id' => 0]; // get tags: $preFilled['tags'] = join(',', $journal->tags->pluck('tag')->toArray()); $category = $journal->categories()->first(); if (!is_null($category)) { $preFilled['category'] = $category->name; } $budget = $journal->budgets()->first(); if (!is_null($budget)) { $preFilled['budget_id'] = $budget->id; } if ($journal->piggyBankEvents()->count() > 0) { $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; } $preFilled['amount'] = $journal->amount_positive; if ($journal->isWithdrawal()) { $preFilled['account_id'] = $journal->source_account->id; $preFilled['expense_account'] = $journal->destination_account->name_for_editform; } else { $preFilled['account_id'] = $journal->destination_account->id; $preFilled['revenue_account'] = $journal->source_account->name_for_editform; } $preFilled['account_from_id'] = $journal->source_account->id; $preFilled['account_to_id'] = $journal->destination_account->id; 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::get('transactions.edit.fromUpdate') !== true) { Session::put('transactions.edit.url', URL::previous()); } Session::forget('transactions.edit.fromUpdate'); return view('transactions.edit', compact('journal', 'uploadSize', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled); }
/** * @param TransactionJournal $journal * @param ImportEntry $entry */ private function storeCategory(TransactionJournal $journal, ImportEntry $entry) { if (!is_null($entry->fields['category']) && !is_null($entry->fields['category']->id)) { $journal->categories()->save($entry->fields['category']); Log::debug('Attached category', ['id' => $entry->fields['category']->id, 'name' => $entry->fields['category']->name]); $journal->save(); } }
/** * @param TransactionJournal $journal */ protected function saveCategory(TransactionJournal $journal) { // add category: if (!is_null($this->importData['category'])) { $journal->categories()->save($this->importData['category']); } }
/** * @param TransactionJournal $journal * @param array $data * * @return TransactionJournal */ public function update(TransactionJournal $journal, array $data) : TransactionJournal { // update actual journal. $journal->transaction_currency_id = $data['amount_currency_id_amount']; $journal->description = $data['description']; $journal->date = $data['date']; // unlink all categories, recreate them: $journal->categories()->detach(); if (strlen($data['category']) > 0) { $category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]); $journal->categories()->save($category); } // unlink all budgets and recreate them: $journal->budgets()->detach(); if (intval($data['budget_id']) > 0 && $journal->transactionType->type !== TransactionType::TRANSFER) { /** @var \FireflyIII\Models\Budget $budget */ $budget = Budget::where('user_id', $this->user->id)->where('id', $data['budget_id'])->first(); $journal->budgets()->save($budget); } // store accounts (depends on type) list($fromAccount, $toAccount) = $this->storeAccounts($journal->transactionType, $data); // update the from and to transaction. /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if ($transaction->amount < 0) { // this is the from transaction, negative amount: $transaction->amount = $data['amount'] * -1; $transaction->account_id = $fromAccount->id; $transaction->save(); } if ($transaction->amount > 0) { $transaction->amount = $data['amount']; $transaction->account_id = $toAccount->id; $transaction->save(); } } $journal->save(); // update tags: if (isset($data['tags']) && is_array($data['tags'])) { $this->updateTags($journal, $data['tags']); } // update meta fields: $result = $journal->save(); if ($result) { foreach ($data as $key => $value) { if (in_array($key, $this->validMetaFields)) { $journal->setMeta($key, $value); continue; } Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id)); } return $journal; } return $journal; }
/** * @param TransactionJournal $journal * @param array $data * * @return TransactionJournal */ public function update(TransactionJournal $journal, array $data) { // update actual journal. $journal->transaction_currency_id = $data['amount_currency_id']; $journal->description = $data['description']; $journal->date = $data['date']; // unlink all categories, recreate them: $journal->categories()->detach(); if (strlen($data['category']) > 0) { $category = Category::firstOrCreateEncrypted(['name' => $data['category'], 'user_id' => $data['user']]); $journal->categories()->save($category); } // unlink all budgets and recreate them: $journal->budgets()->detach(); if (intval($data['budget_id']) > 0) { /** @var \FireflyIII\Models\Budget $budget */ $budget = Budget::find($data['budget_id']); $journal->budgets()->save($budget); } // store accounts (depends on type) list($from, $to) = $this->storeAccounts($journal->transactionType, $data); // update the from and to transaction. /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if (floatval($transaction->amount) < 0) { // this is the from transaction, negative amount: $transaction->amount = $data['amount'] * -1; $transaction->account_id = $from->id; $transaction->save(); } if (floatval($transaction->amount) > 0) { $transaction->amount = $data['amount']; $transaction->account_id = $to->id; $transaction->save(); } } $journal->save(); // update tags: if (isset($data['tags']) && is_array($data['tags'])) { $this->updateTags($journal, $data['tags']); } return $journal; }
/** * @param TransactionJournal $journal * * @return bool */ public function act(TransactionJournal $journal) : bool { $journal->categories()->detach(); Log::debug(sprintf('RuleAction ClearCategory removed all categories from journal %d.', $journal->id)); return true; }
/** * @param TransactionJournal $journal * * @return string */ public static function categoryAsString(TransactionJournal $journal) : string { $category = $journal->categories()->first(); if (!is_null($category)) { return $category->name; } return ''; }
/** * Shows the view to edit a transaction. * * @param AccountRepositoryInterface $repository * @param TransactionJournal $journal * * @return $this */ public function edit(AccountRepositoryInterface $repository, TransactionJournal $journal) { $what = strtolower($journal->transactionType->type); $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); $budgets[0] = trans('form.noBudget'); $piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get()); $piggies[0] = trans('form.noPiggybank'); $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); $preFilled = ['date' => $journal->date->format('Y-m-d'), 'category' => '', 'budget_id' => 0, 'piggy_bank_id' => 0]; // get tags: $tags = []; foreach ($journal->tags as $tag) { $tags[] = $tag->tag; } $preFilled['tags'] = join(',', $tags); $category = $journal->categories()->first(); if (!is_null($category)) { $preFilled['category'] = $category->name; } $budget = $journal->budgets()->first(); if (!is_null($budget)) { $preFilled['budget_id'] = $budget->id; } if ($journal->piggyBankEvents()->count() > 0) { $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; } $preFilled['amount'] = $journal->actual_amount; if ($journal->transactionType->type == 'Withdrawal') { $preFilled['account_id'] = $journal->source_account->id; $preFilled['expense_account'] = $journal->destination_account->name_for_editform; } else { $preFilled['account_id'] = $journal->destination_account->id; $preFilled['revenue_account'] = $journal->source_account->name_for_editform; } $preFilled['account_from_id'] = $journal->source_account->id; $preFilled['account_to_id'] = $journal->destination_account->id; 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::get('transactions.edit.fromUpdate') !== true) { Session::put('transactions.edit.url', URL::previous()); } Session::forget('transactions.edit.fromUpdate'); return View::make('transactions.edit', compact('journal', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled); }