Client.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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 rmCompany() {
  425. return $this->hasOne(Company::class, 'id', 'rm_company_id');
  426. }
  427. public function upcomingAppointments()
  428. {
  429. return $this->hasMany(Appointment::class, 'client_id', 'id')
  430. ->where('raw_date', '>=', date('Y-m-d'))
  431. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  432. ->orderBy('start_time', 'desc')
  433. ->limit(5);
  434. }
  435. public function nextAppointment() {
  436. return Appointment
  437. ::where('client_id', $this->id)
  438. ->where('raw_date', '>=', DB::raw('NOW()'))
  439. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  440. ->orderBy('start_time')
  441. ->first();
  442. }
  443. public function appointmentsFromLastWeek()
  444. {
  445. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  446. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  447. return $this->hasMany(Appointment::class, 'client_id', 'id')
  448. ->where('raw_date', '>=', $dateLastWeek)
  449. ->orderBy('start_time', 'desc');
  450. }
  451. public function memos()
  452. {
  453. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  454. ->where('is_cancelled', false)
  455. ->orderBy('created_at', 'desc');
  456. }
  457. public function latestMemo()
  458. {
  459. return ClientMemo::where('client_id', $this->id)
  460. ->where('is_cancelled', false)
  461. ->orderBy('created_at', 'desc')
  462. ->first();
  463. }
  464. public function devices()
  465. {
  466. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  467. ->where('is_active', true)
  468. ->orderBy('created_at', 'desc');
  469. }
  470. public function deactivatedDevices()
  471. {
  472. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  473. ->where('is_active', false)
  474. ->orderBy('created_at', 'desc');
  475. }
  476. public function hasDevice($_device)
  477. {
  478. $count = ClientBDTDevice::where('client_id', $this->id)
  479. ->where('device_id', $_device->id)
  480. ->where('is_active', true)
  481. ->count();
  482. return !!$count;
  483. }
  484. public function deviceMeasurements()
  485. {
  486. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  487. ->orderBy('created_at', 'desc');
  488. }
  489. public function activeMcpRequest()
  490. {
  491. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  492. }
  493. public function clientPrograms()
  494. {
  495. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  496. ->where('is_active', true)
  497. ->orderBy('title', 'desc');
  498. }
  499. public function tickets()
  500. {
  501. return $this->hasMany(Ticket::class, 'client_id', 'id')
  502. ->orderBy('is_open', 'desc')
  503. ->orderBy('created_at', 'desc');
  504. }
  505. public function mcpDisplayName()
  506. {
  507. }
  508. public function rmeDisplayName()
  509. {
  510. }
  511. public function supplyOrderForCellularBPDevice() {
  512. return SupplyOrder::where('product_id', 1)
  513. ->where('is_cancelled', false)
  514. ->where('client_id', $this->id)
  515. ->orderBy('id', 'desc')
  516. ->first();
  517. }
  518. public function supplyOrderForCellularWeightScale() {
  519. return SupplyOrder::where('product_id', 2)
  520. ->where('is_cancelled', false)
  521. ->where('client_id', $this->id)
  522. ->orderBy('id', 'desc')
  523. ->first();
  524. }
  525. public function firstCellularBPDevice()
  526. {
  527. $devices = $this->devices;
  528. $x = null;
  529. foreach($devices as $device){
  530. if($device->device->category == 'BP'){
  531. $x = $device;
  532. break;
  533. }
  534. }
  535. return $x;
  536. }
  537. public function getFirstCellularBPMeasurementAt()
  538. {
  539. }
  540. public function getLatestCellularBPMeasurementAt()
  541. {
  542. }
  543. public function getTotalCellularBPMeasurements()
  544. {
  545. }
  546. public function firstCellularWeightDevice()
  547. {
  548. $devices = $this->devices;
  549. $x = null;
  550. foreach($devices as $device){
  551. if($device->device->category == 'WEIGHT'){
  552. $x = $device;
  553. break;
  554. }
  555. }
  556. return $x;
  557. }
  558. public function getFirstCellularWeightMeasurementAt()
  559. {
  560. }
  561. public function getLatestCellularWeightMeasurementAt()
  562. {
  563. }
  564. public function getTotalCellularWeightMeasurements()
  565. {
  566. }
  567. public function prosWithAccess()
  568. {
  569. $pros = [];
  570. // directly associated pros
  571. $pro = $this->mcp;
  572. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  573. $pro = $this->pcp;
  574. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  575. $pro = $this->cm;
  576. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  577. $pro = $this->rmm;
  578. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  579. $pro = $this->rme;
  580. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  581. $pro = $this->defaultNaPro;
  582. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Care Coordinator'];
  583. // via client pro access
  584. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  585. foreach ($cpAccesses as $cpAccess) {
  586. if (!$cpAccess->pro) continue;
  587. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => $cpAccess->reason_category. ' - Via Client Pro Access', 'isClientProAccess'=>true, 'clientProAccess'=>$cpAccess];
  588. }
  589. // via appointments
  590. $appointments = Appointment::where('client_id', $this->id)->get();
  591. foreach ($appointments as $appointment) {
  592. if (!$appointment->pro) continue;
  593. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  594. }
  595. // via client program
  596. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  597. foreach ($clientPrograms as $clientProgram) {
  598. if ($clientProgram->mcp)
  599. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  600. if ($clientProgram->manager)
  601. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  602. }
  603. // sort by pro name
  604. $name = array_column($pros, 'pro');
  605. array_multisort($name, SORT_ASC, $pros);
  606. return $pros;
  607. }
  608. public function mcpRequests()
  609. {
  610. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  611. ->orderBy('created_at', 'desc');
  612. }
  613. public function eligibleRefreshes()
  614. {
  615. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  616. ->orderBy('created_at', 'desc');
  617. }
  618. public function mbPayerValidationResults()
  619. {
  620. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  621. ->orderBy('created_at', 'desc');
  622. }
  623. public function payer()
  624. {
  625. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  626. }
  627. public function supplyOrders()
  628. {
  629. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  630. ->orderBy('created_at', 'desc');
  631. }
  632. public function activeSupplyOrders()
  633. {
  634. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  635. ->where('is_cancelled', false)
  636. ->orderBy('created_at', 'desc');
  637. }
  638. public function cancelledSupplyOrders()
  639. {
  640. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  641. ->where('is_cancelled', true)
  642. ->orderBy('created_at', 'desc');
  643. }
  644. public function readyToShipSupplyOrders()
  645. {
  646. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  647. ->where('is_cancelled', false)
  648. ->where('is_cleared_for_shipment', true)
  649. ->whereNull('shipment_id')
  650. ->orderBy('created_at', 'desc');
  651. }
  652. public function shipments()
  653. {
  654. return $this->hasMany(Shipment::class, 'client_id', 'id')
  655. ->where('is_cancelled', false)
  656. ->orderBy('created_at', 'desc');
  657. }
  658. public function numSignedNotes() {
  659. return Note::where('client_id', $this->id)
  660. ->where('is_cancelled', false)
  661. ->where('is_signed_by_hcp', true)
  662. ->count();
  663. }
  664. public function smsReminders()
  665. {
  666. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  667. ->orderBy('created_at', 'asc');
  668. }
  669. public function measurementConfirmationNumbers()
  670. {
  671. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  672. ->orderBy('created_at', 'asc');
  673. }
  674. public function shadowOfPro() {
  675. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  676. }
  677. public function clientTags()
  678. {
  679. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  680. ->where('is_cancelled', false)
  681. ->orderBy('tag', 'asc');
  682. }
  683. public function medicalTeam()
  684. {
  685. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  686. ->where('is_active', true)
  687. ->whereNotNull('pro_id')
  688. ->orderBy('created_at', 'asc');
  689. }
  690. public function accountInvites()
  691. {
  692. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  693. ->orderBy('created_at', 'desc');
  694. }
  695. public function linkedAccounts()
  696. {
  697. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  698. ->orderBy('created_at', 'desc');
  699. }
  700. public function pages($_type, $_returnShadows, $_note) {
  701. return Page
  702. ::where('client_id', $this->id)
  703. ->where('note_id', $_note->id)
  704. ->where('category', $_type)
  705. ->where('is_shadow', !!$_returnShadows)
  706. ->orderBy('key', 'ASC')
  707. ->get();
  708. }
  709. public function firstPageByCategoryAndKey($_category, $_key) {
  710. return Page
  711. ::where('client_id', $this->id)
  712. ->where('category', $_category)
  713. ->where('key', $_key)
  714. ->first();
  715. }
  716. public function cmReasons()
  717. {
  718. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  719. ->where('cm_or_rm', 'CM')
  720. ->where('is_removed', false)
  721. ->orderBy('position_index', 'ASC')
  722. ->orderBy('code', 'ASC');
  723. }
  724. public function rmReasons()
  725. {
  726. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  727. ->where('cm_or_rm', 'RM')
  728. ->where('is_removed', false)
  729. ->orderBy('position_index', 'ASC')
  730. ->orderBy('code', 'ASC');
  731. }
  732. public function cmSetupNote()
  733. {
  734. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  735. }
  736. public function rmSetupCareMonth()
  737. {
  738. return $this->hasOne(CareMonth::class, 'id', 'rm_setup_care_month_id');
  739. }
  740. public function defaultMcpCompanyPro()
  741. {
  742. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  743. }
  744. public function defaultMcpCompanyProPayer()
  745. {
  746. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  747. }
  748. public function defaultMcpCompanyLocation()
  749. {
  750. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  751. }
  752. public function hasNewNoteForPro($_pro) {
  753. $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();
  754. return !!$count;
  755. }
  756. public function systemSourcePro()
  757. {
  758. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  759. }
  760. public function systemSourceProTeam()
  761. {
  762. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  763. }
  764. public function adminEngagementAssessmentStatus(){
  765. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  766. }
  767. public function mcpEngagementAssessmentStatus(){
  768. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  769. }
  770. public function defaultNaEngagementAssessmentStatus(){
  771. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  772. }
  773. public function clientSelfSatisfactionStatus(){
  774. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  775. }
  776. public function recentNotes($_pro = null) {
  777. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  778. if($_pro) {
  779. $notes = $notes->where('hcp_pro_id', $_pro->id);
  780. }
  781. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  782. return $notes;
  783. }
  784. public function cmMeasurementsMatrix($_careMonth, $pro = null) {
  785. $days = [];
  786. $matches = DB::select(
  787. "
  788. SELECT m.id AS measurement_id,
  789. m.uid AS measurement_uid,
  790. cm.id AS care_month_id,
  791. cm.uid AS care_month_uid,
  792. m.label,
  793. m.value,
  794. m.dbp_mm_hg,
  795. m.sbp_mm_hg,
  796. m.value_pulse,
  797. m.value_irregular,
  798. m.numeric_value,
  799. m.effective_date,
  800. m.ts,
  801. m.has_been_stamped_by_mcp,
  802. m.has_been_stamped_by_rmm,
  803. m.has_been_stamped_by_non_hcp
  804. FROM measurement m
  805. JOIN care_month cm ON m.care_month_id = cm.id
  806. WHERE m.care_month_id = :careMonthID
  807. AND m.label NOT IN ('SBP', 'DBP')
  808. AND m.bdt_measurement_id IS NOT NULL
  809. AND m.is_active IS TRUE
  810. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  811. AND m.ts IS NOT NULL
  812. AND m.client_bdt_measurement_id IS NOT NULL
  813. ORDER BY m.ts DESC
  814. ",
  815. ['careMonthID' => $_careMonth->id]
  816. );
  817. foreach ($matches as $match) {
  818. $time = (floor($match->ts / 1000));
  819. $realTimezone = resolve_timezone('EASTERN');
  820. $date = new \DateTime("@$time");
  821. $date->setTimezone(new \DateTimeZone($realTimezone));
  822. $match->date = $date->format("m/d/Y");
  823. $match->dateYMD = $date->format("Y-m-d");
  824. $match->time = $date->format("h:i A");
  825. // get existing entries for listing
  826. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  827. ->where('is_removed', false)
  828. ->where('effective_date', $match->dateYMD);
  829. if(!!$pro) {
  830. $match->entries = $match->entries->where('pro_id', $pro->id);
  831. }
  832. $match->entries = $match->entries->orderBy('created_at')->get();
  833. if(!isset($days[$match->date])) {
  834. $days[$match->date] = [];
  835. }
  836. $days[$match->date][] = $match;
  837. }
  838. return $days;
  839. }
  840. public function getPrimaryCoverage()
  841. {
  842. return $this->effectiveClientPrimaryCoverage;
  843. }
  844. // return value will be YES, NO or UNKNOWN
  845. public function getPrimaryCoverageStatus() {
  846. $coverage = $this->getPrimaryCoverage();
  847. if(!$coverage) return 'NO';
  848. return $coverage->getStatus();
  849. }
  850. public function getMcpAssignedOn() {
  851. $change = ClientProChange::where('client_id', $this->id)
  852. ->where('new_pro_id', $this->mcp_pro_id)
  853. ->where('responsibility_type', 'MCP')
  854. ->orderBy('created_at', 'DESC')
  855. ->first();
  856. if(!!$change) {
  857. return friendlier_date($change->created_at);
  858. }
  859. return '-';
  860. }
  861. public function coreNote(){
  862. return $this->hasOne(Note::class, 'id', 'core_note_id');
  863. }
  864. public function nonCoreVisitNotes() {
  865. return $this->hasMany(Note::class, 'client_id', 'id')
  866. ->where('id', '<>', $this->core_note_id)
  867. ->whereNotNull('visit_template_id')
  868. ->orderBy('created_at', 'desc');
  869. }
  870. public function clientProChanges() {
  871. return $this->hasMany(ClientProChange::class, 'client_id', 'id')
  872. ->orderBy('created_at', 'desc');
  873. }
  874. public function mostRecentWeightMeasurement(){
  875. return $this->hasOne(Measurement::class, 'id', 'most_recent_weight_measurement_id');
  876. }
  877. public function hasBPDevice() {
  878. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  879. foreach ($cbds as $cbd) {
  880. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'BP') return true;
  881. }
  882. return false;
  883. }
  884. public function hasWeightScaleDevice() {
  885. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  886. foreach ($cbds as $cbd) {
  887. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'WEIGHT') return true;
  888. }
  889. return false;
  890. }
  891. public function clientEngagementStatus(){
  892. return $this->hasOne(Status::class, 'id', 'client_engagement_status_id');
  893. }
  894. public function clientBpWeightPhoneNumberStatus(){
  895. return $this->hasOne(ClientBpWeightPhoneNumberStatus::class, 'id', 'client_bp_weight_phone_number_status_id');
  896. }
  897. public function getDeviceDeliveryStatus($productId){
  898. $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");
  899. if (count($result)){
  900. return $result[0]->status;
  901. }
  902. return '';
  903. }
  904. public function hasDataInCanvas($_type) {
  905. $page = Page::where('client_id', $this->id)->where('category', 'CANVAS')->where('key', $_type)->first();
  906. $contentData = [];
  907. if($page){
  908. $contentData = json_decode($page->data, true);
  909. }else{
  910. if($this->canvas_data) {
  911. $canvasData = json_decode($this->canvas_data, true);
  912. if(isset($canvasData[$_type])) {
  913. $contentData = $canvasData[$_type];
  914. if($_type !== 'vitals') {
  915. if(!isset($contentData['items'])){
  916. $contentData['items'] = [];
  917. }
  918. }
  919. }
  920. }
  921. }
  922. if($_type !== 'vitals') {
  923. if (isset($contentData['items']) && count($contentData['items'])) {
  924. return true;
  925. }
  926. }
  927. else {
  928. foreach ($contentData as $cd) {
  929. if(isset($cd['value']) && !empty($cd['value'])) return true;
  930. }
  931. }
  932. return false;
  933. }
  934. // 4 Infra-red Temperature gun
  935. public function temparatureGunDeliveryStatus(){
  936. return $this->getDeviceDeliveryStatus(4);
  937. }
  938. // 3 Pulse Oximeter (
  939. public function pulseOximeterDeliveryStatus(){
  940. return $this->getDeviceDeliveryStatus(3);
  941. }
  942. // 1 Cellular BP - Standard Arm Cuff (if delivered, then it should show its status as Delivered, Dispatched, In-transit, Returned)
  943. public function cellularBPDeliveryStatus(){
  944. return $this->getDeviceDeliveryStatus(1);
  945. }
  946. // 2 Weight scale
  947. public function weightScaleDeliveryStatus(){
  948. return $this->getDeviceDeliveryStatus(2);
  949. }
  950. public function carePlanFlaggedBy(){
  951. return $this->hasOne(Pro::class, 'id', 'flagged_by_pro_id');
  952. }
  953. public function carePlanFlagAcknowledgedBy(){
  954. return $this->hasOne(Pro::class, 'id', 'flag_acknowledged_by_pro_id');
  955. }
  956. public function clientRepFollowUp(){
  957. return $this->hasOne(ClientRepFollowUp::class, 'id', 'client_rep_follow_up_id');
  958. }
  959. public function customers() {
  960. return $this->hasMany(Customer::class, 'client_id', 'id')
  961. ->orderBy('created_at', 'desc');
  962. }
  963. public function lastMemo() {
  964. return ClientMemo::where('client_id', $this->id)->orderBy('created_at', 'DESC')->first();
  965. }
  966. public function dentalCompany()
  967. {
  968. return $this->hasOne(Company::class, 'id', 'dental_company_id');
  969. }
  970. }