Skip to content

Commit

Permalink
Fixing a bug that caused an incorrect valid_end to be set when a sche…
Browse files Browse the repository at this point in the history
…duled commission was created that overlapped with a current.

When there was a current commission with a valid end and a scheduled commission was created that had a valid start less than the valid end the current commissions end date was being incorrectly set to now instead of the start of the scheduled.
  • Loading branch information
joshforbes committed Jan 19, 2017
1 parent 6dea8df commit 04eae35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,18 @@ protected function startCannotBeAfterEnd()
protected function removeSchedulingConflicts()
{
if (is_null($this->valid_end)) {
$conflicts = $this->getQuery()->where('valid_start', '>', Carbon::now())->get();
$this->deleteTemporals($conflicts);
return;
return $this->getQuery()->where('valid_start', '>', $this->valid_start)->delete();
}

$conflicts = $this->getQuery()
return $this->getQuery()
->where('valid_start', '<', $this->valid_end)
->where(function ($query) {
$query->whereNull('valid_end')
->orWhere('valid_end', '>', $this->valid_start);
})
->get();

$this->deleteTemporals($conflicts);
}

/**
* Deletes the provided temporal objects. Delete is called on the object
* itself so that the observers will be called properly.
*
* @param $temporals
*/
protected function deleteTemporals($temporals)
{
collect($temporals)->each(function($temporal) {
$temporal->delete();
});
->update([
'valid_end' => $this->valid_start
]);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/TemporalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ public function testItOnlyRemovesAScheduledCommissionWhenThereIsAConflict()
$this->assertNotNull($scheduledCommission->fresh());
}

/**
* Tests...
*/
public function testItCorrectlySetsTheValidEndOfTheCurrentWhenThereIsASchedulingConflict()
{
$currentCommission = TemporalTestCommission::create([
'id' => 3,
'agent_id' => 1,
'valid_start' => Carbon::now(),
'valid_end' => Carbon::now()->addDays(20)
]);
TemporalTestCommission::create([
'id' => 2,
'agent_id' => 1,
'valid_start' => Carbon::now()->addDays(15),
'valid_end' => null
]);

$this->assertEquals(Carbon::now()->addDays(15), $currentCommission->fresh()->valid_end);
}

/**
* Tests...
*/
Expand Down

0 comments on commit 04eae35

Please sign in to comment.