AppointmentController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\BDTDevice;
  5. use App\Models\CareMonth;
  6. use App\Models\Client;
  7. use App\Models\ClientInfoLine;
  8. use App\Models\Facility;
  9. use App\Models\NoteTemplate;
  10. use App\Models\Pro;
  11. use App\Models\SectionTemplate;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\File;
  14. class AppointmentController extends Controller
  15. {
  16. public function events(Request $request)
  17. {
  18. $proIds = explode(',', $request->get('proIds'));
  19. $start = $request->get('start');
  20. $end = $request->get('end');
  21. $clientId = $request->get('clientId');
  22. $timeZone = $request->get('timeZone');
  23. $appointments = Appointment
  24. ::where('status', '!=', 'COMPLETED')
  25. ->where('status', '!=', 'CANCELLED')
  26. ->where('start_time', '>=', $start)
  27. ->where('start_time', '<=', $end)
  28. ->where(function ($query) use ($proIds, $clientId) {
  29. $query
  30. ->whereIn('pro_id', $proIds)
  31. ->orWhere('client_id', '=', $clientId);
  32. })
  33. ->get();
  34. $events = [];
  35. foreach ($appointments as $appointment) {
  36. $events[] = [
  37. "title" => ($appointment->client->id != $clientId ? '* ' : '') . $appointment->pro->displayName(),
  38. "_title" => $appointment->title,
  39. "description" => $appointment->description,
  40. "clientName" => $appointment->client->displayName(),
  41. "appointmentUid" => $appointment->uid,
  42. "clientUid" => $appointment->client->uid,
  43. "proId" => $appointment->pro->id,
  44. "proUid" => $appointment->pro->uid,
  45. "start" => $this->convertToTimezone($appointment->start_time, $timeZone),
  46. "end" => $this->convertToTimezone($appointment->end_time, $timeZone),
  47. "clientOnly" => !in_array($appointment->pro->id, $proIds),
  48. "otherClient" => ($appointment->client->id != $clientId),
  49. "editable" => true
  50. ];
  51. }
  52. return json_encode($events);
  53. }
  54. private function convertToTimezone($_dateTime, $_targetTimezone) {
  55. if(!$_dateTime) return $_dateTime;
  56. $timezone = 'US/Eastern';
  57. switch($_targetTimezone) {
  58. case 'ALASKA':
  59. $timezone = "US/Alaska";
  60. break;
  61. case 'CENTRAL':
  62. $timezone = "US/Central";
  63. break;
  64. case 'HAWAII':
  65. $timezone = "US/Hawaii";
  66. break;
  67. case 'MOUNTAIN':
  68. $timezone = "US/Mountain";
  69. break;
  70. case 'PACIFIC':
  71. $timezone = "US/Pacific";
  72. break;
  73. case 'PUERTO_RICO':
  74. $timezone = "America/Puerto_Rico";
  75. break;
  76. default:
  77. $timezone = "US/Eastern";
  78. break;
  79. }
  80. $date = new \DateTime($_dateTime, new \DateTimeZone("UTC"));
  81. $date->setTimezone(new \DateTimeZone($timezone));
  82. return $date->format('Y-m-d H:i:s');
  83. }
  84. }