Client.php 24 KB

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