Client.php 26 KB

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