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) public function openingBalanceTransaction(\Account $account)
{ {
return \TransactionJournal::withRelevantData() return \TransactionJournal::withRelevantData()
->account($account) ->accountIs($account)
->leftJoin('transaction_types', 'transaction_types.id', '=', ->leftJoin('transaction_types', 'transaction_types.id', '=',
'transaction_journals.transaction_type_id') 'transaction_journals.transaction_type_id')
->where('transaction_types.type', 'Opening balance') ->where('transaction_types.type', 'Opening balance')
@ -54,7 +54,7 @@ class Account implements AccountInterface
// build a query: // build a query:
$query = \TransactionJournal::withRelevantData() $query = \TransactionJournal::withRelevantData()
->defaultSorting() ->defaultSorting()
->account($account) ->accountIs($account)
->after($start) ->after($start)
->before($end); ->before($end);
// filter some: // filter some:
@ -110,16 +110,16 @@ class Account implements AccountInterface
// statistics (transactions) // 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')); ->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')); ->transactionTypes(['Deposit', 'Withdrawal'])->sum('transactions.amount'));
$trDiff = $trIn + $trOut; $trDiff = $trIn + $trOut;
// statistics (transfers) // 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')); ->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')); ->transactionTypes(['Transfer'])->sum('transactions.amount'));
$trfDiff = $trfIn + $trfOut; $trfDiff = $trfIn + $trfOut;

View File

@ -3,6 +3,8 @@
namespace Firefly\Helper\Toolkit; namespace Firefly\Helper\Toolkit;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/** /**
* Class Toolkit * Class Toolkit
@ -140,4 +142,41 @@ class Toolkit implements ToolkitInterface
return $end; 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; namespace Firefly\Helper\Toolkit;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Collection;
/** /**
* Interface ToolkitInterface * Interface ToolkitInterface
@ -17,4 +18,14 @@ interface ToolkitInterface
*/ */
public function getDateRange(); 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(); 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 * Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types. * strings in this array to actual (model) account types.
@ -35,11 +27,11 @@ interface AccountRepositoryInterface
public function getOfTypes(array $types); public function getOfTypes(array $types);
/** /**
* @param $name * @param array $data
* *
* @return mixed * @return mixed
*/ */
public function createOrFindBeneficiary($name); public function firstOrCreate(array $data);
/** /**
* @param \Account $account * @param \Account $account
@ -61,19 +53,6 @@ interface AccountRepositoryInterface
*/ */
public function findAccountType($type); 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 * @return mixed
*/ */
@ -84,16 +63,6 @@ interface AccountRepositoryInterface
*/ */
public function getActiveDefault(); public function getActiveDefault();
/**
* @return mixed
*/
public function getActiveDefaultAsSelectList();
/**
* @return mixed
*/
public function getBeneficiaries();
/** /**
* @param $ids * @param $ids
* *

View File

@ -15,6 +15,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface
protected $_user = null; protected $_user = null;
/** /**
* *
*/ */
@ -23,6 +25,10 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$this->_user = \Auth::user(); $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 * Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types. * strings in this array to actual (model) account types.
@ -45,56 +51,56 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $this->_user->accounts()->count(); return $this->_user->accounts()->count();
} }
//
/** // /**
* @param $name // * @param $name
* // *
* @return \Account|mixed|null // * @return \Account|mixed|null
*/ // */
public function createOrFindBeneficiary($name) // public function createOrFindBeneficiary($name)
{ // {
if (is_null($name) || strlen($name) == 0) { // if (is_null($name) || strlen($name) == 0) {
return null; // return null;
} // }
$type = \AccountType::where('type', 'Expense account')->first(); // $type = \AccountType::where('type', 'Expense account')->first();
return $this->createOrFind($name, $type); // return $this->createOrFind($name, $type);
} // }
//
/** // /**
* @param $name // * @param $name
* @param \AccountType $type // * @param \AccountType $type
* // *
* @return \Account|mixed // * @return \Account|mixed
*/ // */
public function createOrFind($name, \AccountType $type = null) // public function createOrFind($name, \AccountType $type = null)
{ // {
$account = $this->findByName($name, $type); // $account = $this->findByName($name, $type);
if (!$account) { // if (!$account) {
$data = [ // $data = [
'name' => $name, // 'name' => $name,
'account_type' => $type // 'account_type' => $type
]; // ];
//
return $this->store($data); // return $this->store($data);
} // }
//
return $account; // return $account;
} // }
//
/** // /**
* @param $name // * @param $name
* @param \AccountType $type // * @param \AccountType $type
* // *
* @return mixed // * @return mixed
*/ // */
public function findByName($name, \AccountType $type = null) // public function findByName($name, \AccountType $type = null)
{ // {
$type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type; // $type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type;
//
return $this->_user->accounts()->where('account_type_id', $type->id) // return $this->_user->accounts()->where('account_type_id', $type->id)
->where('name', 'like', '%' . $name . '%') // ->where('name', 'like', '%' . $name . '%')
->first(); // ->first();
} // }
/** /**
* @param $data * @param $data
@ -149,44 +155,44 @@ class EloquentAccountRepository implements AccountRepositoryInterface
// whatever the result, return the account. // whatever the result, return the account.
return $account; return $account;
} }
//
/** // /**
* @param \Account $account // * @param \Account $account
* @param int $amount // * @param int $amount
* @param Carbon $date // * @param Carbon $date
* // *
* @return bool // * @return bool
* @SuppressWarnings(PHPMD.CamelCaseMethodName) // * @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/ // */
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date) // protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{ // {
// get account type: // // get account type:
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first(); // $initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
//
// create new account: // // create new account:
$initial = new \Account; // $initial = new \Account;
$initial->accountType()->associate($initialBalanceAT); // $initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate($this->_user); // $initial->user()->associate($this->_user);
$initial->name = $account->name . ' initial balance'; // $initial->name = $account->name . ' initial balance';
$initial->active = 0; // $initial->active = 0;
if ($initial->validate()) { // if ($initial->validate()) {
$initial->save(); // $initial->save();
// create new transaction journal (and transactions): // // create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */ // /** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make( // $transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface' // 'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
); // );
$transactionJournal->overruleUser($this->_user); // $transactionJournal->overruleUser($this->_user);
//
$transactionJournal->createSimpleJournal( // $transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date // $initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
); // );
//
return true; // return true;
} // }
//
return false; // return false;
} // }
/** /**
* @param \Account $account * @param \Account $account
@ -249,19 +255,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \AccountType::where('type', $type)->first(); return \AccountType::where('type', $type)->first();
} }
/** // /**
* Used for import // * Used for import
* // *
* @param $name // * @param $name
* // *
* @return mixed // * @return mixed
*/ // */
public function findByNameAny($name) // public function findByNameAny($name)
{ // {
return $this->_user->accounts() // return $this->_user->accounts()
->where('name', 'like', '%' . $name . '%') // ->where('name', 'like', '%' . $name . '%')
->first(); // ->first();
} // }
/** /**
* @return mixed * @return mixed
@ -271,19 +277,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get(); return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
} }
/** // /**
* @return array|mixed // * @return array|mixed
*/ // */
public function getActiveDefaultAsSelectList() // public function getActiveDefaultAsSelectList()
{ // {
$list = $this->getActiveDefault(); // $list = $this->getActiveDefault();
$return = []; // $return = [];
foreach ($list as $entry) { // foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name; // $return[intval($entry->id)] = $entry->name;
} // }
//
return $return; // return $return;
} // }
/** /**
* @return mixed * @return mixed
@ -297,19 +303,19 @@ class EloquentAccountRepository implements AccountRepositoryInterface
->get(['accounts.*']); ->get(['accounts.*']);
} }
/** // /**
* @return mixed // * @return mixed
*/ // */
public function getBeneficiaries() // public function getBeneficiaries()
{ // {
$list = $this->_user->accounts()->accountTypeIn(['Beneficiary account', 'Expense account'])->where( // $list = $this->_user->accounts()->accountTypeIn(['Beneficiary account', 'Expense account'])->where(
'accounts.active', 1 // 'accounts.active', 1
)->orderBy( // )->orderBy(
'accounts.name', 'ASC' // 'accounts.name', 'ASC'
)->get(['accounts.*']); // )->get(['accounts.*']);
//
return $list; // return $list;
} // }
public function getByAccountType(\AccountType $type) 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. * Account type.
* *

View File

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

View File

@ -81,19 +81,27 @@
<a href="#"><i class="fa fa-tags fa-fw"></i> Tags</a> <a href="#"><i class="fa fa-tags fa-fw"></i> Tags</a>
</li> </li>
<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> <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> <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"> <ul class="nav nav-second-level">
<li> <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>
<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>
<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> </li>
</ul> </ul>
@ -113,17 +121,21 @@
</ul> </ul>
<!-- /.nav-second-level --> <!-- /.nav-second-level -->
</li> </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> <a href="#"><i class="fa fa-plus fa-fw"></i> Create new<span class="fa arrow"></span></a>
<ul class="nav nav-second-level"> <ul class="nav nav-second-level">
<li> <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>
<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>
<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>
<!-- <!--
<li> <li>