diff --git a/app/Entities/Projects/Project.php b/app/Entities/Projects/Project.php index 38f86f3..7dfc082 100755 --- a/app/Entities/Projects/Project.php +++ b/app/Entities/Projects/Project.php @@ -224,4 +224,24 @@ class Project extends Model ->with('worker', 'tasks') ->get(); } + + public function getWorkDurationAttribute() + { + $startDate = $this->start_date; + $endDate = $this->end_date; + + if (is_null($endDate)) { + return '-'; + } + + $workDuration = dateDifference($startDate, $endDate); + + if ((int) $workDuration > 365) { + return dateDifference($startDate, $endDate, '%y Year(s) %m Month(s)'); + } elseif ((int) $workDuration > 30) { + return dateDifference($startDate, $endDate, '%m Month(s) %d Day(s)'); + } + + return $workDuration.' Day(s)'; + } } diff --git a/app/Entities/Projects/ProjectPresenter.php b/app/Entities/Projects/ProjectPresenter.php index f48ca05..0678ba7 100644 --- a/app/Entities/Projects/ProjectPresenter.php +++ b/app/Entities/Projects/ProjectPresenter.php @@ -21,18 +21,4 @@ class ProjectPresenter extends Presenter { return ProjectStatus::getNameById($this->entity->status_id); } - - public function workDuration() - { - if (is_null($this->entity->end_date)) { - return '-'; - } - - $workDuration = dateDifference($this->entity->start_date, $this->entity->end_date); - if ((int) $workDuration > 30) { - return dateDifference($this->entity->start_date, $this->entity->end_date, '%m Bulan %d Hari'); - } - - return $workDuration.' Hari'; - } } diff --git a/resources/views/customers/projects.blade.php b/resources/views/customers/projects.blade.php index 04ba086..3da94f7 100755 --- a/resources/views/customers/projects.blade.php +++ b/resources/views/customers/projects.blade.php @@ -20,7 +20,7 @@ {{ 1 + $key }} {{ $project->nameLink() }} {{ $project->start_date }} - {{ $project->present()->workDuration }} + {{ $project->work_duration }} {{ formatRp($project->project_value) }} {{ $project->present()->status }} diff --git a/resources/views/projects/index.blade.php b/resources/views/projects/index.blade.php index a9c1556..f5027bd 100755 --- a/resources/views/projects/index.blade.php +++ b/resources/views/projects/index.blade.php @@ -42,7 +42,7 @@ {{ $projects->firstItem() + $key }} {{ $project->nameLink() }} {{ $project->start_date }} - {{ $project->present()->workDuration }} + {{ $project->work_duration }} @if (request('status_id') == 2) {{ formatDecimal($project->getJobOveralProgress()) }} % {{ $project->due_date }} diff --git a/resources/views/users/projects.blade.php b/resources/views/users/projects.blade.php index 671dce4..34e1075 100755 --- a/resources/views/users/projects.blade.php +++ b/resources/views/users/projects.blade.php @@ -29,7 +29,7 @@ {{ $projects->firstItem() + $key }} {{ $project->nameLink() }} {{ $project->start_date }} - {{ $project->present()->workDuration }} + {{ $project->work_duration }} {{ formatRp($project->project_value) }} {{ $project->present()->status }} {{ $project->customer->name }} diff --git a/tests/Unit/Models/ProjectTest.php b/tests/Unit/Models/ProjectTest.php index 5811409..6fb19d3 100644 --- a/tests/Unit/Models/ProjectTest.php +++ b/tests/Unit/Models/ProjectTest.php @@ -209,4 +209,26 @@ class ProjectTest extends TestCase $this->assertInstanceOf(Collection::class, $project->comments); $this->assertInstanceOf(Comment::class, $project->comments->first()); } + + /** @test */ + public function project_has_work_duration_attribute() + { + $project = factory(Project::class)->create([ + 'start_date' => '2016-06-10', + 'end_date' => '2016-07-21', + ]); + + $this->assertEquals('1 Month(s) 11 Day(s)', $project->work_duration); + } + + /** @test */ + public function project_work_duration_attribute_returns_proper_multi_years_work_duration() + { + $project = factory(Project::class)->create([ + 'start_date' => '2015-04-10', + 'end_date' => '2017-07-21', + ]); + + $this->assertEquals('2 Year(s) 3 Month(s)', $project->work_duration); + } }