Client.php 35 KB

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