-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeSummary.php
76 lines (63 loc) · 2.63 KB
/
EmployeeSummary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace SampleProject\Domain;
use Spark\Adr\DomainInterface;
use Spark\Payload;
use SampleProject\Domain\Database\Employee;
use SampleProject\Domain\Database\Shift;
use Carbon\Carbon;
use SampleProject\Payload\SuccessPayload;
use SampleProject\Payload\ErrorPayload;
use SampleProject\Payload\InvalidPayload;
use SampleProject\Auth;
class EmployeeSummary implements DomainInterface
{
public function __invoke(array $input)
{
$employeeId = $input['employeeId'];
$startTime = Carbon::now()->startOfYear();
$endTime = Carbon::now()->endOfYear();
// Make sure this employee isn't looking at another employee's information
if(Auth::isEmployee() && Auth::getId() != $employeeId) {
return InvalidPayload::create('You can not list the summary for another employee');
}
if(!empty($input['startTime'])) {
$startTime = $input['startTime'];
}
if(!empty($input['endTime'])) {
$endTime = $input['endTime'];
}
$Employee = new Employee();
if(!$Employee->load($employeeId)) {
return ErrorPayload::create('Employee not found');
}
// limit filtered results to this employee and within the start/end times
$filters = [];
$filters[] = ['key' => 'employee_id', 'value' => $employeeId];
$filters[] = ['key' => 'start_time', 'operator' => '>=', 'value' => $startTime];
$filters[] = ['key' => 'end_time', 'operator' => '<=', 'value' => $endTime];
$summary = [];
$Shift = new Shift();
if($Shifts = $Shift->filter($startTime, $endTime, $filters)) {
foreach($Shifts as $Shift) {
$startTime = Carbon::createFromFormat('Y-m-d H:i:s', $Shift['startTime']);
$endTime = Carbon::createFromFormat('Y-m-d H:i:s', $Shift['endTime']);
$startOfWeek = max($startTime->copy()->startOfWeek(), $startTime->copy()->startOfYear());
$endOfWeek = min($endTime->copy()->endOfWeek(), $endTime->copy()->endOfYear());
$weekDates = $startOfWeek->format('Y-m-d') . ' to ' . $endOfWeek->format('Y-m-d');
$diff = $endTime->diff($startTime);
$timeWorked = $diff->d * 24 + $diff->h + $diff->i / 60;
$timeWorked = $timeWorked - $Shift['break'];
$summary[$weekDates] = @$summary[$weekDates] + $timeWorked;
}
foreach($summary as $week => $hours) {
$summary[$week] = "$hours hour" . (($hours == 1) ? '' : 's');
}
}
if(count($summary)) {
return SuccessPayload::create($summary);
}
else {
return ErrorPayload::create('No shifts found');
}
}
}