Client.php 27 KB

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