123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace App\Http\Controllers;
- use App\Helpers\TimeLine;
- use App\Models\Appointment;
- use App\Models\BDTDevice;
- use App\Models\CareMonth;
- use App\Models\Client;
- use App\Models\ClientInfoLine;
- use App\Models\Facility;
- use App\Models\NoteTemplate;
- use App\Models\Pro;
- use App\Models\ProGeneralAvailability;
- use App\Models\ProSpecificAvailability;
- use App\Models\ProSpecificUnavailability;
- use App\Models\SectionTemplate;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\File;
- class AppointmentController extends Controller
- {
- public function events(Request $request)
- {
- $performerPro = null;
- if($this->performer && $this->performer->pro) {
- $performerPro = $this->performer->pro;
- }
- if(empty($request->get('proIds'))) {
- $proIds = [$performerPro->id];
- }
- else {
- $proIds = explode(',', $request->get('proIds'));
- }
- $start = $request->get('start');
- $end = $request->get('end');
- $clientId = $request->get('clientId');
- $timeZone = $request->get('timeZone');
- // get appointments
- $appointments = Appointment
- ::where('start_time', '>=', $start)
- ->where('start_time', '<=', $end);
- // for dna, limit to accessible pros
- if($performerPro && $performerPro->isDefaultNA()) {
- $teams = $performerPro->teamsWhereAssistant;
- foreach ($teams as $team) {
- if(!in_array($team->mcp_pro_id, $proIds)) {
- $proIds[] = $team->mcp_pro_id;
- }
- }
- }
- if(count($proIds)) {
- $appointments = $appointments
- ->where(function ($query) use ($proIds, $clientId) {
- $query
- ->whereIn('pro_id', $proIds)
- ->orWhere('client_id', $clientId)
- ->orWhereRaw('client_id IN (SELECT shadow_client_id FROM pro WHERE id IN (' . implode(',', $proIds) . '))');
- });
- $appointments = $appointments->get();
- }
- else {
- $appointments = [];
- }
- $events = [];
- foreach ($appointments as $appointment) {
- // default - as used from patient calendar
- $title = ($appointment->client->id != $clientId ? '* ' : '') . $appointment->pro->displayName() .
- " (" . strtolower($appointment->status) . ")";
- if($clientId == "-1") {
- $shortStatus = '';
- switch($appointment->status) {
- case 'PENDING':
- $shortStatus = 'new';
- break;
- case 'CONFIRMED':
- $shortStatus = 'conf';
- break;
- case 'CANCELLED':
- $shortStatus = 'canc';
- break;
- case 'COMPLETED':
- $shortStatus = 'done';
- break;
- }
- if(count($proIds) === 1) {
- $title = "($shortStatus) " . $appointment->client->displayName();
- }
- else {
- $title = '[' . $appointment->pro->initials() . '] ' .
- "($shortStatus) " . $appointment->client->displayName();
- }
- }
- $events[] = [
- "type" => "appointment",
- "title" => $title,
- "_title" => $appointment->title,
- "description" => $appointment->description,
- "clientName" => $appointment->client->displayName(),
- "appointmentUid" => $appointment->uid,
- "clientUid" => $appointment->client->uid,
- "proId" => $appointment->pro->id,
- "proUid" => $appointment->pro->uid,
- "isTrainingEvent" => !!$appointment->client->shadowOfPro,
- "proName" => $appointment->pro->displayName(),
- "start" => convertToTimezone($appointment->start_time, $timeZone),
- "end" => convertToTimezone($appointment->end_time, $timeZone),
- "clientOnly" => !in_array($appointment->pro->id, $proIds),
- "otherClient" => ($appointment->client->id != $clientId),
- "status" => $appointment->status,
- "editable" => true
- ];
- }
- if(count($proIds)) {
- // get availability
- $genAvail = ProGeneralAvailability
- ::where('is_cancelled', false)
- ->whereIn('pro_id', $proIds)
- ->get();
- $specAvail = ProSpecificAvailability
- ::where('is_cancelled', false)
- ->whereIn('pro_id', $proIds)
- ->where(function ($query) use ($start, $end) {
- $query
- ->where(function ($query2) use ($start, $end) {
- $query2
- ->where('start_time', '>=', $start)
- ->where('start_time', '<=', $end);
- })
- ->orWhere(function ($query2) use ($start, $end) {
- $query2
- ->where('end_time', '>=', $start)
- ->where('end_time', '<=', $end);
- });
- })
- ->get();
- $specUnavail = ProSpecificUnavailability
- ::where('is_cancelled', false)
- ->whereIn('pro_id', $proIds)
- ->where(function ($query) use ($start, $end) {
- $query
- ->where(function ($query2) use ($start, $end) {
- $query2
- ->where('start_time', '>=', $start)
- ->where('start_time', '<=', $end);
- })
- ->orWhere(function ($query2) use ($start, $end) {
- $query2
- ->where('end_time', '>=', $start)
- ->where('end_time', '<=', $end);
- });
- })
- ->get();
- // logic
- // 1. enumerate days between start and end (inclusive)
- // 2. for each pro
- // 3. calculate pairs of start-end of availability
- // 1. enumerate days between start and end (inclusive)
- $phpTZ = appTZtoPHPTZ($timeZone);
- $phpTZObject = new \DateTimeZone($phpTZ);
- $startDate = new \DateTime($start, $phpTZObject);
- $availabilityStartDate = new \DateTime(date('Y-m-d 00:00:00'), $phpTZObject);
- $endDate = new \DateTime($end, $phpTZObject);
- $endDate->setTime(23, 59, 59);
- $period = new \DatePeriod($availabilityStartDate, \DateInterval::createFromDateString('1 day'), $endDate);
- $days = [];
- foreach ($period as $day) {
- $days[] = [
- "day" => strtoupper($day->format("l")), // SUNDAY, etc.
- "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
- ];
- }
- foreach ($proIds as $proId) {
- $proTimeLine = new TimeLine($startDate, $endDate);
- $pro = Pro::where('id', $proId)->first();
- $proGenAvail = $genAvail->filter(function ($record) use ($proId) {
- return $record->pro_id == $proId;
- });
- // if no gen avail, assume mon to fri, 9 to 7
- /*if (!count($proGenAvail)) {
- $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
- foreach ($dayNames as $dayName) {
- $item = new \stdClass();
- $item->day_of_week = $dayName;
- $item->timezone = $timeZone;
- $item->start_time = '09:00:00';
- $item->end_time = '18:00:00';
- $proGenAvail->push($item);
- }
- }*/
- $proSpecAvail = $specAvail->filter(function ($record) use ($proId) {
- return $record->pro_id == $proId;
- });
- $proSpecUnavail = $specUnavail->filter(function ($record) use ($proId) {
- return $record->pro_id == $proId;
- });
- // general availability
- foreach ($days as $day) {
- $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
- return $record->day_of_week === $day["day"];
- });
- foreach ($proGenAvailForTheDay as $ga) {
- $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
- $parts = explode(":", $ga->start_time);
- $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
- $gaStart->setTimezone($phpTZObject);
- $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
- $parts = explode(":", $ga->end_time);
- $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
- $gaEnd->setTimezone($phpTZObject);
- $proTimeLine->addAvailability($gaStart, $gaEnd);
- }
- }
- // specific availability
- foreach ($proSpecAvail as $sa) {
- $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
- $saStart->setTimezone($phpTZObject);
- $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
- $saEnd->setTimezone($phpTZObject);
- $proTimeLine->addAvailability($saStart, $saEnd);
- }
- // specific unavailability
- foreach ($proSpecUnavail as $sua) {
- $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
- $suaStart->setTimezone($phpTZObject);
- $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
- $suaEnd->setTimezone($phpTZObject);
- $proTimeLine->removeAvailability($suaStart, $suaEnd);
- }
- // make already booked slots unavailable
- $proAppointments = $appointments->filter(function ($record) use ($proId, $pro) {
- return ($record->pro_id == $proId || $record->client_id == $pro->shadow_client_id) && !in_array($record->status, ['CANCELLED', 'COMPLETED']);
- });
- foreach ($proAppointments as $appointment) {
- if ($appointment->start_time && $appointment->end_time) {
- $appStart = convertToTimezone($appointment->start_time, $timeZone, 'UTC', true);
- $appEnd = convertToTimezone($appointment->end_time, $timeZone, 'UTC', true);
- $proTimeLine->removeAvailability($appStart, $appEnd);
- }
- }
- foreach ($proTimeLine->available as $item) {
- $eStart = new \DateTime('@' . $item->start);
- $eStart->setTimezone($phpTZObject);
- $eEnd = new \DateTime('@' . $item->end);
- $eEnd->setTimezone($phpTZObject);
- $events[] = [
- "type" => "availability",
- "title" => $pro->displayName() . " (available)",
- "proId" => $pro->id,
- "proUid" => $pro->uid,
- "proName" => $pro->displayName(),
- "start" => $eStart->format('Y-m-d H:i:s'),
- "end" => $eEnd->format('Y-m-d H:i:s'),
- "editable" => false
- ];
- }
- }
- }
- return json_encode($events);
- }
- public function appointmentConfirmationHistory(Request $request, Appointment $appointment) {
- return view('app.patient.partials.appointment-confirmation-history', compact('appointment'));
- }
- }
|