Client.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 prosInMeetingWith()
  42. {
  43. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  44. }
  45. public function notes()
  46. {
  47. return $this->hasMany(Note::class, 'client_id', 'id')
  48. ->orderBy('effective_dateest', 'desc');
  49. }
  50. public function activeNotes()
  51. {
  52. return $this->hasMany(Note::class, 'client_id', 'id')
  53. ->where('is_cancelled', false)
  54. ->orderBy('effective_dateest', 'desc');
  55. }
  56. public function cancelledNotes()
  57. {
  58. return $this->hasMany(Note::class, 'client_id', 'id')
  59. ->where('is_cancelled', true)
  60. ->orderBy('effective_dateest', 'desc');
  61. }
  62. public function sections()
  63. {
  64. return $this->hasMany(Section::class, 'client_id', 'id')
  65. ->where('is_active', true)
  66. ->orderBy('created_at', 'asc');
  67. }
  68. public function handouts()
  69. {
  70. $mappings = HandoutClient::where('client_id', $this->id)->get();
  71. $handouts = new Collection();
  72. foreach ($mappings as $mapping) {
  73. $handout = Handout::where('id', $mapping->handout_id)->first();
  74. $handout->handout_client_uid = $mapping->uid;
  75. $handouts->add($handout);
  76. }
  77. $handouts = $handouts->sortBy('created_at');
  78. return $handouts;
  79. }
  80. public function duplicateOf()
  81. {
  82. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  83. }
  84. public function actionItems()
  85. {
  86. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  87. ->orderBy('action_item_category', 'asc')
  88. ->orderBy('created_at', 'desc');
  89. }
  90. public function infoLines()
  91. {
  92. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  93. }
  94. public function measurements()
  95. {
  96. return $this->hasMany(Measurement::class, 'client_id', 'id')
  97. /*->distinct('label')*/
  98. ->where('is_removed', false)
  99. ->orderByRaw('ts DESC NULLS LAST');
  100. }
  101. public function nonZeroMeasurements()
  102. {
  103. return $this->hasMany(Measurement::class, 'client_id', 'id')
  104. /*->distinct('label')*/
  105. ->where('is_removed', false)
  106. ->where('is_cellular_zero', false)
  107. ->orderBy('effective_date', 'desc');
  108. }
  109. public function getNonZeroBpMeasurements(){
  110. return $this->hasMany(Measurement::class, 'client_id', 'id')
  111. /*->distinct('label')*/
  112. ->where('is_removed', false)
  113. ->where('label', '=', 'BP')
  114. ->where('sbp_mm_hg', '>', 0)
  115. ->where('dbp_mm_hg', '>', 0)
  116. ->orderBy('ts', 'desc');
  117. }
  118. public function getNonZeroWeightMeasurements(){
  119. return $this->hasMany(Measurement::class, 'client_id', 'id')
  120. /*->distinct('label')*/
  121. ->where('is_removed', false)
  122. ->where('label', '=', 'Wt. (lbs.)')
  123. ->where('numeric_value', '>', 0)
  124. ->orderBy('ts', 'desc');
  125. }
  126. public function currentCareMonth()
  127. {
  128. $cmStartDate = strtotime(date('Y-m-d'));
  129. $month = date("n", $cmStartDate);
  130. $year = date("Y", $cmStartDate);
  131. return CareMonth
  132. ::where('client_id', $this->id)
  133. ->whereRaw('EXTRACT(MONTH FROM start_date) = ?', [$month])
  134. ->whereRaw('EXTRACT(YEAR FROM start_date) = ?', [$year])
  135. ->first();
  136. }
  137. public function measurementsInCareMonth(CareMonth $careMonth)
  138. {
  139. $cmStartDate = strtotime($careMonth->start_date);
  140. $month = date("n", $cmStartDate);
  141. $year = date("Y", $cmStartDate);
  142. $measurements = Measurement
  143. ::where('client_id', $this->id)
  144. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  145. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  146. ->where('is_removed', false)
  147. ->orderBy('effective_date', 'desc')
  148. ->get();
  149. return $measurements;
  150. }
  151. public function allMeasurements()
  152. {
  153. return $this->hasMany(Measurement::class, 'client_id', 'id')
  154. ->where('is_removed', false)
  155. ->whereNull('parent_measurement_id')
  156. ->orderBy('label', 'asc')
  157. ->orderBy('effective_date', 'desc');
  158. }
  159. public function smses()
  160. {
  161. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  162. ->orderBy('created_at', 'desc');
  163. }
  164. public function documents()
  165. {
  166. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  167. ->orderBy('created_at', 'desc');
  168. }
  169. public function incomingReports() {
  170. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  171. ->orderBy('created_at', 'desc');
  172. }
  173. public function smsNumbers() {
  174. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  175. ->orderBy('created_at', 'desc');
  176. }
  177. public function nextMcpAppointment()
  178. {
  179. if ($this->mcp) {
  180. return Appointment::where('client_id', $this->id)
  181. ->where('pro_id', $this->mcp->id)
  182. ->where('start_time', '>=', date('Y-m-d'))
  183. ->orderBy('start_time', 'asc')
  184. ->first();
  185. }
  186. return false;
  187. }
  188. public function appointments()
  189. {
  190. return $this->hasMany(Appointment::class, 'client_id', 'id')
  191. ->orderBy('start_time', 'desc');
  192. }
  193. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  194. {
  195. $appointments = Appointment::where('client_id', $this->id);
  196. if($forPro !== 'all') {
  197. $forPro = Pro::where('uid', $forPro)->first();
  198. $appointments = $appointments->where('pro_id', $forPro->id);
  199. }
  200. if($status !== 'ALL') {
  201. $appointments = $appointments->where('status', $status);
  202. }
  203. $appointments = $appointments->orderBy('start_time', 'desc');
  204. return $appointments->get();
  205. }
  206. public function upcomingAppointments()
  207. {
  208. return $this->hasMany(Appointment::class, 'client_id', 'id')
  209. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  210. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  211. ->orderBy('start_time', 'desc');
  212. }
  213. public function appointmentsFromLastWeek()
  214. {
  215. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
  216. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  217. return $this->hasMany(Appointment::class, 'client_id', 'id')
  218. ->where('raw_date', '>=', $dateLastWeek)
  219. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  220. ->orderBy('start_time', 'desc');
  221. }
  222. public function memos()
  223. {
  224. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  225. ->where('is_cancelled', false)
  226. ->orderBy('created_at', 'desc');
  227. }
  228. public function devices()
  229. {
  230. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  231. ->where('is_active', true)
  232. ->orderBy('created_at', 'desc');
  233. }
  234. public function deactivatedDevices()
  235. {
  236. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  237. ->where('is_active', false)
  238. ->orderBy('created_at', 'desc');
  239. }
  240. public function hasDevice($_device)
  241. {
  242. $count = ClientBDTDevice::where('client_id', $this->id)
  243. ->where('device_id', $_device->id)
  244. ->where('is_active', true)
  245. ->count();
  246. return !!$count;
  247. }
  248. public function deviceMeasurements()
  249. {
  250. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  251. ->orderBy('created_at', 'desc');
  252. }
  253. public function activeMcpRequest()
  254. {
  255. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  256. }
  257. public function clientPrograms()
  258. {
  259. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  260. ->where('is_active', true)
  261. ->orderBy('title', 'desc');
  262. }
  263. public function tickets()
  264. {
  265. return $this->hasMany(Ticket::class, 'client_id', 'id')
  266. ->orderBy('is_open', 'desc')
  267. ->orderBy('created_at', 'desc');
  268. }
  269. public function mcpDisplayName()
  270. {
  271. }
  272. public function rmeDisplayName()
  273. {
  274. }
  275. public function firstCellularBPDevice()
  276. {
  277. $devices = $this->devices();
  278. $x = null;
  279. foreach($devices as $device){
  280. if($device->device->category == 'BP'){
  281. $x = $device;
  282. continue;
  283. }
  284. }
  285. return $x;
  286. }
  287. public function getFirstCellularBPMeasurementAt()
  288. {
  289. }
  290. public function getLatestCellularBPMeasurementAt()
  291. {
  292. }
  293. public function getTotalCellularBPMeasurements()
  294. {
  295. }
  296. public function firstCellularWeightDevice()
  297. {
  298. }
  299. public function getFirstCellularWeightMeasurementAt()
  300. {
  301. }
  302. public function getLatestCellularWeightMeasurementAt()
  303. {
  304. }
  305. public function getTotalCellularWeightMeasurements()
  306. {
  307. }
  308. public function prosWithAccess()
  309. {
  310. $pros = [];
  311. // directly associated pros
  312. $pro = $this->mcp;
  313. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  314. $pro = $this->pcp;
  315. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  316. $pro = $this->cm;
  317. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  318. $pro = $this->rmm;
  319. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  320. $pro = $this->rme;
  321. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  322. // via client pro access
  323. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  324. foreach ($cpAccesses as $cpAccess) {
  325. if (!$cpAccess->pro) continue;
  326. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  327. }
  328. // via appointments
  329. $appointments = Appointment::where('client_id', $this->id)->get();
  330. foreach ($appointments as $appointment) {
  331. if (!$appointment->pro) continue;
  332. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  333. }
  334. // via client program
  335. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  336. foreach ($clientPrograms as $clientProgram) {
  337. if ($clientProgram->mcp)
  338. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  339. if ($clientProgram->manager)
  340. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  341. }
  342. // sort by pro name
  343. $name = array_column($pros, 'pro');
  344. array_multisort($name, SORT_ASC, $pros);
  345. return $pros;
  346. }
  347. public function mcpRequests()
  348. {
  349. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  350. ->orderBy('created_at', 'desc');
  351. }
  352. public function eligibleRefreshes()
  353. {
  354. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  355. ->orderBy('created_at', 'desc');
  356. }
  357. public function mbPayerValidationResults()
  358. {
  359. return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
  360. ->orderBy('created_at', 'desc');
  361. }
  362. public function supplyOrders()
  363. {
  364. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  365. ->where('is_cancelled', false)
  366. ->orderBy('created_at', 'desc');
  367. }
  368. public function readyToShipSupplyOrders()
  369. {
  370. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  371. ->where('is_cancelled', false)
  372. ->where('is_cleared_for_shipment', true)
  373. ->whereNull('shipment_id')
  374. ->orderBy('created_at', 'desc');
  375. }
  376. public function shipments()
  377. {
  378. return $this->hasMany(Shipment::class, 'client_id', 'id')
  379. ->where('is_cancelled', false)
  380. ->orderBy('created_at', 'desc');
  381. }
  382. }