Cleaned up some code and added a small feature for transaction views.

This commit is contained in:
James Cole 2014-08-23 10:01:40 +02:00
parent a6b89879c5
commit d56c00915c
6 changed files with 119 additions and 72 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
use Carbon\Carbon;
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI; use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
/** /**
@ -102,15 +103,15 @@ class TransactionController extends BaseController
$piggies = $piggyRepository->get(); $piggies = $piggyRepository->get();
// piggy bank id? // piggy bank id?
$piggyBankId = null; $piggyBankId = null;
foreach($journal->transactions as $t) { foreach ($journal->transactions as $t) {
$piggyBankId = $t->piggybank_id; $piggyBankId = $t->piggybank_id;
} }
// data to properly display form: // data to properly display form:
$data = [ $data = [
'date' => $journal->date->format('Y-m-d'), 'date' => $journal->date->format('Y-m-d'),
'category' => '', 'category' => '',
'budget_id' => 0, 'budget_id' => 0,
'piggybank_id' => $piggyBankId 'piggybank_id' => $piggyBankId
]; ];
$category = $journal->categories()->first(); $category = $journal->categories()->first();
@ -141,7 +142,7 @@ class TransactionController extends BaseController
return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with( return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with(
'what', $what 'what', $what
)->with('budgets', $budgets)->with('data', $data)->with('piggies',$piggies); )->with('budgets', $budgets)->with('data', $data)->with('piggies', $piggies);
} }
/** /**
@ -149,9 +150,22 @@ class TransactionController extends BaseController
*/ */
public function index() public function index()
{ {
$journals = $this->_repository->paginate(25); $start = is_null(Input::get('startdate')) ? null : new Carbon(Input::get('startdate'));
$end = is_null(Input::get('enddate')) ? null : new Carbon(Input::get('enddate'));
if ($start <= $end && !is_null($start) && !is_null($end)) {
$journals = $this->_repository->paginate(25, $start, $end);
$filtered = true;
$filters = ['start' => $start, 'end' => $end];
} else {
$journals = $this->_repository->paginate(25);
$filtered = false;
$filters = null;
}
return View::make('transactions.index')->with('journals', $journals);
return View::make('transactions.index')->with('journals', $journals)->with('filtered', $filtered)->with(
'filters', $filters
);
} }
/** /**

View File

@ -24,14 +24,17 @@ class Budget implements BudgetInterface
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) { foreach ($budget->limits as $limit) {
/** @var \LimitRepetition $rep */
foreach ($limit->limitrepetitions as $rep) { foreach ($limit->limitrepetitions as $rep) {
$periodOrder = $rep->periodOrder(); $periodOrder = $rep->periodOrder();
$period = $rep->periodShow(); $period = $rep->periodShow();
$return[$periodOrder] = isset($return[$periodOrder]) $return[$periodOrder] = isset($return[$periodOrder])
? $return[$periodOrder] ? $return[$periodOrder]
: ['date' => $period, : ['date' => $period,
'dateObject' => $rep->startdate, 'dateObject' => $rep->startdate,
'budget_id' => $limit->budget_id]; 'start' => $rep->startdate,
'end' => $rep->enddate,
'budget_id' => $limit->budget_id];
} }
} }
@ -63,27 +66,27 @@ class Budget implements BudgetInterface
$repetition = \LimitRepetition::with('limit', 'limit.budget')->leftJoin( $repetition = \LimitRepetition::with('limit', 'limit.budget')->leftJoin(
'limits', 'limit_repetitions.limit_id', '=', 'limits.id' 'limits', 'limit_repetitions.limit_id', '=', 'limits.id'
)->leftJoin('components', 'limits.component_id', '=', 'components.id')->where( )->leftJoin('components', 'limits.component_id', '=', 'components.id')->where(
'components.user_id', \Auth::user()->id 'components.user_id', \Auth::user()->id
) )
->where('limit_repetitions.id', $repetitionId)->first(['limit_repetitions.*']); ->where('limit_repetitions.id', $repetitionId)->first(['limit_repetitions.*']);
// get transactions: // get transactions:
$set = $repetition->limit->budget->transactionjournals()->with( $set = $repetition->limit->budget->transactionjournals()->with(
'transactions', 'transactions.account', 'components', 'transactiontype' 'transactions', 'transactions.account', 'components', 'transactiontype'
)->leftJoin( )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id' 'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Withdrawal')->where( )->where('transaction_types.type', 'Withdrawal')->where(
'date', '>=', $repetition->startdate->format('Y-m-d') 'date', '>=', $repetition->startdate->format('Y-m-d')
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy( )->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy(
'id', 'DESC' 'id', 'DESC'
)->get(['transaction_journals.*']); )->get(['transaction_journals.*']);
$result[0] = [ $result[0] = [
'date' => $repetition->periodShow(), 'date' => $repetition->periodShow(),
'limit' => $repetition->limit, 'limit' => $repetition->limit,
'limitrepetition' => $repetition, 'limitrepetition' => $repetition,
'journals' => $set, 'journals' => $set,
'paginated' => false 'paginated' => false
]; ];
return $result; return $result;
@ -91,7 +94,8 @@ class Budget implements BudgetInterface
/** /**
* @param \Budget $budget * @param \Budget $budget
* @param bool $useSessionDates * @param bool $useSessionDates
*
* @return array|mixed * @return array|mixed
* @throws \Firefly\Exception\FireflyException * @throws \Firefly\Exception\FireflyException
*/ */
@ -105,8 +109,9 @@ class Budget implements BudgetInterface
// get the limits: // get the limits:
if ($useSessionDates) { if ($useSessionDates) {
$limits = $budget->limits()->where('startdate', '>=', $sessionStart->format('Y-m-d'))-> $limits = $budget->limits()->where('startdate', '>=', $sessionStart->format('Y-m-d'))->where(
where('startdate', '<=', $sessionEnd->format('Y-m-d'))->get(); 'startdate', '<=', $sessionEnd->format('Y-m-d')
)->get();
} else { } else {
$limits = $budget->limits; $limits = $budget->limits;
} }
@ -116,22 +121,22 @@ class Budget implements BudgetInterface
foreach ($limit->limitrepetitions as $repetition) { foreach ($limit->limitrepetitions as $repetition) {
$order = $repetition->periodOrder(); $order = $repetition->periodOrder();
$result[$order] = [ $result[$order] = [
'date' => $repetition->periodShow(), 'date' => $repetition->periodShow(),
'limitrepetition' => $repetition, 'limitrepetition' => $repetition,
'limit' => $limit, 'limit' => $limit,
'journals' => [], 'journals' => [],
'paginated' => false 'paginated' => false
]; ];
$transactions = []; $transactions = [];
$set = $budget->transactionjournals()->with( $set = $budget->transactionjournals()->with(
'transactions', 'transactions.account', 'components', 'transactiontype' 'transactions', 'transactions.account', 'components', 'transactiontype'
)->leftJoin( )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id' 'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Withdrawal')->where( )->where('transaction_types.type', 'Withdrawal')->where(
'date', '>=', $repetition->startdate->format('Y-m-d') 'date', '>=', $repetition->startdate->format('Y-m-d')
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy( )->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy(
'id', 'DESC' 'id', 'DESC'
)->get(['transaction_journals.*']); )->get(['transaction_journals.*']);
foreach ($set as $entry) { foreach ($set as $entry) {
$transactions[] = $entry; $transactions[] = $entry;
$inRepetition[] = $entry->id; $inRepetition[] = $entry->id;
@ -146,17 +151,17 @@ class Budget implements BudgetInterface
'transactions', 'transactions.account', 'components', 'transactiontype', 'transactions', 'transactions.account', 'components', 'transactiontype',
'transactions.account.accounttype' 'transactions.account.accounttype'
)->whereNotIn( )->whereNotIn(
'transaction_journals.id', $inRepetition 'transaction_journals.id', $inRepetition
)->orderBy('date', 'DESC')->orderBy( )->orderBy('date', 'DESC')->orderBy(
'transaction_journals.id', 'DESC' 'transaction_journals.id', 'DESC'
); );
} else { } else {
$query = $budget->transactionjournals()->with( $query = $budget->transactionjournals()->with(
'transactions', 'transactions.account', 'components', 'transactiontype', 'transactions', 'transactions.account', 'components', 'transactiontype',
'transactions.account.accounttype' 'transactions.account.accounttype'
)->orderBy('date', 'DESC')->orderBy( )->orderBy('date', 'DESC')->orderBy(
'transaction_journals.id', 'DESC' 'transaction_journals.id', 'DESC'
); );
} }
// build paginator: // build paginator:
@ -173,8 +178,8 @@ class Budget implements BudgetInterface
$items[] = $item; $items[] = $item;
} }
$paginator = \Paginator::make($items, $totalItems, $perPage); $paginator = \Paginator::make($items, $totalItems, $perPage);
$result['0000'] = ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true, $result['0000'] = ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true,
'journals' => $paginator]; 'journals' => $paginator];
} }
krsort($result); krsort($result);
@ -194,10 +199,10 @@ class Budget implements BudgetInterface
$set = $budget->transactionjournals()->leftJoin( $set = $budget->transactionjournals()->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id' 'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Withdrawal')->where( )->where('transaction_types.type', 'Withdrawal')->where(
'date', '>=', $repetition->startdate->format('Y-m-d') 'date', '>=', $repetition->startdate->format('Y-m-d')
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->get( )->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->get(
['transaction_journals.id'] ['transaction_journals.id']
); );
foreach ($set as $item) { foreach ($set as $item) {
$inRepetitions[] = $item->id; $inRepetitions[] = $item->id;
} }
@ -209,10 +214,10 @@ class Budget implements BudgetInterface
'transactions', 'transactions.account', 'components', 'transactiontype', 'transactions', 'transactions.account', 'components', 'transactiontype',
'transactions.account.accounttype' 'transactions.account.accounttype'
)->whereNotIn( )->whereNotIn(
'transaction_journals.id', $inRepetitions 'transaction_journals.id', $inRepetitions
)->orderBy('date', 'DESC')->orderBy( )->orderBy('date', 'DESC')->orderBy(
'transaction_journals.id', 'DESC' 'transaction_journals.id', 'DESC'
); );
// build paginator: // build paginator:
$perPage = 25; $perPage = 25;
@ -228,8 +233,8 @@ class Budget implements BudgetInterface
$items[] = $item; $items[] = $item;
} }
$paginator = \Paginator::make($items, $totalItems, $perPage); $paginator = \Paginator::make($items, $totalItems, $perPage);
$result = [0 => ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true, $result = [0 => ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true,
'journals' => $paginator]]; 'journals' => $paginator]];
return $result; return $result;
} }

