AppointmentController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "_title" => $appointment->title,
  45. "description" => $appointment->description,
  46. "clientName" => $appointment->client->displayName(),
  47. "appointmentUid" => $appointment->uid,
  48. "clientUid" => $appointment->client->uid,
  49. "proId" => $appointment->pro->id,
  50. "proUid" => $appointment->pro->uid,
  51. "start" => $this->convertToTimezone($appointment->start_time, $timeZone),
  52. "end" => $this->convertToTimezone($appointment->end_time, $timeZone),
  53. "clientOnly" => !in_array($appointment->pro->id, $proIds),
  54. "otherClient" => ($appointment->client->id != $clientId),
  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. $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
  121. return $record->pro_id == $proId;
  122. });
  123. $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
  124. return $record->pro_id == $proId;
  125. });
  126. // general availability
  127. foreach ($days as $day) {
  128. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  129. return $record->day_of_week === $day["day"];
  130. });
  131. foreach ($proGenAvailForTheDay as $ga) {
  132. $gaStart = new \DateTime($day["date"], new \DateTimeZone($this->appTZtoPHPTZ($ga->timezone)));
  133. $parts = explode(":", $ga->start_time);
  134. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  135. $gaStart->setTimezone(new \DateTimeZone($phpTZ));
  136. $gaEnd = new \DateTime($day["date"], new \DateTimeZone($this->appTZtoPHPTZ($ga->timezone)));
  137. $parts = explode(":", $ga->end_time);
  138. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  139. $gaEnd->setTimezone(new \DateTimeZone($phpTZ));
  140. $proTimeLine->addAvailability($gaStart, $gaEnd);
  141. }
  142. }
  143. // specific availability
  144. foreach ($proSpecAvail as $sa) {
  145. $saStart = new \DateTime($sa->start_time, new \DateTimeZone($this->appTZtoPHPTZ($sa->timezone)));
  146. $saStart->setTimezone(new \DateTimeZone($phpTZ));
  147. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone($this->appTZtoPHPTZ($sa->timezone)));
  148. $saEnd->setTimezone(new \DateTimeZone($phpTZ));
  149. $proTimeLine->addAvailability($saStart, $saEnd);
  150. }
  151. // specific unavailability
  152. foreach ($proSpecUnavail as $sua) {
  153. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone($this->appTZtoPHPTZ($sua->timezone)));
  154. $suaStart->setTimezone(new \DateTimeZone($phpTZ));
  155. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone($this->appTZtoPHPTZ($sua->timezone)));
  156. $suaEnd->setTimezone(new \DateTimeZone($phpTZ));
  157. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  158. }
  159. foreach ($proTimeLine->available as $item) {
  160. $eStart = new \DateTime('@' . $item->start);
  161. $eStart->setTimezone(new \DateTimeZone($phpTZ));
  162. $eEnd = new \DateTime('@' . $item->end);
  163. $eEnd->setTimezone(new \DateTimeZone($phpTZ));
  164. $events[] = [
  165. "type" => "availability",
  166. "title" => $pro->displayName(),
  167. "proId" => $pro->id,
  168. "proUid" => $pro->uid,
  169. "start" => $eStart->format('Y-m-d H:i:s'),
  170. "end" => $eEnd->format('Y-m-d H:i:s'),
  171. "editable" => false
  172. ];
  173. }
  174. }
  175. return json_encode($events);
  176. }
  177. private function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false) {
  178. if(!$_dateTime) return $_dateTime;
  179. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  180. $date->setTimezone(new \DateTimeZone($this->appTZtoPHPTZ($_targetTimezone)));
  181. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  182. }
  183. private function appTZtoPHPTZ($_timezone) {
  184. switch($_timezone) {
  185. case 'ALASKA':
  186. $timezone = "US/Alaska";
  187. break;
  188. case 'CENTRAL':
  189. $timezone = "US/Central";
  190. break;
  191. case 'HAWAII':
  192. $timezone = "US/Hawaii";
  193. break;
  194. case 'MOUNTAIN':
  195. $timezone = "US/Mountain";
  196. break;
  197. case 'PACIFIC':
  198. $timezone = "US/Pacific";
  199. break;
  200. case 'PUERTO_RICO':
  201. $timezone = "America/Puerto_Rico";
  202. break;
  203. default:
  204. $timezone = "US/Eastern";
  205. break;
  206. }
  207. return $timezone;
  208. }
  209. }