AppointmentController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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" => convertToTimezone($appointment->start_time, $timeZone),
  59. "end" => 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 = appTZtoPHPTZ($timeZone);
  112. $phpTZObject = new \DateTimeZone($phpTZ);
  113. $startDate = new \DateTime($start, $phpTZObject);
  114. $endDate = new \DateTime($end, $phpTZObject);
  115. $endDate->setTime(23, 59, 59);
  116. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  117. $days = [];
  118. foreach ($period as $day) {
  119. $days[] = [
  120. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  121. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  122. ];
  123. }
  124. foreach ($proIds as $proId) {
  125. $proTimeLine = new TimeLine($startDate, $endDate);
  126. $pro = Pro::where('id', $proId)->first();
  127. $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
  128. return $record->pro_id == $proId;
  129. });
  130. // if no gen avail, assume mon to fri, 9 to 7
  131. if (!count($proGenAvail)) {
  132. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  133. foreach ($dayNames as $dayName) {
  134. $item = new \stdClass();
  135. $item->day_of_week = $dayName;
  136. $item->timezone = $timeZone;
  137. $item->start_time = '09:00:00';
  138. $item->end_time = '18:00:00';
  139. $proGenAvail->push($item);
  140. }
  141. }
  142. $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
  143. return $record->pro_id == $proId;
  144. });
  145. $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
  146. return $record->pro_id == $proId;
  147. });
  148. // general availability
  149. foreach ($days as $day) {
  150. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  151. return $record->day_of_week === $day["day"];
  152. });
  153. foreach ($proGenAvailForTheDay as $ga) {
  154. $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  155. $parts = explode(":", $ga->start_time);
  156. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  157. $gaStart->setTimezone($phpTZObject);
  158. $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  159. $parts = explode(":", $ga->end_time);
  160. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  161. $gaEnd->setTimezone($phpTZObject);
  162. $proTimeLine->addAvailability($gaStart, $gaEnd);
  163. }
  164. }
  165. // specific availability
  166. foreach ($proSpecAvail as $sa) {
  167. $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  168. $saStart->setTimezone($phpTZObject);
  169. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  170. $saEnd->setTimezone($phpTZObject);
  171. $proTimeLine->addAvailability($saStart, $saEnd);
  172. }
  173. // specific unavailability
  174. foreach ($proSpecUnavail as $sua) {
  175. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  176. $suaStart->setTimezone($phpTZObject);
  177. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  178. $suaEnd->setTimezone($phpTZObject);
  179. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  180. }
  181. // make already booked slots unavailable
  182. $proAppointments = $appointments->filter(function ($record) use ($proId) {
  183. return $record->pro_id == $proId && !in_array($record->status, ['CANCELLED', 'COMPLETED', 'ABANDONED']);
  184. });
  185. foreach ($proAppointments as $appointment) {
  186. if ($appointment->start_time && $appointment->end_time) {
  187. $appStart = convertToTimezone($appointment->start_time, $timeZone, 'UTC', true);
  188. $appEnd = convertToTimezone($appointment->end_time, $timeZone, 'UTC', true);
  189. $proTimeLine->removeAvailability($appStart, $appEnd);
  190. }
  191. }
  192. foreach ($proTimeLine->available as $item) {
  193. $eStart = new \DateTime('@' . $item->start);
  194. $eStart->setTimezone($phpTZObject);
  195. $eEnd = new \DateTime('@' . $item->end);
  196. $eEnd->setTimezone($phpTZObject);
  197. $events[] = [
  198. "type" => "availability",
  199. "title" => $pro->displayName() . " (available)",
  200. "proId" => $pro->id,
  201. "proUid" => $pro->uid,
  202. "start" => $eStart->format('Y-m-d H:i:s'),
  203. "end" => $eEnd->format('Y-m-d H:i:s'),
  204. "editable" => false
  205. ];
  206. }
  207. }
  208. }
  209. return json_encode($events);
  210. }
  211. }