Client.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Relations\HasOne;
  4. use Illuminate\Support\Collection;
  5. # use Illuminate\Database\Eloquent\Model;
  6. class Client extends Model
  7. {
  8. protected $table = 'client';
  9. public function displayName() {
  10. return $this->name_last . ', '. $this->name_first;
  11. }
  12. public function mcp() {
  13. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  14. }
  15. public function pcp() {
  16. return $this->hasOne(Pro::class, 'id', 'physician_pro_id');
  17. }
  18. public function cm() {
  19. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  20. }
  21. public function rmm() {
  22. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  23. }
  24. public function prosInMeetingWith() {
  25. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  26. }
  27. public function notes() {
  28. return $this->hasMany(Note::class, 'client_id', 'id')
  29. ->orderBy('effective_dateest', 'desc');
  30. }
  31. public function activeNotes() {
  32. return $this->hasMany(Note::class, 'client_id', 'id')
  33. ->where('is_cancelled', false)
  34. ->orderBy('effective_dateest', 'desc');
  35. }
  36. public function cancelledNotes() {
  37. return $this->hasMany(Note::class, 'client_id', 'id')
  38. ->where('is_cancelled', true)
  39. ->orderBy('effective_dateest', 'desc');
  40. }
  41. public function sections() {
  42. return $this->hasMany(Section::class, 'client_id', 'id')
  43. ->where('is_active', true)
  44. ->orderBy('created_at', 'asc');
  45. }
  46. public function handouts() {
  47. $mappings = HandoutClient::where('client_id', $this->id)->get();
  48. $handouts = new Collection();
  49. foreach ($mappings as $mapping) {
  50. $handout = Handout::where('id', $mapping->handout_id)->first();
  51. $handout->handout_client_uid = $mapping->uid;
  52. $handouts->add($handout);
  53. }
  54. $handouts = $handouts->sortBy('created_at');
  55. return $handouts;
  56. }
  57. public function duplicateOf() {
  58. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  59. }
  60. public function actionItems () {
  61. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  62. ->orderBy('action_item_category', 'asc')
  63. ->orderBy('created_at', 'desc');
  64. }
  65. public function infoLines() {
  66. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  67. }
  68. public function measurements() {
  69. return $this->hasMany(Measurement::class, 'client_id', 'id')
  70. ->distinct('label')
  71. ->where('is_removed', false)
  72. ->orderBy('label', 'asc')
  73. ->orderBy('effective_date', 'desc');
  74. }
  75. public function smses() {
  76. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  77. ->orderBy('created_at', 'desc');
  78. }
  79. public function documents() {
  80. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  81. ->orderBy('created_at', 'desc');
  82. }
  83. public function smsNumbers() {
  84. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  85. ->orderBy('created_at', 'desc');
  86. }
  87. public function nextMcpAppointment() {
  88. if($this->mcp) {
  89. return Appointment::where('client_id', $this->id)
  90. ->where('pro_id', $this->mcp->id)
  91. ->where('start_time', '>=', date('Y-m-d'))
  92. ->orderBy('start_time', 'asc')
  93. ->first();
  94. }
  95. return false;
  96. }
  97. public function appointments() {
  98. return $this->hasMany(Appointment::class, 'client_id', 'id')
  99. ->orderBy('start_time', 'desc');
  100. }
  101. public function upcomingAppointments() {
  102. return $this->hasMany(Appointment::class, 'client_id', 'id')
  103. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  104. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  105. ->orderBy('start_time', 'desc');
  106. }
  107. public function memos() {
  108. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  109. ->where('is_cancelled', false)
  110. ->orderBy('created_at', 'desc');
  111. }
  112. public function devices() {
  113. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  114. ->where('is_active', true)
  115. ->orderBy('created_at', 'desc');
  116. }
  117. public function hasDevice($_device) {
  118. $count = ClientBDTDevice::where('client_id', $this->id)
  119. ->where('device_id', $_device->id)
  120. ->where('is_active', true)
  121. ->count();
  122. return !!$count;
  123. }
  124. public function deviceMeasurements() {
  125. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  126. ->orderBy('created_at', 'desc');
  127. }
  128. public function activeMcpRequest() {
  129. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  130. }
  131. }