AppointmentController.php 11 KB

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