Some more work done. Mainly accounts. [skip ci]

This commit is contained in:
James Cole
2014-07-26 08:05:02 +02:00
parent 30d5b88769
commit d088b2c558
25 changed files with 932 additions and 282 deletions

View File

@@ -4,8 +4,6 @@
namespace Firefly\Storage\Account;
use Carbon\Carbon;
use Firefly\Exception\FireflyException;
use Illuminate\Database\QueryException;
/**
* Class EloquentAccountRepository
@@ -14,8 +12,6 @@ use Illuminate\Database\QueryException;
*/
class EloquentAccountRepository implements AccountRepositoryInterface
{
public $validator;
/**
*
*/
@@ -45,16 +41,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $list;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return \Auth::user()->accounts()->where('id', $accountId)->first();
}
/**
* @param $ids
*
@@ -118,73 +104,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
}
/**
* @param $data
* @param Carbon $date
* @param int $amount
*
* @return \Account|mixed
* @throws \Firefly\Exception\FireflyException
*/
public function storeWithInitialBalance($data, 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;
try {
$initial->save();
} catch (QueryException $e) {
\Log::error('DB ERROR: ' . $e->getMessage());
throw new FireflyException('Could not save counterbalance account for ' . $data['name']);
}
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $data['name'], $amount, $date
);
return $account;
}
/**
* @param $data
*
* @return \Account|mixed
* @throws \Firefly\Exception\FireflyException
*/
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;
try {
$account->save();
} catch (QueryException $e) {
\Log::error('DB ERROR: ' . $e->getMessage());
throw new FireflyException('Could not save account ' . $data['name']);
}
return $account;
}
/**
* @param $name
*
@@ -210,7 +129,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$beneficiary = $this->findByName($name);
if (!$beneficiary) {
$data = [
'name' => $name,
'name' => $name,
'account_type' => $type
];
return $this->store($data);
@@ -228,6 +147,142 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
}
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
{
$defaultAccountType = \AccountType::where('description', 'Default account')->first();
$accountType = isset($data['account_type']) ? $data['account_type'] : $defaultAccountType;
// create Account:
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate(\Auth::user());
$account->name = $data['name'];
$account->active
= isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
// try to save it:
if ($account->save()) {
// create initial balance, if necessary:
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
$this->_createInitialBalance($account, $amount, $date);
}
}
// whatever the result, return the account.
return $account;
}
/**
* @param \Account $account
* @param int $amount
* @param Carbon $date
*
* @return bool
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
// get account type:
$initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate(\Auth::user());
$initial->name = $account->name . ' initial balance';
$initial->active = 0;
if ($initial->validate()) {
$initial->save();
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
/**
* @param $data
*
* @return \Account|void
*/
public function update($data)
{
$account = $this->find($data['id']);
if ($account) {
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
if ($account->accounttype->description == 'Default account') {
$journal = $this->findOpeningBalanceTransaction($account);
$journal->date = new Carbon($data['openingbalancedate']);
$journal->transactions[0]->amount = floatval($data['openingbalance']) * -1;
$journal->transactions[1]->amount = floatval($data['openingbalance']);
$journal->transactions[0]->save();
$journal->transactions[1]->save();
$journal->save();
}
}
return $account;
}
public function destroy($accountId) {
$account = $this->find($accountId);
if($account) {
$account->delete();
return true;
}
return false;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return \Auth::user()->accounts()->where('id', $accountId)->first();
}
/**
* @param \Account $account
*
* @return mixed|void
*/
public function findOpeningBalanceTransaction(\Account $account)
{
$transactionType = \TransactionType::where('type', 'Opening balance')->first();
$journal = \TransactionJournal::
with(
['transactions' => function ($q) {
$q->orderBy('amount', 'ASC');
}]
)->where('transaction_type_id', $transactionType->id)
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transactions.account_id', $account->id)->first(['transaction_journals.*']);
return $journal;
}
/**
* @return mixed
*/