Client.php 33 KB

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