View File

@ -121,7 +121,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$fromTransaction->description = null; $fromTransaction->description = null;
$fromTransaction->amount = $amountFrom; $fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) { if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first()); throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first(
));
} }
$fromTransaction->save(); $fromTransaction->save();
@ -150,9 +151,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
{ {
return \Auth::user()->transactionjournals()->with( return \Auth::user()->transactionjournals()->with(
['transactions' => function ($q) { ['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC'); return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account', }, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype'] 'transactions.account.accounttype']
) )
->where('id', $journalId)->first(); ->where('id', $journalId)->first();
} }
@ -167,7 +168,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** /**
* @param \Account $account * @param \Account $account
* @param Carbon $date * @param Carbon $date
* *
* @return mixed * @return mixed
*/ */
@ -196,9 +197,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** /**
* @param \Account $account * @param \Account $account
* @param int $count * @param int $count
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
@ -230,13 +231,13 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
* *
* @return mixed * @return mixed
*/ */
public function paginate($count = 25) public function paginate($count = 25, Carbon $start = null, Carbon $end = null)
{ {
$query = \Auth::user()->transactionjournals()->with( $query = \Auth::user()->transactionjournals()->with(
[ [
'transactions' => function ($q) { 'transactions' => function ($q) {
return $q->orderBy('amount', 'ASC'); return $q->orderBy('amount', 'ASC');
}, },
'transactions.account', 'transactions.account',
'transactions.account.accounttype', 'transactions.account.accounttype',
'transactioncurrency', 'transactioncurrency',
@ -244,10 +245,17 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
] ]
) )
->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC') ->orderBy('transaction_journals.id', 'DESC');
->paginate($count); if (!is_null($start)) {
$query->where('transaction_journals.date', '>=', $start->format('Y-m-d'));
}
if (!is_null($end)) {
$query->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
}
return $query; $result = $query->paginate($count);
return $result;
} }
/** /**
@ -328,12 +336,17 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$connected = true; $connected = true;
$transaction->piggybank()->associate($piggyBank); $transaction->piggybank()->associate($piggyBank);
$transaction->save(); $transaction->save();
\Event::fire('piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]); \Event::fire(
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
);
break; break;
} }
} }
if ($connected === false) { if ($connected === false) {
\Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer'); \Session::flash(
'warning', 'Piggy bank "' . e($piggyBank->name)
. '" is not set to draw money from any of the accounts in this transfer'
);
} }
} }
} }
@ -462,7 +475,10 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
} }
} }
if ($connected === false) { if ($connected === false) {
\Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer'); \Session::flash(
'warning', 'Piggy bank "' . e($piggyBank->name)
. '" is not set to draw money from any of the accounts in this transfer'
);
} }
} }
} }
@ -479,7 +495,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
if ($journal->validate()) { if ($journal->validate()) {
$journal->save(); $journal->save();
} }
if($fireEvent) { if ($fireEvent) {
\Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank]); \Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank]);
} }

View File

@ -73,6 +73,6 @@ interface TransactionJournalRepositoryInterface
* *
* @return mixed * @return mixed
*/ */
public function paginate($count = 25); public function paginate($count = 25, Carbon $start = null, Carbon $end = null);
} }

