Client.php 24 KB

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