diff --git a/app/lib/Firefly/Helper/Controllers/Account.php b/app/lib/Firefly/Helper/Controllers/Account.php index b789bc9749..a7fb95a02b 100644 --- a/app/lib/Firefly/Helper/Controllers/Account.php +++ b/app/lib/Firefly/Helper/Controllers/Account.php @@ -17,7 +17,7 @@ class Account implements AccountInterface public function openingBalanceTransaction(\Account $account) { return \TransactionJournal::withRelevantData() - ->account($account) + ->accountIs($account) ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') ->where('transaction_types.type', 'Opening balance') @@ -54,7 +54,7 @@ class Account implements AccountInterface // build a query: $query = \TransactionJournal::withRelevantData() ->defaultSorting() - ->account($account) + ->accountIs($account) ->after($start) ->before($end); // filter some: @@ -110,16 +110,16 @@ class Account implements AccountInterface // statistics (transactions) - $trIn = floatval(\Transaction::before($end)->after($start)->account($account)->moreThan(0) + $trIn = floatval(\Transaction::before($end)->after($start)->accountIs($account)->moreThan(0) ->transactionTypes(['Deposit', 'Withdrawal'])->sum('transactions.amount')); - $trOut = floatval(\Transaction::before($end)->after($start)->account($account)->lessThan(0) + $trOut = floatval(\Transaction::before($end)->after($start)->accountIs($account)->lessThan(0) ->transactionTypes(['Deposit', 'Withdrawal'])->sum('transactions.amount')); $trDiff = $trIn + $trOut; // statistics (transfers) - $trfIn = floatval(\Transaction::before($end)->after($start)->account($account)->moreThan(0) + $trfIn = floatval(\Transaction::before($end)->after($start)->accountIs($account)->moreThan(0) ->transactionTypes(['Transfer'])->sum('transactions.amount')); - $trfOut = floatval(\Transaction::before($end)->after($start)->account($account)->lessThan(0) + $trfOut = floatval(\Transaction::before($end)->after($start)->accountIs($account)->lessThan(0) ->transactionTypes(['Transfer'])->sum('transactions.amount')); $trfDiff = $trfIn + $trfOut; diff --git a/app/lib/Firefly/Helper/Toolkit/Toolkit.php b/app/lib/Firefly/Helper/Toolkit/Toolkit.php index 3dd5f6def5..5d7bfeef54 100644 --- a/app/lib/Firefly/Helper/Toolkit/Toolkit.php +++ b/app/lib/Firefly/Helper/Toolkit/Toolkit.php @@ -3,6 +3,8 @@ namespace Firefly\Helper\Toolkit; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; /** * Class Toolkit @@ -140,4 +142,41 @@ class Toolkit implements ToolkitInterface return $end; } + + /** + * Takes any collection and tries to make a sensible select list compatible array of it. + * + * @param Collection $set + * @param null $titleField + * + * @return mixed + */ + public function makeSelectList(Collection $set, $titleField = null) + { + $selectList = []; + /** @var Model $entry */ + foreach ($set as $entry) { + $id = intval($entry->id); + $title = null; + if (is_null($titleField)) { + // try 'title' field. + if (isset($entry->title)) { + $title = $entry->title; + } + // try 'name' field + if (is_null($title)) { + $title = $entry->name; + } + + // try 'description' field + if (is_null($title)) { + $title = $entry->description; + } + } else { + $title = $entry->$titleField; + } + $selectList[$id] = $title; + } + return $selectList; + } } \ No newline at end of file diff --git a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php index df754556b4..e496c17997 100644 --- a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php +++ b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php @@ -3,6 +3,7 @@ namespace Firefly\Helper\Toolkit; use Illuminate\Http\Request; +use Illuminate\Support\Collection; /** * Interface ToolkitInterface @@ -17,4 +18,14 @@ interface ToolkitInterface */ public function getDateRange(); + /** + * Takes any collection and tries to make a sensible select list compatible array of it. + * + * @param Collection $set + * @param null $titleField + * + * @return mixed + */ + public function makeSelectList(Collection $set, $titleField = null); + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php index 576b51fad1..288fb8ce7e 100644 --- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php @@ -16,14 +16,6 @@ interface AccountRepositoryInterface */ public function count(); - /** - * @param $name - * @param \AccountType $type - * - * @return mixed - */ - public function createOrFind($name, \AccountType $type); - /** * Gets a list of accounts that have the mentioned type. Will automatically convert * strings in this array to actual (model) account types. @@ -35,11 +27,11 @@ interface AccountRepositoryInterface public function getOfTypes(array $types); /** - * @param $name + * @param array $data * * @return mixed */ - public function createOrFindBeneficiary($name); + public function firstOrCreate(array $data); /** * @param \Account $account @@ -61,19 +53,6 @@ interface AccountRepositoryInterface */ public function findAccountType($type); - /** - * @param $name - * @param \AccountType $type - * @return mixed - */ - public function findByName($name, \AccountType $type = null); - - /** - * @param $name - * @return mixed - */ - public function findByNameAny($name); - /** * @return mixed */ @@ -84,16 +63,6 @@ interface AccountRepositoryInterface */ public function getActiveDefault(); - /** - * @return mixed - */ - public function getActiveDefaultAsSelectList(); - - /** - * @return mixed - */ - public function getBeneficiaries(); - /** * @param $ids * diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php index 87167e1ab5..968fa1b841 100644 --- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php +++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php @@ -15,6 +15,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface protected $_user = null; + + /** * */ @@ -23,6 +25,10 @@ class EloquentAccountRepository implements AccountRepositoryInterface $this->_user = \Auth::user(); } + public function firstOrCreate(array $data) { + return \Account::firstOrCreate($data); + } + /** * Gets a list of accounts that have the mentioned type. Will automatically convert * strings in this array to actual (model) account types. @@ -45,56 +51,56 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $this->_user->accounts()->count(); } - - /** - * @param $name - * - * @return \Account|mixed|null - */ - public function createOrFindBeneficiary($name) - { - if (is_null($name) || strlen($name) == 0) { - return null; - } - $type = \AccountType::where('type', 'Expense account')->first(); - return $this->createOrFind($name, $type); - } - - /** - * @param $name - * @param \AccountType $type - * - * @return \Account|mixed - */ - public function createOrFind($name, \AccountType $type = null) - { - $account = $this->findByName($name, $type); - if (!$account) { - $data = [ - 'name' => $name, - 'account_type' => $type - ]; - - return $this->store($data); - } - - return $account; - } - - /** - * @param $name - * @param \AccountType $type - * - * @return mixed - */ - public function findByName($name, \AccountType $type = null) - { - $type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type; - - return $this->_user->accounts()->where('account_type_id', $type->id) - ->where('name', 'like', '%' . $name . '%') - ->first(); - } +// +// /** +// * @param $name +// * +// * @return \Account|mixed|null +// */ +// public function createOrFindBeneficiary($name) +// { +// if (is_null($name) || strlen($name) == 0) { +// return null; +// } +// $type = \AccountType::where('type', 'Expense account')->first(); +// return $this->createOrFind($name, $type); +// } +// +// /** +// * @param $name +// * @param \AccountType $type +// * +// * @return \Account|mixed +// */ +// public function createOrFind($name, \AccountType $type = null) +// { +// $account = $this->findByName($name, $type); +// if (!$account) { +// $data = [ +// 'name' => $name, +// 'account_type' => $type +// ]; +// +// return $this->store($data); +// } +// +// return $account; +// } +// +// /** +// * @param $name +// * @param \AccountType $type +// * +// * @return mixed +// */ +// public function findByName($name, \AccountType $type = null) +// { +// $type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type; +// +// return $this->_user->accounts()->where('account_type_id', $type->id) +// ->where('name', 'like', '%' . $name . '%') +// ->first(); +// } /** * @param $data @@ -149,44 +155,44 @@ class EloquentAccountRepository implements AccountRepositoryInterface // whatever the result, return the account. return $account; } - - /** - * @param \Account $account - * @param int $amount - * @param Carbon $date - * - * @return bool - * @SuppressWarnings(PHPMD.CamelCaseMethodName) - */ - protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date) - { - // get account type: - $initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first(); - - // create new account: - $initial = new \Account; - $initial->accountType()->associate($initialBalanceAT); - $initial->user()->associate($this->_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->overruleUser($this->_user); - - $transactionJournal->createSimpleJournal( - $initial, $account, 'Initial Balance for ' . $account->name, $amount, $date - ); - - return true; - } - - return false; - } +// +// /** +// * @param \Account $account +// * @param int $amount +// * @param Carbon $date +// * +// * @return bool +// * @SuppressWarnings(PHPMD.CamelCaseMethodName) +// */ +// protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date) +// { +// // get account type: +// $initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first(); +// +// // create new account: +// $initial = new \Account; +// $initial->accountType()->associate($initialBalanceAT); +// $initial->user()->associate($this->_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->overruleUser($this->_user); +// +// $transactionJournal->createSimpleJournal( +// $initial, $account, 'Initial Balance for ' . $account->name, $amount, $date +// ); +// +// return true; +// } +// +// return false; +// } /** * @param \Account $account @@ -249,19 +255,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface return \AccountType::where('type', $type)->first(); } - /** - * Used for import - * - * @param $name - * - * @return mixed - */ - public function findByNameAny($name) - { - return $this->_user->accounts() - ->where('name', 'like', '%' . $name . '%') - ->first(); - } +// /** +// * Used for import +// * +// * @param $name +// * +// * @return mixed +// */ +// public function findByNameAny($name) +// { +// return $this->_user->accounts() +// ->where('name', 'like', '%' . $name . '%') +// ->first(); +// } /** * @return mixed @@ -271,19 +277,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get(); } - /** - * @return array|mixed - */ - public function getActiveDefaultAsSelectList() - { - $list = $this->getActiveDefault(); - $return = []; - foreach ($list as $entry) { - $return[intval($entry->id)] = $entry->name; - } - - return $return; - } +// /** +// * @return array|mixed +// */ +// public function getActiveDefaultAsSelectList() +// { +// $list = $this->getActiveDefault(); +// $return = []; +// foreach ($list as $entry) { +// $return[intval($entry->id)] = $entry->name; +// } +// +// return $return; +// } /** * @return mixed @@ -297,19 +303,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface ->get(['accounts.*']); } - /** - * @return mixed - */ - public function getBeneficiaries() - { - $list = $this->_user->accounts()->accountTypeIn(['Beneficiary account', 'Expense account'])->where( - 'accounts.active', 1 - )->orderBy( - 'accounts.name', 'ASC' - )->get(['accounts.*']); - - return $list; - } +// /** +// * @return mixed +// */ +// public function getBeneficiaries() +// { +// $list = $this->_user->accounts()->accountTypeIn(['Beneficiary account', 'Expense account'])->where( +// 'accounts.active', 1 +// )->orderBy( +// 'accounts.name', 'ASC' +// )->get(['accounts.*']); +// +// return $list; +// } public function getByAccountType(\AccountType $type) { diff --git a/app/models/Account.php b/app/models/Account.php index 846de07a47..2ed2106fdc 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -42,6 +42,8 @@ class Account extends Ardent ]; + protected $fillable = ['name','user_id','account_type_id','active']; + /** * Account type. * diff --git a/app/views/layouts/default.blade.php b/app/views/layouts/default.blade.php index 807711963e..1d8e51f57a 100644 --- a/app/views/layouts/default.blade.php +++ b/app/views/layouts/default.blade.php @@ -9,6 +9,9 @@ @if(isset($title) && $title != 'Firefly') // {{{$title}}} @endif + @if(isset($subTitle)) + // {{{$subTitle}}} + @endif @@ -28,9 +31,15 @@