Client.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 pages($_type, $_returnShadows, $_note) {
  707. return Page
  708. ::where('client_id', $this->id)
  709. ->where('note_id', $_note->id)
  710. ->where('category', $_type)
  711. ->where('is_shadow', !!$_returnShadows)
  712. ->orderBy('key', 'ASC')
  713. ->get();
  714. }
  715. public function firstPageByCategoryAndKey($_category, $_key) {
  716. return Page
  717. ::where('client_id', $this->id)
  718. ->where('category', $_category)
  719. ->where('key', $_key)
  720. ->first();
  721. }
  722. public function cmReasons()
  723. {
  724. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  725. ->where('cm_or_rm', 'CM')
  726. ->where('is_removed', false)
  727. ->orderBy('position_index', 'ASC')
  728. ->orderBy('code', 'ASC');
  729. }
  730. public function rmReasons()
  731. {
  732. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  733. ->where('cm_or_rm', 'RM')
  734. ->where('is_removed', false)
  735. ->orderBy('position_index', 'ASC')
  736. ->orderBy('code', 'ASC');
  737. }
  738. public function cmSetupNote()
  739. {
  740. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  741. }
  742. public function rmSetupCareMonth()
  743. {
  744. return $this->hasOne(CareMonth::class, 'id', 'rm_setup_care_month_id');
  745. }
  746. public function defaultMcpCompanyPro()
  747. {
  748. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  749. }
  750. public function defaultMcpCompanyProPayer()
  751. {
  752. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  753. }
  754. public function defaultMcpCompanyLocation()
  755. {
  756. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  757. }
  758. public function hasNewNoteForPro($_pro) {
  759. $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();
  760. return !!$count;
  761. }
  762. public function systemSourcePro()
  763. {
  764. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  765. }
  766. public function systemSourceProTeam()
  767. {
  768. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  769. }
  770. public function adminEngagementAssessmentStatus(){
  771. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  772. }
  773. public function mcpEngagementAssessmentStatus(){
  774. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  775. }
  776. public function defaultNaEngagementAssessmentStatus(){
  777. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  778. }
  779. public function clientSelfSatisfactionStatus(){
  780. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  781. }
  782. public function recentNotes($_pro = null) {
  783. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  784. if($_pro) {
  785. $notes = $notes->where('hcp_pro_id', $_pro->id);
  786. }
  787. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  788. return $notes;
  789. }
  790. public function cmMeasurementsMatrix($_careMonth, $pro = null) {
  791. $days = [];
  792. $matches = DB::select(
  793. "
  794. SELECT m.id AS measurement_id,
  795. m.uid AS measurement_uid,
  796. cm.id AS care_month_id,
  797. cm.uid AS care_month_uid,
  798. m.label,
  799. m.value,
  800. m.dbp_mm_hg,
  801. m.sbp_mm_hg,
  802. m.value_pulse,
  803. m.value_irregular,
  804. m.numeric_value,
  805. m.effective_date,
  806. m.ts,
  807. m.has_been_stamped_by_mcp,
  808. m.has_been_stamped_by_rmm,
  809. m.has_been_stamped_by_non_hcp
  810. FROM measurement m
  811. JOIN care_month cm ON m.care_month_id = cm.id
  812. WHERE m.care_month_id = :careMonthID
  813. AND m.label NOT IN ('SBP', 'DBP')
  814. AND m.bdt_measurement_id IS NOT NULL
  815. AND m.is_active IS TRUE
  816. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  817. AND m.ts IS NOT NULL
  818. AND m.client_bdt_measurement_id IS NOT NULL
  819. ORDER BY m.ts DESC
  820. ",
  821. ['careMonthID' => $_careMonth->id]
  822. );
  823. foreach ($matches as $match) {
  824. $time = (floor($match->ts / 1000));
  825. $realTimezone = resolve_timezone('EASTERN');
  826. $date = new \DateTime("@$time");
  827. $date->setTimezone(new \DateTimeZone($realTimezone));
  828. $match->date = $date->format("m/d/Y");
  829. $match->dateYMD = $date->format("Y-m-d");
  830. $match->time = $date->format("h:i A");
  831. // get existing entries for listing
  832. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  833. ->where('is_removed', false)
  834. ->where('effective_date', $match->dateYMD);
  835. if(!!$pro) {
  836. $match->entries = $match->entries->where('pro_id', $pro->id);
  837. }
  838. $match->entries = $match->entries->orderBy('created_at')->get();
  839. if(!isset($days[$match->date])) {
  840. $days[$match->date] = [];
  841. }
  842. $days[$match->date][] = $match;
  843. }
  844. return $days;
  845. }
  846. public function getPrimaryCoverage()
  847. {
  848. return $this->effectiveClientPrimaryCoverage;
  849. }
  850. // return value will be YES, NO or UNKNOWN
  851. public function getPrimaryCoverageStatus() {
  852. $coverage = $this->getPrimaryCoverage();
  853. if(!$coverage) return 'NO';
  854. return $coverage->getStatus();
  855. }
  856. public function getMcpAssignedOn() {
  857. $change = ClientProChange::where('client_id', $this->id)
  858. ->where('new_pro_id', $this->mcp_pro_id)
  859. ->where('responsibility_type', 'MCP')
  860. ->orderBy('created_at', 'DESC')
  861. ->first();
  862. if(!!$change) {
  863. return friendlier_date($change->created_at);
  864. }
  865. return '-';
  866. }
  867. public function coreNote(){
  868. return $this->hasOne(Note::class, 'id', 'core_note_id');
  869. }
  870. public function nonCoreVisitNotes() {
  871. return $this->hasMany(Note::class, 'client_id', 'id')
  872. ->where('id', '<>', $this->core_note_id)
  873. ->whereNotNull('visit_template_id')
  874. ->orderBy('created_at', 'desc');
  875. }
  876. public function clientProChanges() {
  877. return $this->hasMany(ClientProChange::class, 'client_id', 'id')
  878. ->orderBy('created_at', 'desc');
  879. }
  880. public function mostRecentWeightMeasurement(){
  881. return $this->hasOne(Measurement::class, 'id', 'most_recent_weight_measurement_id');
  882. }
  883. public function hasBPDevice() {
  884. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  885. foreach ($cbds as $cbd) {
  886. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'BP') return true;
  887. }
  888. return false;
  889. }
  890. public function hasWeightScaleDevice() {
  891. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  892. foreach ($cbds as $cbd) {
  893. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'WEIGHT') return true;
  894. }
  895. return false;
  896. }
  897. public function clientEngagementStatus(){
  898. return $this->hasOne(Status::class, 'id', 'client_engagement_status_id');
  899. }
  900. public function clientBpWeightPhoneNumberStatus(){
  901. return $this->hasOne(ClientBpWeightPhoneNumberStatus::class, 'id', 'client_bp_weight_phone_number_status_id');
  902. }
  903. public function getDeviceDeliveryStatus($productId){
  904. $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");
  905. if (count($result)){
  906. return $result[0]->status;
  907. }
  908. return '';
  909. }
  910. public function hasDataInCanvas($_type) {
  911. $page = Page::where('client_id', $this->id)->where('category', 'CANVAS')->where('key', $_type)->first();
  912. $contentData = [];
  913. if($page){
  914. $contentData = json_decode($page->data, true);
  915. }else{
  916. if($this->canvas_data) {
  917. $canvasData = json_decode($this->canvas_data, true);
  918. if(isset($canvasData[$_type])) {
  919. $contentData = $canvasData[$_type];
  920. if($_type !== 'vitals') {
  921. if(!isset($contentData['items'])){
  922. $contentData['items'] = [];
  923. }
  924. }
  925. }
  926. }
  927. }
  928. if($_type !== 'vitals') {
  929. if (isset($contentData['items']) && count($contentData['items'])) {
  930. return true;
  931. }
  932. }
  933. else {
  934. foreach ($contentData as $cd) {
  935. if(isset($cd['value']) && !empty($cd['value'])) return true;
  936. }
  937. }
  938. return false;
  939. }
  940. // 4 Infra-red Temperature gun
  941. public function temparatureGunDeliveryStatus(){
  942. return $this->getDeviceDeliveryStatus(4);
  943. }
  944. // 3 Pulse Oximeter (
  945. public function pulseOximeterDeliveryStatus(){
  946. return $this->getDeviceDeliveryStatus(3);
  947. }
  948. // 1 Cellular BP - Standard Arm Cuff (if delivered, then it should show its status as Delivered, Dispatched, In-transit, Returned)
  949. public function cellularBPDeliveryStatus(){
  950. return $this->getDeviceDeliveryStatus(1);
  951. }
  952. // 2 Weight scale
  953. public function weightScaleDeliveryStatus(){
  954. return $this->getDeviceDeliveryStatus(2);
  955. }
  956. public function carePlanFlaggedBy(){
  957. return $this->hasOne(Pro::class, 'id', 'flagged_by_pro_id');
  958. }
  959. public function carePlanFlagAcknowledgedBy(){
  960. return $this->hasOne(Pro::class, 'id', 'flag_acknowledged_by_pro_id');
  961. }
  962. public function clientRepFollowUp(){
  963. return $this->hasOne(ClientRepFollowUp::class, 'id', 'client_rep_follow_up_id');
  964. }
  965. public function customers() {
  966. return $this->hasMany(Customer::class, 'client_id', 'id')
  967. ->orderBy('created_at', 'desc');
  968. }
  969. public function lastMemo() {
  970. return ClientMemo::where('client_id', $this->id)->orderBy('created_at', 'DESC')->first();
  971. }
  972. }