Client.php 18 KB

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