Client.php 33 KB

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