diff --git a/src/FuturePlanner.php b/src/FuturePlanner.php index 80c00e8..76b890a 100644 --- a/src/FuturePlanner.php +++ b/src/FuturePlanner.php @@ -66,19 +66,17 @@ public function plan(array $attributes) * @param boolean $needed * Set to false to remove the approval need from the future * - * @return \Dixie\EloquentModelFuture\Models\Future + * @return \Dixie\EloquentModelFuture\FuturePlanner */ public function needsApproval($needed=true) { if($needed) { - $this->newFuture->needs_approve = true; + $this->newFuture->needs_approval = true; } else { - $this->newFuture->needs_approve = null; + $this->newFuture->needs_approval = null; } - $this->newFuture->save(); - - return $this->newFuture; + return $this; } /** diff --git a/src/Traits/HasFuture.php b/src/Traits/HasFuture.php index eda0ce0..d816832 100644 --- a/src/Traits/HasFuture.php +++ b/src/Traits/HasFuture.php @@ -83,11 +83,15 @@ public function commitFuturePlan(Future $futurePlan) /** * Approve to the presented result of the model * + * @param Carbon Until what date should futures be approved * @return boolean */ - public function approve() + public function approveUntil(Carbon $date = null) { - $this->future()->getPlansUntil(Carbon::now()) + if (is_null($date)) { + $date = Carbon::now(); + } + $this->future()->getPlansUntil($date) ->each([$this, 'approveFuturePlan']); return $this->save(); @@ -100,9 +104,10 @@ public function approve() */ public function approveFuturePlan(Future $futurePlan) { - $futurePlan->approved_at = Carbon::now(); - $futurePlan->approver()->associate(Auth::user()); - + if ($futurePlan->needs_approval && is_null($futurePlan->approved_at)) { + $futurePlan->approved_at = Carbon::now(); + $futurePlan->approver()->associate(Auth::user()); + } return $futurePlan->save(); } }