From d7b0106e7d632b3f2730cae54a3e44c877bdd37a Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 1 Dec 2014 05:57:03 +0100 Subject: [PATCH] Expand code for reports. --- app/controllers/ReportController.php | 45 +---- app/database/seeds/DatabaseSeeder.php | 1 + app/database/seeds/DefaultUserSeeder.php | 11 +- app/database/seeds/TestContentSeeder.php | 176 ++++++++++++++++++ .../FireflyIII/Shared/Mail/Registration.php | 13 +- app/views/reports/unbalanced.blade.php | 50 ++--- 6 files changed, 219 insertions(+), 77 deletions(-) create mode 100644 app/database/seeds/TestContentSeeder.php diff --git a/app/controllers/ReportController.php b/app/controllers/ReportController.php index 368296481b..9bceec4d4d 100644 --- a/app/controllers/ReportController.php +++ b/app/controllers/ReportController.php @@ -200,50 +200,17 @@ class ReportController extends BaseController // ); /* - * Filter withdrawals without a counter-transfer (into this account) + * TODO Filter withdrawals without a counter-transfer (into this account) */ - $withdrawals = $withdrawals->filter( - function (TransactionJournal $journal) { - foreach ($journal->transactions as $transaction) { - if (floatval($transaction->amount) < 0) { - $account = $transaction->account; - // find counter transfer: - $counters = $account->transactions()->where('amount', floatval($transaction->amount) * -1) - ->where('account_id', '=', $transaction->account_id) - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where('transaction_journals.description', 'LIKE', '%' . e($journal->description) . '%') - ->get(['transactions.*']); - if ($counters->count() == 0) { - return $journal; - } - } - } - } - ); + /* - * Filter deposits without a counter-transfer (away from this account) + * TODO Filter deposits without a counter-transfer (away from this account) */ - $deposits = $deposits->filter( - function (TransactionJournal $journal) { - foreach ($journal->transactions as $transaction) { - if (floatval($transaction->amount) > 0) { - $account = $transaction->account; - // find counter transfer: - $counters = $account->transactions()->where('amount', floatval($transaction->amount) * -1) - ->where('account_id', '=', $transaction->account_id) - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where('transaction_journals.description', 'LIKE', '%' . e($journal->description) . '%') - ->get(['transactions.*']); - if ($counters->count() == 0) { - return $journal; - } - } - } - } - ); + $journals = $withdrawals->merge($deposits); - return View::make('reports.unbalanced', compact('start', 'end', 'title', 'subTitle', 'subTitleIcon', 'mainTitleIcon', 'withdrawals', 'deposits')); + + return View::make('reports.unbalanced', compact('start', 'end', 'title', 'subTitle', 'subTitleIcon', 'mainTitleIcon', 'journals')); } /** diff --git a/app/database/seeds/DatabaseSeeder.php b/app/database/seeds/DatabaseSeeder.php index dd35eb592a..29319ca38c 100644 --- a/app/database/seeds/DatabaseSeeder.php +++ b/app/database/seeds/DatabaseSeeder.php @@ -19,6 +19,7 @@ class DatabaseSeeder extends Seeder $this->call('TransactionCurrencySeeder'); $this->call('TransactionTypeSeeder'); $this->call('DefaultUserSeeder'); + $this->call('TestContentSeeder'); } } diff --git a/app/database/seeds/DefaultUserSeeder.php b/app/database/seeds/DefaultUserSeeder.php index 88f6759490..729914f8ad 100644 --- a/app/database/seeds/DefaultUserSeeder.php +++ b/app/database/seeds/DefaultUserSeeder.php @@ -8,10 +8,15 @@ class DefaultUserSeeder extends Seeder public function run() { DB::table('users')->delete(); + if (App::environment() == 'homestead') { - User::create( - ['email' => 'thegrumpydictator@gmail.com', 'password' => 'sander', 'reset' => null, 'remember_token' => null, 'migrated' => 0] - ); + User::create( + ['email' => 'thegrumpydictator@gmail.com', 'password' => 'james', 'reset' => null, 'remember_token' => null, 'migrated' => 0] + ); + User::create( + ['email' => 'acceptance@example.com', 'password' => 'acceptance', 'reset' => null, 'remember_token' => null, 'migrated' => 0] + ); + } } diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php new file mode 100644 index 0000000000..5fe1700ff3 --- /dev/null +++ b/app/database/seeds/TestContentSeeder.php @@ -0,0 +1,176 @@ +first(); + $expenseType = AccountType::whereType('Expense account')->first(); + $revenueType = AccountType::whereType('Revenue account')->first(); + $ibType = AccountType::whereType('Initial balance account')->first(); + + $euro = TransactionCurrency::whereCode('EUR')->first(); + + $obType = TransactionType::whereType('Opening balance')->first(); + $withdrawal = TransactionType::whereType('Withdrawal')->first(); + $transfer = TransactionType::whereType('Transfer')->first(); + $deposit = TransactionType::whereType('Deposit')->first(); + + $user = User::whereEmail('thegrumpydictator@gmail.com')->first(); + + if ($user) { + // create two asset accounts. + $checking = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Checking account', 'active' => 1]); + $savings = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Savings account', 'active' => 1]); + + // create two budgets: + $groceriesBudget = Budget::create(['user_id' => $user->id, 'name' => 'Groceries']); + $billsBudget = Budget::create(['user_id' => $user->id, 'name' => 'Bills']); + + // create two categories: + $dailyGroceries = Category::create(['user_id' => $user->id, 'name' => 'Daily groceries']); + $lunch = Category::create(['user_id' => $user->id, 'name' => 'Lunch']); + $house = Category::create(['user_id' => $user->id, 'name' => 'House']); + + // create some expense accounts. + $ah = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Albert Heijn', 'active' => 1]); + $plus = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'PLUS', 'active' => 1]); + $vitens = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Vitens', 'active' => 1]); + $greenchoice = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Greenchoice', 'active' => 1]); + $portaal = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Portaal', 'active' => 1]); + $store = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Buy More', 'active' => 1]); + + // create three revenue accounts. + $employer = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Employer', 'active' => 1]); + $taxes = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'IRS', 'active' => 1]); + $job = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Job', 'active' => 1]); + + // put money in the two accounts (initial balance) + $ibChecking = Account::create( + ['user_id' => $user->id, 'account_type_id' => $ibType->id, 'name' => 'Checking account initial balance', 'active' => 0] + ); + $ibSavings = Account::create( + ['user_id' => $user->id, 'account_type_id' => $ibType->id, 'name' => 'Savings account initial balance', 'active' => 0] + ); + + $this->createTransaction($ibChecking, $checking, 4000, $obType, 'Initial Balance for Checking account', '2014-01-01'); + $this->createTransaction($ibSavings, $savings, 10000, $obType, 'Initial Balance for Savings account', '2014-01-01'); + + + // create some expenses and incomes and what-not (for every month): + $start = new Carbon('2014-01-01'); + $end = Carbon::now()->startOfMonth()->subDay(); + while ($start <= $end) { + $this->createTransaction( + $checking, $portaal, 500, $withdrawal, 'Rent for ' . $start->format('F Y'), $start->format('Y-m-') . '01', $billsBudget, $house + ); + $this->createTransaction( + $checking, $vitens, 12, $withdrawal, 'Water for ' . $start->format('F Y'), $start->format('Y-m-') . '02', $billsBudget, $house + ); + $this->createTransaction( + $checking, $greenchoice, 110, $withdrawal, 'Power for ' . $start->format('F Y'), $start->format('Y-m-') . '02', $billsBudget, $house + ); + + // spend on groceries + $groceriesStart = clone $start; + for ($i = 0; $i < 13; $i++) { + $amt = rand(100, 300) / 10; + $lunchAmount = rand(30, 60) / 10; + $this->createTransaction( + $checking, $plus, $lunchAmount, $withdrawal, 'Lunch', $groceriesStart->format('Y-m-d'), $groceriesBudget, $lunch + ); + $groceriesStart->addDay(); + if (intval($groceriesStart->format('d')) % 2 == 0) { + $this->createTransaction( + $checking, $ah, $amt, $withdrawal, 'Groceries', $groceriesStart->format('Y-m-d'), $groceriesBudget, $dailyGroceries + ); + } + $groceriesStart->addDay(); + } + + // get income: + $this->createTransaction($employer, $checking, rand(1400, 1600), $deposit, 'Salary', $start->format('Y-m-') . '23'); + + // pay taxes: + $this->createTransaction($checking, $taxes, rand(50, 70), $withdrawal, 'Taxes in ' . $start->format('F Y'), $start->format('Y-m-') . '27'); + + // some other stuff. + + + $start->addMonth(); + + } + + // create some big expenses, move some money around. + $this->createTransaction($savings, $checking, 1259, $transfer, 'Money for new PC', $end->format('Y-m') . '-11'); + $this->createTransaction($checking, $store, 1259, $withdrawal, 'New PC', $end->format('Y-m') . '-12'); + + // create two budgets + + // create two categories + + // create + } + + } + } + + /** + * @param Account $from + * @param Account $to + * @param $amount + * @param TransactionType $type + * @param $description + * @param $date + * + * @return TransactionJournal + */ + public function createTransaction( + Account $from, Account $to, $amount, TransactionType $type, $description, $date, Budget $budget = null, Category $category = null + ) { + $user = User::whereEmail('thegrumpydictator@gmail.com')->first(); + $euro = TransactionCurrency::whereCode('EUR')->first(); + + /** @var TransactionJournal $journal */ + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => $type->id, + 'transaction_currency_id' => $euro->id, + 'description' => $description, + 'completed' => 1, + 'date' => $date + ] + ); + + Transaction::create( + [ + 'account_id' => $from->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1 + ] + + ); + Transaction::create( + [ + 'account_id' => $to->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount + ] + + ); + if (!is_null($budget)) { + $journal->budgets()->save($budget); + } + if (!is_null($category)) { + $journal->categories()->save($category); + } + + return $journal; + } +} \ No newline at end of file diff --git a/app/lib/FireflyIII/Shared/Mail/Registration.php b/app/lib/FireflyIII/Shared/Mail/Registration.php index 3fafddbff7..eecfd4a135 100644 --- a/app/lib/FireflyIII/Shared/Mail/Registration.php +++ b/app/lib/FireflyIII/Shared/Mail/Registration.php @@ -1,5 +1,6 @@ $password]; - \Mail::send( - ['emails.user.register-html', 'emails.user.register-text'], $data, function ($message) use ($email) { - $message->to($email, $email)->subject('Welcome to Firefly!'); - } - ); + try { + \Mail::send( + ['emails.user.register-html', 'emails.user.register-text'], $data, function ($message) use ($email) { + $message->to($email, $email)->subject('Welcome to Firefly!'); + } + ); + } catch(Swift_RfcComplianceException $e) {} } /** diff --git a/app/views/reports/unbalanced.blade.php b/app/views/reports/unbalanced.blade.php index 536bb6edfb..73705ad0f4 100644 --- a/app/views/reports/unbalanced.blade.php +++ b/app/views/reports/unbalanced.blade.php @@ -1,14 +1,14 @@ @extends('layouts.default') @section('content') {{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $start) }} -@if(count($withdrawals) == 0 && count($deposits) == 0) +@if(count($journals) == 0)

Everything accounted for.

@endif -@if(count($withdrawals) > 0) +@if(count($journals) > 0)

Withdrawals

@@ -16,47 +16,37 @@
- @foreach($withdrawals as $journal) + @foreach($journals as $journal)
-
+
+ @if($journal->transactiontype->type == 'Withdrawal') + + @endif + @if($journal->transactiontype->type == 'Deposit') + + @endif + @if($journal->transactiontype->type == 'Transfer') + + @endif + @if($journal->transactiontype->type == 'Opening balance') + + @endif {{{$journal->description}}}

Spent {{mf($journal->getAmount())}}

No counter transaction!

+

+ Add counter transaction +

@endforeach
@endif -@if(count($deposits) > 0) -
-
-

Deposits

-
-
-
- @foreach($deposits as $journal) -
-
- -
-

Received {{mf($journal->getAmount())}}

-

No counter transaction!

-
-
-
- @endforeach -
-@endif - - - - @stop @section('scripts') + @stop \ No newline at end of file