Client.php 34 KB

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