AppointmentController.php 11 KB

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