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 lastMeasurementOfType($_type) {
  337. return Measurement::where('client_id', $this->id)
  338. ->whereNotNull('bdt_measurement_id')
  339. ->whereNotNull('ts')
  340. ->where('is_cellular_zero', false)
  341. ->where('is_active', true)
  342. ->where('label', '=', $_type)
  343. ->orderBy('ts', 'desc')
  344. ->first();
  345. }
  346. public function appointments()
  347. {
  348. return $this->hasMany(Appointment::class, 'client_id', 'id')
  349. ->orderBy('start_time', 'desc');
  350. }
  351. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  352. {
  353. $appointments = Appointment::where('client_id', $this->id);
  354. if($forPro !== 'all') {
  355. $forPro = Pro::where('uid', $forPro)->first();
  356. $appointments = $appointments->where('pro_id', $forPro->id);
  357. }
  358. if($status !== 'ALL') {
  359. $appointments = $appointments->where('status', $status);
  360. }
  361. $appointments = $appointments->orderBy('raw_date', 'desc')->orderBy('raw_start_time', 'desc');
  362. return $appointments->get();
  363. }
  364. public function upcomingAppointments()
  365. {
  366. return $this->hasMany(Appointment::class, 'client_id', 'id')
  367. ->where('raw_date', '>=', date('Y-m-d'))
  368. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  369. ->orderBy('start_time', 'desc')
  370. ->limit(5);
  371. }
  372. public function nextAppointment() {
  373. return Appointment
  374. ::where('client_id', $this->id)
  375. ->where('raw_date', '>=', DB::raw('NOW()'))
  376. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  377. ->orderBy('start_time')
  378. ->first();
  379. }
  380. public function appointmentsFromLastWeek()
  381. {
  382. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  383. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  384. return $this->hasMany(Appointment::class, 'client_id', 'id')
  385. ->where('raw_date', '>=', $dateLastWeek)
  386. ->orderBy('start_time', 'desc');
  387. }
  388. public function memos()
  389. {
  390. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  391. ->where('is_cancelled', false)
  392. ->orderBy('created_at', 'desc');
  393. }
  394. public function latestMemo()
  395. {
  396. return ClientMemo::where('client_id', $this->id)
  397. ->where('is_cancelled', false)
  398. ->orderBy('created_at', 'desc')
  399. ->first();
  400. }
  401. public function devices()
  402. {
  403. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  404. ->where('is_active', true)
  405. ->orderBy('created_at', 'desc');
  406. }
  407. public function deactivatedDevices()
  408. {
  409. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  410. ->where('is_active', false)
  411. ->orderBy('created_at', 'desc');
  412. }
  413. public function hasDevice($_device)
  414. {
  415. $count = ClientBDTDevice::where('client_id', $this->id)
  416. ->where('device_id', $_device->id)
  417. ->where('is_active', true)
  418. ->count();
  419. return !!$count;
  420. }
  421. public function deviceMeasurements()
  422. {
  423. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  424. ->orderBy('created_at', 'desc');
  425. }
  426. public function activeMcpRequest()
  427. {
  428. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  429. }
  430. public function clientPrograms()
  431. {
  432. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  433. ->where('is_active', true)
  434. ->orderBy('title', 'desc');
  435. }
  436. public function tickets()
  437. {
  438. return $this->hasMany(Ticket::class, 'client_id', 'id')
  439. ->orderBy('is_open', 'desc')
  440. ->orderBy('created_at', 'desc');
  441. }
  442. public function mcpDisplayName()
  443. {
  444. }
  445. public function rmeDisplayName()
  446. {
  447. }
  448. public function supplyOrderForCellularBPDevice() {
  449. return SupplyOrder::where('product_id', 1)
  450. ->where('is_cancelled', false)
  451. ->where('client_id', $this->id)
  452. ->orderBy('id', 'desc')
  453. ->first();
  454. }
  455. public function supplyOrderForCellularWeightScale() {
  456. return SupplyOrder::where('product_id', 2)
  457. ->where('is_cancelled', false)
  458. ->where('client_id', $this->id)
  459. ->orderBy('id', 'desc')
  460. ->first();
  461. }
  462. public function firstCellularBPDevice()
  463. {
  464. $devices = $this->devices;
  465. $x = null;
  466. foreach($devices as $device){
  467. if($device->device->category == 'BP'){
  468. $x = $device;
  469. break;
  470. }
  471. }
  472. return $x;
  473. }
  474. public function getFirstCellularBPMeasurementAt()
  475. {
  476. }
  477. public function getLatestCellularBPMeasurementAt()
  478. {
  479. }
  480. public function getTotalCellularBPMeasurements()
  481. {
  482. }
  483. public function firstCellularWeightDevice()
  484. {
  485. $devices = $this->devices;
  486. $x = null;
  487. foreach($devices as $device){
  488. if($device->device->category == 'WEIGHT'){
  489. $x = $device;
  490. break;
  491. }
  492. }
  493. return $x;
  494. }
  495. public function getFirstCellularWeightMeasurementAt()
  496. {
  497. }
  498. public function getLatestCellularWeightMeasurementAt()
  499. {
  500. }
  501. public function getTotalCellularWeightMeasurements()
  502. {
  503. }
  504. public function prosWithAccess()
  505. {
  506. $pros = [];
  507. // directly associated pros
  508. $pro = $this->mcp;
  509. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  510. $pro = $this->pcp;
  511. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  512. $pro = $this->cm;
  513. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  514. $pro = $this->rmm;
  515. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  516. $pro = $this->rme;
  517. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  518. $pro = $this->defaultNaPro;
  519. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Care Coordinator'];
  520. // via client pro access
  521. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  522. foreach ($cpAccesses as $cpAccess) {
  523. if (!$cpAccess->pro) continue;
  524. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => $cpAccess->reason_category. ' - Via Client Pro Access', 'isClientProAccess'=>true, 'clientProAccess'=>$cpAccess];
  525. }
  526. // via appointments
  527. $appointments = Appointment::where('client_id', $this->id)->get();
  528. foreach ($appointments as $appointment) {
  529. if (!$appointment->pro) continue;
  530. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  531. }
  532. // via client program
  533. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  534. foreach ($clientPrograms as $clientProgram) {
  535. if ($clientProgram->mcp)
  536. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  537. if ($clientProgram->manager)
  538. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  539. }
  540. // sort by pro name
  541. $name = array_column($pros, 'pro');
  542. array_multisort($name, SORT_ASC, $pros);
  543. return $pros;
  544. }
  545. public function mcpRequests()
  546. {
  547. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  548. ->orderBy('created_at', 'desc');
  549. }
  550. public function eligibleRefreshes()
  551. {
  552. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  553. ->orderBy('created_at', 'desc');
  554. }
  555. public function mbPayerValidationResults()
  556. {
  557. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  558. ->orderBy('created_at', 'desc');
  559. }
  560. public function payer()
  561. {
  562. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  563. }
  564. public function supplyOrders()
  565. {
  566. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  567. ->orderBy('created_at', 'desc');
  568. }
  569. public function activeSupplyOrders()
  570. {
  571. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  572. ->where('is_cancelled', false)
  573. ->orderBy('created_at', 'desc');
  574. }
  575. public function cancelledSupplyOrders()
  576. {
  577. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  578. ->where('is_cancelled', true)
  579. ->orderBy('created_at', 'desc');
  580. }
  581. public function readyToShipSupplyOrders()
  582. {
  583. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  584. ->where('is_cancelled', false)
  585. ->where('is_cleared_for_shipment', true)
  586. ->whereNull('shipment_id')
  587. ->orderBy('created_at', 'desc');
  588. }
  589. public function shipments()
  590. {
  591. return $this->hasMany(Shipment::class, 'client_id', 'id')
  592. ->where('is_cancelled', false)
  593. ->orderBy('created_at', 'desc');
  594. }
  595. public function numSignedNotes() {
  596. return Note::where('client_id', $this->id)
  597. ->where('is_cancelled', false)
  598. ->where('is_signed_by_hcp', true)
  599. ->count();
  600. }
  601. public function smsReminders()
  602. {
  603. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  604. ->orderBy('created_at', 'asc');
  605. }
  606. public function measurementConfirmationNumbers()
  607. {
  608. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  609. ->orderBy('created_at', 'asc');
  610. }
  611. public function shadowOfPro() {
  612. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  613. }
  614. public function clientTags()
  615. {
  616. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  617. ->where('is_cancelled', false)
  618. ->orderBy('tag', 'asc');
  619. }
  620. public function medicalTeam()
  621. {
  622. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  623. ->where('is_active', true)
  624. ->whereNotNull('pro_id')
  625. ->orderBy('created_at', 'asc');
  626. }
  627. public function accountInvites()
  628. {
  629. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  630. ->orderBy('created_at', 'desc');
  631. }
  632. public function linkedAccounts()
  633. {
  634. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  635. ->orderBy('created_at', 'desc');
  636. }
  637. public function pages($_type, $_returnShadows, $_note) {
  638. return Page
  639. ::where('client_id', $this->id)
  640. ->where('note_id', $_note->id)
  641. ->where('category', $_type)
  642. ->where('is_shadow', !!$_returnShadows)
  643. ->orderBy('key', 'ASC')
  644. ->get();
  645. }
  646. public function firstPageByCategoryAndKey($_category, $_key) {
  647. return Page
  648. ::where('client_id', $this->id)
  649. ->where('category', $_category)
  650. ->where('key', $_key)
  651. ->first();
  652. }
  653. public function cmReasons()
  654. {
  655. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  656. ->where('cm_or_rm', 'CM')
  657. ->where('is_removed', false)
  658. ->orderBy('position_index', 'ASC')
  659. ->orderBy('code', 'ASC');
  660. }
  661. public function rmReasons()
  662. {
  663. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  664. ->where('cm_or_rm', 'RM')
  665. ->where('is_removed', false)
  666. ->orderBy('position_index', 'ASC')
  667. ->orderBy('code', 'ASC');
  668. }
  669. public function cmSetupNote()
  670. {
  671. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  672. }
  673. public function rmSetupCareMonth()
  674. {
  675. return $this->hasOne(CareMonth::class, 'id', 'rm_setup_care_month_id');
  676. }
  677. public function defaultMcpCompanyPro()
  678. {
  679. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  680. }
  681. public function defaultMcpCompanyProPayer()
  682. {
  683. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  684. }
  685. public function defaultMcpCompanyLocation()
  686. {
  687. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  688. }
  689. public function hasNewNoteForPro($_pro) {
  690. $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();
  691. return !!$count;
  692. }
  693. public function systemSourcePro()
  694. {
  695. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  696. }
  697. public function systemSourceProTeam()
  698. {
  699. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  700. }
  701. public function adminEngagementAssessmentStatus(){
  702. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  703. }
  704. public function mcpEngagementAssessmentStatus(){
  705. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  706. }
  707. public function defaultNaEngagementAssessmentStatus(){
  708. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  709. }
  710. public function clientSelfSatisfactionStatus(){
  711. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  712. }
  713. public function recentNotes($_pro = null) {
  714. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  715. if($_pro) {
  716. $notes = $notes->where('hcp_pro_id', $_pro->id);
  717. }
  718. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  719. return $notes;
  720. }
  721. public function cmMeasurementsMatrix($_careMonth, $pro = null) {
  722. $days = [];
  723. $matches = DB::select(
  724. "
  725. SELECT m.id AS measurement_id,
  726. m.uid AS measurement_uid,
  727. cm.id AS care_month_id,
  728. cm.uid AS care_month_uid,
  729. m.label,
  730. m.value,
  731. m.dbp_mm_hg,
  732. m.sbp_mm_hg,
  733. m.value_pulse,
  734. m.value_irregular,
  735. m.numeric_value,
  736. m.effective_date,
  737. m.ts,
  738. m.has_been_stamped_by_mcp,
  739. m.has_been_stamped_by_rmm,
  740. m.has_been_stamped_by_non_hcp
  741. FROM measurement m
  742. JOIN care_month cm ON m.care_month_id = cm.id
  743. WHERE m.care_month_id = :careMonthID
  744. AND m.label NOT IN ('SBP', 'DBP')
  745. AND m.bdt_measurement_id IS NOT NULL
  746. AND m.is_active IS TRUE
  747. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  748. AND m.ts IS NOT NULL
  749. AND m.client_bdt_measurement_id IS NOT NULL
  750. ORDER BY m.ts DESC
  751. ",
  752. ['careMonthID' => $_careMonth->id]
  753. );
  754. foreach ($matches as $match) {
  755. $time = (floor($match->ts / 1000));
  756. $realTimezone = resolve_timezone('EASTERN');
  757. $date = new \DateTime("@$time");
  758. $date->setTimezone(new \DateTimeZone($realTimezone));
  759. $match->date = $date->format("m/d/Y");
  760. $match->dateYMD = $date->format("Y-m-d");
  761. $match->time = $date->format("h:i A");
  762. // get existing entries for listing
  763. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  764. ->where('is_removed', false)
  765. ->where('effective_date', $match->dateYMD);
  766. if(!!$pro) {
  767. $match->entries = $match->entries->where('pro_id', $pro->id);
  768. }
  769. $match->entries = $match->entries->orderBy('created_at')->get();
  770. if(!isset($days[$match->date])) {
  771. $days[$match->date] = [];
  772. }
  773. $days[$match->date][] = $match;
  774. }
  775. return $days;
  776. }
  777. public function getPrimaryCoverage()
  778. {
  779. return $this->effectiveClientPrimaryCoverage;
  780. }
  781. // return value will be YES, NO or UNKNOWN
  782. public function getPrimaryCoverageStatus() {
  783. $coverage = $this->getPrimaryCoverage();
  784. if(!$coverage) return 'NO';
  785. return $coverage->getStatus();
  786. }
  787. public function getMcpAssignedOn() {
  788. $change = ClientProChange::where('client_id', $this->id)
  789. ->where('new_pro_id', $this->mcp_pro_id)
  790. ->where('responsibility_type', 'MCP')
  791. ->orderBy('created_at', 'DESC')
  792. ->first();
  793. if(!!$change) {
  794. return friendlier_date($change->created_at);
  795. }
  796. return '-';
  797. }
  798. public function coreNote(){
  799. return $this->hasOne(Note::class, 'id', 'core_note_id');
  800. }
  801. public function mostRecentCompletedMcpNote(){
  802. return $this->hasOne(Note::class, 'id', 'most_recent_completed_mcp_note_id');
  803. }
  804. public function nonCoreVisitNotes() {
  805. return $this->hasMany(Note::class, 'client_id', 'id')
  806. ->where('id', '<>', $this->core_note_id)
  807. ->whereNotNull('visit_template_id')
  808. ->orderBy('created_at', 'desc');
  809. }
  810. public function clientProChanges() {
  811. return $this->hasMany(ClientProChange::class, 'client_id', 'id')
  812. ->orderBy('created_at', 'desc');
  813. }
  814. public function mostRecentWeightMeasurement(){
  815. return $this->hasOne(Measurement::class, 'id', 'most_recent_weight_measurement_id');
  816. }
  817. public function hasBPDevice() {
  818. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  819. foreach ($cbds as $cbd) {
  820. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'BP') return true;
  821. }
  822. return false;
  823. }
  824. public function hasWeightScaleDevice() {
  825. $cbds = ClientBDTDevice::where('client_id', $this->id)->get();
  826. foreach ($cbds as $cbd) {
  827. if($cbd->is_active && !!$cbd->device && $cbd->device->is_active && $cbd->device->category === 'WEIGHT') return true;
  828. }
  829. return false;
  830. }
  831. public function clientEngagementStatus(){
  832. return $this->hasOne(Status::class, 'id', 'client_engagement_status_id');
  833. }
  834. public function clientBpWeightPhoneNumberStatus(){
  835. return $this->hasOne(ClientBpWeightPhoneNumberStatus::class, 'id', 'client_bp_weight_phone_number_status_id');
  836. }
  837. public function getDeviceDeliveryStatus($productId){
  838. $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");
  839. if (count($result)){
  840. return $result[0]->status;
  841. }
  842. return '';
  843. }
  844. public function hasDataInCanvas($_type) {
  845. $page = Page::where('client_id', $this->id)->where('category', 'CANVAS')->where('key', $_type)->first();
  846. $contentData = [];
  847. if($page){
  848. $contentData = json_decode($page->data, true);
  849. }else{
  850. if($this->canvas_data) {
  851. $canvasData = json_decode($this->canvas_data, true);
  852. if(isset($canvasData[$_type])) {
  853. $contentData = $canvasData[$_type];
  854. if($_type !== 'vitals') {
  855. if(!isset($contentData['items'])){
  856. $contentData['items'] = [];
  857. }
  858. }
  859. }
  860. }
  861. }
  862. if($_type !== 'vitals') {
  863. if (isset($contentData['items']) && count($contentData['items'])) {
  864. return true;
  865. }
  866. }
  867. else {
  868. foreach ($contentData as $cd) {
  869. if(isset($cd['value']) && !empty($cd['value'])) return true;
  870. }
  871. }
  872. return false;
  873. }
  874. // 4 Infra-red Temperature gun
  875. public function temparatureGunDeliveryStatus(){
  876. return $this->getDeviceDeliveryStatus(4);
  877. }
  878. // 3 Pulse Oximeter (
  879. public function pulseOximeterDeliveryStatus(){
  880. return $this->getDeviceDeliveryStatus(3);
  881. }
  882. // 1 Cellular BP - Standard Arm Cuff (if delivered, then it should show its status as Delivered, Dispatched, In-transit, Returned)
  883. public function cellularBPDeliveryStatus(){
  884. return $this->getDeviceDeliveryStatus(1);
  885. }
  886. // 2 Weight scale
  887. public function weightScaleDeliveryStatus(){
  888. return $this->getDeviceDeliveryStatus(2);
  889. }
  890. public function carePlanFlaggedBy(){
  891. return $this->hasOne(Pro::class, 'id', 'flagged_by_pro_id');
  892. }
  893. public function carePlanFlagAcknowledgedBy(){
  894. return $this->hasOne(Pro::class, 'id', 'flag_acknowledged_by_pro_id');
  895. }
  896. public function clientRepFollowUp(){
  897. return $this->hasOne(ClientRepFollowUp::class, 'id', 'client_rep_follow_up_id');
  898. }
  899. public function customers() {
  900. return $this->hasMany(Customer::class, 'client_id', 'id')
  901. ->orderBy('created_at', 'desc');
  902. }
  903. }