View File

@ -34,7 +34,7 @@
@foreach($budgets as $date => $entry) @foreach($budgets as $date => $entry)
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<h3><a href="#transactions-in-this-period">{{$entry['date']}}</a> <h3><a href="{{route('transactions.index')}}?startdate={{$entry['start']->format('Y-m-d')}}&amp;enddate={{$entry['end']->format('Y-m-d')}}">{{$entry['date']}}</a>
<a class="btn btn-default btn-xs" href ="{{route('budgets.limits.create')}}?startdate={{$entry['dateObject']->format('Y-m-d')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope for {{$entry['date']}}</a> <a class="btn btn-default btn-xs" href ="{{route('budgets.limits.create')}}?startdate={{$entry['dateObject']->format('Y-m-d')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope for {{$entry['date']}}</a>
</h3> </h3>
<table class="table table-bordered table-striped"> <table class="table table-bordered table-striped">

View File

@ -1,6 +1,18 @@
@extends('layouts.default') @extends('layouts.default')
@section('content') @section('content')
@if($filtered === true)
<p class="bg-primary" style="padding:15px;">
This view is filtered to show only the transactions between
{{$filters['start']->format('M jS, Y')}} and {{$filters['end']->format('M jS, Y')}}.
</p>
<p class="bg-info" style="padding:15px;">
<a href="{{route('transactions.index')}}" class="text-info">Reset the filter.</a>
</p>
@endif
@include('paginated.transactions') @include('paginated.transactions')