From f301da83c6f977903eb1ffd03fd8661f403014b9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 2 Apr 2015 22:39:31 +0200 Subject: [PATCH] Fixed the tests. --- app/Http/Controllers/BillController.php | 49 +++++++++++++++++++++ app/Http/routes.php | 1 + database/seeds/TestDataSeeder.php | 31 ++++++++++--- resources/views/list/bills.blade.php | 6 +++ tests/controllers/AccountControllerTest.php | 6 ++- 5 files changed, 87 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index a2dcc48624..1535577102 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -3,6 +3,8 @@ use Auth; use FireflyIII\Http\Requests; use FireflyIII\Http\Requests\BillFormRequest; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; use FireflyIII\Models\Bill; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; @@ -21,12 +23,59 @@ use View; class BillController extends Controller { + /** + * + */ public function __construct() { View::share('title', 'Bills'); View::share('mainTitleIcon', 'fa-calendar-o'); } + /** + * @param Bill $bill + * + * @return \Illuminate\Http\RedirectResponse + */ + public function add(Bill $bill) + { + $matches = explode(',', $bill->match); + $description = []; + $expense = null; + + // get users expense accounts: + $type = AccountType::where('type', 'Expense account')->first(); + $accounts = Auth::user()->accounts()->where('account_type_id', $type->id)->get(); + + foreach ($matches as $match) { + $match = strtolower($match); + // find expense account for each word if not found already: + if (is_null($expense)) { + /** @var Account $account */ + foreach ($accounts as $account) { + $name = strtolower($account->name); + if (!(strpos($name, $match) === false)) { + $expense = $account; + break; + } + } + + + } + if (is_null($expense)) { + $description[] = $match; + } + } + $parameters = [ + 'description' => ucfirst(join(' ', $description)), + 'expense_account' => is_null($expense) ? '' : $expense->name, + 'amount' => round(($bill->amount_min + $bill->amount_max), 2), + ]; + Session::put('preFilled', $parameters); + + return Redirect::to(route('transactions.create', 'withdrawal')); + } + /** * @return $this */ diff --git a/app/Http/routes.php b/app/Http/routes.php index bc4c4fba3a..3b6a5bc63f 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -167,6 +167,7 @@ Route::group( Route::get('/bills/rescan/{bill}', ['uses' => 'BillController@rescan', 'as' => 'bills.rescan']); # rescan for matching. Route::get('/bills/create', ['uses' => 'BillController@create', 'as' => 'bills.create']); Route::get('/bills/edit/{bill}', ['uses' => 'BillController@edit', 'as' => 'bills.edit']); + Route::get('/bills/add/{bill}', ['uses' => 'BillController@add', 'as' => 'bills.add']); Route::get('/bills/delete/{bill}', ['uses' => 'BillController@delete', 'as' => 'bills.delete']); Route::get('/bills/show/{bill}', ['uses' => 'BillController@show', 'as' => 'bills.show']); diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php index 0b6e69794d..beb5a03eef 100644 --- a/database/seeds/TestDataSeeder.php +++ b/database/seeds/TestDataSeeder.php @@ -514,6 +514,27 @@ class TestDataSeeder extends Seeder return null; } + /** + * @param $name + * + * @return PiggyBank|null + */ + protected function findPiggyBank($name) + { + // account + $user = User::whereEmail('thegrumpydictator@gmail.com')->first(); + /** @var Budget $budget */ + foreach (PiggyBank::get() as $piggyBank) { + $account = $piggyBank->account()->first(); + if ($piggyBank->name == $name && $user->id == $account->user_id) { + return $piggyBank; + break; + } + } + + return null; + } + /** * @param $name * @@ -638,13 +659,13 @@ class TestDataSeeder extends Seeder // piggy bank event // add money to this piggy bank // create a piggy bank event to match: - $checking = Account::whereName('Checking account')->orderBy('id', 'DESC')->first(); - $savings = Account::whereName('Savings account')->orderBy('id', 'DESC')->first(); + $checking = $this->findAccount('Checking account'); + $savings = $this->findAccount('Savings account'); $transfer = TransactionType::whereType('Transfer')->first(); $euro = TransactionCurrency::whereCode('EUR')->first(); - $groceries = Budget::whereName('Groceries')->orderBy('id', 'DESC')->first(); - $house = Category::whereName('House')->orderBy('id', 'DESC')->first(); - $piggyBank = PiggyBank::whereName('New camera')->orderBy('id', 'DESC')->first(); + $groceries = $this->findBudget('Groceries'); + $house = $this->findCategory('House'); + $piggyBank = $this->findPiggyBank('New camera'); $intoPiggy = $this->createJournal( ['from' => $checking, 'to' => $savings, 'amount' => 100, 'transactionType' => $transfer, 'description' => 'Money for piggy', 'date' => $this->yaeom, 'transactionCurrency' => $euro, 'category' => $house, 'budget' => $groceries] diff --git a/resources/views/list/bills.blade.php b/resources/views/list/bills.blade.php index 9b491ed391..c0e4c4f724 100644 --- a/resources/views/list/bills.blade.php +++ b/resources/views/list/bills.blade.php @@ -9,6 +9,7 @@ Is active Will be automatched Repeats every +   @foreach($bills as $entry) @@ -66,6 +67,11 @@ skips over {{$entry->skip}} @endif + + @if($entry->active) + + @endif + @endforeach diff --git a/tests/controllers/AccountControllerTest.php b/tests/controllers/AccountControllerTest.php index f16fb238ec..ea9687accd 100644 --- a/tests/controllers/AccountControllerTest.php +++ b/tests/controllers/AccountControllerTest.php @@ -1,5 +1,6 @@ data = '1M'; + // CURRENCY: + $currency = new TransactionCurrency; + Preferences::shouldReceive('get', 'viewRange')->andReturn($pref); - Amount::shouldReceive('getDefaultCurrency')->andReturn(null); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); $response = $this->call('GET', '/accounts/create/asset'); $this->assertResponseOk();