AppointmentController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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->pro->initials() . ') ' . $appointment->client->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. "editable" => true
  49. ];
  50. }
  51. return json_encode($events);
  52. }
  53. private function convertToTimezone($_dateTime, $_targetTimezone) {
  54. if(!$_dateTime) return $_dateTime;
  55. $timezone = 'US/Eastern';
  56. switch($_targetTimezone) {
  57. case 'ALASKA':
  58. $timezone = "US/Alaska";
  59. break;
  60. case 'CENTRAL':
  61. $timezone = "US/Central";
  62. break;
  63. case 'HAWAII':
  64. $timezone = "US/Hawaii";
  65. break;
  66. case 'MOUNTAIN':
  67. $timezone = "US/Mountain";
  68. break;
  69. case 'PACIFIC':
  70. $timezone = "US/Pacific";
  71. break;
  72. case 'PUERTO_RICO':
  73. $timezone = "America/Puerto_Rico";
  74. break;
  75. default:
  76. $timezone = "US/Eastern";
  77. break;
  78. }
  79. $date = new \DateTime($_dateTime, new \DateTimeZone("UTC"));
  80. $date->setTimezone(new \DateTimeZone($timezone));
  81. return $date->format('Y-m-d H:i:s');
  82. }
  83. }