Client.php 32 KB

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