Client.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. {
  11. return $this->name_last . ', ' . $this->name_first;
  12. }
  13. public function mcp()
  14. {
  15. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  16. }
  17. public function pcp()
  18. {
  19. return $this->hasOne(Pro::class, 'id', 'physician_pro_id');
  20. }
  21. public function cm()
  22. {
  23. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  24. }
  25. public function rmm()
  26. {
  27. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  28. }
  29. public function rme()
  30. {
  31. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  32. }
  33. public function rms()
  34. {
  35. return $this->hasOne(Pro::class, 'id', 'rms_pro_id');
  36. }
  37. public function rmg()
  38. {
  39. return $this->hasOne(Pro::class, 'id', 'rmg_pro_id');
  40. }
  41. public function defaultNaPro()
  42. {
  43. return $this->hasOne(Pro::class, 'id', 'default_na_pro_id');
  44. }
  45. public function prosInMeetingWith()
  46. {
  47. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  48. }
  49. public function notes()
  50. {
  51. return $this->hasMany(Note::class, 'client_id', 'id')
  52. ->orderBy('effective_dateest', 'desc');
  53. }
  54. public function notesAscending()
  55. {
  56. return $this->hasMany(Note::class, 'client_id', 'id')
  57. ->orderBy('effective_dateest', 'asc');
  58. }
  59. public function activeNotes()
  60. {
  61. return $this->hasMany(Note::class, 'client_id', 'id')
  62. ->where('is_cancelled', false)
  63. ->orderBy('effective_dateest', 'desc');
  64. }
  65. public function cancelledNotes()
  66. {
  67. return $this->hasMany(Note::class, 'client_id', 'id')
  68. ->where('is_cancelled', true)
  69. ->orderBy('effective_dateest', 'desc');
  70. }
  71. public function sections()
  72. {
  73. return $this->hasMany(Section::class, 'client_id', 'id')
  74. ->where('is_active', true)
  75. ->orderBy('created_at', 'asc');
  76. }
  77. public function handouts()
  78. {
  79. $mappings = HandoutClient::where('client_id', $this->id)->get();
  80. $handouts = new Collection();
  81. foreach ($mappings as $mapping) {
  82. $handout = Handout::where('id', $mapping->handout_id)->first();
  83. $handout->handout_client_uid = $mapping->uid;
  84. $handouts->add($handout);
  85. }
  86. $handouts = $handouts->sortBy('created_at');
  87. return $handouts;
  88. }
  89. public function duplicateOf()
  90. {
  91. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  92. }
  93. public function actionItems()
  94. {
  95. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  96. ->orderBy('action_item_category', 'asc')
  97. ->orderBy('created_at', 'desc');
  98. }
  99. public function infoLines()
  100. {
  101. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  102. }
  103. public function measurements()
  104. {
  105. return $this->hasMany(Measurement::class, 'client_id', 'id')
  106. /*->distinct('label')*/
  107. ->where('is_removed', false)
  108. ->orderByRaw('ts DESC NULLS LAST');
  109. }
  110. public function nonZeroMeasurements()
  111. {
  112. return $this->hasMany(Measurement::class, 'client_id', 'id')
  113. /*->distinct('label')*/
  114. ->where('is_removed', false)
  115. ->where('is_cellular_zero', false)
  116. ->orderBy('effective_date', 'desc');
  117. }
  118. public function getNonZeroBpMeasurements(){
  119. return $this->hasMany(Measurement::class, 'client_id', 'id')
  120. /*->distinct('label')*/
  121. ->where('is_removed', false)
  122. ->where('label', '=', 'BP')
  123. ->where('sbp_mm_hg', '>', 0)
  124. ->where('dbp_mm_hg', '>', 0)
  125. ->orderBy('ts', 'desc');
  126. }
  127. public function getNonZeroWeightMeasurements(){
  128. return $this->hasMany(Measurement::class, 'client_id', 'id')
  129. /*->distinct('label')*/
  130. ->where('is_removed', false)
  131. ->where('label', '=', 'Wt. (lbs.)')
  132. ->where('numeric_value', '>', 0)
  133. ->orderBy('ts', 'desc');
  134. }
  135. public function currentCareMonth()
  136. {
  137. $cmStartDate = strtotime(date('Y-m-d'));
  138. $month = date("n", $cmStartDate);
  139. $year = date("Y", $cmStartDate);
  140. return CareMonth
  141. ::where('client_id', $this->id)
  142. ->whereRaw('EXTRACT(MONTH FROM start_date) = ?', [$month])
  143. ->whereRaw('EXTRACT(YEAR FROM start_date) = ?', [$year])
  144. ->first();
  145. }
  146. public function measurementsInCareMonth(CareMonth $careMonth)
  147. {
  148. $cmStartDate = strtotime($careMonth->start_date);
  149. $month = date("n", $cmStartDate);
  150. $year = date("Y", $cmStartDate);
  151. $measurements = Measurement
  152. ::where('client_id', $this->id)
  153. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  154. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  155. ->where('is_removed', false)
  156. ->orderBy('ts', 'desc')
  157. ->get();
  158. return $measurements;
  159. }
  160. public function allMeasurements()
  161. {
  162. return $this->hasMany(Measurement::class, 'client_id', 'id')
  163. ->where('is_removed', false)
  164. ->whereNull('parent_measurement_id')
  165. ->orderBy('label', 'asc')
  166. ->orderBy('effective_date', 'desc');
  167. }
  168. public function smses()
  169. {
  170. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  171. ->orderBy('created_at', 'desc');
  172. }
  173. public function documents()
  174. {
  175. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  176. ->orderBy('created_at', 'desc');
  177. }
  178. public function incomingReports() {
  179. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  180. ->orderBy('created_at', 'desc');
  181. }
  182. public function smsNumbers() {
  183. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  184. ->orderBy('created_at', 'desc');
  185. }
  186. public function nextMcpAppointment()
  187. {
  188. if ($this->mcp) {
  189. return Appointment::where('client_id', $this->id)
  190. ->where('pro_id', $this->mcp->id)
  191. ->where('start_time', '>=', date('Y-m-d'))
  192. ->orderBy('start_time', 'asc')
  193. ->first();
  194. }
  195. return false;
  196. }
  197. public function appointments()
  198. {
  199. return $this->hasMany(Appointment::class, 'client_id', 'id')
  200. ->orderBy('start_time', 'desc');
  201. }
  202. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  203. {
  204. $appointments = Appointment::where('client_id', $this->id);
  205. if($forPro !== 'all') {
  206. $forPro = Pro::where('uid', $forPro)->first();
  207. $appointments = $appointments->where('pro_id', $forPro->id);
  208. }
  209. if($status !== 'ALL') {
  210. $appointments = $appointments->where('status', $status);
  211. }
  212. $appointments = $appointments->orderBy('start_time', 'desc');
  213. return $appointments->get();
  214. }
  215. public function upcomingAppointments()
  216. {
  217. return $this->hasMany(Appointment::class, 'client_id', 'id')
  218. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  219. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  220. ->orderBy('start_time', 'desc');
  221. }
  222. public function appointmentsFromLastWeek()
  223. {
  224. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
  225. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  226. return $this->hasMany(Appointment::class, 'client_id', 'id')
  227. ->where('raw_date', '>=', $dateLastWeek)
  228. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  229. ->orderBy('start_time', 'desc');
  230. }
  231. public function memos()
  232. {
  233. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  234. ->where('is_cancelled', false)
  235. ->orderBy('created_at', 'desc');
  236. }
  237. public function devices()
  238. {
  239. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  240. ->where('is_active', true)
  241. ->orderBy('created_at', 'desc');
  242. }
  243. public function deactivatedDevices()
  244. {
  245. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  246. ->where('is_active', false)
  247. ->orderBy('created_at', 'desc');
  248. }
  249. public function hasDevice($_device)
  250. {
  251. $count = ClientBDTDevice::where('client_id', $this->id)
  252. ->where('device_id', $_device->id)
  253. ->where('is_active', true)
  254. ->count();
  255. return !!$count;
  256. }
  257. public function deviceMeasurements()
  258. {
  259. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  260. ->orderBy('created_at', 'desc');
  261. }
  262. public function activeMcpRequest()
  263. {
  264. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  265. }
  266. public function clientPrograms()
  267. {
  268. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  269. ->where('is_active', true)
  270. ->orderBy('title', 'desc');
  271. }
  272. public function tickets()
  273. {
  274. return $this->hasMany(Ticket::class, 'client_id', 'id')
  275. ->orderBy('is_open', 'desc')
  276. ->orderBy('created_at', 'desc');
  277. }
  278. public function mcpDisplayName()
  279. {
  280. }
  281. public function rmeDisplayName()
  282. {
  283. }
  284. public function firstCellularBPDevice()
  285. {
  286. $devices = $this->devices();
  287. $x = null;
  288. foreach($devices as $device){
  289. if($device->device->category == 'BP'){
  290. $x = $device;
  291. continue;
  292. }
  293. }
  294. return $x;
  295. }
  296. public function getFirstCellularBPMeasurementAt()
  297. {
  298. }
  299. public function getLatestCellularBPMeasurementAt()
  300. {
  301. }
  302. public function getTotalCellularBPMeasurements()
  303. {
  304. }
  305. public function firstCellularWeightDevice()
  306. {
  307. }
  308. public function getFirstCellularWeightMeasurementAt()
  309. {
  310. }
  311. public function getLatestCellularWeightMeasurementAt()
  312. {
  313. }
  314. public function getTotalCellularWeightMeasurements()
  315. {
  316. }
  317. public function prosWithAccess()
  318. {
  319. $pros = [];
  320. // directly associated pros
  321. $pro = $this->mcp;
  322. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  323. $pro = $this->pcp;
  324. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  325. $pro = $this->cm;
  326. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  327. $pro = $this->rmm;
  328. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  329. $pro = $this->rme;
  330. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  331. // via client pro access
  332. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  333. foreach ($cpAccesses as $cpAccess) {
  334. if (!$cpAccess->pro) continue;
  335. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  336. }
  337. // via appointments
  338. $appointments = Appointment::where('client_id', $this->id)->get();
  339. foreach ($appointments as $appointment) {
  340. if (!$appointment->pro) continue;
  341. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  342. }
  343. // via client program
  344. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  345. foreach ($clientPrograms as $clientProgram) {
  346. if ($clientProgram->mcp)
  347. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  348. if ($clientProgram->manager)
  349. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  350. }
  351. // sort by pro name
  352. $name = array_column($pros, 'pro');
  353. array_multisort($name, SORT_ASC, $pros);
  354. return $pros;
  355. }
  356. public function mcpRequests()
  357. {
  358. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  359. ->orderBy('created_at', 'desc');
  360. }
  361. public function eligibleRefreshes()
  362. {
  363. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  364. ->orderBy('created_at', 'desc');
  365. }
  366. public function mbPayerValidationResults()
  367. {
  368. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  369. ->orderBy('created_at', 'desc');
  370. }
  371. public function payer()
  372. {
  373. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  374. }
  375. public function supplyOrders()
  376. {
  377. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  378. ->orderBy('created_at', 'desc');
  379. }
  380. public function activeSupplyOrders()
  381. {
  382. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  383. ->where('is_cancelled', false)
  384. ->orderBy('created_at', 'desc');
  385. }
  386. public function cancelledSupplyOrders()
  387. {
  388. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  389. ->where('is_cancelled', true)
  390. ->orderBy('created_at', 'desc');
  391. }
  392. public function readyToShipSupplyOrders()
  393. {
  394. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  395. ->where('is_cancelled', false)
  396. ->where('is_cleared_for_shipment', true)
  397. ->whereNull('shipment_id')
  398. ->orderBy('created_at', 'desc');
  399. }
  400. public function shipments()
  401. {
  402. return $this->hasMany(Shipment::class, 'client_id', 'id')
  403. ->where('is_cancelled', false)
  404. ->orderBy('created_at', 'desc');
  405. }
  406. public function numSignedNotes() {
  407. return Note::where('client_id', $this->id)
  408. ->where('is_cancelled', false)
  409. ->where('is_signed_by_hcp', true)
  410. ->count();
  411. }
  412. public function smsReminders()
  413. {
  414. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  415. ->orderBy('created_at', 'asc');
  416. }
  417. public function measurementConfirmationNumbers()
  418. {
  419. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  420. ->orderBy('created_at', 'asc');
  421. }
  422. public function shadowOfPro() {
  423. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  424. }
  425. public function clientTags()
  426. {
  427. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  428. ->where('is_cancelled', false)
  429. ->orderBy('tag', 'asc');
  430. }
  431. public function medicalTeam()
  432. {
  433. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  434. ->where('is_active', true)
  435. ->whereNotNull('pro_id')
  436. ->orderBy('created_at', 'asc');
  437. }
  438. public function accountInvites()
  439. {
  440. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  441. ->orderBy('created_at', 'desc');
  442. }
  443. public function linkedAccounts()
  444. {
  445. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  446. ->orderBy('created_at', 'desc');
  447. }
  448. }