Completed
Push — master ( d3922b...937be3 )
by Christopher
01:16
created

Task::deductHours()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Models;
4
5
use Chriscreates\Projects\Traits\HasPriority;
6
use Chriscreates\Projects\Traits\IsRecordable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphMany;
10
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11
12
class Task extends Model
13
{
14
    use IsRecordable, HasPriority;
15
16
    protected $table = 'tasks';
17
18
    public $primaryKey = 'id';
19
20
    public $guarded = ['id'];
21
22
    public $timestamps = true;
23
24
    protected $casts = [
25
        'complete' => 'bool',
26
        'deduct_hours' => 'integer',
27
    ];
28
29
    public function creator() : BelongsTo
30
    {
31
        return $this->belongsTo(get_class(user_model()));
32
    }
33
34
    public function priority() : BelongsTo
35
    {
36
        return $this->belongsTo(Priority::class);
37
    }
38
39
    public function projects() : MorphToMany
40
    {
41
        return $this->morphToMany(Project::class, 'projectable');
42
    }
43
44
    public function records() : MorphMany
45
    {
46
        return $this->morphMany(Record::class, 'recordable');
47
    }
48
49
    public function comments() : MorphMany
50
    {
51
        return $this->morphMany(Comment::class, 'commentable');
52
    }
53
54
    public function assignTo($project) : void
55
    {
56
        $this->projects()->save($project);
57
58
        $this->refresh();
59
    }
60
61
    public function markCompletion(bool $bool) : void
62
    {
63
        $this->update(['complete' => $bool]);
64
    }
65
66
    public function completed() : bool
67
    {
68
        return $this->attributes['complete'];
69
    }
70
71
    public function recordTime($from, $to) : void
72
    {
73
        $this->records()->create([
74
            'time_from' => $from,
75
            'time_to' => $to,
76
        ]);
77
78
        $this->refresh();
79
    }
80
81
    public function removeRecord(Record $record) : void
82
    {
83
        if ($record = $this->records->firstWhere('id', $record->id)) {
84
            $record->delete();
85
        }
86
87
        $this->refresh();
88
    }
89
90
    public function deductHours($time) : void
91
    {
92
        $this->records()->create([
93
            'deduct_hours' => (float) $time,
94
            'deductable' => true,
95
        ]);
96
97
        $this->refresh();
98
    }
99
100
    public function addHours($time) : void
101
    {
102
        $this->records()->create([
103
            'add_hours' => (float) $time,
104
            'deductable' => false,
105
        ]);
106
107
        $this->refresh();
108
    }
109
}
110