diff --git a/app/assets/javascripts/firefly/piggybanks-create.js b/app/assets/javascripts/firefly/piggybanks-create.js index fbf5fb2588..e69de29bb2 100644 --- a/app/assets/javascripts/firefly/piggybanks-create.js +++ b/app/assets/javascripts/firefly/piggybanks-create.js @@ -1,25 +0,0 @@ -$(function () { - -updatePiggyFields(); -$('input[name="repeats"]').on('change',updatePiggyFields); - -}); - -function updatePiggyFields() { - //var val = $('input[name="repeats"]').checked; - - console.log('Repeating elements: ' + $('.repeat-piggy').length); - console.log('Non-repeating elements: ' + $('.no-repeat-piggy').length); - - if($('input[name="repeats"]').prop( "checked" )) { - // checked, repeats! - console.log('repeats!'); - $('.repeat-piggy').show(); - $('.no-repeat-piggy').hide(); - } else { - console.log('no repeats!'); - // unchecked, does not repeat! - $('.no-repeat-piggy').show(); - $('.repeat-piggy').hide(); - } -} \ No newline at end of file diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index add254c912..1da2a19443 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -31,15 +31,18 @@ class PiggybankController extends BaseController $periods = Config::get('firefly.piggybank_periods'); $accounts = $this->_accounts->getActiveDefaultAsSelectList(); - return View::make('piggybanks.create-piggybank')->with('accounts', $accounts)->with('periods',$periods); + return View::make('piggybanks.create-piggybank')->with('accounts', $accounts)->with('periods', $periods); } + /** + * @return $this + */ public function createRepeated() { $periods = Config::get('firefly.piggybank_periods'); $accounts = $this->_accounts->getActiveDefaultAsSelectList(); - return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods',$periods); + return View::make('piggybanks.create-repeated')->with('accounts', $accounts)->with('periods', $periods); } @@ -60,7 +63,9 @@ class PiggybankController extends BaseController */ public function destroy(Piggybank $piggyBank) { - $piggyBank->delete(); + // TODO move to repository. + $this->_repository->destroy($piggyBank); + Event::fire('piggybanks.change'); Session::flash('success', 'Piggy bank deleted.'); return Redirect::route('piggybanks.index'); @@ -74,8 +79,16 @@ class PiggybankController extends BaseController public function edit(Piggybank $piggyBank) { $accounts = $this->_accounts->getActiveDefaultAsSelectList(); + $periods = Config::get('firefly.piggybank_periods'); + if ($piggyBank->repeats == 1) { + return View::make('piggybanks.edit-repeated')->with('piggybank', $piggyBank)->with('accounts', $accounts) + ->with('periods', $periods); + } else { + return View::make('piggybanks.edit-piggybank')->with('piggybank', $piggyBank)->with('accounts', $accounts) + ->with('periods', $periods); + } + - return View::make('piggybanks.edit')->with('piggybank', $piggyBank)->with('accounts', $accounts); } /** @@ -85,10 +98,12 @@ class PiggybankController extends BaseController { $countRepeating = $this->_repository->countRepeating(); $countNonRepeating = $this->_repository->countNonrepeating(); + Event::fire('piggybanks.change'); // TODO remove + $piggybanks = $this->_repository->get(); return View::make('piggybanks.index')->with('piggybanks', $piggybanks) - ->with('countRepeating',$countRepeating) - ->with('countNonRepeating',$countNonRepeating); + ->with('countRepeating', $countRepeating) + ->with('countNonRepeating', $countNonRepeating); } /** @@ -96,6 +111,7 @@ class PiggybankController extends BaseController */ public function show(Piggybank $piggyBank) { + Event::fire('piggybanks.change'); // TODO remove return View::make('piggybanks.show')->with('piggyBank', $piggyBank); } @@ -109,12 +125,14 @@ class PiggybankController extends BaseController // extend the data array with the settings needed to create a piggy bank: $data['repeats'] = 0; - $data['rep_times'] = 0; + $data['rep_times'] = 1; + $data['rep_every'] = 1; $data['order'] = 0; $piggyBank = $this->_repository->store($data); if (!is_null($piggyBank->id)) { Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); + Event::fire('piggybanks.change'); return Redirect::route('piggybanks.index'); @@ -138,19 +156,19 @@ class PiggybankController extends BaseController // extend the data array with the settings needed to create a repeated: $data['repeats'] = 1; - $data['startdate'] = new Carbon; $data['order'] = 0; $piggyBank = $this->_repository->store($data); if ($piggyBank->validate()) { Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); + Event::fire('piggybanks.change'); return Redirect::route('piggybanks.index'); } else { Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first()); - return Redirect::route('piggybanks.create')->withInput(); + return Redirect::route('piggybanks.create.repeated')->withInput()->withErrors($piggyBank->errors()); } } @@ -158,12 +176,12 @@ class PiggybankController extends BaseController /** * @return $this|\Illuminate\Http\RedirectResponse */ - public function update() + public function update(Piggybank $piggyBank) { - - $piggyBank = $this->_repository->update(Input::all()); + $piggyBank = $this->_repository->update($piggyBank, Input::all()); if ($piggyBank->validate()) { Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.'); + Event::fire('piggybanks.change'); return Redirect::route('piggybanks.index'); } else { @@ -180,6 +198,7 @@ class PiggybankController extends BaseController */ public function updateAmount(Piggybank $piggybank) { + Event::fire('piggybanks.change'); $this->_repository->updateAmount($piggybank, Input::get('amount')); } } \ No newline at end of file diff --git a/app/database/migrations/2014_07_05_100651_create_piggybanks_table.php b/app/database/migrations/2014_07_05_100651_create_piggybanks_table.php index c7b3da8e09..b30cae3406 100644 --- a/app/database/migrations/2014_07_05_100651_create_piggybanks_table.php +++ b/app/database/migrations/2014_07_05_100651_create_piggybanks_table.php @@ -25,11 +25,12 @@ class CreatePiggybanksTable extends Migration $table->integer('account_id')->unsigned(); $table->string('name', 100); $table->decimal('targetamount', 10, 2); - $table->date('targetdate')->nullable(); $table->date('startdate')->nullable(); + $table->date('targetdate')->nullable(); $table->boolean('repeats'); $table->enum('rep_length', ['day', 'week', 'month', 'year'])->nullable(); - $table->smallInteger('rep_times')->unsigned(); + $table->smallInteger('rep_every')->unsigned(); + $table->smallInteger('rep_times')->unsigned()->nullable(); $table->enum('reminder', ['day', 'week', 'month', 'year'])->nullable(); $table->smallInteger('reminder_skip')->unsigned(); $table->integer('order')->unsigned(); diff --git a/app/database/migrations/2014_08_12_173919_create_piggy_instance.php b/app/database/migrations/2014_08_12_173919_create_piggy_instance.php index 761a380b73..53fce65e6d 100644 --- a/app/database/migrations/2014_08_12_173919_create_piggy_instance.php +++ b/app/database/migrations/2014_08_12_173919_create_piggy_instance.php @@ -17,10 +17,12 @@ class CreatePiggyInstance extends Migration { $table->increments('id'); $table->timestamps(); $table->integer('piggybank_id')->unsigned(); - $table->date('targetdate')->nullable(); $table->date('startdate')->nullable(); + $table->date('targetdate')->nullable(); $table->decimal('currentamount',10,2); + $table->unique(['piggybank_id','startdate','targetdate']); + // connect instance to piggybank. $table->foreign('piggybank_id') ->references('id')->on('piggybanks') diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php index 49d927f0ea..3189d75589 100644 --- a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -40,6 +40,18 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface )->where('repeats', 1)->count(); } + /** + * @param \Piggybank $piggyBank + * + * @return mixed|void + */ + public function destroy(\Piggybank $piggyBank) + { + $piggyBank->delete(); + + return true; + } + /** * @param $piggyBankId * @@ -57,9 +69,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface */ public function get() { - return \Piggybank::with('account')->leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where( - 'accounts.user_id', \Auth::user()->id - )->get(['piggybanks.*']); + return \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get(); } /** @@ -90,12 +100,12 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface if ($piggyBank->validate()) { if (!is_null($piggyBank->targetdate) && $piggyBank->targetdate < $today) { $piggyBank->errors()->add('targetdate', 'Target date cannot be in the past.'); + return $piggyBank; } 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) { @@ -116,7 +126,10 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface break; } if ($firstReminder > $piggyBank->targetdate) { - $piggyBank->errors()->add('reminder', 'The reminder has been set to remind you after the piggy bank will expire.'); + $piggyBank->errors()->add( + 'reminder', 'The reminder has been set to remind you after the piggy bank will expire.' + ); + return $piggyBank; } } @@ -127,26 +140,51 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface } /** - * @param $data + * @param \Piggybank $piggy + * @param $data * * @return mixed */ - public function update($data) + public function update(\Piggybank $piggy, $data) { - $piggyBank = $this->find($data['id']); - if ($piggyBank) { - $accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); - $account = $accounts->find($data['account_id']); - // update piggybank accordingly: - $piggyBank->name = $data['name']; - $piggyBank->target = floatval($data['target']); - $piggyBank->account()->associate($account); - if ($piggyBank->validate()) { - $piggyBank->save(); - } + /** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */ + $accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); + $account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null; + + if (!is_null($account)) { + $piggy->account()->associate($account); } - return $piggyBank; + $piggy->name = $data['name']; + $piggy->targetamount = floatval($data['targetamount']); + $piggy->reminder = isset($data['reminder']) && $data['reminder'] != 'none' ? $data['reminder'] : null; + $piggy->reminder_skip = $data['reminder_skip']; + $piggy->targetdate = strlen($data['targetdate']) > 0 ? new Carbon($data['targetdate']) : null; + $piggy->startdate + = isset($data['startdate']) && strlen($data['startdate']) > 0 ? new Carbon($data['startdate']) : null; + + // everything we can update for NON repeating piggy banks: + if ($piggy->repeats == 0) { + // if non-repeating there is only one PiggyBank instance and we can delete it safely. + // it will be recreated. + $piggy->piggybankrepetitions()->first()->delete(); + } else { + // we can delete all of them, because reasons + foreach ($piggy->piggybankrepetitions()->get() as $rep) { + $rep->delete(); + } + + $piggy->rep_every = intval($data['rep_every']); + $piggy->rep_length = $data['rep_length']; + } + if ($piggy->validate()) { + // check the things we check for new piggies + $piggy->save(); + } + + + return $piggy; + } /** diff --git a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php index 26d241bdf4..99227ee34f 100644 --- a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php @@ -11,6 +11,28 @@ namespace Firefly\Storage\Piggybank; interface PiggybankRepositoryInterface { + /** + * @return mixed + */ + public function count(); + + /** + * @return mixed + */ + public function countNonrepeating(); + + /** + * @return mixed + */ + public function countRepeating(); + + /** + * @param \Piggybank $piggyBank + * + * @return mixed + */ + public function destroy(\Piggybank $piggyBank); + /** * @param $piggyBankId * @@ -21,17 +43,7 @@ interface PiggybankRepositoryInterface /** * @return mixed */ - public function count(); - - /** - * @return mixed - */ - public function countRepeating(); - - /** - * @return mixed - */ - public function countNonrepeating(); + public function get(); /** * @param $data @@ -41,9 +53,12 @@ interface PiggybankRepositoryInterface public function store($data); /** + * @param \Piggybank $piggy + * @param $data + * * @return mixed */ - public function get(); + public function update(\Piggybank $piggy, $data); /** * @param \Piggybank $piggyBank @@ -53,11 +68,4 @@ interface PiggybankRepositoryInterface */ public function updateAmount(\Piggybank $piggyBank, $amount); - /** - * @param $data - * - * @return mixed - */ - public function update($data); - } \ No newline at end of file diff --git a/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php new file mode 100644 index 0000000000..ba59b08440 --- /dev/null +++ b/app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php @@ -0,0 +1,114 @@ +listen( + 'piggybanks.change', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@updatePiggybankRepetitions' + ); + + } + + /** + * + */ + public function updatePiggybankRepetitions() + { + // grab all piggy banks. + $piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get(); + $today = new Carbon; + /** @var \Piggybank $piggy */ + foreach ($piggybanks as $piggy) { + if (count($piggy->piggybankrepetitions) == 0) { + $rep = new \PiggybankRepetition; + $rep->piggybank()->associate($piggy); + $rep->targetdate = $piggy->targetdate; + $rep->startdate = $piggy->startdate; + $rep->currentamount = 0; + try { + $rep->save(); + } catch (QueryException $e) { + } + } + } + unset($piggy, $piggybanks, $rep); + + // grab all repeated transactions. + $repeatedExpenses = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 1)->get(); + /** @var \Piggybank $repeated */ + foreach ($repeatedExpenses as $repeated) { + // loop from start to today or something + $rep = new \PiggybankRepetition; + $rep->piggybank()->associate($repeated); + $rep->startdate = $repeated->startdate; + $rep->targetdate = $repeated->targetdate; + $rep->currentamount = 0; + try { + \Log::debug( + 'Creating initial rep ('.$repeated->name.') (from ' . ($rep->startdate ? $rep->startdate->format('d-m-Y') + : 'NULL') . ' to ' + . ($rep->targetdate ? $rep->targetdate->format('d-m-Y') : 'NULL') . ')' + ); + $rep->save(); + } catch (QueryException $e) { + \Log::error('FAILED initital repetition.'); + } + unset($rep); + + if ($repeated->targetdate <= $today) { + // add 1 month to startdate, or maybe X period, like 3 weeks. + $startTarget = clone $repeated->targetdate; + while ($startTarget <= $today) { + $startCurrent = clone $startTarget; + + // add some kind of period to start current making $endCurrent. + $endCurrent = clone $startCurrent; + switch ($repeated->rep_length) { + default: + die('No rep lengt!'); + break; + case 'day': + $endCurrent->addDays($repeated->rep_every); + break; + case 'week': + $endCurrent->addWeeks($repeated->rep_every); + break; + case 'month': + $endCurrent->addMonths($repeated->rep_every); + break; + case 'year': + $endCurrent->addYears($repeated->rep_every); + break; + } + + $rep = new \PiggybankRepetition; + $rep->piggybank()->associate($repeated); + $rep->startdate = $startCurrent; + $rep->targetdate = $endCurrent; + $rep->currentamount = 0; + $startTarget = $endCurrent; + try { + $rep->save(); + } catch (QueryException $e) { + + } + } + } + } + } +} \ No newline at end of file diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php index dd8e8e7a9d..6256ef861d 100644 --- a/app/models/Piggybank.php +++ b/app/models/Piggybank.php @@ -5,63 +5,66 @@ use LaravelBook\Ardent\Ardent as Ardent; /** * Piggybank * - * @property integer $id + * @property integer $id * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at - * @property integer $account_id + * @property integer $account_id + * @property string $name + * @property float $targetamount * @property \Carbon\Carbon $targetdate - * @property string $name - * @property float $amount - * @property float $target - * @property integer $order - * @property-read \Account $account - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value) - * @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($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 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 \Carbon\Carbon $startdate + * @property boolean $repeats + * @property string $rep_length + * @property integer $rep_every + * @property integer $rep_times + * @property string $reminder + * @property integer $reminder_skip + * @property integer $order + * @property-read \Account $account + * @property-read \Illuminate\Database\Eloquent\Collection|\PiggybankRepetition[] $piggybankrepetitions + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($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 whereRepEvery($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 whereReminderSkip($value) + * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) */ class Piggybank extends Ardent { 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', - 'reminder_skip' => 'required|min:0|max:100', - 'order' => 'required:min:1', + 'account_id' => 'required|exists:accounts,id', // link to Account + 'name' => 'required|between:1,255', // name + 'targetamount' => 'required|min:0', // amount you want to save + 'startdate' => 'date', // when you started + 'targetdate' => 'date', // when its due + 'repeats' => 'required|between:0,1', // does it repeat? + 'rep_length' => 'in:day,week,month,year', // how long is the period? + 'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years. + 'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times + 'reminder' => 'in:day,week,month,year', // want a reminder to put money in this? + 'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months? + 'order' => 'required:min:1', // not yet used. ]; public $fillable = [ 'account_id', 'name', 'targetamount', - 'targetdate', 'startdate', + 'targetdate', 'repeats', 'rep_length', + 'rep_every', 'rep_times', 'reminder', 'reminder_skip', @@ -78,17 +81,18 @@ class Piggybank extends Ardent $today = new Carbon; return [ - 'account_id' => 'factory|Account', - 'name' => 'string', - 'targetamount' => 'required|min:0', - 'targetdate' => $start, - 'startdate' => $today, - 'repeats' => 0, - 'rep_length' => null, - 'rep_times' => 0, - 'reminder' => null, - 'reminder_skip' => 0, - 'order' => 1, + 'account_id' => 'factory|Account', + 'name' => 'string', + 'targetamount' => 'required|min:0', + 'startdate' => $today, + 'targetdate' => $start, + 'repeats' => 0, + 'rep_length' => null, + 'rep_times' => 0, + 'rep_every' => 0, + 'reminder' => null, + 'reminder_skip' => 0, + 'order' => 1, ]; } @@ -105,9 +109,95 @@ class Piggybank extends Ardent */ public function getDates() { - return ['created_at', 'updated_at', 'targetdate']; + return ['created_at', 'updated_at', 'targetdate','startdate']; } + /** + * Firefly shouldn't create piggybank repetions that completely + * lie in the future, so we should be able to safely grab the "latest" + * one and use that to calculate when the user will be reminded. + */ + public function nextReminderDate() + { + if(is_null($this->reminder)) { + return null; + } + /** @var \PiggybankRepetition $rep */ + $rep = $this->currentRelevantRep(); + if($rep) { + $today = new Carbon; + if(is_null($rep->startdate)) { + switch($this->reminder) { + case 'day': + return $today; + break; + case 'week': + return $today->endOfWeek(); + break; + case 'month': + return $today->endOfMonth(); + break; + case 'year': + return $today->endOfYear(); + break; + + } + } else { + // start with the start date + // when its bigger than today, return it: + $start = clone $rep->startdate; + while($start <= $today) { + switch($this->reminder) { + case 'day': + $start->addDay(); + break; + case 'week': + $start->addWeek(); + break; + case 'month': + $start->addMonth(); + break; + case 'year': + $start->addYear(); + break; + + } + } + return $start; + } + // if start date is null, simple switch on + // the reminder period (if any) and go to the end of the period. + + // otherwise, keep jumping until we are past today. + } + + + return new Carbon; + } + + /** + * Grabs the PiggyBankRepetition that's currently relevant / active + */ + public function currentRelevantRep() { + return $this->piggybankrepetitions() + ->where(function ($q) { + $today = new Carbon; + $q->whereNull('startdate'); + $q->orWhere('startdate','<=',$today->format('Y-m-d')); + }) + ->where(function ($q) { + $today = new Carbon; + $q->whereNull('targetdate'); + $q->orWhere('targetdate','>=',$today->format('Y-m-d')); + }) + ->first(); + + + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ public function piggybankrepetitions() { return $this->hasMany('PiggybankRepetition'); diff --git a/app/models/PiggybankRepetition.php b/app/models/PiggybankRepetition.php index b4809fe723..d733b3dc84 100644 --- a/app/models/PiggybankRepetition.php +++ b/app/models/PiggybankRepetition.php @@ -14,13 +14,13 @@ use LaravelBook\Ardent\Ardent as Ardent; * @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) + * @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 { @@ -63,4 +63,5 @@ class PiggybankRepetition extends Ardent return $this->belongsTo('Piggybank'); } + } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 2da63ef17a..5f22ccc253 100644 --- a/app/routes.php +++ b/app/routes.php @@ -202,7 +202,7 @@ Route::group(['before' => 'csrf|auth'], function () { // piggy bank controller Route::post('/piggybanks/store/piggybank',['uses' => 'PiggybankController@storePiggybank','as' => 'piggybanks.store.piggybank']); Route::post('/piggybanks/store/repeated',['uses' => 'PiggybankController@storeRepeated','as' => 'piggybanks.store.repeated']); - Route::post('/piggybanks/update', ['uses' => 'PiggybankController@update','as' => 'piggybanks.update']); + Route::post('/piggybanks/update/{piggybank}', ['uses' => 'PiggybankController@update','as' => 'piggybanks.update']); Route::post('/piggybanks/destroy/{piggybank}', ['uses' => 'PiggybankController@destroy','as' => 'piggybanks.destroy']); // preferences controller diff --git a/app/views/piggybanks/create-repeated.blade.php b/app/views/piggybanks/create-repeated.blade.php index 8d68ccef23..a5af80812f 100644 --- a/app/views/piggybanks/create-repeated.blade.php +++ b/app/views/piggybanks/create-repeated.blade.php @@ -21,9 +21,9 @@
{{$errors->first('name')}}
+{{$errors->first('name')}}
@else - For example: new bike, new camera + For example: new bike, new camera @endif{{$errors->first('targetdate')}}
@else - A dead line is needed to properly repeat this repeated expesnse. + A deadline is needed to properly repeat this repeated expesnse. @endifNo repeated expenses found.
+ @else +
+ {{{$repeated->name}}}++ + Saving up to {{mf($repeated->targetamount)}}. + + + Currently saved + {{mf($piggyBank->currentRelevantRep()->currentamount)}}. + + + @if(!is_null($piggyBank->startdate)) + Start date: {{$piggyBank->currentRelevantRep()->startdate->format('d M Y')}}. + @endif + + + @if(!is_null($piggyBank->targetdate)) + Target date: {{$piggyBank->currentRelevantRep()->targetdate->format('d M Y')}}. + @endif + + @if(!is_null($piggyBank->reminder)) + Next reminder: {{$piggyBank->nextReminderDate()->format('d M Y')}} + @endif + + + + + |
Set targets and save money
-- Saving money is hard. Piggy banks allow you to group money - from an account together. If you also set a target (and a target date) you - can save towards your goals. -
+Field | +Value | +
---|---|
Type (repeats) | ++ @if($piggyBank->repeats == 1) + Repeated expense + @else + Piggy bank + @endif + | +
Name | +{{{$piggyBank->name}}} (#{{$piggyBank->id}}) | +
Account | +{{{$piggyBank->account->name}}} | +
Target amount | +{{mf($piggyBank->targetamount)}} | +
Start date | ++ @if(is_null($piggyBank->startdate)) + No start date + @else + {{$piggyBank->startdate->format('jS F Y')}} + @endif + | +
Target date | ++ @if(is_null($piggyBank->targetdate)) + No target date + @else + {{$piggyBank->targetdate->format('jS F Y')}} + @endif + | +
Repeats every | ++ @if(!is_null($piggyBank->rep_length)) + Every {{$piggyBank->rep_every}} {{$piggyBank->rep_length}}(s) + @if(!is_null($piggyBank->rep_times)) + ({{$piggyBank->rep_times}} times) + @else + (indefinitely) + @endif + @else + Does not repeat + @endif + | +
Reminder | ++ @if(is_null($piggyBank->reminder)) + (no reminder) + @else + Every {{$piggyBank->reminder_skip}} {{$piggyBank->reminder}}(s) + @endif + | +
Field | +Value | +
---|---|
ID | +#{{$rep->id}} | +
Current amount | +{{mf($rep->currentamount)}} | +
Start date | ++ @if(is_null($rep->startdate)) + No start date + @else + {{$rep->startdate->format('jS F Y')}} + @endif + | +
Target date | ++ @if(is_null($rep->targetdate)) + No target date + @else + {{$rep->targetdate->format('jS F Y')}} + @endif + | +