Client.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. use Illuminate\Support\Facades\DB;
  7. class Client extends Model
  8. {
  9. protected $table = 'client';
  10. public function primaryCoverages()
  11. {
  12. return $this->hasMany(ClientPrimaryCoverage::class, 'client_id', 'id')
  13. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)')
  14. ->orderBy('created_at', 'desc');
  15. }
  16. public function latestManualParentClientPrimaryCoverage(){
  17. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_manual_parent_client_primary_coverage_id');
  18. }
  19. public function effectiveClientPrimaryCoverage(){
  20. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'effective_client_primary_coverage_id');
  21. }
  22. public function latestClientPrimaryCoverage(){
  23. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_client_primary_coverage_id');
  24. }
  25. public function latestNewClientPrimaryCoverage(){
  26. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_new_client_primary_coverage_id');
  27. }
  28. public function latestAutoRefreshClientPrimaryCoverage(){
  29. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_auto_refresh_client_primary_coverage_id')
  30. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)');
  31. }
  32. public function latestManualClientPrimaryCoverage(){
  33. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_manual_client_primary_coverage_id')
  34. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)');
  35. }
  36. public function temporaryOutsiderNewClientPrimaryCoverage(){
  37. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'temporary_outsider_new_client_primary_coverage_id');
  38. }
  39. public function displayName($_flat = true)
  40. {
  41. $result = '';
  42. if($_flat) {
  43. $result = $this->name_first . ' ' . $this->name_last;
  44. }
  45. else {
  46. $result = $this->name_last . ', ' . $this->name_first;
  47. }
  48. if($this->client_engagement_status_category == 'DUMMY') {
  49. $result .= ' [Test Record]';
  50. }
  51. if($this->client_engagement_status_category == 'DUPLICATE') {
  52. $result .= ' [Duplicate Record]';
  53. }
  54. return $result;
  55. }
  56. public function currentBmiManagementSettingsValueUpdatedByPro()
  57. {
  58. return $this->hasOne(Pro::class, 'id', 'current_bmi_management_settings_value_updated_by_pro_id');
  59. }
  60. public function currentBpManagementSettingsValueUpdatedByPro()
  61. {
  62. return $this->hasOne(Pro::class, 'id', 'current_bp_management_settings_value_updated_by_pro_id');
  63. }
  64. public function rtmMskHcp()
  65. {
  66. return $this->hasOne(Pro::class, 'id', 'rtm_msk_hcp_pro_id');
  67. }
  68. public function rtmLungHcp()
  69. {
  70. return $this->hasOne(Pro::class, 'id', 'rtm_lung_hcp_pro_id');
  71. }
  72. public function rtmMskCoach()
  73. {
  74. return $this->hasOne(Pro::class, 'id', 'rtm_msk_coach_pro_id');
  75. }
  76. public function rtmLungCoach()
  77. {
  78. return $this->hasOne(Pro::class, 'id', 'rtm_lung_coach_pro_id');
  79. }
  80. public function mcp()
  81. {
  82. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  83. }
  84. public function rd()
  85. {
  86. return $this->hasOne(Pro::class, 'id', 'rd_pro_id');
  87. }
  88. public function pcp()
  89. {
  90. return $this->hasOne(Pro::class, 'id', 'physician_pro_id');
  91. }
  92. public function cm()
  93. {
  94. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  95. }
  96. public function rmm()
  97. {
  98. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  99. }
  100. public function rme()
  101. {
  102. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  103. }
  104. public function rms()
  105. {
  106. return $this->hasOne(Pro::class, 'id', 'rms_pro_id');
  107. }
  108. public function rmg()
  109. {
  110. return $this->hasOne(Pro::class, 'id', 'rmg_pro_id');
  111. }
  112. public function defaultNaPro()
  113. {
  114. return $this->hasOne(Pro::class, 'id', 'default_na_pro_id');
  115. }
  116. public function watchPatManagerPro()
  117. {
  118. return $this->hasOne(Pro::class, 'id', 'watch_pat_manager_pro_id');
  119. }
  120. public function watchPatMedicalNecessityNote()
  121. {
  122. return $this->hasOne(Note::class, 'id', 'watch_pat_medical_necessity_note_id');
  123. }
  124. public function watchPatStudyInstructionsNote()
  125. {
  126. return $this->hasOne(Note::class, 'id', 'watch_pat_study_instructions_note_id');
  127. }
  128. public function watchPatStudyOrderedNote()
  129. {
  130. return $this->hasOne(Note::class, 'id', 'watch_pat_study_ordered_note_id');
  131. }
  132. public function stickyNoteUpdatedBySession()
  133. {
  134. return $this->hasOne(AppSession::class, 'id', 'sticky_note_updated_by_session_id');
  135. }
  136. public function creator()
  137. {
  138. return $this->hasOne(Pro::class, 'id', 'created_by_pro_id');
  139. }
  140. public function prosInMeetingWith()
  141. {
  142. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  143. }
  144. public function notes()
  145. {
  146. return $this->hasMany(Note::class, 'client_id', 'id')
  147. // ->where('is_core_note', false)
  148. ->orderBy('effective_dateest', 'desc')
  149. ->orderBy('created_at', 'desc');
  150. }
  151. public function notesPendingClaimsClosed()
  152. {
  153. return $this->hasMany(Note::class, 'client_id', 'id')
  154. ->where('is_claim_closed', false)
  155. ->where('is_signed_by_hcp', true)
  156. ->where('is_cancelled', false)
  157. ->orderBy('effective_dateest', 'desc')
  158. ->orderBy('created_at', 'desc');
  159. }
  160. public function prescriptions()
  161. {
  162. return $this->hasMany(Erx::class, 'client_id', 'id')
  163. /*->where(function ($q) {
  164. $q->whereNull('pro_declared_status')
  165. ->orWhere('pro_declared_status', '<>', 'CANCELLED');
  166. })*/
  167. ->orderBy('created_at', 'desc')
  168. ->orderByRaw('note_id DESC NULLS LAST');
  169. }
  170. public function prescriptionsCreatedInNote($note)
  171. {
  172. return Erx::where('client_id', $this->id)
  173. ->where('note_id', $note->id)
  174. ->orderBy('created_at', 'desc')
  175. ->where(function ($q) {
  176. $q->whereNull('pro_declared_status')
  177. ->orWhere('pro_declared_status', '<>', 'CANCELLED');
  178. })
  179. ->get();
  180. }
  181. public function notesAscending()
  182. {
  183. return $this->hasMany(Note::class, 'client_id', 'id')
  184. ->orderBy('effective_dateest', 'asc')
  185. ->orderBy('created_at', 'desc');;
  186. }
  187. public function mcCodeChecks(){
  188. // $tables = DB::select("SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'");
  189. // foreach ($tables as $table) {
  190. // dump($table);
  191. // }
  192. // die();
  193. return $this->hasMany(McCodeCheck::class, 'client_id', 'id')->orderBy('created_at', 'asc');
  194. }
  195. public function activeNotes()
  196. {
  197. return $this->hasMany(Note::class, 'client_id', 'id')
  198. ->where('is_cancelled', false)
  199. ->where('id', '<>', $this->core_note_id)
  200. ->orderBy('effective_dateest', 'desc')
  201. ->orderBy('created_at', 'desc');;
  202. }
  203. public function cancelledNotes()
  204. {
  205. return $this->hasMany(Note::class, 'client_id', 'id')
  206. ->where('is_cancelled', true)
  207. ->where('id', '<>', $this->core_note_id)
  208. ->orderBy('effective_dateest', 'desc')
  209. ->orderBy('created_at', 'desc');;
  210. }
  211. public function sections()
  212. {
  213. return $this->hasMany(Section::class, 'client_id', 'id')
  214. ->where('is_active', true)
  215. ->orderBy('created_at', 'asc');
  216. }
  217. public function handouts($note = null)
  218. {
  219. $mappings = HandoutClient::where('client_id', $this->id);
  220. if($note) {
  221. $mappings = $mappings->where('note_id', $note->id);
  222. }
  223. $mappings = $mappings->get();
  224. return $mappings;
  225. // $handouts = new Collection();
  226. // foreach ($mappings as $mapping) {
  227. // $handout = Handout::where('id', $mapping->handout_id)->first();
  228. // $handout->handout_client_uid = $mapping->uid;
  229. // $handouts->add($handout);
  230. // }
  231. // $handouts = $handouts->sortBy('created_at');
  232. // return $handouts;
  233. }
  234. public function duplicateOf()
  235. {
  236. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  237. }
  238. public function actionItems()
  239. {
  240. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  241. ->orderBy('action_item_category', 'asc')
  242. ->orderBy('created_at', 'desc');
  243. }
  244. public function infoLines()
  245. {
  246. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  247. }
  248. public function measurements()
  249. {
  250. return $this->hasMany(Measurement::class, 'client_id', 'id')
  251. /*->distinct('label')*/
  252. ->where('is_active', true)
  253. ->orderByRaw('ts DESC NULLS LAST');
  254. }
  255. public function cellularMeasurements()
  256. {
  257. return $this->hasMany(Measurement::class, 'client_id', 'id')
  258. /*->distinct('label')*/
  259. ->where('is_active', true)
  260. ->where('is_cellular', true)
  261. ->where('is_cellular_zero', false)
  262. ->orderByRaw('ts DESC NULLS LAST');
  263. }
  264. public function recentMeasurements()
  265. {
  266. return $this->hasMany(Measurement::class, 'client_id', 'id')
  267. ->where('is_active', true)
  268. ->whereNotNull('label')
  269. ->where('label', '<>', 'SBP')
  270. ->where('label', '<>', 'DBP')
  271. ->where('is_cellular_zero', false)
  272. ->orderByRaw('ts DESC NULLS LAST')
  273. ->offset(0)->limit(20);
  274. }
  275. public function nonZeroMeasurements()
  276. {
  277. return $this->hasMany(Measurement::class, 'client_id', 'id')
  278. /*->distinct('label')*/
  279. ->where('is_active', true)
  280. ->where('is_cellular_zero', false)
  281. ->orderBy('effective_date', 'desc');
  282. }
  283. public function getNonZeroBpMeasurements(){
  284. return $this->hasMany(Measurement::class, 'client_id', 'id')
  285. /*->distinct('label')*/
  286. ->where('is_active', true)
  287. ->where('label', '=', 'BP')
  288. ->where('sbp_mm_hg', '>', 0)
  289. ->where('dbp_mm_hg', '>', 0)
  290. ->orderBy('ts', 'desc');
  291. }
  292. public function getNonZeroWeightMeasurements(){
  293. return $this->hasMany(Measurement::class, 'client_id', 'id')
  294. /*->distinct('label')*/
  295. ->where('is_active', true)
  296. ->where('label', '=', 'Wt. (lbs.)')
  297. ->where('numeric_value', '>', 0)
  298. ->orderBy('ts', 'desc');
  299. }
  300. public function currentCareMonth()
  301. {
  302. $cmStartDate = date('Y-m-01');
  303. return CareMonth::where('client_id', $this->id)
  304. ->whereRaw('start_date = ?', [$cmStartDate])
  305. ->first();
  306. }
  307. // returns the current care-month, if in fact patient is awaiting mcp rm interaction
  308. public function isAwaitingRMCommunicationFromMCP() {
  309. // if patient is enrolled in rm AND has current care month AND $careMonth->has_mcp_interacted_with_client_about_rm IS FALSE
  310. $careMonth = $this->currentCareMonth();
  311. if(!$careMonth) return false;
  312. if(!$careMonth->is_client_enrolled_in_rm) return false;
  313. return $careMonth->has_mcp_interacted_with_client_about_rm ? false : $careMonth;
  314. }
  315. public function previousCareMonth()
  316. {
  317. $cmStartDate = date('Y-m-d', strtotime('first day of last month'));
  318. return CareMonth
  319. ::where('client_id', $this->id)
  320. ->where('start_date', $cmStartDate)
  321. ->first();
  322. }
  323. public function measurementsInCareMonth(CareMonth $careMonth)
  324. {
  325. $cmStartDate = strtotime($careMonth->start_date);
  326. $month = date("n", $cmStartDate);
  327. $year = date("Y", $cmStartDate);
  328. $measurements = Measurement
  329. ::where('client_id', $this->id)
  330. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  331. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  332. ->where('is_active', true)
  333. ->orderBy('ts', 'desc')
  334. ->get();
  335. return $measurements;
  336. }
  337. public function allMeasurements()
  338. {
  339. return $this->hasMany(Measurement::class, 'client_id', 'id')
  340. ->where('is_active', true)
  341. ->whereNull('parent_measurement_id')
  342. ->orderBy('label', 'asc')
  343. ->orderBy('effective_date', 'desc');
  344. }
  345. public function smses()
  346. {
  347. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  348. ->orderBy('created_at', 'desc');
  349. }
  350. public function ismses()
  351. {
  352. return $this->hasMany(Isms::class, 'client_id', 'id')
  353. ->orderBy('created_at', 'desc');
  354. }
  355. public function documents()
  356. {
  357. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  358. ->orderBy('created_at', 'desc');
  359. }
  360. public function incomingReports() {
  361. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  362. ->orderBy('created_at', 'desc');
  363. }
  364. public function smsNumbers() {
  365. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  366. ->orderBy('created_at', 'desc');
  367. }
  368. public function nextMcpAppointment()
  369. {
  370. return $this->hasOne(Appointment::class, 'id', 'next_mcp_appointment_id');
  371. }
  372. public function lastMcpAppointment()
  373. {
  374. return $this->hasOne(Appointment::class, 'id', 'previous_mcp_appointment_id');
  375. }
  376. public function mostRecentCompletedMcpNote()
  377. {
  378. return $this->hasOne(Note::class, 'id', 'most_recent_completed_mcp_note_id');
  379. }
  380. public function lastMeasurementOfType($_type) {
  381. return Measurement::where('client_id', $this->id)
  382. ->whereNotNull('bdt_measurement_id')
  383. ->whereNotNull('ts')
  384. ->where('is_cellular_zero', false)
  385. ->where('is_active', true)
  386. ->where('label', '=', $_type)
  387. ->orderBy('ts', 'desc')
  388. ->first();
  389. }
  390. public function appointments()
  391. {
  392. return $this->hasMany(Appointment::class, 'client_id', 'id')
  393. ->orderBy('start_time', 'desc');
  394. }
  395. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  396. {
  397. $appointments = Appointment::where('client_id', $this->id);
  398. if($forPro !== 'all') {
  399. $forPro = Pro::where('uid', $forPro)->first();
  400. $appointments = $appointments->where('pro_id', $forPro->id);
  401. }
  402. if($status !== 'ALL') {
  403. $appointments = $appointments->where('status', $status);
  404. }
  405. $appointments = $appointments->orderBy('raw_date', 'desc')->orderBy('raw_start_time', 'desc');
  406. return $appointments->get();
  407. }
  408. public function upcomingAppointments()
  409. {
  410. return $this->hasMany(Appointment::class, 'client_id', 'id')
  411. ->where('raw_date', '>=', date('Y-m-d'))
  412. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  413. ->orderBy('start_time', 'desc')
  414. ->limit(5);
  415. }
  416. public function nextAppointment() {
  417. return Appointment
  418. ::where('client_id', $this->id)
  419. ->where('raw_date', '>=', DB::raw('NOW()'))
  420. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  421. ->orderBy('start_time')
  422. ->first();
  423. }
  424. public function appointmentsFromLastWeek()
  425. {
  426. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  427. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  428. return $this->hasMany(Appointment::class, 'client_id', 'id')
  429. ->where('raw_date', '>=', $dateLastWeek)
  430. ->orderBy('start_time', 'desc');
  431. }
  432. public function memos()
  433. {
  434. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  435. ->where('is_cancelled', false)
  436. ->orderBy('created_at', 'desc');
  437. }
  438. public function latestMemo()
  439. {
  440. return ClientMemo::where('client_id', $this->id)
  441. ->where('is_cancelled', false)
  442. ->orderBy('created_at', 'desc')
  443. ->first();
  444. }
  445. public function devices()
  446. {
  447. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  448. ->where('is_active', true)
  449. ->orderBy('created_at', 'desc');
  450. }
  451. public function deactivatedDevices()
  452. {
  453. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  454. ->where('is_active', false)
  455. ->orderBy('created_at', 'desc');
  456. }
  457. public function hasDevice($_device)
  458. {
  459. $count = ClientBDTDevice::where('client_id', $this->id)
  460. ->where('device_id', $_device->id)
  461. ->where('is_active', true)
  462. ->count();
  463. return !!$count;
  464. }
  465. public function deviceMeasurements()
  466. {
  467. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  468. ->orderBy('created_at', 'desc');
  469. }
  470. public function activeMcpRequest()
  471. {
  472. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  473. }
  474. public function clientPrograms()
  475. {
  476. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  477. ->where('is_active', true)
  478. ->orderBy('title', 'desc');
  479. }
  480. public function tickets()
  481. {
  482. return $this->hasMany(Ticket::class, 'client_id', 'id')
  483. ->orderBy('is_open', 'desc')
  484. ->orderBy('created_at', 'desc');
  485. }
  486. public function mcpDisplayName()
  487. {
  488. }
  489. public function rmeDisplayName()
  490. {
  491. }
  492. public function supplyOrderForCellularBPDevice() {
  493. return SupplyOrder::where('product_id', 1)
  494. ->where('is_cancelled', false)
  495. ->where('client_id', $this->id)
  496. ->orderBy('id', 'desc')
  497. ->first();
  498. }
  499. public function supplyOrderForCellularWeightScale() {
  500. return SupplyOrder::where('product_id', 2)
  501. ->where('is_cancelled', false)
  502. ->where('client_id', $this->id)
  503. ->orderBy('id', 'desc')
  504. ->first();
  505. }
  506. public function firstCellularBPDevice()
  507. {
  508. $devices = $this->devices;
  509. $x = null;
  510. foreach($devices as $device){
  511. if($device->device->category == 'BP'){
  512. $x = $device;
  513. break;
  514. }
  515. }
  516. return $x;
  517. }
  518. public function getFirstCellularBPMeasurementAt()
  519. {
  520. }
  521. public function getLatestCellularBPMeasurementAt()
  522. {
  523. }
  524. public function getTotalCellularBPMeasurements()
  525. {
  526. }
  527. public function firstCellularWeightDevice()
  528. {
  529. $devices = $this->devices;
  530. $x = null;
  531. foreach($devices as $device){
  532. if($device->device->category == 'WEIGHT'){
  533. $x = $device;
  534. break;
  535. }
  536. }
  537. return $x;
  538. }
  539. public function getFirstCellularWeightMeasurementAt()
  540. {
  541. }
  542. public function getLatestCellularWeightMeasurementAt()
  543. {
  544. }
  545. public function getTotalCellularWeightMeasurements()
  546. {
  547. }
  548. public function prosWithAccess()
  549. {
  550. $pros = [];
  551. // directly associated pros
  552. $pro = $this->mcp;
  553. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  554. $pro = $this->pcp;
  555. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  556. $pro = $this->cm;
  557. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  558. $pro = $this->rmm;
  559. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  560. $pro = $this->rme;
  561. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  562. $pro = $this->defaultNaPro;
  563. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Care Coordinator'];
  564. // via client pro access
  565. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  566. foreach ($cpAccesses as $cpAccess) {
  567. if (!$cpAccess->pro) continue;
  568. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => $cpAccess->reason_category. ' - Via Client Pro Access', 'isClientProAccess'=>true, 'clientProAccess'=>$cpAccess];
  569. }
  570. // via appointments
  571. $appointments = Appointment::where('client_id', $this->id)->get();
  572. foreach ($appointments as $appointment) {
  573. if (!$appointment->pro) continue;
  574. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  575. }
  576. // via client program
  577. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  578. foreach ($clientPrograms as $clientProgram) {
  579. if ($clientProgram->mcp)
  580. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  581. if ($clientProgram->manager)
  582. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  583. }
  584. // sort by pro name
  585. $name = array_column($pros, 'pro');
  586. array_multisort($name, SORT_ASC, $pros);
  587. return $pros;
  588. }
  589. public function mcpRequests()
  590. {
  591. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  592. ->orderBy('created_at', 'desc');
  593. }
  594. public function eligibleRefreshes()
  595. {
  596. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  597. ->orderBy('created_at', 'desc');
  598. }
  599. public function mbPayerValidationResults()
  600. {
  601. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  602. ->orderBy('created_at', 'desc');
  603. }
  604. public function payer()
  605. {
  606. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  607. }
  608. public function supplyOrders()
  609. {
  610. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  611. ->orderBy('created_at', 'desc');
  612. }
  613. public function activeSupplyOrders()
  614. {
  615. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  616. ->where('is_cancelled', false)
  617. ->orderBy('created_at', 'desc');
  618. }
  619. public function cancelledSupplyOrders()
  620. {
  621. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  622. ->where('is_cancelled', true)
  623. ->orderBy('created_at', 'desc');
  624. }
  625. public function readyToShipSupplyOrders()
  626. {
  627. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  628. ->where('is_cancelled', false)
  629. ->where('is_cleared_for_shipment', true)
  630. ->whereNull('shipment_id')
  631. ->orderBy('created_at', 'desc');
  632. }
  633. public function shipments()
  634. {
  635. return $this->hasMany(Shipment::class, 'client_id', 'id')
  636. ->where('is_cancelled', false)
  637. ->orderBy('created_at', 'desc');
  638. }
  639. public function numSignedNotes() {
  640. return Note::where('client_id', $this->id)
  641. ->where('is_cancelled', false)
  642. ->where('is_signed_by_hcp', true)
  643. ->count();
  644. }
  645. public function smsReminders()
  646. {
  647. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  648. ->orderBy('created_at', 'asc');
  649. }
  650. public function measurementConfirmationNumbers()
  651. {
  652. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  653. ->orderBy('created_at', 'asc');
  654. }
  655. public function shadowOfPro() {
  656. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  657. }
  658. public function clientTags()
  659. {
  660. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  661. ->where('is_cancelled', false)
  662. ->orderBy('tag', 'asc');
  663. }
  664. public function medicalTeam()
  665. {
  666. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  667. ->where('is_active', true)
  668. ->whereNotNull('pro_id')
  669. ->orderBy('created_at', 'asc');
  670. }
  671. public function accountInvites()
  672. {
  673. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  674. ->orderBy('created_at', 'desc');
  675. }
  676. public function linkedAccounts()
  677. {
  678. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  679. ->orderBy('created_at', 'desc');
  680. }
  681. public function pages($_type, $_returnShadows, $_note) {
  682. return Page
  683. ::where('client_id', $this->id)
  684. ->where('note_id', $_note->id)
  685. ->where('category', $_type)
  686. ->where('is_shadow', !!$_returnShadows)
  687. ->orderBy('key', 'ASC')
  688. ->get();
  689. }
  690. public function firstPageByCategoryAndKey($_category, $_key) {
  691. return Page
  692. ::where('client_id', $this->id)
  693. ->where('category', $_category)
  694. ->where('key', $_key)
  695. ->first();
  696. }
  697. public function cmReasons()
  698. {
  699. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  700. ->where('cm_or_rm', 'CM')
  701. ->where('is_removed', false)
  702. ->orderBy('position_index', 'ASC')
  703. ->orderBy('code', 'ASC');
  704. }
  705. public function rmReasons()
  706. {
  707. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  708. ->where('cm_or_rm', 'RM')
  709. ->where('is_removed', false)
  710. ->orderBy('position_index', 'ASC')
  711. ->orderBy('code', 'ASC');
  712. }
  713. public function cmSetupNote()
  714. {
  715. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  716. }
  717. public function rmSetupCareMonth()
  718. {
  719. return $this->hasOne(CareMonth::class, 'id', 'rm_setup_care_month_id');
  720. }
  721. public function defaultMcpCompanyPro()
  722. {
  723. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  724. }
  725. public function defaultMcpCompanyProPayer()
  726. {
  727. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  728. }
  729. public function defaultMcpCompanyLocation()
  730. {
  731. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  732. }
  733. public function hasNewNoteForPro($_pro) {
  734. $count = Note::where('client_id', $this->id)->where('hcp_pro_id', $_pro->id)->where('is_cancelled', false)->where('new_or_fu_or_na', 'NEW')->count();
  735. return !!$count;
  736. }
  737. public function systemSourcePro()
  738. {
  739. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  740. }
  741. public function systemSourceProTeam()
  742. {
  743. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  744. }
  745. public function adminEngagementAssessmentStatus(){
  746. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  747. }
  748. public function mcpEngagementAssessmentStatus(){
  749. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  750. }
  751. public function defaultNaEngagementAssessmentStatus(){
  752. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  753. }
  754. public function clientSelfSatisfactionStatus(){
  755. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  756. }
  757. public function recentNotes($_pro = null) {
  758. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  759. if($_pro) {
  760. $notes = $notes->where('hcp_pro_id', $_pro->id);
  761. }
  762. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  763. return $notes;
  764. }
  765. public function cmMeasurementsMatrix($_careMonth, $pro = null) {
  766. $days = [];
  767. $matches = DB::select(
  768. "
  769. SELECT m.id AS measurement_id,
  770. m.uid AS measurement_uid,
  771. cm.id AS care_month_id,
  772. cm.uid AS care_month_uid,
  773. m.label,
  774. m.value,
  775. m.dbp_mm_hg,
  776. m.sbp_mm_hg,
  777. m.value_pulse,
  778. m.value_irregular,
  779. m.numeric_value,
  780. m.effective_date,
  781. m.ts,
  782. m.has_been_stamped_by_mcp,
  783. m.has_been_stamped_by_rmm,
  784. m.has_been_stamped_by_non_hcp
  785. FROM measurement m
  786. JOIN care_month cm ON m.care_month_id = cm.id
  787. WHERE m.care_month_id = :careMonthID
  788. AND m.label NOT IN ('SBP', 'DBP')
  789. AND m.bdt_measurement_id IS NOT NULL
  790. AND m.is_active IS TRUE
  791. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  792. AND m.ts IS NOT NULL
  793. AND m.client_bdt_measurement_id IS NOT NULL
  794. ORDER BY m.ts DESC
  795. ",
  796. ['careMonthID' => $_careMonth->id]
  797. );
  798. foreach ($matches as $match) {
  799. $time = (floor($match->ts / 1000));
  800. $realTimezone = resolve_timezone('EASTERN');
  801. $date = new \DateTime("@$time");
  802. $date->setTimezone(new \DateTimeZone($realTimezone));
  803. $match->date = $date->format("m/d/Y");
  804. $match->dateYMD = $date->format("Y-m-d");
  805. $match->time = $date->format("h:i A");
  806. // get existing entries for listing
  807. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  808. ->where('is_removed', false)
  809. ->where('effective_date', $match->dateYMD);
  810. if(!!$pro) {
  811. $match->entries = $match->entries->where('pro_id', $pro->id);
  812. }
  813. $match->entries = $match->entries->orderBy('created_at')->get();
  814. if(!isset($days[$match->date])) {
  815. $days[$match->date] = [];
  816. }
  817. $days[$match->date][] = $match;
  818. }
  819. return $days;
  820. }
  821. public function getPrimaryCoverage()
  822. {
  823. return $this->effectiveClientPrimaryCoverage;
  824. }
  825. // return value will be YES, NO or UNKNOWN
  826. public function getPrimaryCoverageStatus() {
  827. $coverage = $this->getPrimaryCoverage();
  828. if(!$coverage) return 'NO';
  829. return $coverage->getStatus();
  830. }
  831. public function getMcpAssignedOn() {
  832. $change = ClientProChange::where('client_id', $this->id)
  833. ->where('new_pro_id', $this->mcp_pro_id)
  834. ->where('responsibility_type', 'MCP')
  835. ->orderBy('created_at', 'DESC')
  836. ->first();
  837. if(!!$change) {
  838. return friendlier_date($change->created_at);
  839. }
  840. return '-';
  841. }
  842. public function coreNote(){
  843. return $this->hasOne(Note::class, 'id', 'core_note_id');
  844. }
  845. public function nonCoreVisitNotes() {
  846. return $this->hasMany(Note::class, 'client_id', 'id')
  847. ->where('id', '<>', $this->core_note_id)
  848. ->whereNotNull('visit_template_id')
  849. ->orderBy('created_at', 'desc');
  850. }
  851. public function clientProChanges() {
  852. return $this->hasMany(ClientProChange::class, 'client_id', 'id')
  853. ->orderBy('created_at', 'desc');
  854. }
  855. public function mostRecentWeightMeasurement(){
  856. return $this->hasOne(Measurement::class, 'id', 'most_recent_weight_measurement_id');
  857. }
  858. public function hasBPDevice() {
  859. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  860. foreach ($cbds as $cbd) {
  861. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'BP') return true;
  862. }
  863. return false;
  864. }
  865. public function hasWeightScaleDevice() {
  866. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  867. foreach ($cbds as $cbd) {
  868. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'WEIGHT') return true;
  869. }
  870. return false;
  871. }
  872. public function clientEngagementStatus(){
  873. return $this->hasOne(Status::class, 'id', 'client_engagement_status_id');
  874. }
  875. public function clientBpWeightPhoneNumberStatus(){
  876. return $this->hasOne(ClientBpWeightPhoneNumberStatus::class, 'id', 'client_bp_weight_phone_number_status_id');
  877. }
  878. public function getDeviceDeliveryStatus($productId){
  879. $result = DB::select("SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id = ".$productId." AND so.client_id = ".$this->id." ORDER BY sh.created_at DESC LIMIT 1");
  880. if (count($result)){
  881. return $result[0]->status;
  882. }
  883. return '';
  884. }
  885. public function hasDataInCanvas($_type) {
  886. $page = Page::where('client_id', $this->id)->where('category', 'CANVAS')->where('key', $_type)->first();
  887. $contentData = [];
  888. if($page){
  889. $contentData = json_decode($page->data, true);
  890. }else{
  891. if($this->canvas_data) {
  892. $canvasData = json_decode($this->canvas_data, true);
  893. if(isset($canvasData[$_type])) {
  894. $contentData = $canvasData[$_type];
  895. if($_type !== 'vitals') {
  896. if(!isset($contentData['items'])){
  897. $contentData['items'] = [];
  898. }
  899. }
  900. }
  901. }
  902. }
  903. if($_type !== 'vitals') {
  904. if (isset($contentData['items']) && count($contentData['items'])) {
  905. return true;
  906. }
  907. }
  908. else {
  909. foreach ($contentData as $cd) {
  910. if(isset($cd['value']) && !empty($cd['value'])) return true;
  911. }
  912. }
  913. return false;
  914. }
  915. // 4 Infra-red Temperature gun
  916. public function temparatureGunDeliveryStatus(){
  917. return $this->getDeviceDeliveryStatus(4);
  918. }
  919. // 3 Pulse Oximeter (
  920. public function pulseOximeterDeliveryStatus(){
  921. return $this->getDeviceDeliveryStatus(3);
  922. }
  923. // 1 Cellular BP - Standard Arm Cuff (if delivered, then it should show its status as Delivered, Dispatched, In-transit, Returned)
  924. public function cellularBPDeliveryStatus(){
  925. return $this->getDeviceDeliveryStatus(1);
  926. }
  927. // 2 Weight scale
  928. public function weightScaleDeliveryStatus(){
  929. return $this->getDeviceDeliveryStatus(2);
  930. }
  931. public function carePlanFlaggedBy(){
  932. return $this->hasOne(Pro::class, 'id', 'flagged_by_pro_id');
  933. }
  934. public function carePlanFlagAcknowledgedBy(){
  935. return $this->hasOne(Pro::class, 'id', 'flag_acknowledged_by_pro_id');
  936. }
  937. public function clientRepFollowUp(){
  938. return $this->hasOne(ClientRepFollowUp::class, 'id', 'client_rep_follow_up_id');
  939. }
  940. public function customers() {
  941. return $this->hasMany(Customer::class, 'client_id', 'id')
  942. ->orderBy('created_at', 'desc');
  943. }
  944. }