Client.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)')
  14. ->orderBy('created_at', 'desc');
  15. }
  16. public function latestManualParentClientPrimaryCoverage(){
  17. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_manual_parent_client_primary_coverage_id');
  18. }
  19. public function effectiveClientPrimaryCoverage(){
  20. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'effective_client_primary_coverage_id');
  21. }
  22. public function latestClientPrimaryCoverage(){
  23. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_client_primary_coverage_id');
  24. }
  25. public function latestNewClientPrimaryCoverage(){
  26. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_new_client_primary_coverage_id');
  27. }
  28. public function latestAutoRefreshClientPrimaryCoverage(){
  29. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_auto_refresh_client_primary_coverage_id')
  30. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)');
  31. }
  32. public function latestManualClientPrimaryCoverage(){
  33. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'latest_manual_client_primary_coverage_id')
  34. ->whereRaw('(is_cancelled IS NULL OR is_cancelled IS FALSE)');
  35. }
  36. public function temporaryOutsiderNewClientPrimaryCoverage(){
  37. return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'temporary_outsider_new_client_primary_coverage_id');
  38. }
  39. public function displayName($_flat = true)
  40. {
  41. $result = '';
  42. if($_flat) {
  43. $result = $this->name_first . ' ' . $this->name_last;
  44. }
  45. else {
  46. $result = $this->name_last . ', ' . $this->name_first;
  47. }
  48. if($this->client_engagement_status_category == 'DUMMY') {
  49. $result .= ' [Test Record]';
  50. }
  51. if($this->client_engagement_status_category == 'DUPLICATE') {
  52. $result .= ' [Duplicate Record]';
  53. }
  54. return $result;
  55. }
  56. public function currentBmiManagementSettingsValueUpdatedByPro()
  57. {
  58. return $this->hasOne(Pro::class, 'id', 'current_bmi_management_settings_value_updated_by_pro_id');
  59. }
  60. public function currentBpManagementSettingsValueUpdatedByPro()
  61. {
  62. return $this->hasOne(Pro::class, 'id', 'current_bp_management_settings_value_updated_by_pro_id');
  63. }
  64. public function rtmMskHcp()
  65. {
  66. return $this->hasOne(Pro::class, 'id', 'rtm_msk_hcp_pro_id');
  67. }
  68. public function rtmLungHcp()
  69. {
  70. return $this->hasOne(Pro::class, 'id', 'rtm_lung_hcp_pro_id');
  71. }
  72. public function rtmMskCoach()
  73. {
  74. return $this->hasOne(Pro::class, 'id', 'rtm_msk_coach_pro_id');
  75. }
  76. public function rtmLungCoach()
  77. {
  78. return $this->hasOne(Pro::class, 'id', 'rtm_lung_coach_pro_id');
  79. }
  80. public function mcp()
  81. {
  82. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  83. }
  84. public function rd()
  85. {
  86. return $this->hasOne(Pro::class, 'id', 'rd_pro_id');
  87. }
  88. public function pcp()
  89. {
  90. return $this->hasOne(Pro::class, 'id', 'physician_pro_id');
  91. }
  92. public function cm()
  93. {
  94. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  95. }
  96. public function rmm()
  97. {
  98. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  99. }
  100. public function rme()
  101. {
  102. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  103. }
  104. public function rms()
  105. {
  106. return $this->hasOne(Pro::class, 'id', 'rms_pro_id');
  107. }
  108. public function rmg()
  109. {
  110. return $this->hasOne(Pro::class, 'id', 'rmg_pro_id');
  111. }
  112. public function defaultNaPro()
  113. {
  114. return $this->hasOne(Pro::class, 'id', 'default_na_pro_id');
  115. }
  116. public function watchPatManagerPro()
  117. {
  118. return $this->hasOne(Pro::class, 'id', 'watch_pat_manager_pro_id');
  119. }
  120. public function watchPatSleepSpecialistPro()
  121. {
  122. return $this->hasOne(Pro::class, 'id', 'watch_pat_sleep_specialist_id');
  123. }
  124. public function watchPatMedicalNecessityNote()
  125. {
  126. return $this->hasOne(Note::class, 'id', 'watch_pat_medical_necessity_note_id');
  127. }
  128. public function watchPatStudyInstructionsNote()
  129. {
  130. return $this->hasOne(Note::class, 'id', 'watch_pat_study_instructions_note_id');
  131. }
  132. public function watchPatStudyOrderedNote()
  133. {
  134. return $this->hasOne(Note::class, 'id', 'watch_pat_study_ordered_note_id');
  135. }
  136. public function stickyNoteUpdatedBySession()
  137. {
  138. return $this->hasOne(AppSession::class, 'id', 'sticky_note_updated_by_session_id');
  139. }
  140. public function creator()
  141. {
  142. return $this->hasOne(Pro::class, 'id', 'created_by_pro_id');
  143. }
  144. public function prosInMeetingWith()
  145. {
  146. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  147. }
  148. public function notes()
  149. {
  150. return $this->hasMany(Note::class, 'client_id', 'id')
  151. // ->where('is_core_note', false)
  152. ->orderBy('effective_dateest', 'desc')
  153. ->orderBy('created_at', 'desc');
  154. }
  155. public function notesPendingClaimsClosed()
  156. {
  157. return $this->hasMany(Note::class, 'client_id', 'id')
  158. ->where('is_claim_closed', false)
  159. ->where('is_signed_by_hcp', true)
  160. ->where('is_cancelled', false)
  161. ->orderBy('effective_dateest', 'desc')
  162. ->orderBy('created_at', 'desc');
  163. }
  164. public function prescriptions()
  165. {
  166. return $this->hasMany(Erx::class, 'client_id', 'id')
  167. /*->where(function ($q) {
  168. $q->whereNull('pro_declared_status')
  169. ->orWhere('pro_declared_status', '<>', 'CANCELLED');
  170. })*/
  171. ->orderBy('created_at', 'desc')
  172. ->orderByRaw('note_id DESC NULLS LAST');
  173. }
  174. public function prescriptionsCreatedInNote($note)
  175. {
  176. return Erx::where('client_id', $this->id)
  177. ->where('note_id', $note->id)
  178. ->orderBy('created_at', 'desc')
  179. ->where(function ($q) {
  180. $q->whereNull('pro_declared_status')
  181. ->orWhere('pro_declared_status', '<>', 'CANCELLED');
  182. })
  183. ->get();
  184. }
  185. public function notesAscending()
  186. {
  187. return $this->hasMany(Note::class, 'client_id', 'id')
  188. ->orderBy('effective_dateest', 'asc')
  189. ->orderBy('created_at', 'desc');;
  190. }
  191. public function mcCodeChecks(){
  192. // $tables = DB::select("SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'");
  193. // foreach ($tables as $table) {
  194. // dump($table);
  195. // }
  196. // die();
  197. return $this->hasMany(McCodeCheck::class, 'client_id', 'id')->orderBy('created_at', 'asc');
  198. }
  199. public function activeNotes()
  200. {
  201. return $this->hasMany(Note::class, 'client_id', 'id')
  202. ->where('is_cancelled', false)
  203. ->where('id', '<>', $this->core_note_id)
  204. ->orderBy('effective_dateest', 'desc')
  205. ->orderBy('created_at', 'desc');;
  206. }
  207. public function cancelledNotes()
  208. {
  209. return $this->hasMany(Note::class, 'client_id', 'id')
  210. ->where('is_cancelled', true)
  211. ->where('id', '<>', $this->core_note_id)
  212. ->orderBy('effective_dateest', 'desc')
  213. ->orderBy('created_at', 'desc');;
  214. }
  215. public function sections()
  216. {
  217. return $this->hasMany(Section::class, 'client_id', 'id')
  218. ->where('is_active', true)
  219. ->orderBy('created_at', 'asc');
  220. }
  221. public function handouts($note = null)
  222. {
  223. $mappings = HandoutClient::where('client_id', $this->id);
  224. if($note) {
  225. $mappings = $mappings->where('note_id', $note->id);
  226. }
  227. $mappings = $mappings->get();
  228. return $mappings;
  229. // $handouts = new Collection();
  230. // foreach ($mappings as $mapping) {
  231. // $handout = Handout::where('id', $mapping->handout_id)->first();
  232. // $handout->handout_client_uid = $mapping->uid;
  233. // $handouts->add($handout);
  234. // }
  235. // $handouts = $handouts->sortBy('created_at');
  236. // return $handouts;
  237. }
  238. public function duplicateOf()
  239. {
  240. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  241. }
  242. public function actionItems()
  243. {
  244. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  245. ->orderBy('action_item_category', 'asc')
  246. ->orderBy('created_at', 'desc');
  247. }
  248. public function infoLines()
  249. {
  250. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  251. }
  252. public function measurements()
  253. {
  254. return $this->hasMany(Measurement::class, 'client_id', 'id')
  255. /*->distinct('label')*/
  256. ->where('is_active', true)
  257. ->orderByRaw('ts DESC NULLS LAST');
  258. }
  259. public function cellularMeasurements()
  260. {
  261. return $this->hasMany(Measurement::class, 'client_id', 'id')
  262. /*->distinct('label')*/
  263. ->where('is_active', true)
  264. ->where('is_cellular', true)
  265. ->where('is_cellular_zero', false)
  266. ->orderByRaw('ts DESC NULLS LAST');
  267. }
  268. public function recentMeasurements()
  269. {
  270. return $this->hasMany(Measurement::class, 'client_id', 'id')
  271. ->where('is_active', true)
  272. ->whereNotNull('label')
  273. ->where('label', '<>', 'SBP')
  274. ->where('label', '<>', 'DBP')
  275. ->where('is_cellular_zero', false)
  276. ->orderByRaw('ts DESC NULLS LAST')
  277. ->offset(0)->limit(20);
  278. }
  279. public function nonZeroMeasurements()
  280. {
  281. return $this->hasMany(Measurement::class, 'client_id', 'id')
  282. /*->distinct('label')*/
  283. ->where('is_active', true)
  284. ->where('is_cellular_zero', false)
  285. ->orderBy('effective_date', 'desc');
  286. }
  287. public function getNonZeroBpMeasurements(){
  288. return $this->hasMany(Measurement::class, 'client_id', 'id')
  289. /*->distinct('label')*/
  290. ->where('is_active', true)
  291. ->where('label', '=', 'BP')
  292. ->where('sbp_mm_hg', '>', 0)
  293. ->where('dbp_mm_hg', '>', 0)
  294. ->orderBy('ts', 'desc');
  295. }
  296. public function getNonZeroWeightMeasurements(){
  297. return $this->hasMany(Measurement::class, 'client_id', 'id')
  298. /*->distinct('label')*/
  299. ->where('is_active', true)
  300. ->where('label', '=', 'Wt. (lbs.)')
  301. ->where('numeric_value', '>', 0)
  302. ->orderBy('ts', 'desc');
  303. }
  304. public function currentCareMonth()
  305. {
  306. $cmStartDate = date('Y-m-01');
  307. return CareMonth::where('client_id', $this->id)
  308. ->whereRaw('start_date = ?', [$cmStartDate])
  309. ->first();
  310. }
  311. // returns the current care-month, if in fact patient is awaiting mcp rm interaction
  312. public function isAwaitingRMCommunicationFromMCP() {
  313. // if patient is enrolled in rm AND has current care month AND $careMonth->has_mcp_interacted_with_client_about_rm IS FALSE
  314. $careMonth = $this->currentCareMonth();
  315. if(!$careMonth) return false;
  316. if(!$careMonth->is_client_enrolled_in_rm) return false;
  317. return $careMonth->has_mcp_interacted_with_client_about_rm ? false : $careMonth;
  318. }
  319. public function previousCareMonth()
  320. {
  321. $cmStartDate = date('Y-m-d', strtotime('first day of last month'));
  322. return CareMonth
  323. ::where('client_id', $this->id)
  324. ->where('start_date', $cmStartDate)
  325. ->first();
  326. }
  327. public function measurementsInCareMonth(CareMonth $careMonth)
  328. {
  329. $cmStartDate = strtotime($careMonth->start_date);
  330. $month = date("n", $cmStartDate);
  331. $year = date("Y", $cmStartDate);
  332. $measurements = Measurement
  333. ::where('client_id', $this->id)
  334. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  335. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  336. ->where('is_active', true)
  337. ->orderBy('ts', 'desc')
  338. ->get();
  339. return $measurements;
  340. }
  341. public function allMeasurements()
  342. {
  343. return $this->hasMany(Measurement::class, 'client_id', 'id')
  344. ->where('is_active', true)
  345. ->whereNull('parent_measurement_id')
  346. ->orderBy('label', 'asc')
  347. ->orderBy('effective_date', 'desc');
  348. }
  349. public function smses()
  350. {
  351. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  352. ->orderBy('created_at', 'desc');
  353. }
  354. public function ismses()
  355. {
  356. return $this->hasMany(Isms::class, 'client_id', 'id')
  357. ->orderBy('created_at', 'desc');
  358. }
  359. public function documents()
  360. {
  361. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  362. ->orderBy('created_at', 'desc');
  363. }
  364. public function incomingReports() {
  365. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  366. ->orderBy('created_at', 'desc');
  367. }
  368. public function smsNumbers() {
  369. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  370. ->orderBy('created_at', 'desc');
  371. }
  372. public function nextMcpAppointment()
  373. {
  374. return $this->hasOne(Appointment::class, 'id', 'next_mcp_appointment_id');
  375. }
  376. public function lastMcpAppointment()
  377. {
  378. return $this->hasOne(Appointment::class, 'id', 'previous_mcp_appointment_id');
  379. }
  380. public function mostRecentCompletedMcpNote()
  381. {
  382. return $this->hasOne(Note::class, 'id', 'most_recent_completed_mcp_note_id');
  383. }
  384. public function lastMeasurementOfType($_type) {
  385. return Measurement::where('client_id', $this->id)
  386. ->whereNotNull('bdt_measurement_id')
  387. ->whereNotNull('ts')
  388. ->where('is_cellular_zero', false)
  389. ->where('is_active', true)
  390. ->where('label', '=', $_type)
  391. ->orderBy('ts', 'desc')
  392. ->first();
  393. }
  394. public function appointments()
  395. {
  396. return $this->hasMany(Appointment::class, 'client_id', 'id')
  397. ->orderBy('start_time', 'desc');
  398. }
  399. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  400. {
  401. $appointments = Appointment::where('client_id', $this->id);
  402. if($forPro !== 'all') {
  403. $forPro = Pro::where('uid', $forPro)->first();
  404. $appointments = $appointments->where('pro_id', $forPro->id);
  405. }
  406. if($status !== 'ALL') {
  407. $appointments = $appointments->where('status', $status);
  408. }
  409. $appointments = $appointments->orderBy('raw_date', 'desc')->orderBy('raw_start_time', 'desc');
  410. return $appointments->get();
  411. }
  412. public function upcomingAppointments()
  413. {
  414. return $this->hasMany(Appointment::class, 'client_id', 'id')
  415. ->where('raw_date', '>=', date('Y-m-d'))
  416. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  417. ->orderBy('start_time', 'desc')
  418. ->limit(5);
  419. }
  420. public function nextAppointment() {
  421. return Appointment
  422. ::where('client_id', $this->id)
  423. ->where('raw_date', '>=', DB::raw('NOW()'))
  424. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  425. ->orderBy('start_time')
  426. ->first();
  427. }
  428. public function appointmentsFromLastWeek()
  429. {
  430. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  431. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  432. return $this->hasMany(Appointment::class, 'client_id', 'id')
  433. ->where('raw_date', '>=', $dateLastWeek)
  434. ->orderBy('start_time', 'desc');
  435. }
  436. public function memos()
  437. {
  438. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  439. ->where('is_cancelled', false)
  440. ->orderBy('created_at', 'desc');
  441. }
  442. public function latestMemo()
  443. {
  444. return ClientMemo::where('client_id', $this->id)
  445. ->where('is_cancelled', false)
  446. ->orderBy('created_at', 'desc')
  447. ->first();
  448. }
  449. public function devices()
  450. {
  451. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  452. ->where('is_active', true)
  453. ->orderBy('created_at', 'desc');
  454. }
  455. public function deactivatedDevices()
  456. {
  457. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  458. ->where('is_active', false)
  459. ->orderBy('created_at', 'desc');
  460. }
  461. public function hasDevice($_device)
  462. {
  463. $count = ClientBDTDevice::where('client_id', $this->id)
  464. ->where('device_id', $_device->id)
  465. ->where('is_active', true)
  466. ->count();
  467. return !!$count;
  468. }
  469. public function deviceMeasurements()
  470. {
  471. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  472. ->orderBy('created_at', 'desc');
  473. }
  474. public function activeMcpRequest()
  475. {
  476. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  477. }
  478. public function clientPrograms()
  479. {
  480. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  481. ->where('is_active', true)
  482. ->orderBy('title', 'desc');
  483. }
  484. public function tickets()
  485. {
  486. return $this->hasMany(Ticket::class, 'client_id', 'id')
  487. ->orderBy('is_open', 'desc')
  488. ->orderBy('created_at', 'desc');
  489. }
  490. public function mcpDisplayName()
  491. {
  492. }
  493. public function rmeDisplayName()
  494. {
  495. }
  496. public function supplyOrderForCellularBPDevice() {
  497. return SupplyOrder::where('product_id', 1)
  498. ->where('is_cancelled', false)
  499. ->where('client_id', $this->id)
  500. ->orderBy('id', 'desc')
  501. ->first();
  502. }
  503. public function supplyOrderForCellularWeightScale() {
  504. return SupplyOrder::where('product_id', 2)
  505. ->where('is_cancelled', false)
  506. ->where('client_id', $this->id)
  507. ->orderBy('id', 'desc')
  508. ->first();
  509. }
  510. public function firstCellularBPDevice()
  511. {
  512. $devices = $this->devices;
  513. $x = null;
  514. foreach($devices as $device){
  515. if($device->device->category == 'BP'){
  516. $x = $device;
  517. break;
  518. }
  519. }
  520. return $x;
  521. }
  522. public function getFirstCellularBPMeasurementAt()
  523. {
  524. }
  525. public function getLatestCellularBPMeasurementAt()
  526. {
  527. }
  528. public function getTotalCellularBPMeasurements()
  529. {
  530. }
  531. public function firstCellularWeightDevice()
  532. {
  533. $devices = $this->devices;
  534. $x = null;
  535. foreach($devices as $device){
  536. if($device->device->category == 'WEIGHT'){
  537. $x = $device;
  538. break;
  539. }
  540. }
  541. return $x;
  542. }
  543. public function getFirstCellularWeightMeasurementAt()
  544. {
  545. }
  546. public function getLatestCellularWeightMeasurementAt()
  547. {
  548. }
  549. public function getTotalCellularWeightMeasurements()
  550. {
  551. }
  552. public function prosWithAccess()
  553. {
  554. $pros = [];
  555. // directly associated pros
  556. $pro = $this->mcp;
  557. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  558. $pro = $this->pcp;
  559. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  560. $pro = $this->cm;
  561. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  562. $pro = $this->rmm;
  563. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  564. $pro = $this->rme;
  565. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  566. $pro = $this->defaultNaPro;
  567. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Care Coordinator'];
  568. // via client pro access
  569. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  570. foreach ($cpAccesses as $cpAccess) {
  571. if (!$cpAccess->pro) continue;
  572. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => $cpAccess->reason_category. ' - Via Client Pro Access', 'isClientProAccess'=>true, 'clientProAccess'=>$cpAccess];
  573. }
  574. // via appointments
  575. $appointments = Appointment::where('client_id', $this->id)->get();
  576. foreach ($appointments as $appointment) {
  577. if (!$appointment->pro) continue;
  578. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  579. }
  580. // via client program
  581. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  582. foreach ($clientPrograms as $clientProgram) {
  583. if ($clientProgram->mcp)
  584. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  585. if ($clientProgram->manager)
  586. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  587. }
  588. // sort by pro name
  589. $name = array_column($pros, 'pro');
  590. array_multisort($name, SORT_ASC, $pros);
  591. return $pros;
  592. }
  593. public function mcpRequests()
  594. {
  595. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  596. ->orderBy('created_at', 'desc');
  597. }
  598. public function eligibleRefreshes()
  599. {
  600. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  601. ->orderBy('created_at', 'desc');
  602. }
  603. public function mbPayerValidationResults()
  604. {
  605. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  606. ->orderBy('created_at', 'desc');
  607. }
  608. public function payer()
  609. {
  610. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  611. }
  612. public function supplyOrders()
  613. {
  614. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  615. ->orderBy('created_at', 'desc');
  616. }
  617. public function activeSupplyOrders()
  618. {
  619. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  620. ->where('is_cancelled', false)
  621. ->orderBy('created_at', 'desc');
  622. }
  623. public function cancelledSupplyOrders()
  624. {
  625. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  626. ->where('is_cancelled', true)
  627. ->orderBy('created_at', 'desc');
  628. }
  629. public function readyToShipSupplyOrders()
  630. {
  631. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  632. ->where('is_cancelled', false)
  633. ->where('is_cleared_for_shipment', true)
  634. ->whereNull('shipment_id')
  635. ->orderBy('created_at', 'desc');
  636. }
  637. public function shipments()
  638. {
  639. return $this->hasMany(Shipment::class, 'client_id', 'id')
  640. ->where('is_cancelled', false)
  641. ->orderBy('created_at', 'desc');
  642. }
  643. public function numSignedNotes() {
  644. return Note::where('client_id', $this->id)
  645. ->where('is_cancelled', false)
  646. ->where('is_signed_by_hcp', true)
  647. ->count();
  648. }
  649. public function smsReminders()
  650. {
  651. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  652. ->orderBy('created_at', 'asc');
  653. }
  654. public function measurementConfirmationNumbers()
  655. {
  656. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  657. ->orderBy('created_at', 'asc');
  658. }
  659. public function shadowOfPro() {
  660. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  661. }
  662. public function clientTags()
  663. {
  664. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  665. ->where('is_cancelled', false)
  666. ->orderBy('tag', 'asc');
  667. }
  668. public function medicalTeam()
  669. {
  670. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  671. ->where('is_active', true)
  672. ->whereNotNull('pro_id')
  673. ->orderBy('created_at', 'asc');
  674. }
  675. public function accountInvites()
  676. {
  677. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  678. ->orderBy('created_at', 'desc');
  679. }
  680. public function linkedAccounts()
  681. {
  682. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  683. ->orderBy('created_at', 'desc');
  684. }
  685. public function pages($_type, $_returnShadows, $_note) {
  686. return Page
  687. ::where('client_id', $this->id)
  688. ->where('note_id', $_note->id)
  689. ->where('category', $_type)
  690. ->where('is_shadow', !!$_returnShadows)
  691. ->orderBy('key', 'ASC')
  692. ->get();
  693. }
  694. public function firstPageByCategoryAndKey($_category, $_key) {
  695. return Page
  696. ::where('client_id', $this->id)
  697. ->where('category', $_category)
  698. ->where('key', $_key)
  699. ->first();
  700. }
  701. public function cmReasons()
  702. {
  703. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  704. ->where('cm_or_rm', 'CM')
  705. ->where('is_removed', false)
  706. ->orderBy('position_index', 'ASC')
  707. ->orderBy('code', 'ASC');
  708. }
  709. public function rmReasons()
  710. {
  711. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  712. ->where('cm_or_rm', 'RM')
  713. ->where('is_removed', false)
  714. ->orderBy('position_index', 'ASC')
  715. ->orderBy('code', 'ASC');
  716. }
  717. public function cmSetupNote()
  718. {
  719. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  720. }
  721. public function rmSetupCareMonth()
  722. {
  723. return $this->hasOne(CareMonth::class, 'id', 'rm_setup_care_month_id');
  724. }
  725. public function defaultMcpCompanyPro()
  726. {
  727. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  728. }
  729. public function defaultMcpCompanyProPayer()
  730. {
  731. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  732. }
  733. public function defaultMcpCompanyLocation()
  734. {
  735. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  736. }
  737. public function hasNewNoteForPro($_pro) {
  738. $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();
  739. return !!$count;
  740. }
  741. public function systemSourcePro()
  742. {
  743. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  744. }
  745. public function systemSourceProTeam()
  746. {
  747. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  748. }
  749. public function adminEngagementAssessmentStatus(){
  750. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  751. }
  752. public function mcpEngagementAssessmentStatus(){
  753. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  754. }
  755. public function defaultNaEngagementAssessmentStatus(){
  756. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  757. }
  758. public function clientSelfSatisfactionStatus(){
  759. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  760. }
  761. public function recentNotes($_pro = null) {
  762. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  763. if($_pro) {
  764. $notes = $notes->where('hcp_pro_id', $_pro->id);
  765. }
  766. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  767. return $notes;
  768. }
  769. public function cmMeasurementsMatrix($_careMonth, $pro = null) {
  770. $days = [];
  771. $matches = DB::select(
  772. "
  773. SELECT m.id AS measurement_id,
  774. m.uid AS measurement_uid,
  775. cm.id AS care_month_id,
  776. cm.uid AS care_month_uid,
  777. m.label,
  778. m.value,
  779. m.dbp_mm_hg,
  780. m.sbp_mm_hg,
  781. m.value_pulse,
  782. m.value_irregular,
  783. m.numeric_value,
  784. m.effective_date,
  785. m.ts,
  786. m.has_been_stamped_by_mcp,
  787. m.has_been_stamped_by_rmm,
  788. m.has_been_stamped_by_non_hcp
  789. FROM measurement m
  790. JOIN care_month cm ON m.care_month_id = cm.id
  791. WHERE m.care_month_id = :careMonthID
  792. AND m.label NOT IN ('SBP', 'DBP')
  793. AND m.bdt_measurement_id IS NOT NULL
  794. AND m.is_active IS TRUE
  795. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  796. AND m.ts IS NOT NULL
  797. AND m.client_bdt_measurement_id IS NOT NULL
  798. ORDER BY m.ts DESC
  799. ",
  800. ['careMonthID' => $_careMonth->id]
  801. );
  802. foreach ($matches as $match) {
  803. $time = (floor($match->ts / 1000));
  804. $realTimezone = resolve_timezone('EASTERN');
  805. $date = new \DateTime("@$time");
  806. $date->setTimezone(new \DateTimeZone($realTimezone));
  807. $match->date = $date->format("m/d/Y");
  808. $match->dateYMD = $date->format("Y-m-d");
  809. $match->time = $date->format("h:i A");
  810. // get existing entries for listing
  811. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  812. ->where('is_removed', false)
  813. ->where('effective_date', $match->dateYMD);
  814. if(!!$pro) {
  815. $match->entries = $match->entries->where('pro_id', $pro->id);
  816. }
  817. $match->entries = $match->entries->orderBy('created_at')->get();
  818. if(!isset($days[$match->date])) {
  819. $days[$match->date] = [];
  820. }
  821. $days[$match->date][] = $match;
  822. }
  823. return $days;
  824. }
  825. public function getPrimaryCoverage()
  826. {
  827. return $this->effectiveClientPrimaryCoverage;
  828. }
  829. // return value will be YES, NO or UNKNOWN
  830. public function getPrimaryCoverageStatus() {
  831. $coverage = $this->getPrimaryCoverage();
  832. if(!$coverage) return 'NO';
  833. return $coverage->getStatus();
  834. }
  835. public function getMcpAssignedOn() {
  836. $change = ClientProChange::where('client_id', $this->id)
  837. ->where('new_pro_id', $this->mcp_pro_id)
  838. ->where('responsibility_type', 'MCP')
  839. ->orderBy('created_at', 'DESC')
  840. ->first();
  841. if(!!$change) {
  842. return friendlier_date($change->created_at);
  843. }
  844. return '-';
  845. }
  846. public function coreNote(){
  847. return $this->hasOne(Note::class, 'id', 'core_note_id');
  848. }
  849. public function nonCoreVisitNotes() {
  850. return $this->hasMany(Note::class, 'client_id', 'id')
  851. ->where('id', '<>', $this->core_note_id)
  852. ->whereNotNull('visit_template_id')
  853. ->orderBy('created_at', 'desc');
  854. }
  855. public function clientProChanges() {
  856. return $this->hasMany(ClientProChange::class, 'client_id', 'id')
  857. ->orderBy('created_at', 'desc');
  858. }
  859. public function mostRecentWeightMeasurement(){
  860. return $this->hasOne(Measurement::class, 'id', 'most_recent_weight_measurement_id');
  861. }
  862. public function hasBPDevice() {
  863. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  864. foreach ($cbds as $cbd) {
  865. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'BP') return true;
  866. }
  867. return false;
  868. }
  869. public function hasWeightScaleDevice() {
  870. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  871. foreach ($cbds as $cbd) {
  872. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'WEIGHT') return true;
  873. }
  874. return false;
  875. }
  876. public function clientEngagementStatus(){
  877. return $this->hasOne(Status::class, 'id', 'client_engagement_status_id');
  878. }
  879. public function clientBpWeightPhoneNumberStatus(){
  880. return $this->hasOne(ClientBpWeightPhoneNumberStatus::class, 'id', 'client_bp_weight_phone_number_status_id');
  881. }
  882. public function getDeviceDeliveryStatus($productId){
  883. $result = DB::select("SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id = ".$productId." AND so.client_id = ".$this->id." ORDER BY sh.created_at DESC LIMIT 1");
  884. if (count($result)){
  885. return $result[0]->status;
  886. }
  887. return '';
  888. }
  889. public function hasDataInCanvas($_type) {
  890. $page = Page::where('client_id', $this->id)->where('category', 'CANVAS')->where('key', $_type)->first();
  891. $contentData = [];
  892. if($page){
  893. $contentData = json_decode($page->data, true);
  894. }else{
  895. if($this->canvas_data) {
  896. $canvasData = json_decode($this->canvas_data, true);
  897. if(isset($canvasData[$_type])) {
  898. $contentData = $canvasData[$_type];
  899. if($_type !== 'vitals') {
  900. if(!isset($contentData['items'])){
  901. $contentData['items'] = [];
  902. }
  903. }
  904. }
  905. }
  906. }
  907. if($_type !== 'vitals') {
  908. if (isset($contentData['items']) && count($contentData['items'])) {
  909. return true;
  910. }
  911. }
  912. else {
  913. foreach ($contentData as $cd) {
  914. if(isset($cd['value']) && !empty($cd['value'])) return true;
  915. }
  916. }
  917. return false;
  918. }
  919. // 4 Infra-red Temperature gun
  920. public function temparatureGunDeliveryStatus(){
  921. return $this->getDeviceDeliveryStatus(4);
  922. }
  923. // 3 Pulse Oximeter (
  924. public function pulseOximeterDeliveryStatus(){
  925. return $this->getDeviceDeliveryStatus(3);
  926. }
  927. // 1 Cellular BP - Standard Arm Cuff (if delivered, then it should show its status as Delivered, Dispatched, In-transit, Returned)
  928. public function cellularBPDeliveryStatus(){
  929. return $this->getDeviceDeliveryStatus(1);
  930. }
  931. // 2 Weight scale
  932. public function weightScaleDeliveryStatus(){
  933. return $this->getDeviceDeliveryStatus(2);
  934. }
  935. public function carePlanFlaggedBy(){
  936. return $this->hasOne(Pro::class, 'id', 'flagged_by_pro_id');
  937. }
  938. public function carePlanFlagAcknowledgedBy(){
  939. return $this->hasOne(Pro::class, 'id', 'flag_acknowledged_by_pro_id');
  940. }
  941. public function clientRepFollowUp(){
  942. return $this->hasOne(ClientRepFollowUp::class, 'id', 'client_rep_follow_up_id');
  943. }
  944. public function customers() {
  945. return $this->hasMany(Customer::class, 'client_id', 'id')
  946. ->orderBy('created_at', 'desc');
  947. }
  948. }