Client.php 36 KB

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