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