Client.php 35 KB

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