Client.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 lastMcpAppointment()
  244. {
  245. if ($this->mcp) {
  246. return Appointment::where('client_id', $this->id)
  247. ->where('pro_id', $this->mcp->id)
  248. ->where('start_time', '<', date('Y-m-d'))
  249. ->orderBy('start_time', 'asc')
  250. ->first();
  251. }
  252. return false;
  253. }
  254. public function lastMeasurementOfType($_type) {
  255. return Measurement::where('client_id', $this->id)
  256. ->whereNotNull('bdt_measurement_id')
  257. ->whereNotNull('ts')
  258. ->where('is_cellular_zero', false)
  259. ->where('is_removed', false)
  260. ->where('label', '=', $_type)
  261. ->orderBy('ts', 'desc')
  262. ->first();
  263. }
  264. public function appointments()
  265. {
  266. return $this->hasMany(Appointment::class, 'client_id', 'id')
  267. ->orderBy('start_time', 'desc');
  268. }
  269. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  270. {
  271. $appointments = Appointment::where('client_id', $this->id);
  272. if($forPro !== 'all') {
  273. $forPro = Pro::where('uid', $forPro)->first();
  274. $appointments = $appointments->where('pro_id', $forPro->id);
  275. }
  276. if($status !== 'ALL') {
  277. $appointments = $appointments->where('status', $status);
  278. }
  279. $appointments = $appointments->orderBy('start_time', 'desc');
  280. return $appointments->get();
  281. }
  282. public function upcomingAppointments()
  283. {
  284. return $this->hasMany(Appointment::class, 'client_id', 'id')
  285. ->where('raw_date', '>=', date('Y-m-d'))
  286. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  287. ->orderBy('start_time', 'desc')
  288. ->limit(5);
  289. }
  290. public function nextAppointment() {
  291. return Appointment
  292. ::where('client_id', $this->id)
  293. ->where('raw_date', '>=', DB::raw('NOW()'))
  294. ->whereIn('status', ['CONFIRMED', 'CREATED'])
  295. ->orderBy('start_time')
  296. ->first();
  297. }
  298. public function appointmentsFromLastWeek()
  299. {
  300. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
  301. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  302. return $this->hasMany(Appointment::class, 'client_id', 'id')
  303. ->where('raw_date', '>=', $dateLastWeek)
  304. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  305. ->orderBy('start_time', 'desc');
  306. }
  307. public function memos()
  308. {
  309. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  310. ->where('is_cancelled', false)
  311. ->orderBy('created_at', 'desc');
  312. }
  313. public function devices()
  314. {
  315. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  316. ->where('is_active', true)
  317. ->orderBy('created_at', 'desc');
  318. }
  319. public function deactivatedDevices()
  320. {
  321. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  322. ->where('is_active', false)
  323. ->orderBy('created_at', 'desc');
  324. }
  325. public function hasDevice($_device)
  326. {
  327. $count = ClientBDTDevice::where('client_id', $this->id)
  328. ->where('device_id', $_device->id)
  329. ->where('is_active', true)
  330. ->count();
  331. return !!$count;
  332. }
  333. public function deviceMeasurements()
  334. {
  335. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  336. ->orderBy('created_at', 'desc');
  337. }
  338. public function activeMcpRequest()
  339. {
  340. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  341. }
  342. public function clientPrograms()
  343. {
  344. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  345. ->where('is_active', true)
  346. ->orderBy('title', 'desc');
  347. }
  348. public function tickets()
  349. {
  350. return $this->hasMany(Ticket::class, 'client_id', 'id')
  351. ->orderBy('is_open', 'desc')
  352. ->orderBy('created_at', 'desc');
  353. }
  354. public function mcpDisplayName()
  355. {
  356. }
  357. public function rmeDisplayName()
  358. {
  359. }
  360. public function supplyOrderForCellularBPDevice() {
  361. return SupplyOrder::where('product_id', 1)
  362. ->where('is_cancelled', false)
  363. ->where('client_id', $this->id)
  364. ->orderBy('id', 'desc')
  365. ->first();
  366. }
  367. public function supplyOrderForCellularWeightScale() {
  368. return SupplyOrder::where('product_id', 2)
  369. ->where('is_cancelled', false)
  370. ->where('client_id', $this->id)
  371. ->orderBy('id', 'desc')
  372. ->first();
  373. }
  374. public function firstCellularBPDevice()
  375. {
  376. $devices = $this->devices;
  377. $x = null;
  378. foreach($devices as $device){
  379. if($device->device->category == 'BP'){
  380. $x = $device;
  381. break;
  382. }
  383. }
  384. return $x;
  385. }
  386. public function getFirstCellularBPMeasurementAt()
  387. {
  388. }
  389. public function getLatestCellularBPMeasurementAt()
  390. {
  391. }
  392. public function getTotalCellularBPMeasurements()
  393. {
  394. }
  395. public function firstCellularWeightDevice()
  396. {
  397. $devices = $this->devices;
  398. $x = null;
  399. foreach($devices as $device){
  400. if($device->device->category == 'WEIGHT'){
  401. $x = $device;
  402. break;
  403. }
  404. }
  405. return $x;
  406. }
  407. public function getFirstCellularWeightMeasurementAt()
  408. {
  409. }
  410. public function getLatestCellularWeightMeasurementAt()
  411. {
  412. }
  413. public function getTotalCellularWeightMeasurements()
  414. {
  415. }
  416. public function prosWithAccess()
  417. {
  418. $pros = [];
  419. // directly associated pros
  420. $pro = $this->mcp;
  421. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  422. $pro = $this->pcp;
  423. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  424. $pro = $this->cm;
  425. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  426. $pro = $this->rmm;
  427. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  428. $pro = $this->rme;
  429. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  430. $pro = $this->defaultNaPro;
  431. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Nurse'];
  432. // via client pro access
  433. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  434. foreach ($cpAccesses as $cpAccess) {
  435. if (!$cpAccess->pro) continue;
  436. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  437. }
  438. // via appointments
  439. $appointments = Appointment::where('client_id', $this->id)->get();
  440. foreach ($appointments as $appointment) {
  441. if (!$appointment->pro) continue;
  442. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  443. }
  444. // via client program
  445. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  446. foreach ($clientPrograms as $clientProgram) {
  447. if ($clientProgram->mcp)
  448. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  449. if ($clientProgram->manager)
  450. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  451. }
  452. // sort by pro name
  453. $name = array_column($pros, 'pro');
  454. array_multisort($name, SORT_ASC, $pros);
  455. return $pros;
  456. }
  457. public function mcpRequests()
  458. {
  459. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  460. ->orderBy('created_at', 'desc');
  461. }
  462. public function eligibleRefreshes()
  463. {
  464. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  465. ->orderBy('created_at', 'desc');
  466. }
  467. public function mbPayerValidationResults()
  468. {
  469. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  470. ->orderBy('created_at', 'desc');
  471. }
  472. public function payer()
  473. {
  474. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  475. }
  476. public function supplyOrders()
  477. {
  478. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  479. ->orderBy('created_at', 'desc');
  480. }
  481. public function activeSupplyOrders()
  482. {
  483. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  484. ->where('is_cancelled', false)
  485. ->orderBy('created_at', 'desc');
  486. }
  487. public function cancelledSupplyOrders()
  488. {
  489. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  490. ->where('is_cancelled', true)
  491. ->orderBy('created_at', 'desc');
  492. }
  493. public function readyToShipSupplyOrders()
  494. {
  495. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  496. ->where('is_cancelled', false)
  497. ->where('is_cleared_for_shipment', true)
  498. ->whereNull('shipment_id')
  499. ->orderBy('created_at', 'desc');
  500. }
  501. public function shipments()
  502. {
  503. return $this->hasMany(Shipment::class, 'client_id', 'id')
  504. ->where('is_cancelled', false)
  505. ->orderBy('created_at', 'desc');
  506. }
  507. public function numSignedNotes() {
  508. return Note::where('client_id', $this->id)
  509. ->where('is_cancelled', false)
  510. ->where('is_signed_by_hcp', true)
  511. ->count();
  512. }
  513. public function smsReminders()
  514. {
  515. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  516. ->orderBy('created_at', 'asc');
  517. }
  518. public function measurementConfirmationNumbers()
  519. {
  520. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  521. ->orderBy('created_at', 'asc');
  522. }
  523. public function shadowOfPro() {
  524. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  525. }
  526. public function clientTags()
  527. {
  528. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  529. ->where('is_cancelled', false)
  530. ->orderBy('tag', 'asc');
  531. }
  532. public function medicalTeam()
  533. {
  534. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  535. ->where('is_active', true)
  536. ->whereNotNull('pro_id')
  537. ->orderBy('created_at', 'asc');
  538. }
  539. public function accountInvites()
  540. {
  541. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  542. ->orderBy('created_at', 'desc');
  543. }
  544. public function linkedAccounts()
  545. {
  546. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  547. ->orderBy('created_at', 'desc');
  548. }
  549. public function pages($_type, $_returnShadows, $_note) {
  550. return Page
  551. ::where('client_id', $this->id)
  552. ->where('note_id', $_note->id)
  553. ->where('category', $_type)
  554. ->where('is_shadow', !!$_returnShadows)
  555. ->orderBy('key', 'ASC')
  556. ->get();
  557. }
  558. public function firstPageByCategoryAndKey($_category, $_key) {
  559. return Page
  560. ::where('client_id', $this->id)
  561. ->where('category', $_category)
  562. ->where('key', $_key)
  563. ->first();
  564. }
  565. public function cmReasons()
  566. {
  567. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  568. ->where('cm_or_rm', 'CM')
  569. ->orderBy('position_index', 'ASC')
  570. ->orderBy('code', 'ASC');
  571. }
  572. public function rmReasons()
  573. {
  574. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  575. ->where('cm_or_rm', 'RM')
  576. ->orderBy('position_index', 'ASC')
  577. ->orderBy('code', 'ASC');
  578. }
  579. public function cmSetupNote()
  580. {
  581. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  582. }
  583. public function defaultMcpCompanyPro()
  584. {
  585. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  586. }
  587. public function defaultMcpCompanyProPayer()
  588. {
  589. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  590. }
  591. public function defaultMcpCompanyLocation()
  592. {
  593. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  594. }
  595. public function hasNewNoteForPro($_pro) {
  596. $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();
  597. return !!$count;
  598. }
  599. public function systemSourcePro()
  600. {
  601. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  602. }
  603. public function systemSourceProTeam()
  604. {
  605. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  606. }
  607. public function recentNotes($_pro = null) {
  608. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  609. if($_pro) {
  610. $notes = $notes->where('hcp_pro_id', $_pro->id);
  611. }
  612. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  613. return $notes;
  614. }
  615. public function cmMeasurementsMatrix($_careMonth = null) {
  616. $days = [];
  617. $matches = DB::select(
  618. "
  619. SELECT m.id AS measurement_id,
  620. m.uid AS measurement_uid,
  621. cm.id AS care_month_id,
  622. cm.uid AS care_month_uid,
  623. m.label,
  624. m.value,
  625. m.dbp_mm_hg,
  626. m.sbp_mm_hg,
  627. m.value_pulse,
  628. m.value_irregular,
  629. m.numeric_value,
  630. m.effective_date,
  631. m.ts,
  632. m.has_been_stamped_by_mcp,
  633. m.has_been_stamped_by_non_hcp
  634. FROM measurement m
  635. JOIN care_month cm ON m.care_month_id = cm.id
  636. WHERE m.care_month_id = :careMonthID
  637. AND m.label NOT IN ('SBP', 'DBP')
  638. AND m.bdt_measurement_id IS NOT NULL
  639. AND m.is_removed IS FALSE
  640. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  641. AND m.ts IS NOT NULL
  642. AND m.client_bdt_measurement_id IS NOT NULL
  643. ORDER BY m.ts DESC
  644. ",
  645. ['careMonthID' => $_careMonth->id]
  646. );
  647. foreach ($matches as $match) {
  648. $time = (floor($match->ts / 1000));
  649. $realTimezone = resolve_timezone('EASTERN');
  650. $date = new \DateTime("@$time");
  651. $date->setTimezone(new \DateTimeZone($realTimezone));
  652. $match->date = $date->format("m/d/Y");
  653. $match->dateYMD = $date->format("Y-m-d");
  654. $match->time = $date->format("h:i A");
  655. // get existing entries for listing
  656. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  657. ->where('is_removed', false)
  658. ->where('effective_date', $match->dateYMD)
  659. ->orderBy('created_at')
  660. ->get();
  661. if(!isset($days[$match->date])) {
  662. $days[$match->date] = [];
  663. }
  664. $days[$match->date][] = $match;
  665. }
  666. return $days;
  667. }
  668. public function getPrimaryCoverage()
  669. {
  670. // try the latest manual coverage
  671. $coverage = $this->latestManualClientPrimaryCoverage;
  672. if (!$coverage) {
  673. // try the latest coverage
  674. $coverage = $this->latestClientPrimaryCoverage;
  675. }
  676. return $coverage;
  677. }
  678. // return value will be YES, NO or UNKNOWN
  679. public function getPrimaryCoverageStatus() {
  680. $coverage = $this->getPrimaryCoverage();
  681. if(!$coverage) return 'NO';
  682. return $coverage->getStatus();
  683. }
  684. public function getMcpAssignedOn() {
  685. $change = ClientProChange::where('client_id', $this->id)
  686. ->where('new_pro_id', $this->mcp_pro_id)
  687. ->where('responsibility_type', 'MCP')
  688. ->orderBy('created_at', 'DESC')
  689. ->first();
  690. if(!!$change) {
  691. return friendlier_date($change->created_at);
  692. }
  693. return '-';
  694. }
  695. }