Client.php 32 KB

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