From 18362494073c22700d8516d7225298ccbb16ecc6 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 3 Jul 2014 21:31:32 +0200 Subject: [PATCH] First attempt at an import/migration procedure. --- app/config/testing/auth.php | 2 +- app/controllers/HomeController.php | 5 +- ...4620_create_transaction_journals_table.php | 1 + .../Account/AccountRepositoryInterface.php | 3 +- .../Account/EloquentAccountRepository.php | 76 ++++++------ .../Storage/StorageServiceProvider.php | 4 + .../EloquentTransactionJournalRepository.php | 114 ++++++++++++++++++ .../TransactionJournalInterface.php | 16 +++ app/models/Transaction.php | 10 +- app/models/TransactionJournal.php | 26 +++- app/routes.php | 6 +- app/views/index.blade.php | 32 ++++- app/views/migrate/index.blade.php | 53 +++----- 13 files changed, 259 insertions(+), 89 deletions(-) create mode 100644 app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php create mode 100644 app/lib/Firefly/Storage/TransactionJournal/TransactionJournalInterface.php 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 @@ 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 @@ 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() { + public function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0) + { + + $account = $this->store($data); + + $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(); + + // 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 + ); - $default = \AccountType::where('description','Default account')->first(); - $balanceAT = \AccountType::where('description','Initial balance account')->first(); + return $account; - $account = new \Account; - $account->active = true; - - $account->user()->associate(\Auth::user()); - $account->name = \Input::get('name'); - $account->accountType()->associate($default); - - if(!$account->isValid()) { - \Log::error('Could not create account: ' . $account->validator->messages()->first()); - $this->validator = $account->validator; - return false; - } - - $account->save(); - - $balance = floatval(\Input::get('openingbalance')); - if($balance != 0.00) { - // create account - $initial = new \Account; - $account->active = false; - - $account->user()->associate(\Auth::user()); - $account->name = \Input::get('name').' initial balance'; - $account->accountType()->associate($balanceAT); - $account->save(); - - // create journal (access helper!) - - - // create journal - - // create transaction - - // create - } } + public function store($data) + { + $defaultAT = \AccountType::where('description', 'Default account')->first(); + + $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 @@ +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 @@ + '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 @@ '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')
-

Firefly
+

Firefly + @if($count > 0) +
What's playing? + @endif

+@if($count == 0) +
+
+

Welcome to Firefly III.

+

+ To get get started, choose below: +

+
+
+
+
+

Migrate from Firefly II

+

+ Use this option if you have a JSON file from your current Firefly II installation. +

+
+
+

Start from scratch

+

+ Use this option if you are new to Firefly (III). +

+
+@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 @@

Firefly
- Migration instructions + Migration

    -
  1. Open app/config/database.php
  2. -
  3. Fill in the old-firefly connection records.
  4. -
  5. Refresh this page.
  6. +
  7. Upload firefly-export-****-**-**.json
  8. +
  9. Wait..
  10. +
  11. Done!
+

- It should look something like this: -

-
-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'    => '',
-        ],
-        
-        'old-firefly'  => [
-            'driver'    => 'mysql',
-            'host'      => 'localhost',
-            'database'  => '(previous database)',
-            'username'  => '',
-            'password'  => '',
-            'charset'   => 'utf8',
-            'collation' => 'utf8_unicode_ci',
-            'prefix'    => '',
-        ],
-    ],
-
-

- This page will disappear when the connection is valid. -

-

- Current error: {{$error or ''}} +  

+ {{Form::open(['files' => true])}} +
+ + +

Upload the export file here.

+
+ + {{Form::close()}}