New stuff! [skip ci]

This commit is contained in:
James Cole
2014-07-27 20:29:58 +02:00
parent b782bb8d93
commit 92f2e30ed1
22 changed files with 868 additions and 251 deletions

View File

@@ -41,6 +41,7 @@ class Account implements AccountInterface
}
}
return $list;
}
@@ -53,6 +54,7 @@ class Account implements AccountInterface
public function openingBalanceTransaction(\Account $account)
{
$transactionType = \TransactionType::where('type', 'Opening balance')->first();
return \TransactionJournal::
with(
['transactions' => function ($q) {
@@ -63,4 +65,118 @@ class Account implements AccountInterface
->where('transactions.account_id', $account->id)->first(['transaction_journals.*']);
}
/**
* @param \Account $account
* @param $perPage
*
* @return mixed|void
*/
public function show(\Account $account, $perPage)
{
$start = \Session::get('start');
$end = \Session::get('end');
$stats = [
'budgets' => [],
'categories' => [],
'accounts' => []
];
$items = [];
// build a query:
$query = \TransactionJournal::with(
['transactions' => function ($q) {
$q->orderBy('amount', 'ASC');
}, 'transactiontype', 'components' => function ($q) {
$q->orderBy('class');
}, 'transactions.account.accounttype']
)->orderBy('date', 'DESC')->leftJoin(
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
)->where('transactions.account_id', $account->id)->where('date', '>=', $start->format('Y-m-d'))->where(
'date', '<=', $end->format('Y-m-d')
)->orderBy('transaction_journals.id', 'DESC');
// build paginator:
$totalItems = $query->count();
$page = intval(\Input::get('page')) > 1 ? intval(\Input::get('page')) : 1;
$skip = ($page - 1) * $perPage;
$result = $query->skip($skip)->take($perPage)->get(['transaction_journals.*']);
// in the mean time, build list of categories, budgets and other accounts:
/** @var $item \TransactionJournal */
foreach ($result as $item) {
$items[] = $item;
foreach ($item->components as $component) {
if ($component->class == 'Budget') {
$stats['budgets'][$component->id] = $component;
}
if ($component->class == 'Category') {
$stats['categories'][$component->id] = $component;
}
}
$fromAccount = $item->transactions[0]->account;
$toAccount = $item->transactions[1]->account;
$stats['accounts'][$fromAccount->id] = $fromAccount;
$stats['accounts'][$toAccount->id] = $toAccount;
}
unset($result, $page);
$paginator = \Paginator::make($items, $totalItems, $perPage);
// statistics
$stats['period']['in'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '>', 0)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->whereIn('transaction_types.type', ['Deposit', 'Withdrawal'])->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('amount')
);
$stats['period']['out'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '<', 0)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->whereIn('transaction_types.type', ['Deposit', 'Withdrawal'])->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('amount')
);
$stats['period']['diff'] = $stats['period']['in'] + $stats['period']['out'];
$stats['period']['t_in'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '>', 0)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Transfer')->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('amount')
);
$stats['period']['t_out'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '<', 0)->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Transfer')->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->sum('amount')
);
$stats['period']['t_diff'] = $stats['period']['t_in'] + $stats['period']['t_out'];
$return = [
'journals' => $paginator,
'statistics' => $stats
];
return $return;
}
}

View File

@@ -28,4 +28,12 @@ interface AccountInterface
*/
public function openingBalanceTransaction(\Account $account);
/**
* @param \Account $account
* @param $perPage
*
* @return mixed
*/
public function show(\Account $account, $perPage);
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: sander
* Date: 27/07/14
* Time: 16:28
*/
namespace Firefly\Helper\Controllers;
use Illuminate\Database\Eloquent\Collection;
/**
* Class Budget
*
* @package Firefly\Helper\Controllers
*/
class Budget implements BudgetInterface
{
/**
* @param Collection $budgets
*
* @return mixed|void
*/
public function organizeByDate(Collection $budgets)
{
$return = [];
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
if (is_null($dateFormats)) {
throw new \Firefly\Exception\FireflyException('No date formats for ' . $limit->repeat_freq);
}
foreach ($limit->limitrepetitions as $rep) {
$periodOrder = $rep->startdate->format($dateFormats['group_date']);
$period = $rep->startdate->format($dateFormats['display_date']);
$return[$periodOrder] = isset($return[$periodOrder]) ? $return[$periodOrder] : ['date' => $period];
}
}
}
// put all the budgets under their respective date:
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
$month = $rep->startdate->format($dateFormats['group_date']);
$return[$month]['limitrepetitions'][] = $rep;
}
}
}
krsort($return);
return $return;
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Firefly\Helper\Controllers;
use Illuminate\Database\Eloquent\Collection;
/**
* Interface BudgetInterface
*
* @package Firefly\Helper\Controllers
*/
interface BudgetInterface {
/**
* @param Collection $budgets
*
* @return mixed
*/
public function organizeByDate(Collection $budgets);
}

View File

@@ -23,6 +23,11 @@ class HelperServiceProvider extends ServiceProvider
'Firefly\Helper\Controllers\Account'
);
$this->app->bind(
'Firefly\Helper\Controllers\BudgetInterface',
'Firefly\Helper\Controllers\Budget'
);
// mail:
$this->app->bind(
'Firefly\Helper\Email\EmailHelperInterface',

View File

@@ -12,6 +12,40 @@ use Carbon\Carbon;
class EloquentBudgetRepository implements BudgetRepositoryInterface
{
/**
* @param $budgetId
*
* @return mixed
*/
public function find($budgetId)
{
return \Auth::user()->budgets()->find($budgetId);
}
/**
* @return mixed
*/
public function get()
{
$set = \Auth::user()->budgets()->with(
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
$rep->left = $rep->left();
}
}
}
return $set;
}
/**
* @return array|mixed
*/
@@ -24,6 +58,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
return $return;
}
@@ -69,6 +104,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->count += count($limit->limitrepetitions);
}
}
return $set;
}
@@ -123,30 +159,4 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $budget;
}
/**
* @return mixed
*/
public function get()
{
return \Auth::user()->budgets()->with(
['limits' => function ($q) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
}]
)->orderBy('name', 'ASC')->get();
}
/**
* @param $budgetId
*
* @return mixed
*/
public function find($budgetId)
{
return \Auth::user()->budgets()->find($budgetId);
}
}

View File

@@ -100,7 +100,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
*/
public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end)
{
$result = $budget->transactionjournals()->after($start)->before($end)->get();
$result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get();
return $result;