Client.php 26 KB

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