1 | <?php |
||
2 | |||
3 | namespace App; |
||
4 | |||
5 | use App\TimeLog; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Cviebrock\EloquentSluggable\Sluggable; |
||
8 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
9 | |||
10 | class Project extends Model |
||
11 | { |
||
12 | use SoftDeletes, Sluggable; |
||
13 | |||
14 | /* @var array $fillable The fields which are mass assignable */ |
||
15 | protected $fillable = ['title', 'description', 'user_id', 'total_seconds']; |
||
16 | |||
17 | /* @var array $with The relationships which we'll always load */ |
||
18 | public $with = ['timelogs']; |
||
19 | |||
20 | /** |
||
21 | * Return the sluggable configuration array for this model. |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | public function sluggable() |
||
26 | { |
||
27 | return [ |
||
28 | 'slug' => [ |
||
29 | 'source' => 'title' |
||
30 | ] |
||
31 | ]; |
||
32 | } |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Set the route key name for the Project Model |
||
37 | */ |
||
38 | public function getRouteKeyName() |
||
39 | { |
||
40 | return 'slug'; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * A project exposes it's URL |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function url() |
||
49 | { |
||
50 | return route('projects.show', $this); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * A project can have many timelogs |
||
55 | * |
||
56 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
57 | */ |
||
58 | public function timelogs() |
||
59 | { |
||
60 | return $this->hasMany(TimeLog::class)->latest('created_at'); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * A project can have many milestones |
||
65 | * |
||
66 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
67 | */ |
||
68 | public function milestones() |
||
69 | { |
||
70 | return $this->hasMany(ProjectMilestone::class); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * A project belongs to a user |
||
75 | * |
||
76 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
77 | */ |
||
78 | public function user() |
||
79 | { |
||
80 | return $this->belongsTo(User::class); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Convert hours to seconds for database storage |
||
85 | */ |
||
86 | public function setTotalSecondsAttribute($hours) |
||
87 | { |
||
88 | // if (env('APP_ENV') !== 'testing') { |
||
89 | $this->attributes['total_seconds'] = $hours * 3600; |
||
90 | // } else { |
||
91 | // $this->attributes['total_seconds'] = $hours; |
||
92 | // } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Convert seconds back to hours for display |
||
97 | * |
||
98 | * @return float |
||
99 | */ |
||
100 | public function getTotalSecondsAttribute($totalSeconds) |
||
101 | { |
||
102 | // if (env('APP_ENV') !== 'testing') { |
||
103 | return $totalSeconds / 3600; |
||
104 | // } else { |
||
105 | // return $totalSeconds; |
||
106 | // } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * A project exposes how much time has been quoted |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getTimeEstimatedAttribute() |
||
115 | { |
||
116 | return timeDiffForHumans($this->getOriginal('total_seconds')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
117 | } |
||
118 | |||
119 | /** |
||
120 | * A project exposes how much time has been logged |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getTimeLoggedAttribute() |
||
125 | { |
||
126 | return timeDiffForHumans($this->timelogs->sum('number_of_seconds')); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * A project exposes how much time has been logged (seconds) |
||
131 | * |
||
132 | * @return int |
||
133 | */ |
||
134 | public function getTimeLoggedSecondsAttribute() |
||
135 | { |
||
136 | return $this->timelogs->sum('number_of_seconds'); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * A project exposes how much of the percentage of time has been logged |
||
141 | * |
||
142 | * @return float |
||
143 | */ |
||
144 | public function getPercentageTakenAttribute() |
||
145 | { |
||
146 | $onePercent = $this->getOriginal('total_seconds') / 100; |
||
147 | $percentage = min(100, ceil($this->time_logged_seconds / $onePercent)); |
||
148 | |||
149 | return $percentage; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * A project exposes how much of the percentage of time has been logged |
||
154 | * |
||
155 | * @return float |
||
156 | */ |
||
157 | public function getPercentageRemainingAttribute() |
||
158 | { |
||
159 | return (100 - $this->percentage_taken); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * A project exposes how many milestones were completed |
||
164 | * |
||
165 | * @return float |
||
166 | */ |
||
167 | public function getCompletedMilestonesAttribute() |
||
168 | { |
||
169 | return $this->milestones()->whereNotNull('completed_at')->count(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * A project exposes how much it is worth |
||
174 | * |
||
175 | * @return float |
||
176 | */ |
||
177 | public function getTotalCostQuotedAttribute() |
||
178 | { |
||
179 | $total = $this->user->settings->hourly_rate * floor($this->getOriginal('total_seconds') / 3600); |
||
180 | return $total; |
||
181 | } |
||
182 | } |
||
183 |