Client.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 allMeasurements() {
  76. return $this->hasMany(Measurement::class, 'client_id', 'id')
  77. ->where('is_removed', false)
  78. ->whereNull('parent_measurement_id')
  79. ->orderBy('label', 'asc')
  80. ->orderBy('effective_date', 'desc');
  81. }
  82. public function smses() {
  83. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  84. ->orderBy('created_at', 'desc');
  85. }
  86. public function documents() {
  87. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  88. ->orderBy('created_at', 'desc');
  89. }
  90. public function smsNumbers() {
  91. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  92. ->orderBy('created_at', 'desc');
  93. }
  94. public function nextMcpAppointment() {
  95. if($this->mcp) {
  96. return Appointment::where('client_id', $this->id)
  97. ->where('pro_id', $this->mcp->id)
  98. ->where('start_time', '>=', date('Y-m-d'))
  99. ->orderBy('start_time', 'asc')
  100. ->first();
  101. }
  102. return false;
  103. }
  104. public function appointments() {
  105. return $this->hasMany(Appointment::class, 'client_id', 'id')
  106. ->orderBy('start_time', 'desc');
  107. }
  108. public function upcomingAppointments() {
  109. return $this->hasMany(Appointment::class, 'client_id', 'id')
  110. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  111. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  112. ->orderBy('start_time', 'desc');
  113. }
  114. public function memos() {
  115. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  116. ->where('is_cancelled', false)
  117. ->orderBy('created_at', 'desc');
  118. }
  119. public function devices() {
  120. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  121. ->where('is_active', true)
  122. ->orderBy('created_at', 'desc');
  123. }
  124. public function hasDevice($_device) {
  125. $count = ClientBDTDevice::where('client_id', $this->id)
  126. ->where('device_id', $_device->id)
  127. ->where('is_active', true)
  128. ->count();
  129. return !!$count;
  130. }
  131. public function deviceMeasurements() {
  132. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  133. ->orderBy('created_at', 'desc');
  134. }
  135. public function activeMcpRequest() {
  136. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  137. }
  138. public function clientPrograms() {
  139. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  140. ->where('is_active', true)
  141. ->orderBy('title', 'desc');
  142. }
  143. public function tickets() {
  144. return $this->hasMany(Ticket::class, 'client_id', 'id')
  145. ->orderBy('created_at', 'desc');
  146. }
  147. public function prosWithAccess() {
  148. $pros = [];
  149. // directly associated pros
  150. $pro = $this->mcp;
  151. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  152. $pro = $this->pcp;
  153. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP'];
  154. $pro = $this->cm;
  155. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  156. $pro = $this->rmm;
  157. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  158. // via client pro access
  159. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  160. foreach ($cpAccesses as $cpAccess) {
  161. if(!$cpAccess->pro) continue;
  162. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  163. }
  164. // via appointments
  165. $appointments = Appointment::where('client_id', $this->id)->get();
  166. foreach ($appointments as $appointment) {
  167. if(!$appointment->pro) continue;
  168. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  169. }
  170. // via client program
  171. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  172. foreach ($clientPrograms as $clientProgram) {
  173. if($clientProgram->mcp)
  174. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  175. if($clientProgram->manager)
  176. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  177. }
  178. // sort by pro name
  179. $name = array_column($pros, 'pro');
  180. array_multisort($name, SORT_ASC, $pros);
  181. return $pros;
  182. }
  183. }