/** * @param TransactionJournal $journal * * @return int */ public static function budgetId(TransactionJournal $journal) : int { $budget = $journal->budgets()->first(); if (!is_null($budget)) { return $budget->id; } return 0; }
/** * @param TransactionJournal $journal * * @return bool */ public function act(TransactionJournal $journal) : bool { /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class, [$journal->user]); $search = $this->action->action_value; $budgets = $repository->getActiveBudgets(); $budget = $budgets->filter(function (Budget $current) use($search) { return $current->name == $search; })->first(); if (is_null($budget)) { Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search)); return true; } if ($journal->transactionType->type == TransactionType::TRANSFER) { Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a transfer.', $journal->id, $search)); return true; } Log::debug(sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal->id, $budget->id, $budget->name)); $journal->budgets()->sync([$budget->id]); return true; }
/** * @param TransactionJournal $journal * * @return bool */ public function act(TransactionJournal $journal) : bool { $journal->budgets()->detach(); Log::debug(sprintf('RuleAction ClearBudget removed all budgets from journal %d.', $journal->id)); 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 */ protected function saveBudget(TransactionJournal $journal) { // add budget: if (!is_null($this->importData['budget'])) { $journal->budgets()->save($this->importData['budget']); } }
/** * @param TransactionJournal $journal * @param ImportEntry $entry */ private function storeBudget(TransactionJournal $journal, ImportEntry $entry) { if (!is_null($entry->fields['budget']) && !is_null($entry->fields['budget']->id)) { $journal->budgets()->save($entry->fields['budget']); Log::debug('Attached budget', ['id' => $entry->fields['budget']->id, 'name' => $entry->fields['budget']->name]); $journal->save(); } }
/** * @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; }
/** * 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); }