Client.php 32 KB

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