Client.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 rme() {
  25. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  26. }
  27. public function prosInMeetingWith() {
  28. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  29. }
  30. public function notes() {
  31. return $this->hasMany(Note::class, 'client_id', 'id')
  32. ->orderBy('effective_dateest', 'desc');
  33. }
  34. public function activeNotes() {
  35. return $this->hasMany(Note::class, 'client_id', 'id')
  36. ->where('is_cancelled', false)
  37. ->orderBy('effective_dateest', 'desc');
  38. }
  39. public function cancelledNotes() {
  40. return $this->hasMany(Note::class, 'client_id', 'id')
  41. ->where('is_cancelled', true)
  42. ->orderBy('effective_dateest', 'desc');
  43. }
  44. public function sections() {
  45. return $this->hasMany(Section::class, 'client_id', 'id')
  46. ->where('is_active', true)
  47. ->orderBy('created_at', 'asc');
  48. }
  49. public function handouts() {
  50. $mappings = HandoutClient::where('client_id', $this->id)->get();
  51. $handouts = new Collection();
  52. foreach ($mappings as $mapping) {
  53. $handout = Handout::where('id', $mapping->handout_id)->first();
  54. $handout->handout_client_uid = $mapping->uid;
  55. $handouts->add($handout);
  56. }
  57. $handouts = $handouts->sortBy('created_at');
  58. return $handouts;
  59. }
  60. public function duplicateOf() {
  61. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  62. }
  63. public function actionItems () {
  64. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  65. ->orderBy('action_item_category', 'asc')
  66. ->orderBy('created_at', 'desc');
  67. }
  68. public function infoLines() {
  69. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  70. }
  71. public function measurements() {
  72. return $this->hasMany(Measurement::class, 'client_id', 'id')
  73. ->distinct('label')
  74. ->where('is_removed', false)
  75. ->orderBy('label', 'asc')
  76. ->orderBy('effective_date', 'desc');
  77. }
  78. public function allMeasurements() {
  79. return $this->hasMany(Measurement::class, 'client_id', 'id')
  80. ->where('is_removed', false)
  81. ->whereNull('parent_measurement_id')
  82. ->orderBy('label', 'asc')
  83. ->orderBy('effective_date', 'desc');
  84. }
  85. public function smses() {
  86. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  87. ->orderBy('created_at', 'desc');
  88. }
  89. public function documents() {
  90. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  91. ->orderBy('created_at', 'desc');
  92. }
  93. public function smsNumbers() {
  94. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  95. ->orderBy('created_at', 'desc');
  96. }
  97. public function nextMcpAppointment() {
  98. if($this->mcp) {
  99. return Appointment::where('client_id', $this->id)
  100. ->where('pro_id', $this->mcp->id)
  101. ->where('start_time', '>=', date('Y-m-d'))
  102. ->orderBy('start_time', 'asc')
  103. ->first();
  104. }
  105. return false;
  106. }
  107. public function appointments() {
  108. return $this->hasMany(Appointment::class, 'client_id', 'id')
  109. ->orderBy('start_time', 'desc');
  110. }
  111. public function upcomingAppointments() {
  112. return $this->hasMany(Appointment::class, 'client_id', 'id')
  113. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  114. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  115. ->orderBy('start_time', 'desc');
  116. }
  117. public function memos() {
  118. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  119. ->where('is_cancelled', false)
  120. ->orderBy('created_at', 'desc');
  121. }
  122. public function devices() {
  123. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  124. ->where('is_active', true)
  125. ->orderBy('created_at', 'desc');
  126. }
  127. public function hasDevice($_device) {
  128. $count = ClientBDTDevice::where('client_id', $this->id)
  129. ->where('device_id', $_device->id)
  130. ->where('is_active', true)
  131. ->count();
  132. return !!$count;
  133. }
  134. public function deviceMeasurements() {
  135. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  136. ->orderBy('created_at', 'desc');
  137. }
  138. public function activeMcpRequest() {
  139. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  140. }
  141. public function clientPrograms() {
  142. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  143. ->where('is_active', true)
  144. ->orderBy('title', 'desc');
  145. }
  146. public function tickets() {
  147. return $this->hasMany(Ticket::class, 'client_id', 'id')
  148. ->orderBy('created_at', 'desc');
  149. }
  150. public function prosWithAccess() {
  151. $pros = [];
  152. // directly associated pros
  153. $pro = $this->mcp;
  154. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  155. $pro = $this->pcp;
  156. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  157. $pro = $this->cm;
  158. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  159. $pro = $this->rmm;
  160. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  161. $pro = $this->rme;
  162. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  163. // via client pro access
  164. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  165. foreach ($cpAccesses as $cpAccess) {
  166. if(!$cpAccess->pro) continue;
  167. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  168. }
  169. // via appointments
  170. $appointments = Appointment::where('client_id', $this->id)->get();
  171. foreach ($appointments as $appointment) {
  172. if(!$appointment->pro) continue;
  173. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  174. }
  175. // via client program
  176. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  177. foreach ($clientPrograms as $clientProgram) {
  178. if($clientProgram->mcp)
  179. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  180. if($clientProgram->manager)
  181. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  182. }
  183. // sort by pro name
  184. $name = array_column($pros, 'pro');
  185. array_multisort($name, SORT_ASC, $pros);
  186. return $pros;
  187. }
  188. }