AppointmentController.php 12 KB

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