diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 6e35f8fe2b..8dc335bc31 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -111,7 +111,7 @@ class PiggybankController extends BaseController $data['order'] = 0; $piggyBank = $this->_repository->store($data); - if ($piggyBank->validate()) { + if (!is_null($piggyBank->id)) { Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); return Redirect::route('piggybanks.index'); diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php index ecb724221b..144b3fab8b 100644 --- a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -1,7 +1,9 @@ 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 * @@ -35,21 +52,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface )->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 */ @@ -68,6 +70,13 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface public function store($data) { var_dump($data); + if ($data['targetdate'] == '') { + unset($data['targetdate']); + } + if ($data['reminder'] == 'none') { + unset($data['reminder']); + } + /** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */ $accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); $account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null; @@ -76,19 +85,50 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface $piggyBank = new \Piggybank($data); $piggyBank->account()->associate($account); $today = new Carbon; + if ($piggyBank->validate()) { echo 'Valid, but some more checking!'; - if($piggyBank->targetdate < $today) { - $piggyBank->errors()->add('targetdate','Target date cannot be in the past.'); - echo 'errrrrrr on target date'; + + if (!is_null($piggyBank->targetdate) && $piggyBank->targetdate < $today) { + $piggyBank->errors()->add('targetdate', 'Target date cannot be in the past.'); + return $piggyBank; } - // first period for reminder is AFTER target date. - // just flash a warning - die('Here be a check. Sorry for the kill-switch. Will continue tomorrow.'); + if (!is_null($piggyBank->reminder) && !is_null($piggyBank->targetdate)) { + // first period for reminder is AFTER target date. + // 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(); + } else { + echo 'Does not validate'; + + print_r($piggyBank->errors()->all()); + exit; } return $piggyBank; diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php index 30c4488a52..dd8e8e7a9d 100644 --- a/app/models/Piggybank.php +++ b/app/models/Piggybank.php @@ -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 whereTarget($value) * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) - * @property float $targetamount - * @property string $startdate - * @property boolean $repeats - * @property string $rep_length - * @property integer $rep_times - * @property string $reminder - * @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 whereRepeats($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 whereReminder($value) + * @property float $targetamount + * @property string $startdate + * @property boolean $repeats + * @property string $rep_length + * @property integer $rep_times + * @property string $reminder + * @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 whereRepeats($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 whereReminder($value) */ class Piggybank extends Ardent { - public $fillable = [ - 'name', - 'account_id', - 'targetamount', - 'repeats', - 'rep_times', - 'order' - ]; - public static $rules = [ - 'account_id' => 'required|exists:accounts,id', - 'name' => 'required|between:1,255', - 'targetamount' => 'required|min:0', - 'targetdate' => 'date', - 'startdate' => 'date', - 'repeats' => 'required|between:0,1', - 'rep_length' => 'in:day,week,month,year', - 'rep_times' => 'required|min:0|max:100', - 'reminder' => 'in:day,week,month,year', - 'order' => 'required:min:1', + 'account_id' => 'required|exists:accounts,id', + 'name' => 'required|between:1,255', + 'targetamount' => 'required|min:0', + 'targetdate' => 'date', + 'startdate' => 'date', + 'repeats' => 'required|between:0,1', + 'rep_length' => 'in:day,week,month,year', + 'rep_times' => 'required|min:0|max:100', + 'reminder' => 'in:day,week,month,year', + '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; return [ - 'account_id' => 'factory|Account', - 'name' => 'string', + 'account_id' => 'factory|Account', + 'name' => 'string', 'targetamount' => 'required|min:0', 'targetdate' => $start, 'startdate' => $today, @@ -81,6 +87,7 @@ class Piggybank extends Ardent 'rep_length' => null, 'rep_times' => 0, 'reminder' => null, + 'reminder_skip' => 0, 'order' => 1, ]; } @@ -93,10 +100,6 @@ class Piggybank extends Ardent return $this->belongsTo('Account'); } - public function piggybankrepetitions() { - return $this->hasMany('PiggybankRepetition'); - } - /** * @return array */ @@ -105,4 +108,9 @@ class Piggybank extends Ardent return ['created_at', 'updated_at', 'targetdate']; } + public function piggybankrepetitions() + { + return $this->hasMany('PiggybankRepetition'); + } + } \ No newline at end of file diff --git a/app/models/PiggybankRepetition.php b/app/models/PiggybankRepetition.php index f26acac862..b4809fe723 100644 --- a/app/models/PiggybankRepetition.php +++ b/app/models/PiggybankRepetition.php @@ -5,6 +5,22 @@ use LaravelBook\Ardent\Ardent as Ardent; /** * 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 { diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php index 52f9c99654..2daaeba095 100644 --- a/app/models/TransactionJournal.php +++ b/app/models/TransactionJournal.php @@ -75,6 +75,10 @@ use LaravelBook\Ardent\Ardent; * 'Budget[] $budgets * @property-read \Illuminate\Database\Eloquent\Collection|\ * 'Category[] $categories + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Budget[] $budgets + * @property-read \Illuminate\Database\Eloquent\Collection|\ + * 'Category[] $categories */ class TransactionJournal extends Ardent { diff --git a/app/views/piggybanks/create-piggybank.blade.php b/app/views/piggybanks/create-piggybank.blade.php index ae06586553..80e2bb86a6 100644 --- a/app/views/piggybanks/create-piggybank.blade.php +++ b/app/views/piggybanks/create-piggybank.blade.php @@ -84,7 +84,7 @@