Client.php 28 KB

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