First attempt at an import/migration procedure.

This commit is contained in:
James Cole
2014-07-03 21:31:32 +02:00
parent 35c90a32e9
commit 1836249407
13 changed files with 259 additions and 89 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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'
);
}
}

View File

@@ -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!';
}
}

View File

@@ -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);
}