Client.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 currentCareMonth()
  110. {
  111. $cmStartDate = strtotime(date('Y-m-d'));
  112. $month = date("n", $cmStartDate);
  113. $year = date("Y", $cmStartDate);
  114. return CareMonth
  115. ::where('client_id', $this->id)
  116. ->whereRaw('EXTRACT(MONTH FROM start_date) = ?', [$month])
  117. ->whereRaw('EXTRACT(YEAR FROM start_date) = ?', [$year])
  118. ->first();
  119. }
  120. public function measurementsInCareMonth(CareMonth $careMonth)
  121. {
  122. $cmStartDate = strtotime($careMonth->start_date);
  123. $month = date("n", $cmStartDate);
  124. $year = date("Y", $cmStartDate);
  125. $measurements = Measurement
  126. ::where('client_id', $this->id)
  127. ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
  128. ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
  129. ->where('is_removed', false)
  130. ->orderBy('effective_date', 'desc')
  131. ->get();
  132. return $measurements;
  133. }
  134. public function allMeasurements()
  135. {
  136. return $this->hasMany(Measurement::class, 'client_id', 'id')
  137. ->where('is_removed', false)
  138. ->whereNull('parent_measurement_id')
  139. ->orderBy('label', 'asc')
  140. ->orderBy('effective_date', 'desc');
  141. }
  142. public function smses()
  143. {
  144. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  145. ->orderBy('created_at', 'desc');
  146. }
  147. public function documents()
  148. {
  149. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  150. ->orderBy('created_at', 'desc');
  151. }
  152. public function incomingReports() {
  153. return $this->hasMany(IncomingReport::class, 'client_id', 'id')
  154. ->orderBy('created_at', 'desc');
  155. }
  156. public function smsNumbers() {
  157. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  158. ->orderBy('created_at', 'desc');
  159. }
  160. public function nextMcpAppointment()
  161. {
  162. if ($this->mcp) {
  163. return Appointment::where('client_id', $this->id)
  164. ->where('pro_id', $this->mcp->id)
  165. ->where('start_time', '>=', date('Y-m-d'))
  166. ->orderBy('start_time', 'asc')
  167. ->first();
  168. }
  169. return false;
  170. }
  171. public function appointments()
  172. {
  173. return $this->hasMany(Appointment::class, 'client_id', 'id')
  174. ->orderBy('start_time', 'desc');
  175. }
  176. public function appointmentsForProByStatus($forPro = 'all', $status = 'all')
  177. {
  178. $appointments = Appointment::where('client_id', $this->id);
  179. if($forPro !== 'all') {
  180. $forPro = Pro::where('uid', $forPro)->first();
  181. $appointments = $appointments->where('pro_id', $forPro->id);
  182. }
  183. if($status !== 'ALL') {
  184. $appointments = $appointments->where('status', $status);
  185. }
  186. $appointments = $appointments->orderBy('start_time', 'desc');
  187. return $appointments->get();
  188. }
  189. public function upcomingAppointments()
  190. {
  191. return $this->hasMany(Appointment::class, 'client_id', 'id')
  192. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  193. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  194. ->orderBy('start_time', 'desc');
  195. }
  196. public function appointmentsFromLastWeek()
  197. {
  198. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
  199. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  200. return $this->hasMany(Appointment::class, 'client_id', 'id')
  201. ->where('raw_date', '>=', $dateLastWeek)
  202. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  203. ->orderBy('start_time', 'desc');
  204. }
  205. public function memos()
  206. {
  207. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  208. ->where('is_cancelled', false)
  209. ->orderBy('created_at', 'desc');
  210. }
  211. public function devices()
  212. {
  213. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  214. ->where('is_active', true)
  215. ->orderBy('created_at', 'desc');
  216. }
  217. public function hasDevice($_device)
  218. {
  219. $count = ClientBDTDevice::where('client_id', $this->id)
  220. ->where('device_id', $_device->id)
  221. ->where('is_active', true)
  222. ->count();
  223. return !!$count;
  224. }
  225. public function deviceMeasurements()
  226. {
  227. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  228. ->orderBy('created_at', 'desc');
  229. }
  230. public function activeMcpRequest()
  231. {
  232. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  233. }
  234. public function clientPrograms()
  235. {
  236. return $this->hasMany(ClientProgram::class, 'client_id', 'id')
  237. ->where('is_active', true)
  238. ->orderBy('title', 'desc');
  239. }
  240. public function tickets()
  241. {
  242. return $this->hasMany(Ticket::class, 'client_id', 'id')
  243. ->orderBy('is_open', 'desc')
  244. ->orderBy('created_at', 'desc');
  245. }
  246. public function mcpDisplayName()
  247. {
  248. }
  249. public function rmeDisplayName()
  250. {
  251. }
  252. public function firstCellularBPDevice()
  253. {
  254. $devices = $this->devices();
  255. $x = null;
  256. foreach($devices as $device){
  257. if($device->device->category == 'BP'){
  258. $x = $device;
  259. continue;
  260. }
  261. }
  262. return $x;
  263. }
  264. public function getFirstCellularBPMeasurementAt()
  265. {
  266. }
  267. public function getLatestCellularBPMeasurementAt()
  268. {
  269. }
  270. public function getTotalCellularBPMeasurements()
  271. {
  272. }
  273. public function firstCellularWeightDevice()
  274. {
  275. }
  276. public function getFirstCellularWeightMeasurementAt()
  277. {
  278. }
  279. public function getLatestCellularWeightMeasurementAt()
  280. {
  281. }
  282. public function getTotalCellularWeightMeasurements()
  283. {
  284. }
  285. public function prosWithAccess()
  286. {
  287. $pros = [];
  288. // directly associated pros
  289. $pro = $this->mcp;
  290. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
  291. $pro = $this->pcp;
  292. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
  293. $pro = $this->cm;
  294. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
  295. $pro = $this->rmm;
  296. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
  297. $pro = $this->rme;
  298. if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
  299. // via client pro access
  300. $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
  301. foreach ($cpAccesses as $cpAccess) {
  302. if (!$cpAccess->pro) continue;
  303. $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
  304. }
  305. // via appointments
  306. $appointments = Appointment::where('client_id', $this->id)->get();
  307. foreach ($appointments as $appointment) {
  308. if (!$appointment->pro) continue;
  309. $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
  310. }
  311. // via client program
  312. $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
  313. foreach ($clientPrograms as $clientProgram) {
  314. if ($clientProgram->mcp)
  315. $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
  316. if ($clientProgram->manager)
  317. $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
  318. }
  319. // sort by pro name
  320. $name = array_column($pros, 'pro');
  321. array_multisort($name, SORT_ASC, $pros);
  322. return $pros;
  323. }
  324. public function mcpRequests()
  325. {
  326. return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
  327. ->orderBy('created_at', 'desc');
  328. }
  329. public function eligibleRefreshes()
  330. {
  331. return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
  332. ->orderBy('created_at', 'desc');
  333. }
  334. public function supplyOrders()
  335. {
  336. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  337. ->where('is_cancelled', false)
  338. ->orderBy('created_at', 'desc');
  339. }
  340. public function unshippedSupplyOrders()
  341. {
  342. return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
  343. ->where('is_cancelled', false)
  344. ->whereNull('shipment_id')
  345. ->orderBy('created_at', 'desc');
  346. }
  347. }