Some more catches and small tweaks for issue #6. [skip ci]

This commit is contained in:
James Cole 2014-08-14 07:40:15 +02:00
parent 2b42cb8ef3
commit 5f710f4c23
6 changed files with 128 additions and 60 deletions

View File

@ -111,7 +111,7 @@ class PiggybankController extends BaseController
$data['order'] = 0; $data['order'] = 0;
$piggyBank = $this->_repository->store($data); $piggyBank = $this->_repository->store($data);
if ($piggyBank->validate()) { if (!is_null($piggyBank->id)) {
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
return Redirect::route('piggybanks.index'); return Redirect::route('piggybanks.index');

View File

@ -1,7 +1,9 @@
<?php <?php
namespace Firefly\Storage\Piggybank; namespace Firefly\Storage\Piggybank;
use Carbon\Carbon; use Carbon\Carbon;
use Firefly\Exception\FireflyException;
/** /**
@ -23,6 +25,21 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
)->count(); )->count();
} }
public function countNonrepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 0)->count();
}
public function countRepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 1)->count();
}
/** /**
* @param $piggyBankId * @param $piggyBankId
* *
@ -35,21 +52,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
)->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']); )->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']);
} }
public function countRepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 1)->count();
}
public function countNonrepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 0)->count();
}
/** /**
* @return mixed * @return mixed
*/ */
@ -68,6 +70,13 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
public function store($data) public function store($data)
{ {
var_dump($data); var_dump($data);
if ($data['targetdate'] == '') {
unset($data['targetdate']);
}
if ($data['reminder'] == 'none') {
unset($data['reminder']);
}
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */ /** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); $accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null; $account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
@ -76,19 +85,50 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
$piggyBank = new \Piggybank($data); $piggyBank = new \Piggybank($data);
$piggyBank->account()->associate($account); $piggyBank->account()->associate($account);
$today = new Carbon; $today = new Carbon;
if ($piggyBank->validate()) { if ($piggyBank->validate()) {
echo 'Valid, but some more checking!'; echo 'Valid, but some more checking!';
if($piggyBank->targetdate < $today) {
$piggyBank->errors()->add('targetdate','Target date cannot be in the past.'); if (!is_null($piggyBank->targetdate) && $piggyBank->targetdate < $today) {
echo 'errrrrrr on target date'; $piggyBank->errors()->add('targetdate', 'Target date cannot be in the past.');
return $piggyBank; return $piggyBank;
} }
// first period for reminder is AFTER target date. if (!is_null($piggyBank->reminder) && !is_null($piggyBank->targetdate)) {
// just flash a warning // first period for reminder is AFTER target date.
die('Here be a check. Sorry for the kill-switch. Will continue tomorrow.'); // just flash a warning
$reminderSkip = $piggyBank->reminder_skip < 1 ? 1 : intval($piggyBank->reminder_skip);
$firstReminder = new Carbon;
switch($piggyBank->reminder) {
case 'day':
$firstReminder->addDays($reminderSkip);
break;
case 'week':
$firstReminder->addWeeks($reminderSkip);
break;
case 'month':
$firstReminder->addMonths($reminderSkip);
break;
case 'year':
$firstReminder->addYears($reminderSkip);
break;
default:
throw new FireflyException('Invalid reminder period');
break;
}
if($firstReminder > $piggyBank->targetdate) {
$piggyBank->errors()->add('reminder','Something reminder bla.');
return $piggyBank;
}
}
$piggyBank->save(); $piggyBank->save();
} else {
echo 'Does not validate';
print_r($piggyBank->errors()->all());
exit;
} }
return $piggyBank; return $piggyBank;

View File

@ -24,42 +24,48 @@ use LaravelBook\Ardent\Ardent as Ardent;
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereAmount($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAmount($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTarget($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTarget($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value)
* @property float $targetamount * @property float $targetamount
* @property string $startdate * @property string $startdate
* @property boolean $repeats * @property boolean $repeats
* @property string $rep_length * @property string $rep_length
* @property integer $rep_times * @property integer $rep_times
* @property string $reminder * @property string $reminder
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value)
*/ */
class Piggybank extends Ardent class Piggybank extends Ardent
{ {
public $fillable = [
'name',
'account_id',
'targetamount',
'repeats',
'rep_times',
'order'
];
public static $rules public static $rules
= [ = [
'account_id' => 'required|exists:accounts,id', 'account_id' => 'required|exists:accounts,id',
'name' => 'required|between:1,255', 'name' => 'required|between:1,255',
'targetamount' => 'required|min:0', 'targetamount' => 'required|min:0',
'targetdate' => 'date', 'targetdate' => 'date',
'startdate' => 'date', 'startdate' => 'date',
'repeats' => 'required|between:0,1', 'repeats' => 'required|between:0,1',
'rep_length' => 'in:day,week,month,year', 'rep_length' => 'in:day,week,month,year',
'rep_times' => 'required|min:0|max:100', 'rep_times' => 'required|min:0|max:100',
'reminder' => 'in:day,week,month,year', 'reminder' => 'in:day,week,month,year',
'order' => 'required:min:1', 'reminder_skip' => 'required|min:0|max:100',
'order' => 'required:min:1',
];
public $fillable
= [
'account_id',
'name',
'targetamount',
'targetdate',
'startdate',
'repeats',
'rep_length',
'rep_times',
'reminder',
'reminder_skip',
'order'
]; ];
/** /**
@ -72,8 +78,8 @@ class Piggybank extends Ardent
$today = new Carbon; $today = new Carbon;
return [ return [
'account_id' => 'factory|Account', 'account_id' => 'factory|Account',
'name' => 'string', 'name' => 'string',
'targetamount' => 'required|min:0', 'targetamount' => 'required|min:0',
'targetdate' => $start, 'targetdate' => $start,
'startdate' => $today, 'startdate' => $today,
@ -81,6 +87,7 @@ class Piggybank extends Ardent
'rep_length' => null, 'rep_length' => null,
'rep_times' => 0, 'rep_times' => 0,
'reminder' => null, 'reminder' => null,
'reminder_skip' => 0,
'order' => 1, 'order' => 1,
]; ];
} }
@ -93,10 +100,6 @@ class Piggybank extends Ardent
return $this->belongsTo('Account'); return $this->belongsTo('Account');
} }
public function piggybankrepetitions() {
return $this->hasMany('PiggybankRepetition');
}
/** /**
* @return array * @return array
*/ */
@ -105,4 +108,9 @@ class Piggybank extends Ardent
return ['created_at', 'updated_at', 'targetdate']; return ['created_at', 'updated_at', 'targetdate'];
} }
public function piggybankrepetitions()
{
return $this->hasMany('PiggybankRepetition');
}
} }

View File

@ -5,6 +5,22 @@ use LaravelBook\Ardent\Ardent as Ardent;
/** /**
* Class PiggybankRepetition * Class PiggybankRepetition
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $piggybank_id
* @property \Carbon\Carbon $targetdate
* @property \Carbon\Carbon $startdate
* @property float $currentamount
* @property-read \Piggybank $piggybank
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereId($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition wherePiggybankId($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereTargetdate($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCurrentamount($value)
*/ */
class PiggybankRepetition extends Ardent class PiggybankRepetition extends Ardent
{ {

View File

@ -75,6 +75,10 @@ use LaravelBook\Ardent\Ardent;
* 'Budget[] $budgets * 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\ * @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories * 'Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
*/ */
class TransactionJournal extends Ardent class TransactionJournal extends Ardent
{ {

View File

@ -84,7 +84,7 @@
<div class="col-sm-8"> <div class="col-sm-8">
<input type="number" step="1" min="1" value="1" style="width:50px;display:inline;" max="100" name="reminder_skip" class="form-control" /> <input type="number" step="1" min="1" value="1" style="width:50px;display:inline;" max="100" name="reminder_skip" class="form-control" />
<select class="form-control" style="width:150px;display: inline"> <select class="form-control" name="reminder" style="width:150px;display: inline">
<option value="none" label="do not remind me">do not remind me</option> <option value="none" label="do not remind me">do not remind me</option>
@foreach($periods as $period) @foreach($periods as $period)
<option value="{{$period}}" label="{{$period}}">{{$period}}</option> <option value="{{$period}}" label="{{$period}}">{{$period}}</option>