diff --git a/app/lib/FireflyIII/Database/Account.php b/app/lib/FireflyIII/Database/Account.php index 9c5c7cd894..6b31029726 100644 --- a/app/lib/FireflyIII/Database/Account.php +++ b/app/lib/FireflyIII/Database/Account.php @@ -464,6 +464,16 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface $accountType = $accountTypeRepos->findByWhat('expense'); + // if name is "", find cash account: + if (strlen($name) == 0) { + $cashAccountType = $accountTypeRepos->findByWhat('cash'); + + // find or create cash account: + return \Account::firstOrCreate( + ['name' => 'Cash account', 'account_type_id' => $cashAccountType->id, 'active' => 1, 'user_id' => $this->getUser()->id,] + ); + } + $data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1]; return \Account::firstOrCreate($data); @@ -488,9 +498,11 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface $start = \Session::get('start'); $end = \Session::get('end'); $offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0; - $set = $this->getUser()->transactionJournals()->withRelevantData()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transactions.account_id', $account->id)->take($limit)->offset($offset)->before($end)->after($start)->orderBy('date', 'DESC') - ->get(['transaction_journals.*']); + $set = $this->getUser()->transactionJournals()->withRelevantData()->leftJoin( + 'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id' + )->where('transactions.account_id', $account->id)->take($limit)->offset($offset)->before($end)->after($start)->orderBy('date', 'DESC')->get( + ['transaction_journals.*'] + ); $count = $this->getUser()->transactionJournals()->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') ->before($end)->after($start)->orderBy('date', 'DESC')->where('transactions.account_id', $account->id)->count(); $items = []; diff --git a/app/lib/FireflyIII/Database/AccountType.php b/app/lib/FireflyIII/Database/AccountType.php index 0c5341d1c5..302b8b2335 100644 --- a/app/lib/FireflyIII/Database/AccountType.php +++ b/app/lib/FireflyIII/Database/AccountType.php @@ -113,6 +113,9 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls case 'revenue': return \AccountType::whereType('Revenue account')->first(); break; + case 'cash': + return \AccountType::whereType('Cash account')->first(); + break; case 'initial': return \AccountType::whereType('Initial balance account')->first(); break; diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php index 98c1ab8b14..ef901d29ec 100644 --- a/app/lib/FireflyIII/Database/Budget.php +++ b/app/lib/FireflyIII/Database/Budget.php @@ -142,8 +142,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function find($id) { - // TODO: Implement find() method. - throw new NotImplementedException; + return $this->getUser()->budgets()->find($id); } /** diff --git a/app/lib/FireflyIII/Database/Category.php b/app/lib/FireflyIII/Database/Category.php index faa11b7f65..253dd3ab99 100644 --- a/app/lib/FireflyIII/Database/Category.php +++ b/app/lib/FireflyIII/Database/Category.php @@ -170,6 +170,11 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface throw new NotImplementedException; } + public function firstOrCreate($name) + { + return \Category::firstOrCreate(['user_id' => $this->getUser()->id, 'name' => $name]); + } + public function getTransactionJournals(\Category $category, $limit = 50) { $offset = intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) * $limit : 0; diff --git a/app/lib/FireflyIII/Database/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal.php index 55d4c0ac9e..2367a18ad0 100644 --- a/app/lib/FireflyIII/Database/TransactionJournal.php +++ b/app/lib/FireflyIII/Database/TransactionJournal.php @@ -48,7 +48,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData */ public function store(array $data) { - /** @var \FireflyIII\Database\TransactionType $typeRepository */ $typeRepository = \App::make('FireflyIII\Database\TransactionType'); @@ -127,12 +126,33 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData } /* - * TODO store budget and category. + * Store the budget. */ + if(isset($data['budget_id']) && intval($data['budget_id']) > 0) { + /** @var \FireflyIII\Database\Budget $budgetRepository */ + $budgetRepository = \App::make('FireflyIII\Database\Budget'); + $budget = $budgetRepository->find(intval($data['budget_id'])); + if($budget) { + $journal->budgets()->save($budget); + } + } + if(strlen($data['category']) > 0) { + /** @var \FireflyIII\Database\Category $categoryRepository */ + $categoryRepository = \App::make('FireflyIII\Database\Category'); + $category = $categoryRepository->firstOrCreate($data['category']); + if($category) { + $journal->categories()->save($category); + } + } $journal->completed = 1; $journal->save(); + /* + * Trigger a search for a relevant recurring transaction. + */ + + return $journal; } @@ -199,6 +219,27 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".'); break; } + + /* + * Store the budget. + */ + if(isset($data['budget_id']) && intval($data['budget_id']) > 0) { + /** @var \FireflyIII\Database\Budget $budgetRepository */ + $budgetRepository = \App::make('FireflyIII\Database\Budget'); + $budget = $budgetRepository->find(intval($data['budget_id'])); + if($budget) { + $model->budgets()->sync([$budget->id]); + } + } + if(strlen($data['category']) > 0) { + /** @var \FireflyIII\Database\Category $categoryRepository */ + $categoryRepository = \App::make('FireflyIII\Database\Category'); + $category = $categoryRepository->firstOrCreate($data['category']); + if($category) { + $model->categories()->sync([$category->id]); + } + } + /* * Now we can update the transactions related to this journal. */