Client.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 firstCellularBPDevice()
  296. {
  297. $devices = $this->devices();
  298. $x = null;
  299. foreach($devices as $device){
  300. if($device->device->category == 'BP'){
  301. $x = $device;
  302. continue;
  303. }
  304. }
  305. return $x;
  306. }
  307. public function getFirstCellularBPMeasurementAt()
  308. {
  309. }
  310. public function getLatestCellularBPMeasurementAt()
  311. {
  312. }
  313. public function getTotalCellularBPMeasurements()
  314. {
  315. }
  316. public function firstCellularWeightDevice()
  317. {
  318. }
  319. public function getFirstCellularWeightMeasurementAt()
  320. {
  321. }
  322. public function getLatestCellularWeightMeasurementAt()
  323. {
  324. }
  325. public function getTotalCellularWeightMeasurements()
  326. {
  327. }
  328. public function prosWithAccess()
  329. {
  330. $pros = [];
  331. // directly associated pros
  332. $pro = $this->mcp;
  333. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  334. $pro = $this->pcp;
  335. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  336. $pro = $this->cm;
  337. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  338. $pro = $this->rmm;
  339. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  340. $pro = $this->rme;
  341. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  342. $pro = $this->defaultNaPro;
  343. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'Default NA'];
  344. // via client pro access
  345. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  346. foreach ($cpAccesses as $cpAccess) {
  347. if (!$cpAccess->pro) continue;
  348. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  349. }
  350. // via appointments
  351. $appointments = Appointment::where('client_id', $this->id)->get();
  352. foreach ($appointments as $appointment) {
  353. if (!$appointment->pro) continue;
  354. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  355. }
  356. // via client program
  357. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  358. foreach ($clientPrograms as $clientProgram) {
  359. if ($clientProgram->mcp)
  360. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  361. if ($clientProgram->manager)
  362. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  363. }
  364. // sort by pro name
  365. $name = array_column($pros, 'pro');
  366. array_multisort($name, SORT_ASC, $pros);
  367. return $pros;
  368. }
  369. public function mcpRequests()
  370. {
  371. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  372. ->orderBy('created_at', 'desc');
  373. }
  374. public function eligibleRefreshes()
  375. {
  376. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  377. ->orderBy('created_at', 'desc');
  378. }
  379. public function mbPayerValidationResults()
  380. {
  381. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  382. ->orderBy('created_at', 'desc');
  383. }
  384. public function payer()
  385. {
  386. return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
  387. }
  388. public function supplyOrders()
  389. {
  390. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  391. ->orderBy('created_at', 'desc');
  392. }
  393. public function activeSupplyOrders()
  394. {
  395. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  396. ->where('is_cancelled', false)
  397. ->orderBy('created_at', 'desc');
  398. }
  399. public function cancelledSupplyOrders()
  400. {
  401. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  402. ->where('is_cancelled', true)
  403. ->orderBy('created_at', 'desc');
  404. }
  405. public function readyToShipSupplyOrders()
  406. {
  407. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  408. ->where('is_cancelled', false)
  409. ->where('is_cleared_for_shipment', true)
  410. ->whereNull('shipment_id')
  411. ->orderBy('created_at', 'desc');
  412. }
  413. public function shipments()
  414. {
  415. return $this->hasMany(Shipment::class, 'client_id', 'id')
  416. ->where('is_cancelled', false)
  417. ->orderBy('created_at', 'desc');
  418. }
  419. public function numSignedNotes() {
  420. return Note::where('client_id', $this->id)
  421. ->where('is_cancelled', false)
  422. ->where('is_signed_by_hcp', true)
  423. ->count();
  424. }
  425. public function smsReminders()
  426. {
  427. return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
  428. ->orderBy('created_at', 'asc');
  429. }
  430. public function measurementConfirmationNumbers()
  431. {
  432. return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
  433. ->orderBy('created_at', 'asc');
  434. }
  435. public function shadowOfPro() {
  436. return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
  437. }
  438. public function clientTags()
  439. {
  440. return $this->hasMany(ClientTag::class, 'client_id', 'id')
  441. ->where('is_cancelled', false)
  442. ->orderBy('tag', 'asc');
  443. }
  444. public function medicalTeam()
  445. {
  446. return $this->hasMany(ClientProAccess::class, 'client_id', 'id')
  447. ->where('is_active', true)
  448. ->whereNotNull('pro_id')
  449. ->orderBy('created_at', 'asc');
  450. }
  451. public function accountInvites()
  452. {
  453. return $this->hasMany(AccountInvite::class, 'for_client_id', 'id')
  454. ->orderBy('created_at', 'desc');
  455. }
  456. public function linkedAccounts()
  457. {
  458. return $this->hasMany(AccountClient::class, 'client_id', 'id')
  459. ->orderBy('created_at', 'desc');
  460. }
  461. public function pages($_type, $_returnShadows, $_note) {
  462. return Page
  463. ::where('client_id', $this->id)
  464. ->where('note_id', $_note->id)
  465. ->where('category', $_type)
  466. ->where('is_shadow', !!$_returnShadows)
  467. ->orderBy('key', 'ASC')
  468. ->get();
  469. }
  470. public function firstPageByCategoryAndKey($_category, $_key) {
  471. return Page
  472. ::where('client_id', $this->id)
  473. ->where('category', $_category)
  474. ->where('key', $_key)
  475. ->first();
  476. }
  477. }