Client.php 33 KB

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