Client.php 8.6 KB

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