AppointmentController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $endDate = new \DateTime($end, $phpTZObject);
  150. $endDate->setTime(23, 59, 59);
  151. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  152. $days = [];
  153. foreach ($period as $day) {
  154. $days[] = [
  155. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  156. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  157. ];
  158. }
  159. foreach ($proIds as $proId) {
  160. $proTimeLine = new TimeLine($startDate, $endDate);
  161. $pro = Pro::where('id', $proId)->first();
  162. $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
  163. return $record->pro_id == $proId;
  164. });
  165. // if no gen avail, assume mon to fri, 9 to 7
  166. /*if (!count($proGenAvail)) {
  167. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  168. foreach ($dayNames as $dayName) {
  169. $item = new \stdClass();
  170. $item->day_of_week = $dayName;
  171. $item->timezone = $timeZone;
  172. $item->start_time = '09:00:00';
  173. $item->end_time = '18:00:00';
  174. $proGenAvail->push($item);
  175. }
  176. }*/
  177. $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
  178. return $record->pro_id == $proId;
  179. });
  180. $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
  181. return $record->pro_id == $proId;
  182. });
  183. // general availability
  184. foreach ($days as $day) {
  185. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  186. return $record->day_of_week === $day["day"];
  187. });
  188. foreach ($proGenAvailForTheDay as $ga) {
  189. $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  190. $parts = explode(":", $ga->start_time);
  191. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  192. $gaStart->setTimezone($phpTZObject);
  193. $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  194. $parts = explode(":", $ga->end_time);
  195. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  196. $gaEnd->setTimezone($phpTZObject);
  197. $proTimeLine->addAvailability($gaStart, $gaEnd);
  198. }
  199. }
  200. // specific availability
  201. foreach ($proSpecAvail as $sa) {
  202. $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  203. $saStart->setTimezone($phpTZObject);
  204. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  205. $saEnd->setTimezone($phpTZObject);
  206. $proTimeLine->addAvailability($saStart, $saEnd);
  207. }
  208. // specific unavailability
  209. foreach ($proSpecUnavail as $sua) {
  210. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  211. $suaStart->setTimezone($phpTZObject);
  212. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  213. $suaEnd->setTimezone($phpTZObject);
  214. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  215. }
  216. // make already booked slots unavailable
  217. $proAppointments = $appointments->filter(function ($record) use ($proId, $pro) {
  218. return ($record->pro_id == $proId || $record->client_id == $pro->shadow_client_id) && !in_array($record->status, ['CANCELLED', 'COMPLETED', 'ABANDONED']);
  219. });
  220. foreach ($proAppointments as $appointment) {
  221. if ($appointment->start_time && $appointment->end_time) {
  222. $appStart = convertToTimezone($appointment->start_time, $timeZone, 'UTC', true);
  223. $appEnd = convertToTimezone($appointment->end_time, $timeZone, 'UTC', true);
  224. $proTimeLine->removeAvailability($appStart, $appEnd);
  225. }
  226. }
  227. foreach ($proTimeLine->available as $item) {
  228. $eStart = new \DateTime('@' . $item->start);
  229. $eStart->setTimezone($phpTZObject);
  230. $eEnd = new \DateTime('@' . $item->end);
  231. $eEnd->setTimezone($phpTZObject);
  232. $events[] = [
  233. "type" => "availability",
  234. "title" => $pro->displayName() . " (available)",
  235. "proId" => $pro->id,
  236. "proUid" => $pro->uid,
  237. "start" => $eStart->format('Y-m-d H:i:s'),
  238. "end" => $eEnd->format('Y-m-d H:i:s'),
  239. "editable" => false
  240. ];
  241. }
  242. }
  243. }
  244. return json_encode($events);
  245. }
  246. public function appointmentConfirmationHistory(Request $request, Appointment $appointment) {
  247. return view('app.patient.partials.appointment-confirmation-history', compact('appointment'));
  248. }
  249. }