Skip to content

Firefly III: a personal finances manager

Code cleanup for all controllers.

James Colefa7a59572a34
diff --git a/app/controllers/BillController.php b/app/controllers/BillController.php
index 521c21736b..0fdd0f7309 100644
--- a/app/controllers/BillController.php
+++ b/app/controllers/BillController.php
@@ -5,8 +5,7 @@ use FireflyIII\Exception\FireflyException;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("NPathComplexity")
+ *
* Class BillController
*
*/
@@ -120,9 +119,9 @@ class BillController extends BaseController
*/
public function show(Bill $bill)
{
- $journals = $bill->transactionjournals()->withRelevantData()->orderBy('date', 'DESC')->get();
+ $journals = $bill->transactionjournals()->withRelevantData()->orderBy('date', 'DESC')->get();
$bill->nextExpectedMatch = $this->_repository->nextExpectedMatch($bill);
- $hideBill = true;
+ $hideBill = true;
return View::make('bills.show', compact('journals', 'hideBill', 'bill'))->with(
@@ -136,7 +135,7 @@ class BillController extends BaseController
*/
public function store()
{
- $data = Input::all();
+ $data = Input::except(['_token', 'post_submit_action']);
$data['user_id'] = Auth::user()->id;
@@ -149,17 +148,19 @@ class BillController extends BaseController
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not store bill: ' . $messages['errors']->first());
+
+ return Redirect::route('bills.create')->withInput();
}
// return to create screen:
- if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
+ if (Input::get('post_submit_action') == 'validate_only') {
return Redirect::route('bills.create')->withInput();
}
// store
$this->_repository->store($data);
Session::flash('success', 'Bill "' . e($data['name']) . '" stored.');
- if ($data['post_submit_action'] == 'store') {
+ if (Input::get('post_submit_action') == 'store') {
return Redirect::route('bills.index');
}
@@ -176,8 +177,8 @@ class BillController extends BaseController
public function update(Bill $bill)
{
$data = Input::except('_token');
- $data['active'] = isset($data['active']) ? 1 : 0;
- $data['automatch'] = isset($data['automatch']) ? 1 : 0;
+ $data['active'] = intval(Input::get('active'));
+ $data['automatch'] = intval(Input::get('automatch'));
$data['user_id'] = Auth::user()->id;
// always validate:
@@ -189,10 +190,12 @@ class BillController extends BaseController
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
Session::flash('error', 'Could not update bill: ' . $messages['errors']->first());
+
+ return Redirect::route('bills.edit', $bill->id)->withInput();
}
// return to update screen:
- if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
+ if ($data['post_submit_action'] == 'validate_only') {
return Redirect::route('bills.edit', $bill->id)->withInput();
}
diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php
index 500ed85573..9365d67d2c 100644
--- a/app/controllers/BudgetController.php
+++ b/app/controllers/BudgetController.php
@@ -8,9 +8,6 @@ use FireflyIII\Shared\Preferences\PreferencesInterface as Pref;
* Class BudgetController
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("TooManyMethods") // I'm also fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
*
*/
class BudgetController extends BaseController
diff --git a/app/controllers/CategoryController.php b/app/controllers/CategoryController.php
index 8459b4c40f..915562cfc2 100644
--- a/app/controllers/CategoryController.php
+++ b/app/controllers/CategoryController.php
@@ -6,7 +6,6 @@ use FireflyIII\Exception\FireflyException;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
* Class CategoryController
*/
diff --git a/app/controllers/CurrencyController.php b/app/controllers/CurrencyController.php
index d8c2d5b5d6..5e79791af3 100644
--- a/app/controllers/CurrencyController.php
+++ b/app/controllers/CurrencyController.php
@@ -4,7 +4,6 @@ use FireflyIII\Database\TransactionCurrency\TransactionCurrency as Repository;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
* Class CurrencyController
*/
diff --git a/app/controllers/GoogleChartController.php b/app/controllers/GoogleChartController.php
index 98696a972f..f2cfb3a0cf 100644
--- a/app/controllers/GoogleChartController.php
+++ b/app/controllers/GoogleChartController.php
@@ -6,10 +6,6 @@ use Grumpydictator\Gchart\GChart as GChart;
/**
* Class GoogleChartController
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("TooManyMethods") // I'm also fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("MethodLength") // There is one with 45 lines and im gonna move it.
- * @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
*/
class GoogleChartController extends BaseController
{
diff --git a/app/controllers/HelpController.php b/app/controllers/HelpController.php
index 813b8faf5c..356f86b397 100644
--- a/app/controllers/HelpController.php
+++ b/app/controllers/HelpController.php
@@ -1,7 +1,6 @@
<?php
/**
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
* Class HelpController
*/
@@ -14,41 +13,70 @@ class HelpController extends BaseController
*/
public function show($route)
{
- $helpText = '<p>There is no help for this route!</p>';
- $helpTitle = 'Help';
+ $content = [
+ 'text' => '<p>There is no help for this route!</p>',
+ 'title' => 'Help',
+ ];
+
if (!Route::has($route)) {
\Log::error('No such route: ' . $route);
- return Response::json(['title' => $helpTitle, 'text' => $helpText]);
+ return Response::json($content);
}
- if (Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text')) {
- $helpText = Cache::get('help.' . $route . '.text');
- $helpTitle = Cache::get('help.' . $route . '.title');
- return Response::json(['title' => $helpTitle, 'text' => $helpText]);
+ if ($this->_inCache($route)) {
+ $content = [
+ 'text' => Cache::get('help.' . $route . '.text'),
+ 'title' => Cache::get('help.' . $route . '.title'),
+ ];
+
+ return Response::json($content);
}
+ $content = $this->_getFromGithub($route);
+
- $uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/' . e($route) . '.md';
- \Log::debug('URL is: ' . $uri);
+ Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week.
+ Cache::put('help.' . $route . '.title', $content['title'], 10080);
+
+ return Response::json($content);
+
+ }
+
+ /**
+ * @param $route
+ *
+ * @return bool
+ */
+ protected function _inCache($route)
+ {
+ return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text');
+ }
+
+ /**
+ * @param $route
+ *
+ * @return array
+ */
+ protected function _getFromGithub($route)
+ {
+ $uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/' . e($route) . '.md';
+ $content = [
+ 'text' => '<p>There is no help for this route!</p>',
+ 'title' => $route,
+ ];
try {
- $helpText = file_get_contents($uri);
+ $content['text'] = file_get_contents($uri);
} catch (ErrorException $e) {
\Log::error(trim($e->getMessage()));
}
- \Log::debug('Found help for ' . $route);
- \Log::debug('Help text length for route ' . $route . ' is ' . strlen($helpText));
- \Log::debug('Help text IS: "' . $helpText . '".');
- if (strlen(trim($helpText)) == 0) {
- $helpText = '<p>There is no help for this route.</p>';
+ if (strlen(trim($content['text'])) == 0) {
+ $content['text'] = '<p>There is no help for this route.</p>';
}
+ $content['text'] = \Michelf\Markdown::defaultTransform($content['text']);
- $helpText = \Michelf\Markdown::defaultTransform($helpText);
- $helpTitle = $route;
+ return $content;
- Cache::put('help.' . $route . '.text', $helpText, 10080); // a week.
- Cache::put('help.' . $route . '.title', $helpTitle, 10080);
-
- return Response::json(['title' => $helpTitle, 'text' => $helpText]);
}
-}
+}
+
diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php
index 16089486f2..1523206116 100644
--- a/app/controllers/PiggybankController.php
+++ b/app/controllers/PiggybankController.php
@@ -8,9 +8,6 @@ use Illuminate\Support\Collection;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("TooManyMethods") // I'm also fine with this.
- * @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
*
*
* Class PiggyBankController
diff --git a/app/controllers/PreferencesController.php b/app/controllers/PreferencesController.php
index ab4b81f717..d0d62d797d 100644
--- a/app/controllers/PreferencesController.php
+++ b/app/controllers/PreferencesController.php
@@ -3,7 +3,6 @@
/**
* Class PreferencesController
*
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
*/
class PreferencesController extends BaseController
diff --git a/app/controllers/ProfileController.php b/app/controllers/ProfileController.php
index e9b397833c..a9f9419bea 100644
--- a/app/controllers/ProfileController.php
+++ b/app/controllers/ProfileController.php
@@ -1,7 +1,6 @@
<?php
/**
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
*
* Class ProfileController
*/
@@ -39,19 +38,9 @@ class ProfileController extends BaseController
return View::make('profile.change-password');
}
- if (strlen(Input::get('new1')) == 0 || strlen(Input::get('new2')) == 0) {
- Session::flash('error', 'Do fill in a password!');
-
- return View::make('profile.change-password');
- }
- if (Input::get('new1') == Input::get('old')) {
- Session::flash('error', 'The idea is to change your password.');
-
- return View::make('profile.change-password');
- }
-
- if (Input::get('new1') !== Input::get('new2')) {
- Session::flash('error', 'New passwords do not match!');
+ $result = $this->_validatePassword(Input::get('old'), Input::get('new1'), Input::get('new2'));
+ if (!($result === true)) {
+ Session::flash('error', $result);
return View::make('profile.change-password');
}
@@ -66,4 +55,29 @@ class ProfileController extends BaseController
return Redirect::route('profile');
}
+ /**
+ * @param string $old
+ * @param string $new1
+ * @param string $new2
+ *
+ * @return string|bool
+ */
+ protected function _validatePassword($old, $new1, $new2)
+ {
+ if (strlen($new1) == 0 || strlen($new2) == 0) {
+ return 'Do fill in a password!';
+
+ }
+ if ($new1 == $old) {
+ return 'The idea is to change your password.';
+ }
+
+ if ($new1 !== $new2) {
+ return 'New passwords do not match!';
+ }
+
+ return true;
+
+ }
+
}
diff --git a/app/controllers/RelatedController.php b/app/controllers/RelatedController.php
index 747dad5e3f..ec533564bb 100644
--- a/app/controllers/RelatedController.php
+++ b/app/controllers/RelatedController.php
@@ -3,6 +3,8 @@ use FireflyIII\Helper\Related\RelatedInterface;
use Illuminate\Support\Collection;
/**
+ * @SuppressWarnings("CamelCase") // I'm fine with this.
+ *
* Class RelatedController
*/
class RelatedController extends BaseController
@@ -10,6 +12,9 @@ class RelatedController extends BaseController
protected $_repository;
+ /**
+ * @param RelatedInterface $repository
+ */
public function __construct(RelatedInterface $repository)
{
$this->_repository = $repository;
diff --git a/app/controllers/RepeatedExpenseController.php b/app/controllers/RepeatedExpenseController.php
index 78272a9453..d9bf9f4002 100644
--- a/app/controllers/RepeatedExpenseController.php
+++ b/app/controllers/RepeatedExpenseController.php
@@ -6,8 +6,6 @@ use FireflyIII\Exception\FireflyException;
/**
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
*
* Class RepeatedExpenseController
*/
diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php
index a7c9b12e71..5cef244661 100644
--- a/app/controllers/TransactionController.php
+++ b/app/controllers/TransactionController.php
@@ -9,10 +9,6 @@ use Illuminate\Support\Collection;
/**
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
- * @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
- * @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
- * @SuppressWarnings("TooManyMethods") // I'm also fine with this.
- * @SuppressWarnings("ExcessiveClassComplexity")
*
* Class TransactionController
*
@@ -198,7 +194,6 @@ class TransactionController extends BaseController
}
-
/**
* @param TransactionJournal $journal
*
diff --git a/app/database/seeds/DefaultUserSeeder.php b/app/database/seeds/DefaultUserSeeder.php
deleted file mode 100644
index 1b40e29097..0000000000
--- a/app/database/seeds/DefaultUserSeeder.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-/**
- * Class DefaultUserSeeder
- */
-class DefaultUserSeeder extends Seeder
-{
- public function run()
- {
- DB::table('users')->delete();
- if (App::environment() == 'testing' || App::environment() == 'homestead') {
-
- User::create(['email' => '[email protected]', 'password' => 'james', 'reset' => null, 'remember_token' => null]);
- User::create(['email' => '[email protected]', 'password' => 'acceptance', 'reset' => null, 'remember_token' => null]);
- User::create(['email' => '[email protected]', 'password' => 'functional', 'reset' => null, 'remember_token' => null]);
- User::create(['email' => '[email protected]', 'password' => 'functional', 'reset' => 'okokokokokokokokokokokokokokokok', 'remember_token' => null]);
-
- }
-
- }
-
-}
diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php
deleted file mode 100644
index e2cdebf7d7..0000000000
--- a/app/database/seeds/TestContentSeeder.php
+++ /dev/null
@@ -1,561 +0,0 @@
-<?php
-
-use Carbon\Carbon;
-
-/**
- *
- * @SuppressWarnings("CamelCase") // I'm fine with this.
- *
- * Class TestContentSeeder
- */
-class TestContentSeeder extends Seeder
-{
- /** @var string */
- public $eom;
- /** @var string */
- public $neom;
- /** @var string */
- public $nsom;
- /** @var string */
- public $som;
- /** @var string */
- public $today;
- /** @var string */
- public $yaeom;
- /** @var string */
- public $yasom;
- /** @var Carbon */
- protected $_endOfMonth;
- /** @var Carbon */
- protected $_nextEndOfMonth;
- /** @var Carbon */
- protected $_nextStartOfMonth;
- /** @var Carbon */
- protected $_startOfMonth;
- /** @var Carbon */
- protected $_today;
- /** @var Carbon */
- protected $_yearAgoEndOfMonth;
- /** @var Carbon */
- protected $_yearAgoStartOfMonth;
-
- /**
- *
- */
- public function __construct()
- {
- $this->_startOfMonth = Carbon::now()->startOfMonth();
- $this->som = $this->_startOfMonth->format('Y-m-d');
-
- $this->_endOfMonth = Carbon::now()->endOfMonth();
- $this->eom = $this->_endOfMonth->format('Y-m-d');
-
- $this->_nextStartOfMonth = Carbon::now()->addMonth()->startOfMonth();
- $this->nsom = $this->_nextStartOfMonth->format('Y-m-d');
-
- $this->_nextEndOfMonth = Carbon::now()->addMonth()->endOfMonth();
- $this->neom = $this->_nextEndOfMonth->format('Y-m-d');
-
- $this->_yearAgoStartOfMonth = Carbon::now()->subYear()->startOfMonth();
- $this->yasom = $this->_yearAgoStartOfMonth->format('Y-m-d');
-
- $this->_yearAgoEndOfMonth = Carbon::now()->subYear()->startOfMonth();
- $this->yaeom = $this->_yearAgoEndOfMonth->format('Y-m-d');
-
-
- $this->_today = Carbon::now();
- $this->today = $this->_today->format('Y-m-d');
- }
-
- /**
- * Dates are always this month, the start of this month or earlier.
- */
- public function run()
- {
- if (App::environment() == 'testing' || App::environment() == 'homestead') {
-
- $user = User::whereEmail('[email protected]')->first();
-
- // create initial accounts and various other stuff:
- $this->createAssetAccounts($user);
- $this->createBudgets($user);
- $this->createCategories($user);
- $this->createPiggyBanks($user);
- $this->createReminders($user);
- $this->createRecurringTransactions($user);
- $this->createBills($user);
- $this->createExpenseAccounts($user);
- $this->createRevenueAccounts($user);
-
- // get some objects from the database:
- $checking = Account::whereName('Checking account')->orderBy('id', 'DESC')->first();
- $savings = Account::whereName('Savings account')->orderBy('id', 'DESC')->first();
- $landLord = Account::whereName('Land lord')->orderBy('id', 'DESC')->first();
- $utilities = Account::whereName('Utilities company')->orderBy('id', 'DESC')->first();
- $television = Account::whereName('TV company')->orderBy('id', 'DESC')->first();
- $phone = Account::whereName('Phone agency')->orderBy('id', 'DESC')->first();
- $employer = Account::whereName('Employer')->orderBy('id', 'DESC')->first();
-
-
- $bills = Budget::whereName('Bills')->orderBy('id', 'DESC')->first();
- $groceries = Budget::whereName('Groceries')->orderBy('id', 'DESC')->first();
-
- $house = Category::whereName('House')->orderBy('id', 'DESC')->first();
-
-
- $withdrawal = TransactionType::whereType('Withdrawal')->first();
- $deposit = TransactionType::whereType('Deposit')->first();
- $transfer = TransactionType::whereType('Transfer')->first();
-
- $euro = TransactionCurrency::whereCode('EUR')->first();
-
- $rentBill = Bill::where('name', 'Rent')->first();
-
-
- $current = clone $this->_yearAgoStartOfMonth;
- while ($current <= $this->_startOfMonth) {
- $cur = $current->format('Y-m-d');
- $formatted = $current->format('F Y');
-
- // create expenses for rent, utilities, TV, phone on the 1st of the month.
- $this->createTransaction($checking, $landLord, 800, $withdrawal, 'Rent for ' . $formatted, $cur, $euro, $bills, $house, $rentBill);
- $this->createTransaction($checking, $utilities, 150, $withdrawal, 'Utilities for ' . $formatted, $cur, $euro, $bills, $house);
- $this->createTransaction($checking, $television, 50, $withdrawal, 'TV for ' . $formatted, $cur, $euro, $bills, $house);
- $this->createTransaction($checking, $phone, 50, $withdrawal, 'Phone bill for ' . $formatted, $cur, $euro, $bills, $house);
-
- // two transactions. One without a budget, one without a category.
- $this->createTransaction($checking, $phone, 10, $withdrawal, 'Extra charges on phone bill for ' . $formatted, $cur, $euro, null, $house);
- $this->createTransaction($checking, $television, 5, $withdrawal, 'Extra charges on TV bill for ' . $formatted, $cur, $euro, $bills, null);
-
- // income from job:
- $this->createTransaction($employer, $checking, rand(3500, 4000), $deposit, 'Salary for ' . $formatted, $cur, $euro);
- $this->createTransaction($checking, $savings, 2000, $transfer, 'Salary to savings account in ' . $formatted, $cur, $euro);
-
- $this->createGroceries($current);
- $this->createBigExpense(clone $current);
-
- echo 'Created test-content for ' . $current->format('F Y') . "\n";
- $current->addMonth();
- }
-
-
- // piggy bank event
- // add money to this piggy bank
- // create a piggy bank event to match:
- $piggyBank = PiggyBank::whereName('New camera')->orderBy('id', 'DESC')->first();
- $intoPiggy = $this->createTransaction($checking, $savings, 100, $transfer, 'Money for piggy', $this->yaeom, $euro, $groceries, $house);
- PiggyBankEvent::create(
- [
- 'piggy_bank_id' => $piggyBank->id,
- 'transaction_journal_id' => $intoPiggy->id,
- 'date' => $this->yaeom,
- 'amount' => 100
- ]
- );
- }
- }
-
- /**
- * @param User $user
- */
- public function createAssetAccounts(User $user)
- {
- $assetType = AccountType::whereType('Asset account')->first();
- $ibType = AccountType::whereType('Initial balance account')->first();
- $obType = TransactionType::whereType('Opening balance')->first();
- $euro = TransactionCurrency::whereCode('EUR')->first();
-
-
- $acc_a = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Checking account', 'active' => 1]);
- $acc_b = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Savings account', 'active' => 1]);
- $acc_c = Account::create(['user_id' => $user->id, 'account_type_id' => $assetType->id, 'name' => 'Delete me', 'active' => 1]);
-
- $acc_d = Account::create(['user_id' => $user->id, 'account_type_id' => $ibType->id, 'name' => 'Checking account initial balance', 'active' => 0]);
- $acc_e = Account::create(['user_id' => $user->id, 'account_type_id' => $ibType->id, 'name' => 'Savings account initial balance', 'active' => 0]);
- $acc_f = Account::create(['user_id' => $user->id, 'account_type_id' => $ibType->id, 'name' => 'Delete me initial balance', 'active' => 0]);
-
-
- $this->createTransaction($acc_d, $acc_a, 4000, $obType, 'Initial Balance for Checking account', $this->yasom, $euro);
- $this->createTransaction($acc_e, $acc_b, 10000, $obType, 'Initial Balance for Savings account', $this->yasom, $euro);
- $this->createTransaction($acc_f, $acc_c, 100, $obType, 'Initial Balance for Delete me', $this->yasom, $euro);
- }
-
- /**
- * @param Account $from
- * @param Account $to
- * @param $amount
- * @param TransactionType $type
- * @param $description
- * @param $date
- * @param TransactionCurrency $currency
- *
- * @param Budget $budget
- * @param Category $category
- * @param Bill $bill
- *
- * @return TransactionJournal
- */
- public function createTransaction(
- Account $from, Account $to, $amount, TransactionType $type, $description, $date, TransactionCurrency $currency, Budget $budget = null,
- Category $category = null, Bill $bill = null
- ) {
- $user = User::whereEmail('[email protected]')->first();
-
- $billID = is_null($bill) ? null : $bill->id;
-
-
- /** @var TransactionJournal $journal */
- $journal = TransactionJournal::create(
- [
- 'user_id' => $user->id, 'transaction_type_id' => $type->id, 'transaction_currency_id' => $currency->id, 'bill_id' => $billID,
- '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;
- }
-
- /**
- * @param User $user
- */
- public function createBudgets(User $user)
- {
-
- $groceries = Budget::create(['user_id' => $user->id, 'name' => 'Groceries']);
- $bills = Budget::create(['user_id' => $user->id, 'name' => 'Bills']);
- $deleteMe = Budget::create(['user_id' => $user->id, 'name' => 'Delete me']);
- Budget::create(['user_id' => $user->id, 'name' => 'Budget without repetition']);
- $groceriesLimit = BudgetLimit::create(
- ['startdate' => $this->som, 'amount' => 201, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $groceries->id]
- );
- $billsLimit = BudgetLimit::create(
- ['startdate' => $this->som, 'amount' => 202, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $bills->id]
- );
- $deleteMeLimit = BudgetLimit::create(
- ['startdate' => $this->som, 'amount' => 203, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $deleteMe->id]
- );
-
- // and because we have no filters, some repetitions:
- LimitRepetition::create(['budget_limit_id' => $groceriesLimit->id, 'startdate' => $this->som, 'enddate' => $this->eom, 'amount' => 201]);
- LimitRepetition::create(['budget_limit_id' => $billsLimit->id, 'startdate' => $this->som, 'enddate' => $this->eom, 'amount' => 202]);
- LimitRepetition::create(['budget_limit_id' => $deleteMeLimit->id, 'startdate' => $this->som, 'enddate' => $this->eom, 'amount' => 203]);
- }
-
- /**
- * @param User $user
- */
- public function createCategories(User $user)
- {
- Category::create(['user_id' => $user->id, 'name' => 'DailyGroceries']);
- Category::create(['user_id' => $user->id, 'name' => 'Lunch']);
- Category::create(['user_id' => $user->id, 'name' => 'House']);
- Category::create(['user_id' => $user->id, 'name' => 'Delete me']);
-
- }
-
- /**
- * @param User $user
- */
- public function createPiggyBanks(User $user)
- {
- // account
- $savings = Account::whereName('Savings account')->orderBy('id', 'DESC')->first();
-
- // some dates
- $endDate = clone $this->_startOfMonth;
- $nextYear = clone $this->_startOfMonth;
-
- $endDate->addMonths(4);
- $nextYear->addYear()->subDay();
-
- $next = $nextYear->format('Y-m-d');
- $end = $endDate->format('Y-m-d');
-
- // piggy bank
- $newCamera = PiggyBank::create(
- [
- 'account_id' => $savings->id,
- 'name' => 'New camera',
- 'targetamount' => 2000,
- 'startdate' => $this->som,
- 'targetdate' => null,
- 'repeats' => 0,
- 'rep_length' => null,
- 'rep_every' => 0,
- 'rep_times' => null,
- 'reminder' => null,
- 'reminder_skip' => 0,
- 'remind_me' => 0,
- 'order' => 0,
- ]
- );
- // and some events!
- PiggyBankEvent::create(['piggy_bank_id' => $newCamera->id, 'date' => $this->som, 'amount' => 100]);
- PiggyBankRepetition::create(['piggy_bank_id' => $newCamera->id, 'startdate' => $this->som, 'targetdate' => null, 'currentamount' => 100]);
-
-
- $newClothes = PiggyBank::create(
- [
- 'account_id' => $savings->id,
- 'name' => 'New clothes',
- 'targetamount' => 2000,
- 'startdate' => $this->som,
- 'targetdate' => $end,
- 'repeats' => 0,
- 'rep_length' => null,
- 'rep_every' => 0,
- 'rep_times' => null,
- 'reminder' => null,
- 'reminder_skip' => 0,
- 'remind_me' => 0,
- 'order' => 0,
- ]
- );
-
- PiggyBankEvent::create(['piggy_bank_id' => $newClothes->id, 'date' => $this->som, 'amount' => 100]);
- PiggyBankRepetition::create(['piggy_bank_id' => $newClothes->id, 'startdate' => $this->som, 'targetdate' => $end, 'currentamount' => 100]);
-
- // weekly reminder piggy bank
- $weekly = PiggyBank::create(
- [
- 'account_id' => $savings->id,
- 'name' => 'Weekly reminder for clothes',
- 'targetamount' => 2000,
- 'startdate' => $this->som,
- 'targetdate' => $next,
- 'repeats' => 0,
- 'rep_length' => null,
- 'rep_every' => 0,
- 'rep_times' => null,
- 'reminder' => 'week',
- 'reminder_skip' => 0,
- 'remind_me' => 1,
- 'order' => 0,
- ]
- );
- PiggyBankRepetition::create(['piggy_bank_id' => $weekly->id, 'startdate' => $this->som, 'targetdate' => $next, 'currentamount' => 0]);
- }
-
- /**
- * @param User $user
- */
- public function createReminders(User $user)
- {
- // for weekly piggy bank (clothes)
- $nextWeek = clone $this->_startOfMonth;
- $piggyBank = PiggyBank::whereName('New clothes')->orderBy('id', 'DESC')->first();
- $nextWeek->addWeek();
- $week = $nextWeek->format('Y-m-d');
-
- Reminder::create(
- ['user_id' => $user->id, 'startdate' => $this->som, 'enddate' => $week, 'active' => 1, 'notnow' => 0,
- 'remindersable_id' => $piggyBank->id, 'remindersable_type' => 'PiggyBank']
- );
-
- // a fake reminder::
- Reminder::create(
- ['user_id' => $user->id, 'startdate' => $this->som, 'enddate' => $week, 'active' => 0, 'notnow' => 0, 'remindersable_id' => 40,
- 'remindersable_type' => 'Transaction']
- );
- }
-
- /**
- * @param User $user
- */
- public function createRecurringTransactions(User $user)
- {
- // account
- $savings = Account::whereName('Savings account')->orderBy('id', 'DESC')->first();
-
- $recurring = PiggyBank::create(
- [
- 'account_id' => $savings->id,
- 'name' => 'Nieuwe spullen',
- 'targetamount' => 1000,
- 'startdate' => $this->som,
- 'targetdate' => $this->eom,
- 'repeats' => 1,
- 'rep_length' => 'month',
- 'rep_every' => 0,
- 'rep_times' => 0,
- 'reminder' => 'month',
- 'reminder_skip' => 0,
- 'remind_me' => 1,
- 'order' => 0,
- ]
- );
- PiggyBankRepetition::create(['piggy_bank_id' => $recurring->id, 'startdate' => $this->som, 'targetdate' => $this->eom, 'currentamount' => 0]);
- PiggyBankRepetition::create(
- ['piggy_bank_id' => $recurring->id, 'startdate' => $this->nsom, 'targetdate' => $this->neom, 'currentamount' => 0]
- );
- Reminder::create(
- ['user_id' => $user->id, 'startdate' => $this->som, 'enddate' => $this->neom, 'active' => 1, 'notnow' => 0,
- 'remindersable_id' => $recurring->id, 'remindersable_type' => 'PiggyBank']
- );
- }
-
- /**
- * @param $user
- */
- public function createBills($user)
- {
- // bill
- Bill::create(
- [
- 'user_id' => $user->id, 'name' => 'Rent', 'match' => 'rent,landlord',
- 'amount_min' => 700,
- 'amount_max' => 900,
- 'date' => $this->som,
- 'active' => 1,
- 'automatch' => 1,
- 'repeat_freq' => 'monthly',
- 'skip' => 0,
- ]
- );
-
- // bill
- Bill::create(
- [
- 'user_id' => $user->id,
- 'name' => 'Gas licht',
- 'match' => 'no,match',
- 'amount_min' => 500,
- 'amount_max' => 700,
- 'date' => $this->som,
- 'active' => 1,
- 'automatch' => 1,
- 'repeat_freq' => 'monthly',
- 'skip' => 0,
- ]
- );
-
- // bill
- Bill::create(
- [
- 'user_id' => $user->id,
- 'name' => 'Something something',
- 'match' => 'mumble,mumble',
- 'amount_min' => 500,
- 'amount_max' => 700,
- 'date' => $this->som,
- 'active' => 0,
- 'automatch' => 1,
- 'repeat_freq' => 'monthly',
- 'skip' => 0,
- ]
- );
-
- }
-
- /**
- * @param $user
- */
- public function createExpenseAccounts($user)
- {
- //// create expenses for rent, utilities, water, TV, phone on the 1st of the month.
- $expenseType = AccountType::whereType('Expense account')->first();
-
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Land lord', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Utilities company', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Water company', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'TV company', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Phone agency', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Super savers', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Groceries House', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Lunch House', 'active' => 1]);
-
-
- Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Buy More', 'active' => 1]);
-
- }
-
- /**
- * @param $user
- */
- public function createRevenueAccounts($user)
- {
- $revenueType = AccountType::whereType('Revenue account')->first();
-
- Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Employer', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'IRS', 'active' => 1]);
- Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Second job employer', 'active' => 1]);
-
- }
-
- /**
- * @param Carbon $date
- */
- public function createGroceries(Carbon $date)
- {
- // variables we need:
- $checking = Account::whereName('Checking account')->orderBy('id', 'DESC')->first();
- $shopOne = Account::whereName('Groceries House')->orderBy('id', 'DESC')->first();
- $shopTwo = Account::whereName('Super savers')->orderBy('id', 'DESC')->first();
- $lunchHouse = Account::whereName('Lunch House')->orderBy('id', 'DESC')->first();
- $lunch = Category::whereName('Lunch')->orderBy('id', 'DESC')->first();
- $daily = Category::whereName('DailyGroceries')->orderBy('id', 'DESC')->first();
- $euro = TransactionCurrency::whereCode('EUR')->first();
- $withdrawal = TransactionType::whereType('Withdrawal')->first();
- $groceries = Budget::whereName('Groceries')->orderBy('id', 'DESC')->first();
-
-
- $shops = [$shopOne, $shopTwo];
-
- // create groceries and lunch (daily, between 5 and 10 euro).
- $mStart = clone $date;
- $mEnd = clone $date;
- $mEnd->endOfMonth();
- while ($mStart <= $mEnd) {
- $mFormat = $mStart->format('Y-m-d');
- $shop = $shops[rand(0, 1)];
-
- $this->createTransaction($checking, $shop, (rand(500, 1000) / 100), $withdrawal, 'Groceries', $mFormat, $euro, $groceries, $daily);
- $this->createTransaction($checking, $lunchHouse, (rand(200, 600) / 100), $withdrawal, 'Lunch', $mFormat, $euro, $groceries, $lunch);
-
- $mStart->addDay();
- }
- }
-
- /**
- * @param $date
- */
- public function createBigExpense($date)
- {
- $date->addDays(12);
- $dollar = TransactionCurrency::whereCode('USD')->first();
- $checking = Account::whereName('Checking account')->orderBy('id', 'DESC')->first();
- $savings = Account::whereName('Savings account')->orderBy('id', 'DESC')->first();
- $buyMore = Account::whereName('Buy More')->orderBy('id', 'DESC')->first();
- $withdrawal = TransactionType::whereType('Withdrawal')->first();
- $transfer = TransactionType::whereType('Transfer')->first();
- $user = User::whereEmail('[email protected]')->first();
-
-
- // create some big expenses, move some money around.
- $amount = rand(500, 2000);
- $one = $this->createTransaction(
- $savings, $checking, $amount, $transfer, 'Money for big expense in ' . $date->format('F Y'), $date->format('Y-m-d'), $dollar
- );
- $two = $this->createTransaction(
- $checking, $buyMore, $amount, $withdrawal, 'Big expense in ' . $date->format('F Y'), $date->format('Y-m-d'), $dollar
- );
- $group = TransactionGroup::create(
- [
- 'user_id' => $user->id,
- 'relation' => 'balance'
- ]
- );
- $group->transactionjournals()->save($one);
- $group->transactionjournals()->save($two);
- }
-}