Client.php 34 KB

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