Client.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 displayName()
  11. {
  12. return $this->name_last . ', ' . $this->name_first;
  13. }
  14. public function mcp()
  15. {
  16. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  17. }
  18. public function pcp()
  19. {
  20. return $this->hasOne(Pro::class, 'id', 'physician_pro_id');
  21. }
  22. public function cm()
  23. {
  24. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  25. }
  26. public function rmm()
  27. {
  28. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  29. }
  30. public function rme()
  31. {
  32. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  33. }
  34. public function rms()
  35. {
  36. return $this->hasOne(Pro::class, 'id', 'rms_pro_id');
  37. }
  38. public function rmg()
  39. {
  40. return $this->hasOne(Pro::class, 'id', 'rmg_pro_id');
  41. }
  42. public function defaultNaPro()
  43. {
  44. return $this->hasOne(Pro::class, 'id', 'default_na_pro_id');
  45. }
  46. public function prosInMeetingWith()
  47. {
  48. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  49. }
  50. public function notes()
  51. {
  52. return $this->hasMany(Note::class, 'client_id', 'id')
  53. ->orderBy('effective_dateest', 'desc');
  54. }
  55. public function notesAscending()
  56. {
  57. return $this->hasMany(Note::class, 'client_id', 'id')
  58. ->orderBy('effective_dateest', 'asc');
  59. }
  60. public function mcCodeChecks(){
  61. // $tables = DB::select("SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'");
  62. // foreach ($tables as $table) {
  63. // dump($table);
  64. // }
  65. // die();
  66. return $this->hasMany(McCodeCheck::class, 'client_id', 'id')->orderBy('created_at', 'asc');
  67. }
  68. public function activeNotes()
  69. {
  70. return $this->hasMany(Note::class, 'client_id', 'id')
  71. ->where('is_cancelled', false)
  72. ->orderBy('effective_dateest', 'desc');
  73. }
  74. public function cancelledNotes()
  75. {
  76. return $this->hasMany(Note::class, 'client_id', 'id')
  77. ->where('is_cancelled', true)
  78. ->orderBy('effective_dateest', 'desc');
  79. }
  80. public function sections()
  81. {
  82. return $this->hasMany(Section::class, 'client_id', 'id')
  83. ->where('is_active', true)
  84. ->orderBy('created_at', 'asc');
  85. }
  86. public function handouts()
  87. {
  88. $mappings = HandoutClient::where('client_id', $this->id)->get();
  89. $handouts = new Collection();
  90. foreach ($mappings as $mapping) {
  91. $handout = Handout::where('id', $mapping->handout_id)->first();
  92. $handout->handout_client_uid = $mapping->uid;
  93. $handouts->add($handout);
  94. }
  95. $handouts = $handouts->sortBy('created_at');
  96. return $handouts;
  97. }
  98. public function duplicateOf()
  99. {
  100. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  101. }
  102. public function actionItems()
  103. {
  104. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  105. ->orderBy('action_item_category', 'asc')
  106. ->orderBy('created_at', 'desc');
  107. }
  108. public function infoLines()
  109. {
  110. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  111. }
  112. public function measurements()
  113. {
  114. return $this->hasMany(Measurement::class, 'client_id', 'id')
  115. /*->distinct('label')*/
  116. ->where('is_removed', false)
  117. ->orderByRaw('ts DESC NULLS LAST');
  118. }
  119. public function recentMeasurements()
  120. {
  121. return $this->hasMany(Measurement::class, 'client_id', 'id')
  122. ->where('is_removed', false)
  123. ->whereNotNull('label')
  124. ->where('label', '<>', 'SBP')
  125. ->where('label', '<>', 'DBP')
  126. ->where('is_cellular_zero', false)
  127. ->orderByRaw('ts DESC NULLS LAST')
  128. ->offset(0)->limit(20);
  129. }
  130. public function nonZeroMeasurements()
  131. {
  132. return $this->hasMany(Measurement::class, 'client_id', 'id')
  133. /*->distinct('label')*/
  134. ->where('is_removed', false)
  135. ->where('is_cellular_zero', false)
  136. ->orderBy('effective_date', 'desc');
  137. }
  138. public function getNonZeroBpMeasurements(){
  139. return $this->hasMany(Measurement::class, 'client_id', 'id')
  140. /*->distinct('label')*/
  141. ->where('is_removed', false)
  142. ->where('label', '=', 'BP')
  143. ->where('sbp_mm_hg', '>', 0)
  144. ->where('dbp_mm_hg', '>', 0)
  145. ->orderBy('ts', 'desc');
  146. }
  147. public function getNonZeroWeightMeasurements(){
  148. return $this->hasMany(Measurement::class, 'client_id', 'id')
  149. /*->distinct('label')*/
  150. ->where('is_removed', false)
  151. ->where('label', '=', 'Wt. (lbs.)')
  152. ->where('numeric_value', '>', 0)
  153. ->orderBy('ts', 'desc');
  154. }
  155. public function currentCareMonth()
  156. {
  157. $cmStartDate = strtotime(date('Y-m-d'));
  158. $month = date("n", $cmStartDate);
  159. $year = date("Y", $cmStartDate);
  160. return CareMonth
  161. ::where('client_id', $this->id)
  162. ->whereRaw('EXTRACT(MONTH FROM start_date) = ?', [$month])
  163. ->whereRaw('EXTRACT(YEAR FROM start_date) = ?', [$year])
  164. ->first();
  165. }
  166. public function measurementsInCareMonth(CareMonth $careMonth)
  167. {
  168. $cmStartDate = strtotime($careMonth->start_date);
  169. $month = date("n", $cmStartDate);
  170. $year = date("Y", $cmStartDate);
  171. $measurements = Measurement
  172. ::where('client_id', $this->id)
  173. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  174. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  175. ->where('is_removed', false)
  176. ->orderBy('ts', 'desc')
  177. ->get();
  178. return $measurements;
  179. }
  180. public function allMeasurements()
  181. {
  182. return $this->hasMany(Measurement::class, 'client_id', 'id')
  183. ->where('is_removed', false)
  184. ->whereNull('parent_measurement_id')
  185. ->orderBy('label', 'asc')
  186. ->orderBy('effective_date', 'desc');
  187. }
  188. public function smses()
  189. {
  190. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  191. ->orderBy('created_at', 'desc');
  192. }
  193. public function documents()
  194. {
  195. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  196. ->orderBy('created_at', 'desc');
  197. }
  198. public function incomingReports() {
  199. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  200. ->orderBy('created_at', 'desc');
  201. }
  202. public function smsNumbers() {
  203. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  204. ->orderBy('created_at', 'desc');
  205. }
  206. public function nextMcpAppointment()
  207. {
  208. if ($this->mcp) {
  209. return Appointment::where('client_id', $this->id)
  210. ->where('pro_id', $this->mcp->id)
  211. ->where('start_time', '>=', date('Y-m-d'))
  212. ->orderBy('start_time', 'asc')
  213. ->first();
  214. }
  215. return false;
  216. }
  217. public function appointments()
  218. {
  219. return $this->hasMany(Appointment::class, 'client_id', 'id')
  220. ->orderBy('start_time', 'desc');
  221. }
  222. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  223. {
  224. $appointments = Appointment::where('client_id', $this->id);
  225. if($forPro !== 'all') {
  226. $forPro = Pro::where('uid', $forPro)->first();
  227. $appointments = $appointments->where('pro_id', $forPro->id);
  228. }
  229. if($status !== 'ALL') {
  230. $appointments = $appointments->where('status', $status);
  231. }
  232. $appointments = $appointments->orderBy('start_time', 'desc');
  233. return $appointments->get();
  234. }
  235. public function upcomingAppointments()
  236. {
  237. return $this->hasMany(Appointment::class, 'client_id', 'id')
  238. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  239. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  240. ->orderBy('start_time', 'desc');
  241. }
  242. public function appointmentsFromLastWeek()
  243. {
  244. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
  245. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  246. return $this->hasMany(Appointment::class, 'client_id', 'id')
  247. ->where('raw_date', '>=', $dateLastWeek)
  248. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  249. ->orderBy('start_time', 'desc');
  250. }
  251. public function memos()
  252. {
  253. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  254. ->where('is_cancelled', false)
  255. ->orderBy('created_at', 'desc');
  256. }
  257. public function devices()
  258. {
  259. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  260. ->where('is_active', true)
  261. ->orderBy('created_at', 'desc');
  262. }
  263. public function deactivatedDevices()
  264. {
  265. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  266. ->where('is_active', false)
  267. ->orderBy('created_at', 'desc');
  268. }
  269. public function hasDevice($_device)
  270. {
  271. $count = ClientBDTDevice::where('client_id', $this->id)
  272. ->where('device_id', $_device->id)
  273. ->where('is_active', true)
  274. ->count();
  275. return !!$count;
  276. }
  277. public function deviceMeasurements()
  278. {
  279. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  280. ->orderBy('created_at', 'desc');
  281. }
  282. public function activeMcpRequest()
  283. {
  284. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  285. }
  286. public function clientPrograms()
  287. {
  288. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  289. ->where('is_active', true)
  290. ->orderBy('title', 'desc');
  291. }
  292. public function tickets()
  293. {
  294. return $this->hasMany(Ticket::class, 'client_id', 'id')
  295. ->orderBy('is_open', 'desc')
  296. ->orderBy('created_at', 'desc');
  297. }
  298. public function mcpDisplayName()
  299. {
  300. }
  301. public function rmeDisplayName()
  302. {
  303. }
  304. public function supplyOrderForCellularBPDevice() {
  305. return SupplyOrder::where('product_id', 1)
  306. ->where('is_cancelled', false)
  307. ->where('client_id', $this->id)
  308. ->orderBy('id', 'desc')
  309. ->first();
  310. }
  311. public function supplyOrderForCellularWeightScale() {
  312. return SupplyOrder::where('product_id', 2)
  313. ->where('is_cancelled', false)
  314. ->where('client_id', $this->id)
  315. ->orderBy('id', 'desc')
  316. ->first();
  317. }
  318. public function firstCellularBPDevice()
  319. {
  320. $devices = $this->devices;
  321. $x = null;
  322. foreach($devices as $device){
  323. if($device->device->category == 'BP'){
  324. $x = $device;
  325. break;
  326. }
  327. }
  328. return $x;
  329. }
  330. public function getFirstCellularBPMeasurementAt()
  331. {
  332. }
  333. public function getLatestCellularBPMeasurementAt()
  334. {
  335. }
  336. public function getTotalCellularBPMeasurements()
  337. {
  338. }
  339. public function firstCellularWeightDevice()
  340. {
  341. $devices = $this->devices;
  342. $x = null;
  343. foreach($devices as $device){
  344. if($device->device->category == 'WEIGHT'){
  345. $x = $device;
  346. break;
  347. }
  348. }
  349. return $x;
  350. }
  351. public function getFirstCellularWeightMeasurementAt()
  352. {
  353. }
  354. public function getLatestCellularWeightMeasurementAt()
  355. {
  356. }
  357. public function getTotalCellularWeightMeasurements()
  358. {
  359. }
  360. public function prosWithAccess()
  361. {
  362. $pros = [];
  363. // directly associated pros
  364. $pro = $this->mcp;
  365. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  366. $pro = $this->pcp;
  367. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  368. $pro = $this->cm;
  369. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  370. $pro = $this->rmm;
  371. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  372. $pro = $this->rme;
  373. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  374. $pro = $this->defaultNaPro;
  375. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Default NA'];
  376. // via client pro access
  377. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  378. foreach ($cpAccesses as $cpAccess) {
  379. if (!$cpAccess->pro) continue;
  380. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  381. }
  382. // via appointments
  383. $appointments = Appointment::where('client_id', $this->id)->get();
  384. foreach ($appointments as $appointment) {
  385. if (!$appointment->pro) continue;
  386. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  387. }
  388. // via client program
  389. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  390. foreach ($clientPrograms as $clientProgram) {
  391. if ($clientProgram->mcp)
  392. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  393. if ($clientProgram->manager)
  394. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  395. }
  396. // sort by pro name
  397. $name = array_column($pros, 'pro');
  398. array_multisort($name, SORT_ASC, $pros);
  399. return $pros;
  400. }
  401. public function mcpRequests()
  402. {
  403. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  404. ->orderBy('created_at', 'desc');
  405. }
  406. public function eligibleRefreshes()
  407. {
  408. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  409. ->orderBy('created_at', 'desc');
  410. }
  411. public function mbPayerValidationResults()
  412. {
  413. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  414. ->orderBy('created_at', 'desc');
  415. }
  416. public function payer()
  417. {
  418. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  419. }
  420. public function supplyOrders()
  421. {
  422. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  423. ->orderBy('created_at', 'desc');
  424. }
  425. public function activeSupplyOrders()
  426. {
  427. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  428. ->where('is_cancelled', false)
  429. ->orderBy('created_at', 'desc');
  430. }
  431. public function cancelledSupplyOrders()
  432. {
  433. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  434. ->where('is_cancelled', true)
  435. ->orderBy('created_at', 'desc');
  436. }
  437. public function readyToShipSupplyOrders()
  438. {
  439. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  440. ->where('is_cancelled', false)
  441. ->where('is_cleared_for_shipment', true)
  442. ->whereNull('shipment_id')
  443. ->orderBy('created_at', 'desc');
  444. }
  445. public function shipments()
  446. {
  447. return $this->hasMany(Shipment::class, 'client_id', 'id')
  448. ->where('is_cancelled', false)
  449. ->orderBy('created_at', 'desc');
  450. }
  451. public function numSignedNotes() {
  452. return Note::where('client_id', $this->id)
  453. ->where('is_cancelled', false)
  454. ->where('is_signed_by_hcp', true)
  455. ->count();
  456. }
  457. public function smsReminders()
  458. {
  459. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  460. ->orderBy('created_at', 'asc');
  461. }
  462. public function measurementConfirmationNumbers()
  463. {
  464. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  465. ->orderBy('created_at', 'asc');
  466. }
  467. public function shadowOfPro() {
  468. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  469. }
  470. public function clientTags()
  471. {
  472. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  473. ->where('is_cancelled', false)
  474. ->orderBy('tag', 'asc');
  475. }
  476. public function medicalTeam()
  477. {
  478. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  479. ->where('is_active', true)
  480. ->whereNotNull('pro_id')
  481. ->orderBy('created_at', 'asc');
  482. }
  483. public function accountInvites()
  484. {
  485. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  486. ->orderBy('created_at', 'desc');
  487. }
  488. public function linkedAccounts()
  489. {
  490. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  491. ->orderBy('created_at', 'desc');
  492. }
  493. public function pages($_type, $_returnShadows, $_note) {
  494. return Page
  495. ::where('client_id', $this->id)
  496. ->where('note_id', $_note->id)
  497. ->where('category', $_type)
  498. ->where('is_shadow', !!$_returnShadows)
  499. ->orderBy('key', 'ASC')
  500. ->get();
  501. }
  502. public function firstPageByCategoryAndKey($_category, $_key) {
  503. return Page
  504. ::where('client_id', $this->id)
  505. ->where('category', $_category)
  506. ->where('key', $_key)
  507. ->first();
  508. }
  509. public function cmReasons()
  510. {
  511. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  512. ->where('cm_or_rm', 'CM')
  513. ->orderBy('position_index', 'ASC')
  514. ->orderBy('code', 'ASC');
  515. }
  516. public function rmReasons()
  517. {
  518. return $this->hasMany(ClientCmRmReason::class, 'client_id', 'id')
  519. ->where('cm_or_rm', 'RM')
  520. ->orderBy('position_index', 'ASC')
  521. ->orderBy('code', 'ASC');
  522. }
  523. public function defaultMcpCompanyPro()
  524. {
  525. return $this->hasOne(CompanyPro::class, 'id', 'default_mcp_company_pro_id');
  526. }
  527. public function defaultMcpCompanyProPayer()
  528. {
  529. return $this->hasOne(CompanyProPayer::class, 'id', 'default_mcp_company_pro_payer_id');
  530. }
  531. public function defaultMcpCompanyLocation()
  532. {
  533. return $this->hasOne(CompanyLocation::class, 'id', 'default_mcp_company_location_id');
  534. }
  535. }