AppointmentController.php 11 KB

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