Client.php 22 KB

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