Client.php 23 KB

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