Client.php 32 KB

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