AppointmentController.php 12 KB

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