Cleaned up a lot of source, finished the ChartController. [skip ci]

This commit is contained in:
James Cole
2014-07-29 19:37:52 +02:00
parent 9054b20700
commit 9af2b47463
20 changed files with 450 additions and 347 deletions

View File

@@ -77,7 +77,7 @@ interface AccountRepositoryInterface
*
* @return mixed
*/
public function getByIds($ids);
public function getByIds(array $ids);
/**
* @return mixed

View File

@@ -19,6 +19,83 @@ class EloquentAccountRepository implements AccountRepositoryInterface
{
}
/**
* @return mixed
*/
public function count()
{
return \Auth::user()->accounts()->count();
}
/**
* @param $name
* @param \AccountType $type
*
* @return \Account|mixed
*/
public function createOrFind($name, \AccountType $type)
{
$beneficiary = $this->findByName($name);
if (!$beneficiary) {
$data = [
'name' => $name,
'account_type' => $type
];
return $this->store($data);
}
return $beneficiary;
}
/**
* @param $name
*
* @return \Account|mixed|null
*/
public function createOrFindBeneficiary($name)
{
if (is_null($name) || strlen($name) == 0) {
return null;
}
$type = \AccountType::where('description', 'Beneficiary account')->first();
return $this->createOrFind($name, $type);
}
public function destroy($accountId)
{
$account = $this->find($accountId);
if ($account) {
$account->delete();
return true;
}
return false;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return \Auth::user()->accounts()->where('id', $accountId)->first();
}
/**
* @param $name
*
* @return mixed
*/
public function findByName($name)
{
return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
}
/**
* @return mixed
*/
@@ -27,45 +104,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \Auth::user()->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
/**
* @return mixed
*/
public function getBeneficiaries()
{
$list = \Auth::user()->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
->where('account_types.description', 'Beneficiary account')->where('accounts.active', 1)
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
return $list;
}
/**
* @param $ids
*
* @return array|mixed
*/
public function getByIds($ids)
{
if (count($ids) > 0) {
return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
} else {
return [];
}
}
/**
* @return mixed
*/
public function getDefault()
{
return \Auth::user()->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.description', 'Default account')
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
/**
* @return mixed
*/
@@ -92,59 +130,60 @@ class EloquentAccountRepository implements AccountRepositoryInterface
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
return $return;
}
/**
* @return mixed
*/
public function count()
public function getBeneficiaries()
{
return \Auth::user()->accounts()->count();
$list = \Auth::user()->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
->where('account_types.description', 'Beneficiary account')->where('accounts.active', 1)
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
return $list;
}
/**
* @param $name
* @param $ids
*
* @return \Account|mixed|null
* @return array|mixed
*/
public function createOrFindBeneficiary($name)
public function getByIds(array $ids)
{
if (is_null($name) || strlen($name) == 0) {
return null;
if (count($ids) > 0) {
return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
} else {
return $this->getActiveDefault();
}
$type = \AccountType::where('description', 'Beneficiary account')->first();
return $this->createOrFind($name, $type);
}
/**
* @param $name
* @param \AccountType $type
*
* @return \Account|mixed
*/
public function createOrFind($name, \AccountType $type)
{
$beneficiary = $this->findByName($name);
if (!$beneficiary) {
$data = [
'name' => $name,
'account_type' => $type
];
return $this->store($data);
}
return $beneficiary;
}
/**
* @param $name
*
* @return mixed
*/
public function findByName($name)
public function getCashAccount()
{
return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
$type = \AccountType::where('description', 'Cash account')->first();
$cash = \Auth::user()->accounts()->where('account_type_id', $type->id)->first();
return $cash;
}
/**
* @return mixed
*/
public function getDefault()
{
return \Auth::user()->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.description', 'Default account')
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
/**
@@ -183,6 +222,35 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
/**
* @param $data
*
* @return \Account|void
*/
public function update($data)
{
$account = $this->find($data['id']);
if ($account) {
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
if ($account->accounttype->description == 'Default account') {
$journal = $this->findOpeningBalanceTransaction($account);
$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
* @param int $amount
@@ -212,67 +280,11 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
/**
* @param $data
*
* @return \Account|void
*/
public function update($data)
{
$account = $this->find($data['id']);
if ($account) {
// update account accordingly:
$account->name = $data['name'];
if ($account->validate()) {
$account->save();
}
// update initial balance if necessary:
if ($account->accounttype->description == 'Default account') {
$journal = $this->findOpeningBalanceTransaction($account);
$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;
}
public function destroy($accountId) {
$account = $this->find($accountId);
if($account) {
$account->delete();
return true;
}
return false;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return \Auth::user()->accounts()->where('id', $accountId)->first();
}
/**
* @return mixed
*/
public function getCashAccount()
{
$type = \AccountType::where('description', 'Cash account')->first();
$cash = \Auth::user()->accounts()->where('account_type_id', $type->id)->first();
return $cash;
}
}

View File

@@ -23,6 +23,7 @@ interface BudgetRepositoryInterface
/**
* @param $data
*
* @return mixed
*/
public function update($data);
@@ -36,6 +37,7 @@ interface BudgetRepositoryInterface
/**
* @param $data
*
* @return mixed
*/
public function destroy($data);

View File

@@ -29,7 +29,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function get()
{
$set = \Auth::user()->budgets()->with(
['limits' => function ($q) {
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
@@ -48,9 +48,11 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
/**
* @param $data
*
* @return mixed
*/
public function update($data) {
public function update($data)
{
$budget = $this->find($data['id']);
if ($budget) {
// update account accordingly:
@@ -59,15 +61,19 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->save();
}
}
return $budget;
}
public function destroy($budgetId) {
public function destroy($budgetId)
{
$budget = $this->find($budgetId);
if($budget) {
if ($budget) {
$budget->delete();
return true;
}
return false;
}
@@ -96,41 +102,8 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function getWithRepetitionsInPeriod(Carbon $date, $range)
{
$set = \Auth::user()->budgets()->with(
['limits' => function ($q) use ($date) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) use ($date) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
$q->where('startdate', $date->format('Y-m-d'));
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
$budget->count = 0;
foreach ($budget->limits as $limit) {
/** @var $rep \LimitRepetition */
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
// overspent:
if ($rep->left < 0) {
$rep->spent = ($rep->left * -1) + $rep->amount;
$rep->overspent = $rep->left * -1;
$total = $rep->spent + $rep->overspent;
$rep->spent_pct = round(($rep->spent / $total) * 100);
$rep->overspent_pct = 100 - $rep->spent_pct;
} else {
$rep->spent = $rep->amount - $rep->left;
$rep->spent_pct = round(($rep->spent / $rep->amount) * 100);
$rep->left_pct = 100 - $rep->spent_pct;
}
}
$budget->count += count($limit->limitrepetitions);
}
}
return $set;
//return $set;
}
/**
@@ -184,6 +157,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
if ($budget->validate()) {
$budget->save();
}
return $budget;
}

View File

@@ -28,6 +28,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
if (!$category) {
return $this->store($name);
}
return $category;
@@ -40,9 +41,10 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
*/
public function findByName($name)
{
if($name == '') {
if ($name == '') {
return null;
}
return \Auth::user()->categories()->where('name', 'LIKE', '%' . $name . '%')->first();
}
@@ -58,6 +60,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
$category->name = $name;
$category->user()->associate(\Auth::user());
$category->save();
return $category;
}

View File

@@ -37,6 +37,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
$budget = \Budget::find($data['budget_id']);
if (is_null($budget)) {
\Session::flash('error', 'No such budget.');
return new \Limit;
}
// set the date to the correct start period:
@@ -76,6 +77,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
)->count();
if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.');
return new \Limit;
}
// create new limit:
@@ -88,6 +90,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
if (!$limit->save()) {
Session::flash('error', 'Could not save: ' . $limit->errors()->first());
}
return $limit;
}

View File

@@ -171,6 +171,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$journal->completed = true;
$journal->save();
return $journal;
}
@@ -209,6 +210,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.id', 'DESC')
->take($count)
->get(['transaction_journals.*']);
return $query;
}
@@ -234,6 +236,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.id', 'DESC')
->take($count)
->paginate($count);
return $query;
}
@@ -245,26 +248,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
*/
public function getByDateRange(Carbon $start, Carbon $end)
{
// lets make this simple.
$types = [];
foreach (\TransactionType::whereIn('type', ['Withdrawal'])->get() as $t) {
$types[] = $t->id;
}
unset($t);
// get all journals, partly filtered:
$journals = \TransactionJournal::
with(
['components', 'transactions' => function ($q) {
$q->where('amount', '>', 0);
}]
)
->after($start)->before($end)
->where('completed', 1)
->whereIn('transaction_type_id', $types)
->get(['transaction_journals.*']);
unset($types);
return $journals;
die('no impl');
}
/**
@@ -292,6 +276,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
return $query;
}

View File

@@ -33,9 +33,11 @@ class EloquentUserRepository implements UserRepositoryInterface
if (!$user->save()) {
\Log::error('Invalid user');
\Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first());
return false;
}
$user->save();
return $user;
}
@@ -51,6 +53,7 @@ class EloquentUserRepository implements UserRepositoryInterface
if (\Hash::check($array['password'], $user->password)) {
}
}
return false;
}
@@ -87,6 +90,7 @@ class EloquentUserRepository implements UserRepositoryInterface
$user->password = $password;
/** @noinspection PhpUndefinedMethodInspection */
$user->save();
return true;
}