AppointmentController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. $proIds = explode(',', $request->get('proIds'));
  23. $start = $request->get('start');
  24. $end = $request->get('end');
  25. $clientId = $request->get('clientId');
  26. $timeZone = $request->get('timeZone');
  27. // get appointments
  28. $appointments = Appointment
  29. ::where('start_time', '>=', $start)
  30. ->where('start_time', '<=', $end)
  31. ->where(function ($query) use ($proIds, $clientId) {
  32. $query
  33. ->whereIn('pro_id', $proIds)
  34. ->orWhere('client_id', '=', $clientId);
  35. })
  36. ->get();
  37. $events = [];
  38. foreach ($appointments as $appointment) {
  39. $events[] = [
  40. "type" => "appointment",
  41. "title" => ($appointment->client->id != $clientId ? '* ' : '') . $appointment->pro->displayName() .
  42. " (" . strtolower($appointment->status) . ")",
  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. "status" => $appointment->status,
  55. "editable" => true
  56. ];
  57. }
  58. // get availability
  59. $genAvail = ProGeneralAvailability
  60. ::where('is_cancelled', false)
  61. ->whereIn('pro_id', $proIds)
  62. ->get();
  63. $specAvail = ProSpecificAvailability
  64. ::where('is_cancelled', false)
  65. ->whereIn('pro_id', $proIds)
  66. ->where(function ($query) use ($start, $end) {
  67. $query
  68. ->where(function ($query2) use ($start, $end) {
  69. $query2
  70. ->where('start_time', '>=', $start)
  71. ->where('start_time', '<=', $end);
  72. })
  73. ->orWhere(function ($query2) use ($start, $end) {
  74. $query2
  75. ->where('end_time', '>=', $start)
  76. ->where('end_time', '<=', $end);
  77. });
  78. })
  79. ->get();
  80. $specUnavail = ProSpecificUnavailability
  81. ::where('is_cancelled', false)
  82. ->whereIn('pro_id', $proIds)
  83. ->where(function ($query) use ($start, $end) {
  84. $query
  85. ->where(function ($query2) use ($start, $end) {
  86. $query2
  87. ->where('start_time', '>=', $start)
  88. ->where('start_time', '<=', $end);
  89. })
  90. ->orWhere(function ($query2) use ($start, $end) {
  91. $query2
  92. ->where('end_time', '>=', $start)
  93. ->where('end_time', '<=', $end);
  94. });
  95. })
  96. ->get();
  97. // logic
  98. // 1. enumerate days between start and end (inclusive)
  99. // 2. for each pro
  100. // 3. calculate pairs of start-end of availability
  101. // 1. enumerate days between start and end (inclusive)
  102. $phpTZ = $this->appTZtoPHPTZ($timeZone);
  103. $startDate = new \DateTime($start, new \DateTimeZone($phpTZ));
  104. $endDate = new \DateTime($end, new \DateTimeZone($phpTZ));
  105. $endDate->setTime(23,59,59);
  106. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  107. $days = [];
  108. foreach ($period as $day) {
  109. $days[] = [
  110. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  111. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  112. ];
  113. }
  114. foreach ($proIds as $proId) {
  115. $proTimeLine = new TimeLine($startDate, $endDate);
  116. $pro = Pro::where('id', $proId)->first();
  117. $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
  118. return $record->pro_id == $proId;
  119. });
  120. // if no gen avail, assume mon to fri, 9 to 7
  121. if(!count($proGenAvail)) {
  122. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  123. foreach ($dayNames as $dayName) {
  124. $item = new \stdClass();
  125. $item->day_of_week = $dayName;
  126. $item->timezone = $timeZone;
  127. $item->start_time = '09:00:00';
  128. $item->end_time = '18:00:00';
  129. $proGenAvail->push($item);
  130. }
  131. }
  132. $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
  133. return $record->pro_id == $proId;
  134. });
  135. $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
  136. return $record->pro_id == $proId;
  137. });
  138. // general availability
  139. foreach ($days as $day) {
  140. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  141. return $record->day_of_week === $day["day"];
  142. });
  143. foreach ($proGenAvailForTheDay as $ga) {
  144. $gaStart = new \DateTime($day["date"], new \DateTimeZone($this->appTZtoPHPTZ($ga->timezone)));
  145. $parts = explode(":", $ga->start_time);
  146. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  147. $gaStart->setTimezone(new \DateTimeZone($phpTZ));
  148. $gaEnd = new \DateTime($day["date"], new \DateTimeZone($this->appTZtoPHPTZ($ga->timezone)));
  149. $parts = explode(":", $ga->end_time);
  150. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  151. $gaEnd->setTimezone(new \DateTimeZone($phpTZ));
  152. $proTimeLine->addAvailability($gaStart, $gaEnd);
  153. }
  154. }
  155. // specific availability
  156. foreach ($proSpecAvail as $sa) {
  157. $saStart = new \DateTime($sa->start_time, new \DateTimeZone($this->appTZtoPHPTZ($sa->timezone)));
  158. $saStart->setTimezone(new \DateTimeZone($phpTZ));
  159. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone($this->appTZtoPHPTZ($sa->timezone)));
  160. $saEnd->setTimezone(new \DateTimeZone($phpTZ));
  161. $proTimeLine->addAvailability($saStart, $saEnd);
  162. }
  163. // specific unavailability
  164. foreach ($proSpecUnavail as $sua) {
  165. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone($this->appTZtoPHPTZ($sua->timezone)));
  166. $suaStart->setTimezone(new \DateTimeZone($phpTZ));
  167. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone($this->appTZtoPHPTZ($sua->timezone)));
  168. $suaEnd->setTimezone(new \DateTimeZone($phpTZ));
  169. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  170. }
  171. // make already booked slots unavailable
  172. $proAppointments = $appointments->filter(function ($record) use ($proId) {
  173. return $record->pro_id == $proId && !in_array($record->status, ['CANCELLED', 'COMPLETED', 'ABANDONED']);
  174. });
  175. foreach ($proAppointments as $appointment) {
  176. if($appointment->start_time && $appointment->end_time) {
  177. $appStart = $this->convertToTimezone($appointment->start_time, $timeZone, 'UTC', true);
  178. $appEnd = $this->convertToTimezone($appointment->end_time, $timeZone, 'UTC', true);
  179. $proTimeLine->removeAvailability($appStart, $appEnd);
  180. }
  181. }
  182. foreach ($proTimeLine->available as $item) {
  183. $eStart = new \DateTime('@' . $item->start);
  184. $eStart->setTimezone(new \DateTimeZone($phpTZ));
  185. $eEnd = new \DateTime('@' . $item->end);
  186. $eEnd->setTimezone(new \DateTimeZone($phpTZ));
  187. $events[] = [
  188. "type" => "availability",
  189. "title" => $pro->displayName() . " (available)",
  190. "proId" => $pro->id,
  191. "proUid" => $pro->uid,
  192. "start" => $eStart->format('Y-m-d H:i:s'),
  193. "end" => $eEnd->format('Y-m-d H:i:s'),
  194. "editable" => false
  195. ];
  196. }
  197. }
  198. return json_encode($events);
  199. }
  200. private function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false) {
  201. if(!$_dateTime) return $_dateTime;
  202. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  203. $date->setTimezone(new \DateTimeZone($this->appTZtoPHPTZ($_targetTimezone)));
  204. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  205. }
  206. private function appTZtoPHPTZ($_timezone) {
  207. switch($_timezone) {
  208. case 'ALASKA':
  209. $timezone = "US/Alaska";
  210. break;
  211. case 'CENTRAL':
  212. $timezone = "US/Central";
  213. break;
  214. case 'HAWAII':
  215. $timezone = "US/Hawaii";
  216. break;
  217. case 'MOUNTAIN':
  218. $timezone = "US/Mountain";
  219. break;
  220. case 'PACIFIC':
  221. $timezone = "US/Pacific";
  222. break;
  223. case 'PUERTO_RICO':
  224. $timezone = "America/Puerto_Rico";
  225. break;
  226. default:
  227. $timezone = "US/Eastern";
  228. break;
  229. }
  230. return $timezone;
  231. }
  232. }