AppointmentController.php 12 KB

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