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,7 +103,7 @@ 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;
} }
@@ -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()
{ {
$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); $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,6 +24,7 @@ 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();
@@ -31,6 +32,8 @@ class Budget implements BudgetInterface
? $return[$periodOrder] ? $return[$periodOrder]
: ['date' => $period, : ['date' => $period,
'dateObject' => $rep->startdate, 'dateObject' => $rep->startdate,
'start' => $rep->startdate,
'end' => $rep->enddate,
'budget_id' => $limit->budget_id]; 'budget_id' => $limit->budget_id];
} }
@@ -92,6 +95,7 @@ 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;
} }

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();
@@ -230,7 +231,7 @@ 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(
[ [
@@ -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')