Client.php 14 KB

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