Client.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. return $this->hasOne(Appointment::class, 'id', 'next_mcp_appointment_id');
  257. }
  258. public function lastMcpAppointment()
  259. {
  260. return $this->hasOne(Appointment::class, 'id', 'previous_mcp_appointment_id');
  261. }
  262. public function lastMeasurementOfType($_type) {
  263. return Measurement::where('client_id', $this->id)
  264. ->whereNotNull('bdt_measurement_id')
  265. ->whereNotNull('ts')
  266. ->where('is_cellular_zero', false)
  267. ->where('is_removed', false)
  268. ->where('label', '=', $_type)
  269. ->orderBy('ts', 'desc')
  270. ->first();
  271. }
  272. public function appointments()
  273. {
  274. return $this->hasMany(Appointment::class, 'client_id', 'id')
  275. ->orderBy('start_time', 'desc');
  276. }
  277. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  278. {
  279. $appointments = Appointment::where('client_id', $this->id);
  280. if($forPro !== 'all') {
  281. $forPro = Pro::where('uid', $forPro)->first();
  282. $appointments = $appointments->where('pro_id', $forPro->id);
  283. }
  284. if($status !== 'ALL') {
  285. $appointments = $appointments->where('status', $status);
  286. }
  287. $appointments = $appointments->orderBy('start_time', 'desc');
  288. return $appointments->get();
  289. }
  290. public function upcomingAppointments()
  291. {
  292. return $this->hasMany(Appointment::class, 'client_id', 'id')
  293. ->where('raw_date', '>=', date('Y-m-d'))
  294. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  295. ->orderBy('start_time', 'desc')
  296. ->limit(5);
  297. }
  298. public function nextAppointment() {
  299. return Appointment
  300. ::where('client_id', $this->id)
  301. ->where('raw_date', '>=', DB::raw('NOW()'))
  302. ->whereIn('status', ['PENDING', 'CONFIRMED'])
  303. ->orderBy('start_time')
  304. ->first();
  305. }
  306. public function appointmentsFromLastWeek()
  307. {
  308. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  309. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  310. return $this->hasMany(Appointment::class, 'client_id', 'id')
  311. ->where('raw_date', '>=', $dateLastWeek)
  312. ->orderBy('start_time', 'desc');
  313. }
  314. public function memos()
  315. {
  316. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  317. ->where('is_cancelled', false)
  318. ->orderBy('created_at', 'desc');
  319. }
  320. public function devices()
  321. {
  322. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  323. ->where('is_active', true)
  324. ->orderBy('created_at', 'desc');
  325. }
  326. public function deactivatedDevices()
  327. {
  328. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  329. ->where('is_active', false)
  330. ->orderBy('created_at', 'desc');
  331. }
  332. public function hasDevice($_device)
  333. {
  334. $count = ClientBDTDevice::where('client_id', $this->id)
  335. ->where('device_id', $_device->id)
  336. ->where('is_active', true)
  337. ->count();
  338. return !!$count;
  339. }
  340. public function deviceMeasurements()
  341. {
  342. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  343. ->orderBy('created_at', 'desc');
  344. }
  345. public function activeMcpRequest()
  346. {
  347. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  348. }
  349. public function clientPrograms()
  350. {
  351. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  352. ->where('is_active', true)
  353. ->orderBy('title', 'desc');
  354. }
  355. public function tickets()
  356. {
  357. return $this->hasMany(Ticket::class, 'client_id', 'id')
  358. ->orderBy('is_open', 'desc')
  359. ->orderBy('created_at', 'desc');
  360. }
  361. public function mcpDisplayName()
  362. {
  363. }
  364. public function rmeDisplayName()
  365. {
  366. }
  367. public function supplyOrderForCellularBPDevice() {
  368. return SupplyOrder::where('product_id', 1)
  369. ->where('is_cancelled', false)
  370. ->where('client_id', $this->id)
  371. ->orderBy('id', 'desc')
  372. ->first();
  373. }
  374. public function supplyOrderForCellularWeightScale() {
  375. return SupplyOrder::where('product_id', 2)
  376. ->where('is_cancelled', false)
  377. ->where('client_id', $this->id)
  378. ->orderBy('id', 'desc')
  379. ->first();
  380. }
  381. public function firstCellularBPDevice()
  382. {
  383. $devices = $this->devices;
  384. $x = null;
  385. foreach($devices as $device){
  386. if($device->device->category == 'BP'){
  387. $x = $device;
  388. break;
  389. }
  390. }
  391. return $x;
  392. }
  393. public function getFirstCellularBPMeasurementAt()
  394. {
  395. }
  396. public function getLatestCellularBPMeasurementAt()
  397. {
  398. }
  399. public function getTotalCellularBPMeasurements()
  400. {
  401. }
  402. public function firstCellularWeightDevice()
  403. {
  404. $devices = $this->devices;
  405. $x = null;
  406. foreach($devices as $device){
  407. if($device->device->category == 'WEIGHT'){
  408. $x = $device;
  409. break;
  410. }
  411. }
  412. return $x;
  413. }
  414. public function getFirstCellularWeightMeasurementAt()
  415. {
  416. }
  417. public function getLatestCellularWeightMeasurementAt()
  418. {
  419. }
  420. public function getTotalCellularWeightMeasurements()
  421. {
  422. }
  423. public function prosWithAccess()
  424. {
  425. $pros = [];
  426. // directly associated pros
  427. $pro = $this->mcp;
  428. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  429. $pro = $this->pcp;
  430. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  431. $pro = $this->cm;
  432. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  433. $pro = $this->rmm;
  434. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  435. $pro = $this->rme;
  436. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  437. $pro = $this->defaultNaPro;
  438. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Nurse'];
  439. // via client pro access
  440. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  441. foreach ($cpAccesses as $cpAccess) {
  442. if (!$cpAccess->pro) continue;
  443. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  444. }
  445. // via appointments
  446. $appointments = Appointment::where('client_id', $this->id)->get();
  447. foreach ($appointments as $appointment) {
  448. if (!$appointment->pro) continue;
  449. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  450. }
  451. // via client program
  452. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  453. foreach ($clientPrograms as $clientProgram) {
  454. if ($clientProgram->mcp)
  455. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  456. if ($clientProgram->manager)
  457. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  458. }
  459. // sort by pro name
  460. $name = array_column($pros, 'pro');
  461. array_multisort($name, SORT_ASC, $pros);
  462. return $pros;
  463. }
  464. public function mcpRequests()
  465. {
  466. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  467. ->orderBy('created_at', 'desc');
  468. }
  469. public function eligibleRefreshes()
  470. {
  471. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  472. ->orderBy('created_at', 'desc');
  473. }
  474. public function mbPayerValidationResults()
  475. {
  476. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  477. ->orderBy('created_at', 'desc');
  478. }
  479. public function payer()
  480. {
  481. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  482. }
  483. public function supplyOrders()
  484. {
  485. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  486. ->orderBy('created_at', 'desc');
  487. }
  488. public function activeSupplyOrders()
  489. {
  490. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  491. ->where('is_cancelled', false)
  492. ->orderBy('created_at', 'desc');
  493. }
  494. public function cancelledSupplyOrders()
  495. {
  496. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  497. ->where('is_cancelled', true)
  498. ->orderBy('created_at', 'desc');
  499. }
  500. public function readyToShipSupplyOrders()
  501. {
  502. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  503. ->where('is_cancelled', false)
  504. ->where('is_cleared_for_shipment', true)
  505. ->whereNull('shipment_id')
  506. ->orderBy('created_at', 'desc');
  507. }
  508. public function shipments()
  509. {
  510. return $this->hasMany(Shipment::class, 'client_id', 'id')
  511. ->where('is_cancelled', false)
  512. ->orderBy('created_at', 'desc');
  513. }
  514. public function numSignedNotes() {
  515. return Note::where('client_id', $this->id)
  516. ->where('is_cancelled', false)
  517. ->where('is_signed_by_hcp', true)
  518. ->count();
  519. }
  520. public function smsReminders()
  521. {
  522. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  523. ->orderBy('created_at', 'asc');
  524. }
  525. public function measurementConfirmationNumbers()
  526. {
  527. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  528. ->orderBy('created_at', 'asc');
  529. }
  530. public function shadowOfPro() {
  531. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  532. }
  533. public function clientTags()
  534. {
  535. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  536. ->where('is_cancelled', false)
  537. ->orderBy('tag', 'asc');
  538. }
  539. public function medicalTeam()
  540. {
  541. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  542. ->where('is_active', true)
  543. ->whereNotNull('pro_id')
  544. ->orderBy('created_at', 'asc');
  545. }
  546. public function accountInvites()
  547. {
  548. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  549. ->orderBy('created_at', 'desc');
  550. }
  551. public function linkedAccounts()
  552. {
  553. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  554. ->orderBy('created_at', 'desc');
  555. }
  556. public function pages($_type, $_returnShadows, $_note) {
  557. return Page
  558. ::where('client_id', $this->id)
  559. ->where('note_id', $_note->id)
  560. ->where('category', $_type)
  561. ->where('is_shadow', !!$_returnShadows)
  562. ->orderBy('key', 'ASC')
  563. ->get();
  564. }
  565. public function firstPageByCategoryAndKey($_category, $_key) {
  566. return Page
  567. ::where('client_id', $this->id)
  568. ->where('category', $_category)
  569. ->where('key', $_key)
  570. ->first();
  571. }
  572. public function cmReasons()
  573. {
  574. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  575. ->where('cm_or_rm', 'CM')
  576. ->orderBy('position_index', 'ASC')
  577. ->orderBy('code', 'ASC');
  578. }
  579. public function rmReasons()
  580. {
  581. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  582. ->where('cm_or_rm', 'RM')
  583. ->orderBy('position_index', 'ASC')
  584. ->orderBy('code', 'ASC');
  585. }
  586. public function cmSetupNote()
  587. {
  588. return $this->hasOne(Note::class, 'id', 'cm_setup_note_id');
  589. }
  590. public function defaultMcpCompanyPro()
  591. {
  592. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  593. }
  594. public function defaultMcpCompanyProPayer()
  595. {
  596. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  597. }
  598. public function defaultMcpCompanyLocation()
  599. {
  600. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  601. }
  602. public function hasNewNoteForPro($_pro) {
  603. $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();
  604. return !!$count;
  605. }
  606. public function systemSourcePro()
  607. {
  608. return $this->hasOne(Pro::class, 'id', 'system_source_pro_id');
  609. }
  610. public function systemSourceProTeam()
  611. {
  612. return $this->hasOne(ProTeam::class, 'id', 'system_source_pro_team_id');
  613. }
  614. public function adminEngagementAssessmentStatus(){
  615. return $this->hasOne(Status::class, 'id', 'admin_engagement_assessment_status_id');
  616. }
  617. public function mcpEngagementAssessmentStatus(){
  618. return $this->hasOne(Status::class, 'id', 'mcp_engagement_assessment_status_id');
  619. }
  620. public function defaultNaEngagementAssessmentStatus(){
  621. return $this->hasOne(Status::class, 'id', 'default_na_engagement_assessment_status_id');
  622. }
  623. public function clientSelfSatisfactionStatus(){
  624. return $this->hasOne(Status::class, 'id', 'client_self_satisfaction_status_id');
  625. }
  626. public function recentNotes($_pro = null) {
  627. $notes = Note::where('client_id', $this->id)->where('is_cancelled', false);
  628. if($_pro) {
  629. $notes = $notes->where('hcp_pro_id', $_pro->id);
  630. }
  631. $notes = $notes->orderBy('effective_dateest', 'DESC')->limit(5)->get();
  632. return $notes;
  633. }
  634. public function cmMeasurementsMatrix($_careMonth = null) {
  635. $days = [];
  636. $matches = DB::select(
  637. "
  638. SELECT m.id AS measurement_id,
  639. m.uid AS measurement_uid,
  640. cm.id AS care_month_id,
  641. cm.uid AS care_month_uid,
  642. m.label,
  643. m.value,
  644. m.dbp_mm_hg,
  645. m.sbp_mm_hg,
  646. m.value_pulse,
  647. m.value_irregular,
  648. m.numeric_value,
  649. m.effective_date,
  650. m.ts,
  651. m.has_been_stamped_by_mcp,
  652. m.has_been_stamped_by_non_hcp
  653. FROM measurement m
  654. JOIN care_month cm ON m.care_month_id = cm.id
  655. WHERE m.care_month_id = :careMonthID
  656. AND m.label NOT IN ('SBP', 'DBP')
  657. AND m.bdt_measurement_id IS NOT NULL
  658. AND m.is_removed IS FALSE
  659. AND (m.is_cellular_zero = FALSE or m.is_cellular_zero IS NULL)
  660. AND m.ts IS NOT NULL
  661. AND m.client_bdt_measurement_id IS NOT NULL
  662. ORDER BY m.ts DESC
  663. ",
  664. ['careMonthID' => $_careMonth->id]
  665. );
  666. foreach ($matches as $match) {
  667. $time = (floor($match->ts / 1000));
  668. $realTimezone = resolve_timezone('EASTERN');
  669. $date = new \DateTime("@$time");
  670. $date->setTimezone(new \DateTimeZone($realTimezone));
  671. $match->date = $date->format("m/d/Y");
  672. $match->dateYMD = $date->format("Y-m-d");
  673. $match->time = $date->format("h:i A");
  674. // get existing entries for listing
  675. $match->entries = CareMonthEntry::where('care_month_id', $match->care_month_id)
  676. ->where('is_removed', false)
  677. ->where('effective_date', $match->dateYMD)
  678. ->orderBy('created_at')
  679. ->get();
  680. if(!isset($days[$match->date])) {
  681. $days[$match->date] = [];
  682. }
  683. $days[$match->date][] = $match;
  684. }
  685. return $days;
  686. }
  687. public function getPrimaryCoverage()
  688. {
  689. $coverage = $this->latestClientPrimaryCoverage;
  690. return $coverage;
  691. }
  692. // return value will be YES, NO or UNKNOWN
  693. public function getPrimaryCoverageStatus() {
  694. $coverage = $this->getPrimaryCoverage();
  695. if(!$coverage) return 'NO';
  696. return $coverage->getStatus();
  697. }
  698. public function getMcpAssignedOn() {
  699. $change = ClientProChange::where('client_id', $this->id)
  700. ->where('new_pro_id', $this->mcp_pro_id)
  701. ->where('responsibility_type', 'MCP')
  702. ->orderBy('created_at', 'DESC')
  703. ->first();
  704. if(!!$change) {
  705. return friendlier_date($change->created_at);
  706. }
  707. return '-';
  708. }
  709. public function coreNote(){
  710. return $this->hasOne(Note::class, 'id', 'core_note_id');
  711. }
  712. }