Client.php 35 KB

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