Client.php 8.2 KB

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