firefly-iii/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php

400 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace Firefly\Storage\Account;
use Carbon\Carbon;
/**
* Class EloquentAccountRepository
*
* @package Firefly\Storage\Account
*/
class EloquentAccountRepository implements AccountRepositoryInterface
{
2014-09-02 10:27:28 -05:00
protected $_user = null;
/**
*
*/
public function __construct()
{
2014-09-02 10:27:28 -05:00
$this->_user = \Auth::user();
}
/**
* @return mixed
*/
public function count()
{
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->count();
}
/**
* @param $name
* @param \AccountType $type
*
* @return \Account|mixed
*/
2014-08-05 13:07:02 -05:00
public function createOrFind($name, \AccountType $type = null)
{
2014-08-05 13:07:02 -05:00
$account = $this->findByName($name, $type);
if (!$account) {
$data = [
2014-08-28 00:53:54 -05:00
'name' => $name,
'account_type' => $type
];
2014-07-14 23:58:08 -05:00
return $this->store($data);
}
2014-08-05 13:07:02 -05:00
return $account;
2014-07-14 23:58:08 -05:00
}
/**
* @param $name
*
* @return \Account|mixed|null
*/
public function createOrFindBeneficiary($name)
{
if (is_null($name) || strlen($name) == 0) {
return null;
}
2014-08-30 07:26:33 -05:00
$type = \AccountType::where('type', 'Beneficiary account')->first();
return $this->createOrFind($name, $type);
}
2014-08-10 08:01:46 -05:00
/**
* @param \Account $account
*
* @return bool|mixed
*/
public function destroy(\Account $account)
{
2014-08-30 07:26:33 -05:00
// find all transaction journals related to this account:
$journals = \TransactionJournal::withRelevantData()->account($account)->get(['transaction_journals.*']);
$accountIDs = [];
/** @var \TransactionJournal $journal */
foreach ($journals as $journal) {
2014-08-30 07:26:33 -05:00
// remember the account id's of the transactions involved:
foreach ($journal->transactions as $t) {
$accountIDs[] = $t->account_id;
}
$journal->delete();
}
2014-08-30 07:26:33 -05:00
$accountIDs = array_unique($accountIDs);
if (count($accountIDs) > 0) {
// find the "initial balance" type accounts in this list. Should be just 1.
2014-09-02 10:27:28 -05:00
$query = $this->_user->accounts()->accountTypeIn(['Initial balance account'])
->whereIn('accounts.id', $accountIDs);
2014-08-30 07:26:33 -05:00
if ($query->count() == 1) {
$iba = $query->first(['accounts.*']);
$iba->delete();
}
}
$account->delete();
/**
*
* TODO
* Also delete: initial balance, initial balance account, and transactions
*/
return true;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->where('id', $accountId)->first();
}
/**
* @param $type
* @return mixed
*/
public function findAccountType($type)
{
return \AccountType::where('type', $type)->first();
}
/**
2014-08-10 08:01:46 -05:00
* @param $name
* @param \AccountType $type
*
* @return mixed
*/
2014-08-05 13:07:02 -05:00
public function findByName($name, \AccountType $type = null)
{
2014-08-30 07:26:33 -05:00
$type = is_null($type) ? \AccountType::where('type', 'Default account')->first() : $type;
2014-08-10 04:30:14 -05:00
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->where('account_type_id', $type->id)
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
2014-09-02 10:27:28 -05:00
* Used for import
*
* @param $name
*
* @return mixed
*/
2014-09-02 10:27:28 -05:00
public function findByNameAny($name)
{
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
* @return mixed
*/
2014-09-02 10:27:28 -05:00
public function get()
{
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
/**
* @return mixed
*/
2014-09-02 10:27:28 -05:00
public function getActiveDefault()
{
return $this->_user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.type', 'Default account')->where('accounts.active', 1)
->get(['accounts.*']);
}
/**
* @return array|mixed
*/
2014-07-14 23:58:08 -05:00
public function getActiveDefaultAsSelectList()
{
2014-09-02 10:27:28 -05:00
$list = $this->_user->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
2014-07-14 23:58:08 -05:00
)
2014-09-02 10:27:28 -05:00
->where('account_types.type', 'Default account')->where('accounts.active', 1)
2014-07-14 23:58:08 -05:00
2014-09-02 10:27:28 -05:00
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
2014-07-14 23:58:08 -05:00
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
2014-07-14 23:58:08 -05:00
return $return;
}
/**
* @return mixed
*/
public function getBeneficiaries()
{
2014-09-02 10:27:28 -05:00
$list = $this->_user->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
2014-09-02 10:27:28 -05:00
->where('account_types.type', 'Beneficiary account')->where('accounts.active', 1)
2014-09-02 10:27:28 -05:00
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
return $list;
}
public function getByAccountType(\AccountType $type)
{
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')
->where('account_type_id', $type->id)->get();
}
/**
* @param $ids
*
* @return array|mixed
*/
public function getByIds(array $ids)
{
if (count($ids) > 0) {
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
} else {
return $this->getActiveDefault();
}
}
/**
* @return mixed
*/
public function getCashAccount()
{
2014-08-30 07:26:33 -05:00
$type = \AccountType::where('type', 'Cash account')->first();
2014-09-02 10:27:28 -05:00
$cash = $this->_user->accounts()->where('account_type_id', $type->id)->first();
if (is_null($cash)) {
$cash = new \Account;
$cash->accountType()->associate($type);
2014-09-02 10:27:28 -05:00
$cash->user()->associate($this->_user);
$cash->name = 'Cash account';
$cash->active = 1;
$cash->save();
}
return $cash;
}
2014-06-30 08:57:05 -05:00
/**
* @return mixed
*/
public function getDefault()
{
2014-09-02 10:27:28 -05:00
return $this->_user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.type', 'Default account')
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
2014-09-02 10:27:28 -05:00
/**
* @param \User $user
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
2014-06-30 08:57:05 -05:00
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
{
2014-08-30 07:26:33 -05:00
/**
* If the AccountType has been passed through, use it:
*/
if (isset($data['account_type']) && is_object($data['account_type'])
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
$accountType = \AccountType::where('type', $data['account_type'])->first();
2014-08-30 07:26:33 -05:00
} else {
$accountType = \AccountType::where('type', 'Default account')->first();
}
2014-06-30 08:57:05 -05:00
2014-08-30 07:26:33 -05:00
/**
* Create new account:
*/
$account = new \Account;
$account->accountType()->associate($accountType);
2014-09-02 10:27:28 -05:00
$account->user()->associate($this->_user);
$account->name = $data['name'];
$account->active
2014-08-30 07:26:33 -05:00
= 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']);
2014-08-30 07:26:33 -05:00
$date = new Carbon($data['openingbalancedate']);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
// whatever the result, return the account.
return $account;
}
/**
2014-08-10 08:01:46 -05:00
* @param \Account $account
* @param $data
*
2014-08-10 08:01:46 -05:00
* @return \Account|mixed
*/
public function update(\Account $account, $data)
{
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
if (floatval($data['openingbalance']) != 0) {
/** @var \Firefly\Helper\Controllers\AccountInterface $interface */
$interface = \App::make('Firefly\Helper\Controllers\AccountInterface');
2014-08-30 07:26:33 -05:00
if ($account->accounttype->type == 'Default account') {
$journal = $interface->openingBalanceTransaction($account);
if ($journal) {
2014-08-30 07:26:33 -05:00
$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;
}
/**
* @param \Account $account
2014-08-30 07:26:33 -05:00
* @param int $amount
* @param Carbon $date
*
* @return bool
2014-08-10 11:22:42 -05:00
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
// get account type:
2014-08-30 07:26:33 -05:00
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
2014-09-02 10:27:28 -05:00
$initial->user()->associate($this->_user);
2014-08-30 07:26:33 -05:00
$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(
2014-08-30 07:26:33 -05:00
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->overruleUser($this->_user);
$transactionJournal->createSimpleJournal(
2014-08-30 07:26:33 -05:00
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
}