AppointmentController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Helpers\TimeLine;
  4. use App\Models\Appointment;
  5. use App\Models\BDTDevice;
  6. use App\Models\CareMonth;
  7. use App\Models\Client;
  8. use App\Models\ClientInfoLine;
  9. use App\Models\Facility;
  10. use App\Models\NoteTemplate;
  11. use App\Models\Pro;
  12. use App\Models\ProGeneralAvailability;
  13. use App\Models\ProSpecificAvailability;
  14. use App\Models\ProSpecificUnavailability;
  15. use App\Models\SectionTemplate;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\File;
  18. class AppointmentController extends Controller
  19. {
  20. public function events(Request $request)
  21. {
  22. if(empty($request->get('proIds'))) {
  23. $proIds = [];
  24. }
  25. else {
  26. $proIds = explode(',', $request->get('proIds'));
  27. }
  28. $start = $request->get('start');
  29. $end = $request->get('end');
  30. $clientId = $request->get('clientId');
  31. $timeZone = $request->get('timeZone');
  32. // get appointments
  33. $appointments = Appointment
  34. ::where('start_time', '>=', $start)
  35. ->where('start_time', '<=', $end);
  36. if(count($proIds)) {
  37. $appointments = $appointments
  38. ->where(function ($query) use ($proIds, $clientId) {
  39. $query
  40. ->whereIn('pro_id', $proIds)
  41. ->orWhere('client_id', '=', $clientId)
  42. ->orWhereRaw('client_id IN (SELECT shadow_client_id FROM pro WHERE id IN (' . implode(',', $proIds) . '))');
  43. });
  44. }
  45. $appointments = $appointments->get();
  46. $events = [];
  47. foreach ($appointments as $appointment) {
  48. // default - as used from patient calendar
  49. $title = ($appointment->client->id != $clientId ? '* ' : '') . $appointment->pro->displayName() .
  50. " (" . strtolower($appointment->status) . ")";
  51. if($clientId == "-1") {
  52. $shortStatus = '';
  53. switch($appointment->status) {
  54. case 'CREATED':
  55. $shortStatus = 'new';
  56. break;
  57. case 'CONFIRMED':
  58. $shortStatus = 'conf';
  59. break;
  60. case 'CANCELLED':
  61. $shortStatus = 'canc';
  62. break;
  63. case 'COMPLETED':
  64. $shortStatus = 'done';
  65. break;
  66. case 'ABANDONED':
  67. $shortStatus = 'aban';
  68. break;
  69. case 'REJECTED':
  70. $shortStatus = 'rej';
  71. break;
  72. }
  73. if(count($proIds) === 1) {
  74. $title = "($shortStatus) " . $appointment->client->displayName();
  75. }
  76. else {
  77. $title = '[' . $appointment->pro->initials() . '] ' .
  78. "($shortStatus) " . $appointment->client->displayName();
  79. }
  80. }
  81. $events[] = [
  82. "type" => "appointment",
  83. "title" => $title,
  84. "_title" => $appointment->title,
  85. "description" => $appointment->description,
  86. "clientName" => $appointment->client->displayName(),
  87. "appointmentUid" => $appointment->uid,
  88. "clientUid" => $appointment->client->uid,
  89. "proId" => $appointment->pro->id,
  90. "proUid" => $appointment->pro->uid,
  91. "isTrainingEvent" => !!$appointment->client->shadowOfPro,
  92. "proName" => $appointment->pro->displayName(),
  93. "start" => convertToTimezone($appointment->start_time, $timeZone),
  94. "end" => convertToTimezone($appointment->end_time, $timeZone),
  95. "clientOnly" => !in_array($appointment->pro->id, $proIds),
  96. "otherClient" => ($appointment->client->id != $clientId),
  97. "status" => $appointment->status,
  98. "editable" => true
  99. ];
  100. }
  101. if(count($proIds)) {
  102. // get availability
  103. $genAvail = ProGeneralAvailability
  104. ::where('is_cancelled', false)
  105. ->whereIn('pro_id', $proIds)
  106. ->get();
  107. $specAvail = ProSpecificAvailability
  108. ::where('is_cancelled', false)
  109. ->whereIn('pro_id', $proIds)
  110. ->where(function ($query) use ($start, $end) {
  111. $query
  112. ->where(function ($query2) use ($start, $end) {
  113. $query2
  114. ->where('start_time', '>=', $start)
  115. ->where('start_time', '<=', $end);
  116. })
  117. ->orWhere(function ($query2) use ($start, $end) {
  118. $query2
  119. ->where('end_time', '>=', $start)
  120. ->where('end_time', '<=', $end);
  121. });
  122. })
  123. ->get();
  124. $specUnavail = ProSpecificUnavailability
  125. ::where('is_cancelled', false)
  126. ->whereIn('pro_id', $proIds)
  127. ->where(function ($query) use ($start, $end) {
  128. $query
  129. ->where(function ($query2) use ($start, $end) {
  130. $query2
  131. ->where('start_time', '>=', $start)
  132. ->where('start_time', '<=', $end);
  133. })
  134. ->orWhere(function ($query2) use ($start, $end) {
  135. $query2
  136. ->where('end_time', '>=', $start)
  137. ->where('end_time', '<=', $end);
  138. });
  139. })
  140. ->get();
  141. // logic
  142. // 1. enumerate days between start and end (inclusive)
  143. // 2. for each pro
  144. // 3. calculate pairs of start-end of availability
  145. // 1. enumerate days between start and end (inclusive)
  146. $phpTZ = appTZtoPHPTZ($timeZone);
  147. $phpTZObject = new \DateTimeZone($phpTZ);
  148. $startDate = new \DateTime($start, $phpTZObject);
  149. $availabilityStartDate = new \DateTime(date('Y-m-d 00:00:00'), $phpTZObject);
  150. $endDate = new \DateTime($end, $phpTZObject);
  151. $endDate->setTime(23, 59, 59);
  152. $period = new \DatePeriod($availabilityStartDate, \DateInterval::createFromDateString('1 day'), $endDate);
  153. $days = [];
  154. foreach ($period as $day) {
  155. $days[] = [
  156. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  157. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  158. ];
  159. }
  160. foreach ($proIds as $proId) {
  161. $proTimeLine = new TimeLine($startDate, $endDate);
  162. $pro = Pro::where('id', $proId)->first();
  163. $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
  164. return $record->pro_id == $proId;
  165. });
  166. // if no gen avail, assume mon to fri, 9 to 7
  167. /*if (!count($proGenAvail)) {
  168. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  169. foreach ($dayNames as $dayName) {
  170. $item = new \stdClass();
  171. $item->day_of_week = $dayName;
  172. $item->timezone = $timeZone;
  173. $item->start_time = '09:00:00';
  174. $item->end_time = '18:00:00';
  175. $proGenAvail->push($item);
  176. }
  177. }*/
  178. $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
  179. return $record->pro_id == $proId;
  180. });
  181. $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
  182. return $record->pro_id == $proId;
  183. });
  184. // general availability
  185. foreach ($days as $day) {
  186. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  187. return $record->day_of_week === $day["day"];
  188. });
  189. foreach ($proGenAvailForTheDay as $ga) {
  190. $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  191. $parts = explode(":", $ga->start_time);
  192. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  193. $gaStart->setTimezone($phpTZObject);
  194. $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  195. $parts = explode(":", $ga->end_time);
  196. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  197. $gaEnd->setTimezone($phpTZObject);
  198. $proTimeLine->addAvailability($gaStart, $gaEnd);
  199. }
  200. }
  201. // specific availability
  202. foreach ($proSpecAvail as $sa) {
  203. $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  204. $saStart->setTimezone($phpTZObject);
  205. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  206. $saEnd->setTimezone($phpTZObject);
  207. $proTimeLine->addAvailability($saStart, $saEnd);
  208. }
  209. // specific unavailability
  210. foreach ($proSpecUnavail as $sua) {
  211. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  212. $suaStart->setTimezone($phpTZObject);
  213. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  214. $suaEnd->setTimezone($phpTZObject);
  215. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  216. }
  217. // make already booked slots unavailable
  218. $proAppointments = $appointments->filter(function ($record) use ($proId, $pro) {
  219. return ($record->pro_id == $proId || $record->client_id == $pro->shadow_client_id) && !in_array($record->status, ['CANCELLED', 'COMPLETED', 'ABANDONED']);
  220. });
  221. foreach ($proAppointments as $appointment) {
  222. if ($appointment->start_time && $appointment->end_time) {
  223. $appStart = convertToTimezone($appointment->start_time, $timeZone, 'UTC', true);
  224. $appEnd = convertToTimezone($appointment->end_time, $timeZone, 'UTC', true);
  225. $proTimeLine->removeAvailability($appStart, $appEnd);
  226. }
  227. }
  228. foreach ($proTimeLine->available as $item) {
  229. $eStart = new \DateTime('@' . $item->start);
  230. $eStart->setTimezone($phpTZObject);
  231. $eEnd = new \DateTime('@' . $item->end);
  232. $eEnd->setTimezone($phpTZObject);
  233. $events[] = [
  234. "type" => "availability",
  235. "title" => $pro->displayName() . " (available)",
  236. "proId" => $pro->id,
  237. "proUid" => $pro->uid,
  238. "start" => $eStart->format('Y-m-d H:i:s'),
  239. "end" => $eEnd->format('Y-m-d H:i:s'),
  240. "editable" => false
  241. ];
  242. }
  243. }
  244. }
  245. return json_encode($events);
  246. }
  247. public function appointmentConfirmationHistory(Request $request, Appointment $appointment) {
  248. return view('app.patient.partials.appointment-confirmation-history', compact('appointment'));
  249. }
  250. }