Client.php 23 KB

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