-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmissionTime.php
More file actions
185 lines (173 loc) · 6.83 KB
/
AdmissionTime.php
File metadata and controls
185 lines (173 loc) · 6.83 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Components\Admissions;
use Components\NDatabase\NDatabase;
class AdmissionTime
{
private $admissionStartDate;
private $admissionEndDate;
private $admissionLength;
private $clinicId;
private $doctorId;
private $admissionId;
private $dtFrom;
private $dtTo;
private $dtLength;
public function __construct($date, $length, $clinicId, $userId, $admissionId = 0)
{
$this->admissionStartDate = $date;
$this->admissionLength = $length;
$this->clinicId = $clinicId;
$this->doctorId = $userId;
$this->admissionId = $admissionId;
}
public function isAllowedAdmissionTime()
{
$this->setStartEndDateTime();
if ($this->isBusyTime()) {
return $this->getAdmissionTimeOffer();
} else {
return [
'result' => true
];
}
}
private function setStartEndDateTime()
{
$this->admissionEndDate = NDatabase::getOne(
"SELECT DATE_ADD(:dateFrom, INTERVAL TIME_TO_SEC(:length) SECOND) AS date_to",
[':dateFrom' => $this->admissionStartDate, ':length' => $this->admissionLength]
);
$this->dtFrom = NDatabase::getOne(
"SELECT DATE_FORMAT(:date, '%Y-%m-%d %H:%i')",
[':date' => $this->admissionStartDate]
);
$this->dtTo = NDatabase::getOne(
"SELECT DATE_FORMAT(:date, '%Y-%m-%d %H:%i')",
[':date' => $this->admissionEndDate]
);
$this->dtLength = NDatabase::getOne(
"SELECT TIME_TO_SEC(:length)",
[':length' => $this->admissionLength]
);
}
private function isBusyTime(): bool
{
$cnt = (int) NDatabase::getOne(
"SELECT
COUNT(1)
FROM admission a
WHERE a.clinic_id = :clinicId
AND a.user_id = :doctorId
AND a.id <> :admissionId
AND DATE(a.admission_date) = DATE(:admissionStartDate)
AND a.status NOT IN('accepted','deleted','delayed')
AND (
UNIX_TIMESTAMP(:admissionStartDate) >= UNIX_TIMESTAMP(a.admission_date)
AND
UNIX_TIMESTAMP(:admissionStartDate) < UNIX_TIMESTAMP(DATE_ADD(a.admission_date, INTERVAL TIME_TO_SEC(a.admission_length)SECOND))
)
",
[
':clinicId' => $this->clinicId,
':doctorId' => $this->doctorId,
':admissionId' => $this->admissionId,
':admissionStartDate' => $this->admissionStartDate,
]
);
return $cnt > 0;
}
private function getTimeSheets(): array
{
$timeSheets = NDatabase::getAllAssoc(
"SELECT
DATE_FORMAT(
GREATEST(t.`begin_datetime`, NOW()), '%Y-%m-%d %H:%i:00'
) AS date_from,
t.`end_datetime` AS date_to
FROM timesheet t
JOIN timesheet_types tt ON tt.`id` = t.`type` AND tt.`is_working_hours` = 1
WHERE t.clinic_id = :clinicId
AND t.doctor_id = :userId
AND (DATE(t.`begin_datetime`) = DATE(:date) OR DATE(t.`end_datetime`) = DATE(:date))
AND t.`end_datetime` > NOW()",
[
':clinicId' => $this->clinicId,
':userId' => $this->doctorId,
':date' => $this->dtFrom,
]
);
if (sizeof($timeSheets) == 0) {
$timeSheets = NDatabase::getAllAssoc("SELECT NOW() AS date_from, NOW() + INTERVAL 1 DAY AS date_to");
}
return $timeSheets;
}
private function getAdmissions($timeSheetDateFrom, $timeSheetDateTo): array
{
return NDatabase::getAllAssoc(
"SELECT
DATE_FORMAT(GREATEST(a.`admission_date`, NOW()), '%Y-%m-%d %H:%i:00') AS date_from,
a.`admission_date` + INTERVAL TIME_TO_SEC(a.`admission_length`) SECOND AS date_to
FROM admission a
WHERE a.`user_id` = :userId
AND a.`clinic_id` = :clinicId
AND a.`admission_date` < :dateTo
AND a.`admission_date` + INTERVAL TIME_TO_SEC(a.`admission_length`) SECOND > :dateFrom
AND a.id <> :admissionId
AND a.status NOT IN('accepted','deleted','delayed')
ORDER BY a.admission_date, a.admission_length;
",
[
':userId' => $this->doctorId,
':clinicId' => $this->clinicId,
':admissionId' => $this->admissionId,
':dateFrom' => $timeSheetDateFrom,
':dateTo' => $timeSheetDateTo
]
);
}
private function getAdmissionTimeOffer(): array
{
$timeSheets = $this->getTimeSheets();
$dtCurrentFrom = null;
$dtCurrentTo = null;
foreach ($timeSheets as $timesheet) {
$dtTimesheetFrom = new \DateTime($timesheet['date_from']);
$dtTimesheetTo = new \DateTime($timesheet['date_to']);
$dtCurrentFrom = $dtTimesheetFrom;
$dtCurrentTo = $dtTimesheetTo;
if ($dtTimesheetTo->getTimestamp() - $dtTimesheetFrom->getTimestamp() < $this->dtLength) {
continue;
}
$admissions = $this->getAdmissions($timesheet['date_from'], $timesheet['date_to']);
foreach ($admissions as $admission) {
$dtAdmissionFrom = new \DateTime($admission['date_from']);
$dtAdmissionTo = new \DateTime($admission['date_to']);
$dtCurrentTo = $dtAdmissionFrom;
$length = $dtCurrentTo->getTimestamp() - $dtCurrentFrom->getTimestamp();
if ($length < $this->dtLength) {
$dtCurrentFrom = $dtAdmissionTo;
$dtCurrentTo = $dtTimesheetTo;
} else {
$dtCurrentTo->setTimestamp($dtCurrentFrom->getTimestamp() + $this->dtLength);
return [
'result' => false,
'clear_dt_from' => $dtCurrentFrom->format('Y-m-d H:i:s'),
'clear_dt_to' => $dtCurrentTo->format('Y-m-d H:i:s'),
];
}
}
$length = $dtCurrentTo->getTimestamp() - $dtCurrentFrom->getTimestamp();
if ($length >= $this->dtLength) {
$dtCurrentTo->setTimestamp($dtCurrentFrom->getTimestamp() + $this->dtLength);
return [
'result' => false,
'clear_dt_from' => $dtCurrentFrom->format('Y-m-d H:i:s'),
'clear_dt_to' => $dtCurrentTo->format('Y-m-d H:i:s'),
];
}
}
return [
'result' => false
];
}
}