Client.php 30 KB

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