1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\Mail; |
8
|
|
|
use SET\Duty; |
9
|
|
|
use SET\Handlers\Duty\DutyList; |
10
|
|
|
use SET\Mail\DutyToday; |
11
|
|
|
use SET\Mail\DutyUpcoming; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Processes the emails for upcoming employees working security checks. Also notifies FSO |
15
|
|
|
* Class UpdateDuty. |
16
|
|
|
*/ |
17
|
|
|
class UpdateDuty extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The name and signature of the console command. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = 'duty:update'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Updates the duty roster and sends out email notifications to users.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @return UpdateDuty |
37
|
|
|
*/ |
38
|
|
|
public function handle() |
39
|
|
|
{ |
40
|
|
|
$duties = Duty::all(); |
41
|
|
|
foreach ($duties as $duty) { |
42
|
|
|
$emailList = (new DutyList($duty))->scheduledUpdate(); |
43
|
|
|
$this->processEmailList($emailList, $duty); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function processEmailList($emailList, $duty) |
50
|
|
|
{ |
51
|
|
|
foreach ($emailList as $usersDateArray) { |
52
|
|
|
if ($usersDateArray['date'] == Carbon::today()->format('Y-m-d')) { |
53
|
|
|
foreach ($usersDateArray['users'] as $user) { |
54
|
|
|
Mail::to($user)->send(new DutyToday($duty, $user)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->isReadyForNotification($duty, $usersDateArray)) { |
59
|
|
|
$this->sendUsersUpcomingEmailNotification($duty, $usersDateArray); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $duty |
66
|
|
|
* @param $usersDateArray |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
private function isReadyForNotification($duty, $usersDateArray) |
71
|
|
|
{ |
72
|
|
|
return ($duty->cycle == 'weekly' && $usersDateArray['date'] == Carbon::today()->addWeeks(2)->format('Y-m-d')) |
73
|
|
|
|| ($duty->cycle == 'daily' && $usersDateArray['date'] == Carbon::today()->addWeeks(1)->format('Y-m-d')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $duty |
78
|
|
|
* @param $usersDateArray |
79
|
|
|
* |
80
|
|
|
* @return UpdateDuty |
81
|
|
|
*/ |
82
|
|
|
private function sendUsersUpcomingEmailNotification($duty, $usersDateArray) |
83
|
|
|
{ |
84
|
|
|
foreach ($usersDateArray['users'] as $user) { |
85
|
|
|
Mail::to($user)->send(new DutyUpcoming($duty, $user, $usersDateArray['date'])); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|