Client.php 25 KB

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