Client.php 25 KB

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