Skip to content

Firefly III: a personal finances manager

First attempt at an import/migration procedure.

James Cole18362494073c
diff --git a/app/config/testing/auth.php b/app/config/testing/auth.php
index 427141b2cd..0fe9e0172d 100644
--- a/app/config/testing/auth.php
+++ b/app/config/testing/auth.php
@@ -1,7 +1,7 @@
<?php
return [
- 'verify_mail' => true,
+ 'verify_mail' => false,
'verify_reset' => true,
'allow_register' => true
diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php
index 8ec3753f84..a45a2627de 100644
--- a/app/controllers/HomeController.php
+++ b/app/controllers/HomeController.php
@@ -1,5 +1,5 @@
<?php
-
+use Carbon\Carbon as Carbon;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
class HomeController extends BaseController {
@@ -10,6 +10,7 @@ class HomeController extends BaseController {
public function index()
{
- return View::make('index');
+ $count = $this->accounts->count();
+ return View::make('index')->with('count',$count);
}
}
diff --git a/app/database/migrations/2014_06_27_164620_create_transaction_journals_table.php b/app/database/migrations/2014_06_27_164620_create_transaction_journals_table.php
index a66e01eb09..d5890a813c 100644
--- a/app/database/migrations/2014_06_27_164620_create_transaction_journals_table.php
+++ b/app/database/migrations/2014_06_27_164620_create_transaction_journals_table.php
@@ -19,6 +19,7 @@ class CreateTransactionJournalsTable extends Migration {
$table->integer('transaction_type_id')->unsigned();
$table->integer('transaction_currency_id')->unsigned();
$table->string('description',255)->nullable();
+ $table->boolean('completed');
$table->date('date');
// connect transaction journals to transaction types
diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
index a8d4a21ddc..61aefb607c 100644
--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
@@ -9,6 +9,7 @@ interface AccountRepositoryInterface
public function count();
- public function store();
+ public function store($data);
+ public function storeWithInitialBalance($data,\Carbon\Carbon $date, $amount = 0);
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
index ce88daa031..10a39e39e2 100644
--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
@@ -11,53 +11,51 @@ class EloquentAccountRepository implements AccountRepositoryInterface
{
}
- public function count() {
+ public function count()
+ {
return \Auth::user()->accounts()->count();
}
- public function store() {
-
-
- $default = \AccountType::where('description','Default account')->first();
- $balanceAT = \AccountType::where('description','Initial balance account')->first();
-
- $account = new \Account;
- $account->active = true;
-
- $account->user()->associate(\Auth::user());
- $account->name = \Input::get('name');
- $account->accountType()->associate($default);
+ public function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0)
+ {
- if(!$account->isValid()) {
- \Log::error('Could not create account: ' . $account->validator->messages()->first());
- $this->validator = $account->validator;
- return false;
- }
+ $account = $this->store($data);
- $account->save();
+ $initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
+ $initial = new \Account;
+ $initial->accountType()->associate($initialBalanceAT);
+ $initial->user()->associate(\Auth::user());
+ $initial->name = $data['name'] . ' initial balance';
+ $initial->active = 0;
+ $initial->save();
- $balance = floatval(\Input::get('openingbalance'));
- if($balance != 0.00) {
- // create account
- $initial = new \Account;
- $account->active = false;
+ // create new transaction journal (and transactions):
+ /** @var \Firefly\Storage\TransactionJournal\TransactionJournalInterface $transactionJournal */
+ $transactionJournal = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalInterface');
+ $transactionJournal->createSimpleJournal(
+ $initial, $account, 'Initial Balance for ' . $data['name'], $amount, $date
+ );
- $account->user()->associate(\Auth::user());
- $account->name = \Input::get('name').' initial balance';
- $account->accountType()->associate($balanceAT);
- $account->save();
- // create journal (access helper!)
+ return $account;
- // create journal
+ }
- // create transaction
+ public function store($data)
+ {
+ $defaultAT = \AccountType::where('description', 'Default account')->first();
- // create
- }
+ $at = isset($data['account_type']) ? $data['account_type'] : $defaultAT;
+ $account = new \Account;
+ $account->accountType()->associate($at);
+ $account->user()->associate(\Auth::user());
+ $account->name = $data['name'];
+ $account->active = isset($data['active']) ? $data['active'] : 1;
+ $account->save();
+ return $account;
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/StorageServiceProvider.php b/app/lib/Firefly/Storage/StorageServiceProvider.php
index 58c6eb2868..644bc46200 100644
--- a/app/lib/Firefly/Storage/StorageServiceProvider.php
+++ b/app/lib/Firefly/Storage/StorageServiceProvider.php
@@ -20,6 +20,10 @@ class StorageServiceProvider extends ServiceProvider
'Firefly\Storage\Account\AccountRepositoryInterface',
'Firefly\Storage\Account\EloquentAccountRepository'
);
+ $this->app->bind(
+ 'Firefly\Storage\TransactionJournal\TransactionJournalInterface',
+ 'Firefly\Storage\TransactionJournal\EloquentTransactionJournalRepository'
+ );
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
new file mode 100644
index 0000000000..d56065b148
--- /dev/null
+++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
@@ -0,0 +1,114 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 03/07/14
+ * Time: 15:24
+ */
+
+namespace Firefly\Storage\TransactionJournal;
+
+
+class EloquentTransactionJournalRepository implements TransactionJournalInterface
+{
+
+ public function createSimpleJournal(\Account $from, \Account $to, $description, $amount, \Carbon\Carbon $date)
+ {
+
+
+ /*
+ * We're building this thinking the money goes from A to B.
+ * If the amount is negative however, the money still goes
+ * from A to B but the balances are reversed.
+ *
+ * Aka:
+ *
+ * Amount = 200
+ * A loses 200 (-200). * -1
+ * B gains 200 (200). * 1
+ *
+ * Final balance: -200 for A, 200 for B.
+ *
+ * When the amount is negative:
+ *
+ * Amount = -200
+ * A gains 200 (200). * -1
+ * B loses 200 (-200). * 1
+ *
+ */
+
+ // amounts:
+ $amountFrom = $amount * -1;
+ $amountTo = $amount;
+
+ // account types for both:
+ $toAT = $to->accountType->description;
+ $fromAT = $from->accountType->description;
+
+
+ switch (true) {
+ // is withdrawal from one of your own accounts:
+ case ($fromAT == 'Default account'):
+ $journalType = \TransactionType::where('type', 'Withdrawal')->first();
+ break;
+ // both are yours:
+ case ($fromAT == 'Default account' && $toAT == 'Default account'):
+ // determin transaction type. If both accounts are new, it's an initial
+ // balance transfer.
+ $journalType = \TransactionType::where('type', 'Transfer')->first();
+ break;
+ case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0):
+ $journalType = \TransactionType::where('type', 'Opening balance')->first();
+ break;
+ default:
+ // is deposit into one of your own accounts:
+ case ($toAT == 'Default account'):
+ $journalType = \TransactionType::where('type', 'Deposit')->first();
+ break;
+ }
+ // always the same currency:
+ $currency = \TransactionCurrency::where('code', 'EUR')->first();
+
+ // new journal:
+ $journal = new \TransactionJournal();
+ $journal->transactionType()->associate($journalType);
+ $journal->transactionCurrency()->associate($currency);
+ $journal->completed = false;
+ $journal->description = $description;
+ $journal->date = $date;
+ if (!$journal->isValid()) {
+ return false;
+ }
+ $journal->save();
+
+ // create transactions:
+ $fromTransaction = new \Transaction;
+ $fromTransaction->account()->associate($from);
+ $fromTransaction->transactionJournal()->associate($journal);
+ $fromTransaction->description = null;
+ $fromTransaction->amount = $amountFrom;
+ if (!$fromTransaction->isValid()) {
+ return false;
+ }
+ $fromTransaction->save();
+
+ $toTransaction = new \Transaction;
+ $toTransaction->account()->associate($to);
+ $toTransaction->transactionJournal()->associate($journal);
+ $toTransaction->description = null;
+ $toTransaction->amount = $amountTo;
+ if (!$toTransaction->isValid()) {
+ return false;
+ }
+ $toTransaction->save();
+
+ $journal->completed = true;
+ $journal->save();
+ return;
+
+
+
+ echo 'saved!';
+
+ }
+}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalInterface.php b/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalInterface.php
new file mode 100644
index 0000000000..f0239936a1
--- /dev/null
+++ b/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalInterface.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 03/07/14
+ * Time: 15:22
+ */
+
+namespace Firefly\Storage\TransactionJournal;
+
+
+interface TransactionJournalInterface {
+
+ public function createSimpleJournal(\Account $from, \Account $to, $description, $amount, \Carbon\Carbon $date);
+
+}
\ No newline at end of file
diff --git a/app/models/Transaction.php b/app/models/Transaction.php
index 55f848c6df..06aa3732b1 100644
--- a/app/models/Transaction.php
+++ b/app/models/Transaction.php
@@ -1,8 +1,16 @@
<?php
-class Transaction extends Eloquent
+class Transaction extends Elegant
{
+ public static $rules
+ = [
+ 'account_id' => 'numeric|required|exists:accounts,id',
+ 'transaction_journal_id' => 'numeric|required|exists:transaction_journals,id',
+ 'description' => 'between:1,255',
+ 'amount' => 'required|between:-65536,65536',
+ ];
+
public function account()
{
diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php
index 2947c2bc06..6f2f8f9cf7 100644
--- a/app/models/TransactionJournal.php
+++ b/app/models/TransactionJournal.php
@@ -1,17 +1,35 @@
<?php
-class TransactionJournal extends Eloquent {
+class TransactionJournal extends Elegant
+{
- public function transactionType() {
+ public static $rules
+ = [
+ 'transaction_type_id' => 'required|exists:transaction_types,id',
+ 'transaction_currency_id' => 'required|exists:transaction_currencies,id',
+ 'description' => 'between:1,255',
+ 'date' => 'date',
+ ];
+
+ public function transactionType()
+ {
return $this->belongsTo('TransactionType');
}
- public function transactionCurrency() {
+
+ public function transactionCurrency()
+ {
return $this->belongsTo('TransactionCurrency');
}
- public function transactions() {
+ public function transactions()
+ {
return $this->hasMany('Transaction');
}
+ public function getDates()
+ {
+ return array('created_at', 'updated_at', 'date');
+ }
+
}
\ No newline at end of file
diff --git a/app/routes.php b/app/routes.php
index b6a0142c8a..ebeba61e2e 100644
--- a/app/routes.php
+++ b/app/routes.php
@@ -17,4 +17,8 @@ Route::post('/remindme',['uses' => 'UserController@postRemindme','before' => 'cs
// profile (after login / logout)
Route::get('/profile',['uses' => 'ProfileController@index','as' => 'profile','before' => 'auth']);
Route::get('/profile/change-password',['uses' => 'ProfileController@changePassword','as' => 'change-password','before' => 'auth']);
-Route::post('/profile/change-password',['uses' => 'ProfileController@postChangePassword','before' => 'csrf|auth']);
\ No newline at end of file
+Route::post('/profile/change-password',['uses' => 'ProfileController@postChangePassword','before' => 'csrf|auth']);
+
+// migrate controller:
+Route::get('/migrate',['uses' => 'MigrationController@index','as' => 'migrate','before' => 'auth']);
+Route::post('/migrate',['uses' => 'MigrationController@postIndex','before' => 'csrf|auth']);
\ No newline at end of file
diff --git a/app/views/index.blade.php b/app/views/index.blade.php
index c45c7d3730..631f5c38a0 100644
--- a/app/views/index.blade.php
+++ b/app/views/index.blade.php
@@ -2,11 +2,41 @@
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
- <h1>Firefly<br/>
+ <h1>Firefly
+ @if($count > 0)
+ <br/>
<small>What's playing?</small>
+ @endif
</h1>
</div>
</div>
+@if($count == 0)
+<div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <p class="lead">Welcome to Firefly III.</p>
+ <p>
+ To get get started, choose below:
+ </p>
+ </div>
+</div>
+<div class="row">
+ <div class="col-lg-6 col-md-6 col-sm-12">
+ <h2><a href="{{route('migrate')}}">Migrate from Firefly II</a></h2>
+ <p>
+ Use this option if you have a JSON file from your current Firefly II installation.
+ </p>
+ </div>
+ <div class="col-lg-6 col-md-6 col-sm-12">
+ <h2><a href="#">Start from scratch</a></h2>
+ <p>
+ Use this option if you are new to Firefly (III).
+ </p>
+ </div>
+@else
+
+
+@endif
+
@stop
@section('scripts')
@stop
\ No newline at end of file
diff --git a/app/views/migrate/index.blade.php b/app/views/migrate/index.blade.php
index d464ebe29c..c9985dd71c 100644
--- a/app/views/migrate/index.blade.php
+++ b/app/views/migrate/index.blade.php
@@ -3,50 +3,25 @@
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-12">
<h1>Firefly<br/>
- <small>Migration instructions</small>
+ <small>Migration</small>
</h1>
<ol>
- <li>Open <code>app/config/database.php</code></li>
- <li>Fill in the <code>old-firefly</code> connection records.</li>
- <li>Refresh this page.</li>
+ <li>Upload <code>firefly-export-****-**-**.json</code></li>
+ <li>Wait..</li>
+ <li>Done!</li>
</ol>
+
<p>
- It should look something like this:
- </p>
-<pre>
-return [
- 'fetch' => PDO::FETCH_CLASS,
- 'default' => 'mysql',
- 'connections' => [
- 'mysql' => [
- 'driver' => 'mysql',
- 'host' => 'localhost',
- 'database' => '(current database)',
- 'username' => '',
- 'password' => '',
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- ],
- <strong>
- 'old-firefly' => [
- 'driver' => 'mysql',
- 'host' => 'localhost',
- 'database' => '(previous database)',
- 'username' => '',
- 'password' => '',
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- ],</strong>
- ],
-</pre>
- <p>
- This page will disappear when the connection is valid.
- </p>
- <p>
- Current error: <code>{{$error or ''}}</code>
+ &nbsp;
</p>
+ {{Form::open(['files' => true])}}
+ <div class="form-group">
+ <label for="exportFile">Export file</label>
+ <input name="exportFile" type="file" id="exportFile">
+ <p class="help-block">Upload the export file here.</p>
+ </div>
+ <button type="submit" class="btn btn-info">Import</button>
+ {{Form::close()}}
</div>
</div>