Completed
Push — master ( facca3...10e5d4 )
by Sheela
02:27
created

RenewTraining::renewedAlready()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SET\Console\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Event;
10
use Illuminate\Support\Facades\Log;
11
use SET\Events\TrainingAssigned;
12
use SET\TrainingUser;
13
14
class RenewTraining extends Command
15
{
16
    /**
17
     * The number of days to renew a training before it expires.
18
     *
19
     * @var int
20
     */
21
    protected $offset = 30;
22
23
    /**
24
     * List of all renewed trainings.
25
     *
26
     * @var Collection
27
     */
28
    protected $trainingAdminRecord;
29
30
    /**
31
     * The name and signature of the console command.
32
     *
33
     * @var string
34
     */
35
    protected $signature = 'training:renew';
36
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Renews training before it expires.';
43
44
    /**
45
     * Create a new command instance.
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct();
50
        $this->trainingAdminRecord = new Collection();
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return RenewTraining
57
     */
58
    public function handle()
59
    {
60
        $trainingUsers = TrainingUser::with('user', 'training')
61
            ->where('due_date', '<', Carbon::today())
62
            ->where(function($query) {
63
                $query->whereNull('stop_renewal')
64
                      ->orWhere('stop_renewal', 0);
65
            })
66
            ->RenewableTrainings()  // Training is renewed (positive renews_in)            
67
            ->activeUsers()
68
            ->orderBy(DB::raw('CASE WHEN completed_date IS NULL THEN 0 ELSE 1 END'))
69
            ->orderBy('completed_date', 'desc')
70
            ->get()
71
            ->unique(function ($item) {
72
                return $item['user_id'].'-'.$item['training_id'];
73
            });
74
        
75
        foreach ($trainingUsers as $trainingUser) {
76
            if (!$this->renewedAlready($trainingUser) && $this->timeToRenew($trainingUser)) {
77
                $this->processRenewal($trainingUser);
78
            }
79
        }
80
81
        return $this;
82
    }
83
84
    public function getList()
85
    {
86
        return $this->trainingAdminRecord->sortBy('userFullName');
87
    }
88
89
    /**
90
     * Check if training note has been renewed already.
91
     *
92
     * @param $trainingUser
93
     *
94
     * @return bool
95
     */
96
    private function renewedAlready($trainingUser)
97
    {
98
        if (is_null($trainingUser->completed_date)) {
99
            return true;
100
        }
101
102
        // Where due_date is in future or withing past renewal period
103
        $trainingRecord = TrainingUser::where('training_id', $trainingUser->training_id)
104
            ->where('user_id', $trainingUser->user_id)
105
            ->where('due_date', '>', Carbon::today())
106
            ->get();
107
        
108
        return !($trainingRecord->isEmpty());
109
    }
110
111
    /**
112
     * Check if the training is past the renews_in value.
113
     *
114
     * @param $trainingUser
115
     *
116
     * @return bool
117
     */
118
    private function timeToRenew($trainingUser)
119
    {
120
        if ($trainingUser->training->renews_in == 0) {
121
            return false;
122
        }
123
        if (!$trainingUser->completed_date) {
124
            return false;
125
        }
126
127
        $today = Carbon::today();
128
129
        $renewalDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date)
130
            ->addDays($trainingUser->training->renews_in);
131
132
        return ($renewalDate <= $today->addDays($this->offset));
133
    }
134
135
    /**
136
     * Generate a new Training note that will be due $this->offset days from now.
137
     *
138
     * @param $trainingUser
139
     */
140
    private function processRenewal($trainingUser)
141
    {
142
        $dueDate = Carbon::createFromFormat('Y-m-d', $trainingUser->completed_date)
143
            ->addDays($trainingUser->training->renews_in);
144
    
145
        $assignedTraining = $this->createRecord($trainingUser, $dueDate);
146
147
        //Email user of new training is due
148
        Event::fire(new TrainingAssigned($assignedTraining));
149
        Log::info('User training renewal notification.', [
150
            'user_id'       => $assignedTraining->user_id,
151
            'user_name'     => $assignedTraining->user->userFullName,
152
            'training_id'   => $assignedTraining->training_id,
153
            'training_name' => $assignedTraining->training->name,
154
            'due_date'      => $assignedTraining->due_date,
155
        ]);
156
157
        // Add the created traininguser record to trainingAdminRecord
158
        $this->trainingAdminRecord->push($assignedTraining);
159
    }
160
161
    /**
162
     * @param $trainingUser
163
     * @param Carbon $dueDate
164
     *
165
     * @return TrainingUser
166
     */
167
    private function createRecord($trainingUser, $dueDate)
168
    {
169
        $newNote = TrainingUser::create([
170
            'user_id'        => $trainingUser->user_id,
171
            'author_id'      => 1,
172
            'training_id'    => $trainingUser->training_id,
173
            'due_date'       => $dueDate->toDateString(),
174
            'completed_date' => null,
175
        ]);
176
177
        return $newNote;
178
    }
179
}
180