Client.php 26 KB

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