Client.php 28 KB

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