2014-06-30 00:26:38 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Firefly\Storage\Account;
|
|
|
|
|
2014-07-05 09:19:15 -05:00
|
|
|
use Firefly\Helper\MigrationException;
|
|
|
|
|
2014-06-30 00:26:38 -05:00
|
|
|
class EloquentAccountRepository implements AccountRepositoryInterface
|
|
|
|
{
|
2014-06-30 08:46:12 -05:00
|
|
|
public $validator;
|
|
|
|
|
2014-06-30 00:26:38 -05:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:44:26 -05:00
|
|
|
public function get() {
|
|
|
|
return \Auth::user()->accounts()->get();
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
public function count()
|
|
|
|
{
|
2014-06-30 00:26:38 -05:00
|
|
|
return \Auth::user()->accounts()->count();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
public function storeWithInitialBalance($data, \Carbon\Carbon $date, $amount = 0)
|
|
|
|
{
|
2014-06-30 08:57:05 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$account = $this->store($data);
|
2014-06-30 08:57:05 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$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;
|
2014-07-05 09:19:15 -05:00
|
|
|
try {
|
|
|
|
$initial->save();
|
|
|
|
} catch (\Illuminate\Database\QueryException $e) {
|
|
|
|
\Log::error('DB ERROR: ' . $e->getMessage());
|
|
|
|
throw new FireflyException('Could not save counterbalance account for ' . $data['name']);
|
|
|
|
}
|
2014-06-30 08:46:12 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
// create new transaction journal (and transactions):
|
2014-07-05 09:19:15 -05:00
|
|
|
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
|
|
|
|
$transactionJournal = \App::make('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$transactionJournal->createSimpleJournal(
|
|
|
|
$initial, $account, 'Initial Balance for ' . $data['name'], $amount, $date
|
|
|
|
);
|
2014-06-30 08:57:05 -05:00
|
|
|
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
return $account;
|
2014-06-30 08:57:05 -05:00
|
|
|
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
}
|
2014-06-30 08:57:05 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
public function store($data)
|
|
|
|
{
|
|
|
|
$defaultAT = \AccountType::where('description', 'Default account')->first();
|
2014-06-30 08:57:05 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$at = isset($data['account_type']) ? $data['account_type'] : $defaultAT;
|
2014-06-30 08:57:05 -05:00
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
$account = new \Account;
|
|
|
|
$account->accountType()->associate($at);
|
|
|
|
$account->user()->associate(\Auth::user());
|
|
|
|
$account->name = $data['name'];
|
|
|
|
$account->active = isset($data['active']) ? $data['active'] : 1;
|
2014-07-05 09:19:15 -05:00
|
|
|
try {
|
|
|
|
$account->save();
|
|
|
|
} catch (\Illuminate\Database\QueryException $e) {
|
|
|
|
\Log::error('DB ERROR: ' . $e->getMessage());
|
|
|
|
throw new \Firefly\Exception\FireflyException('Could not save account ' . $data['name']);
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:31:32 -05:00
|
|
|
return $account;
|
2014-06-30 08:46:12 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 00:26:38 -05:00
|
|
|
}
|