Some small updates to various classes to support new stuff.

This commit is contained in:
James Cole 2014-09-12 17:34:54 +02:00
parent a1ba340ead
commit 0203fee174
8 changed files with 225 additions and 177 deletions

View File

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

View File

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

View File

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

View File

@ -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
*

View File

@ -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)
{

View File

@ -42,6 +42,8 @@ class Account extends Ardent
];
protected $fillable = ['name','user_id','account_type_id','active'];
/**
* Account type.
*

View File

@ -9,6 +9,9 @@
@if(isset($title) && $title != 'Firefly')
// {{{$title}}}
@endif
@if(isset($subTitle))
// {{{$subTitle}}}
@endif
</title>
<?php echo stylesheet_link_tag(); ?>
@ -28,9 +31,15 @@
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">{{$title or '(no title)'}}
<h1 class="page-header">
{{$title or '(no title)'}}
@if(isset($subTitle))
<small>{{$subTitle}}</small>
<small>
@if(isset($titleIcon))
<i class="fa {{{$titleIcon}}}"></i>
@endif
{{$subTitle}}
</small>
@endif
</h1>
</div>

View File

@ -81,19 +81,27 @@
<a href="#"><i class="fa fa-tags fa-fw"></i> Tags</a>
</li>
<li>
<a href="#"><i class="fa fa-tags fa-fw"></i> Reports</a>
<a href="#"><i class="fa fa-line-chart fa-fw"></i> Reports</a>
</li>
<li>
<li
@if(
!(strpos($r,'transactions.expenses') === false) ||
!(strpos($r,'transactions.revenue') === false) ||
!(strpos($r,'transactions.transfers') === false)
)
class="active"
@endif
>
<a href="{{route('transactions.index')}}"><i class="fa fa-repeat fa-fw"></i> Transactions<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="#"><i class="fa fa-long-arrow-left fa-fw"></i> Expenses</a>
<a @if($r == 'transactions.expenses') class="active" @endif href="{{route('transactions.expenses')}}"><i class="fa fa-long-arrow-left fa-fw"></i> Expenses</a>
</li>
<li>
<a href="#"><i class="fa fa-long-arrow-right fa-fw"></i> Revenue / income</a>
<a @if($r == 'transactions.revenue') class="active" @endif href="{{route('transactions.revenue')}}"><i class="fa fa-long-arrow-right fa-fw"></i> Revenue / income</a>
</li>
<li>
<a href="#"><i class="fa fa-arrows-h fa-fw"></i> Transfers</a>
<a @if($r == 'transactions.transfers') class="active" @endif href="{{route('transactions.transfers')}}"><i class="fa fa-arrows-h fa-fw"></i> Transfers</a>
</li>
</ul>
@ -113,17 +121,21 @@
</ul>
<!-- /.nav-second-level -->
</li>
<li>
<li
@if( !(strpos($r,'transactions.create') === false) )
class="active"
@endif
>
<a href="#"><i class="fa fa-plus fa-fw"></i> Create new<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="{{route('transactions.create','withdrawal')}}"><i class="fa fa-long-arrow-left fa-fw"></i> Withdrawal</a>
<a @if($r == 'transactions.create' && isset($what) && $what == 'withdrawal') class="active" @endif href="{{route('transactions.create','withdrawal')}}"><i class="fa fa-long-arrow-left fa-fw"></i> Withdrawal</a>
</li>
<li>
<a href="{{route('transactions.create','withdrawal')}}"><i class="fa fa-long-arrow-right fa-fw"></i> Deposit</a>
<a @if($r == 'transactions.create' && isset($what) && $what == 'deposit') class="active" @endif href="{{route('transactions.create','deposit')}}"><i class="fa fa-long-arrow-right fa-fw"></i> Deposit</a>
</li>
<li>
<a href="{{route('transactions.create','transfer')}}"><i class="fa fa-arrows-h fa-fw"></i> Transfer</a>
<a @if($r == 'transactions.create' && isset($what) && $what == 'transfer') class="active" @endif href="{{route('transactions.create','transfer')}}"><i class="fa fa-arrows-h fa-fw"></i> Transfer</a>
</li>
<!--
<li>