AppointmentController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\BDTDevice;
  5. use App\Models\CareMonth;
  6. use App\Models\Client;
  7. use App\Models\ClientInfoLine;
  8. use App\Models\Facility;
  9. use App\Models\NoteTemplate;
  10. use App\Models\Pro;
  11. use App\Models\ProGeneralAvailability;
  12. use App\Models\ProSpecificAvailability;
  13. use App\Models\ProSpecificUnavailability;
  14. use App\Models\SectionTemplate;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\File;
  17. class AppointmentController extends Controller
  18. {
  19. public function events(Request $request)
  20. {
  21. $proIds = explode(',', $request->get('proIds'));
  22. $start = $request->get('start');
  23. $end = $request->get('end');
  24. $clientId = $request->get('clientId');
  25. $timeZone = $request->get('timeZone');
  26. // get appointments
  27. $appointments = Appointment
  28. ::where('status', '!=', 'COMPLETED')
  29. ->where('status', '!=', 'CANCELLED')
  30. ->where('start_time', '>=', $start)
  31. ->where('start_time', '<=', $end)
  32. ->where(function ($query) use ($proIds, $clientId) {
  33. $query
  34. ->whereIn('pro_id', $proIds)
  35. ->orWhere('client_id', '=', $clientId);
  36. })
  37. ->get();
  38. $events = [];
  39. foreach ($appointments as $appointment) {
  40. $events[] = [
  41. "type" => "appointment",
  42. "title" => ($appointment->client->id != $clientId ? '* ' : '') . $appointment->pro->displayName(),
  43. "_title" => $appointment->title,
  44. "description" => $appointment->description,
  45. "clientName" => $appointment->client->displayName(),
  46. "appointmentUid" => $appointment->uid,
  47. "clientUid" => $appointment->client->uid,
  48. "proId" => $appointment->pro->id,
  49. "proUid" => $appointment->pro->uid,
  50. "start" => $this->convertToTimezone($appointment->start_time, $timeZone),
  51. "end" => $this->convertToTimezone($appointment->end_time, $timeZone),
  52. "clientOnly" => !in_array($appointment->pro->id, $proIds),
  53. "otherClient" => ($appointment->client->id != $clientId),
  54. "editable" => true
  55. ];
  56. }
  57. // get availability
  58. $genAvail = ProGeneralAvailability
  59. ::where('is_cancelled', false)
  60. ->whereIn('pro_id', $proIds)
  61. ->get();
  62. $specAvail = ProSpecificAvailability
  63. ::where('is_cancelled', false)
  64. ->whereIn('pro_id', $proIds)
  65. ->where(function ($query) use ($start, $end) {
  66. $query
  67. ->where(function ($query2) use ($start, $end) {
  68. $query2
  69. ->where('start_time', '>=', $start)
  70. ->where('start_time', '<=', $end);
  71. })
  72. ->orWhere(function ($query2) use ($start, $end) {
  73. $query2
  74. ->where('end_time', '>=', $start)
  75. ->where('end_time', '<=', $end);
  76. });
  77. })
  78. ->get();
  79. $specUnavail = ProSpecificUnavailability
  80. ::where('is_cancelled', false)
  81. ->whereIn('pro_id', $proIds)
  82. ->where(function ($query) use ($start, $end) {
  83. $query
  84. ->where(function ($query2) use ($start, $end) {
  85. $query2
  86. ->where('start_time', '>=', $start)
  87. ->where('start_time', '<=', $end);
  88. })
  89. ->orWhere(function ($query2) use ($start, $end) {
  90. $query2
  91. ->where('end_time', '>=', $start)
  92. ->where('end_time', '<=', $end);
  93. });
  94. })
  95. ->get();
  96. // logic
  97. // 1. enumerate days between start and end (inclusive)
  98. // 2. for each pro
  99. // 3. calculate pairs of start-end of availability
  100. // 1. enumerate days between start and end (inclusive)
  101. $phpTZ = $this->appTZtoPHPTZ($timeZone);
  102. $startDate = new \DateTime($start, new \DateTimeZone($phpTZ));
  103. $endDate = new \DateTime($end, new \DateTimeZone($phpTZ));
  104. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  105. $days = [];
  106. foreach ($period as $day) {
  107. $days[] = [
  108. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  109. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  110. ];
  111. }
  112. foreach ($proIds as $proId) {
  113. $pro = Pro::where('id', $proId)->first();
  114. $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
  115. return $record->pro_id == $proId;
  116. });
  117. foreach ($days as $day) {
  118. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  119. return $record->day_of_week === $day["day"];
  120. });
  121. if(count($proGenAvailForTheDay)) {
  122. foreach ($proGenAvailForTheDay as $ga) {
  123. $gaStart = new \DateTime($day["date"], new \DateTimeZone($phpTZ));
  124. $parts = explode(":", $ga->start_time);
  125. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  126. $gaEnd = new \DateTime($day["date"], new \DateTimeZone($phpTZ));
  127. $parts = explode(":", $ga->end_time);
  128. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  129. $events[] = [
  130. "type" => "availability",
  131. "title" => $pro->displayName(),
  132. "proId" => $pro->id,
  133. "proUid" => $pro->uid,
  134. "start" => $gaStart->format('Y-m-d H:i:s'),
  135. "end" => $gaEnd->format('Y-m-d H:i:s'),
  136. "editable" => false
  137. ];
  138. }
  139. }
  140. // TODO: add spec avail
  141. // TODO: subtract spec unavail
  142. }
  143. }
  144. return json_encode($events);
  145. }
  146. private function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false) {
  147. if(!$_dateTime) return $_dateTime;
  148. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  149. $date->setTimezone(new \DateTimeZone($this->appTZtoPHPTZ($_targetTimezone)));
  150. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  151. }
  152. private function appTZtoPHPTZ($_timezone) {
  153. switch($_timezone) {
  154. case 'ALASKA':
  155. $timezone = "US/Alaska";
  156. break;
  157. case 'CENTRAL':
  158. $timezone = "US/Central";
  159. break;
  160. case 'HAWAII':
  161. $timezone = "US/Hawaii";
  162. break;
  163. case 'MOUNTAIN':
  164. $timezone = "US/Mountain";
  165. break;
  166. case 'PACIFIC':
  167. $timezone = "US/Pacific";
  168. break;
  169. case 'PUERTO_RICO':
  170. $timezone = "America/Puerto_Rico";
  171. break;
  172. default:
  173. $timezone = "US/Eastern";
  174. break;
  175. }
  176. return $timezone;
  177. }
  178. }