Client.php 8.8 KB

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