AppointmentController.php 12 KB

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