Client.php 32 KB

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