Client.php 31 KB

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