Client.php 34 KB

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