Client.php 28 KB

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