AdminController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\BDTDevice;
  5. use App\Models\CareMonth;
  6. use App\Models\Client;
  7. use App\Models\ClientBDTDevice;
  8. use App\Models\ClientInfoLine;
  9. use App\Models\Erx;
  10. use App\Models\Facility;
  11. use App\Models\Handout;
  12. use App\Models\IncomingReport;
  13. use App\Models\MBClaim;
  14. use App\Models\MBPayer;
  15. use App\Models\Note;
  16. use App\Models\NoteTemplate;
  17. use App\Models\Pro;
  18. use App\Models\Product;
  19. use App\Models\ProProAccess;
  20. use App\Models\SectionTemplate;
  21. use App\Models\Shipment;
  22. use App\Models\SupplyOrder;
  23. use App\Models\Ticket;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\File;
  27. use App\Models\Bill;
  28. use App\Models\ClientSMS;
  29. use Illuminate\Support\Facades\Http;
  30. use PDF;
  31. class AdminController extends Controller
  32. {
  33. public function patients(Request $request)
  34. {
  35. $filters = $request->all();
  36. $patients = Client::whereNull('shadow_pro_id');
  37. if ($request->input('name')) {
  38. $name = trim($request->input('name'));
  39. if ($name) {
  40. $patients = $patients->where(function ($q) use ($name) {
  41. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  42. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  43. });
  44. }
  45. }
  46. if ($request->input('mcp')) {
  47. if($request->input('mcp') == 'NO_MCP'){
  48. $patients = $patients->whereNull('mcp_pro_id');
  49. }else{
  50. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  51. if ($mcp) {
  52. $patients = $patients->where('mcp_pro_id', $mcp->id);
  53. }
  54. }
  55. }
  56. if ($request->input('na')) {
  57. if($request->input('na') == 'NO_NA'){
  58. $patients = $patients->whereNull('default_na_pro_id');
  59. }else{
  60. $na = Pro::where('uid', trim($request->input('na')))->first();
  61. if ($na) {
  62. $patients = $patients->where('default_na_pro_id', $na->id);
  63. }
  64. }
  65. }
  66. if ($request->input('next_appointment_category')) {
  67. if($request->input('next_appointment_category') == 'NONE'){
  68. $patients = $patients->whereNull('next_mcp_appointment_id');
  69. }else{
  70. $self = $this;
  71. $patients = $patients->whereHas('nextMcpAppointment', function($pQry) use ($request, $self){
  72. return $self->filterMultiQuery($request, $pQry, 'raw_date', 'next_appointment_category', 'next_appointment_value_1', 'next_appointment_value_2');
  73. });
  74. }
  75. }
  76. if ($request->input('chart_number')) {
  77. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  78. }
  79. if ($request->input('home_address_state')) {
  80. if($request->input('home_address_state') == 'NONE'){
  81. $patients = $patients->whereNull('mailing_address_state');
  82. }else if($request->input('home_address_state') == 'NOT_MD'){
  83. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  84. }else{
  85. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  86. }
  87. }
  88. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  89. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  90. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  91. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  92. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  93. $this->filterMultiQuery($request, $patients, 'created_at', 'created_at', 'created_at_value_1', 'created_at_value_2');
  94. $this->filterMultiQuery($request, $patients, 'most_recent_completed_mcp_note_date', 'last_visit_category', 'last_visit_value_1', 'last_visit_value_2');
  95. if($request->input('number_of_measurements')){
  96. $keyName = $request->input('number_of_measurements');
  97. $measurementCountQuery = '(SELECT COUNT(*) FROM measurement WHERE measurement.client_id = client.id AND is_active IS TRUE AND is_cellular IS TRUE AND is_cellular_zero IS NOT TRUE)';
  98. switch($keyName) {
  99. case 'EXACTLY':
  100. if($request->input('number_of_measurements_value_1')) {
  101. $patients->whereRaw($measurementCountQuery . '='.$request->input('number_of_measurements_value_1'));
  102. }
  103. break;
  104. case 'LESS_THAN':
  105. if($request->input('number_of_measurements_value_1')) {
  106. $patients->whereRaw($measurementCountQuery . '<='.$request->input('number_of_measurements_value_1'));
  107. }
  108. break;
  109. case 'GREATER_THAN':
  110. if($request->input('number_of_measurements_value_1')) {
  111. $patients->whereRaw($measurementCountQuery . '>='.$request->input('number_of_measurements_value_1'));
  112. }
  113. break;
  114. case 'BETWEEN':
  115. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  116. $patients->whereRaw($measurementCountQuery.'>='.$request->input('number_of_measurements_value_1') .' AND '. $measurementCountQuery . '<='.$request->input('number_of_measurements_value_2'));
  117. }
  118. break;
  119. case 'NOT_BETWEEN':
  120. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  121. $patients->where(function ($q) use ($request, $measurementCountQuery) {
  122. $q->whereRaw($measurementCountQuery . '<'.$request->input('number_of_measurements_value_1') .' OR '. $measurementCountQuery . '>'.$request->input('number_of_measurements_value_2'));
  123. });
  124. }
  125. break;
  126. }
  127. }
  128. $status = $request->input('status');
  129. if($status){
  130. if($status == 'ACTIVE'){
  131. $patients->where('is_active', true)->where(function($q) use ($status){
  132. return $q->where('client_engagement_status_category', $status)
  133. ->orWhereNull('client_engagement_status_category');
  134. });
  135. }elseif($status = 'NONE'){
  136. $patients->whereNull('client_engagement_status_category');
  137. }else {
  138. $patients->where('client_engagement_status_category', $status);
  139. }
  140. }
  141. $initiative = $request->input('initiative');
  142. if($initiative){
  143. $wildCardedInitiative = '%'.$initiative.'%';
  144. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  145. }
  146. $include_test_records = $request->input('include_test_records');
  147. if(!$include_test_records && $status != 'DUMMY'){
  148. $patients = $patients->where(function ($q) {
  149. $q->whereNull('client_engagement_status_category')
  150. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  151. });
  152. }
  153. $zero_deductible = $request->input('zero_deductible');
  154. if($zero_deductible){
  155. $patients = $patients->where(function ($q) {
  156. $q->where('mpb_remaining', 0);
  157. });
  158. }
  159. $insurance = $request->get('insurance');
  160. if($insurance){
  161. if($insurance === 'MEDICARE'){
  162. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) {
  163. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  164. });
  165. }else{
  166. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) use ($insurance){
  167. return $cpcQuery->where('commercial_payer_id', '=', $insurance);
  168. });
  169. }
  170. }
  171. $sortBy = $request->input('sort_by') ?: 'created_at';
  172. $sortDir = $request->input('sort_dir') ?: 'DESC';
  173. $patients = $patients->orderByRaw("$sortBy $sortDir NULLS LAST");
  174. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  175. $insurances = DB::select('SELECT DISTINCT commercial_payer_name, commercial_payer_id FROM client_primary_coverage WHERE commercial_payer_name IS NOT NULL ORDER BY commercial_payer_name ASC');
  176. return view('app.admin.patients', compact('patients', 'filters', 'insurances'));
  177. }
  178. public function partBPatients(Request $request){
  179. $filters = $request->all();
  180. $patients = Client::whereNull('shadow_pro_id');
  181. if ($request->input('name')) {
  182. $name = trim($request->input('name'));
  183. if ($name) {
  184. $patients = $patients->where(function ($q) use ($name) {
  185. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  186. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  187. });
  188. }
  189. }
  190. if ($request->input('mcp')) {
  191. if($request->input('mcp') == 'NO_MCP'){
  192. $patients = $patients->whereNull('mcp_pro_id');
  193. }else{
  194. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  195. if ($mcp) {
  196. $patients = $patients->where('mcp_pro_id', $mcp->id);
  197. }
  198. }
  199. }
  200. if ($request->input('na')) {
  201. if($request->input('na') == 'NO_NA'){
  202. $patients = $patients->whereNull('default_na_pro_id');
  203. }else{
  204. $na = Pro::where('uid', trim($request->input('na')))->first();
  205. if ($na) {
  206. $patients = $patients->where('default_na_pro_id', $na->id);
  207. }
  208. }
  209. }
  210. if ($request->input('next_appointment_category')) {
  211. if($request->input('next_appointment_category') == 'NONE'){
  212. $patients = $patients->whereNull('next_mcp_appointment_id');
  213. }
  214. }
  215. if ($request->input('chart_number')) {
  216. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  217. }
  218. if ($request->input('home_address_state')) {
  219. if($request->input('home_address_state') == 'NONE'){
  220. $patients = $patients->whereNull('mailing_address_state');
  221. }else if($request->input('home_address_state') == 'NOT_MD'){
  222. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  223. }else{
  224. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  225. }
  226. }
  227. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
  228. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  229. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  230. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  231. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  232. if($request->input('deductible')){
  233. $keyName = $request->input('deductible');
  234. switch($keyName) {
  235. case 'EXACTLY':
  236. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  237. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  238. });
  239. break;
  240. case 'LESS_THAN':
  241. if($request->input('deductible_value_1')) {
  242. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  243. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  244. });
  245. }
  246. break;
  247. case 'GREATER_THAN':
  248. if($request->input('deductible_value_1')) {
  249. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  250. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  251. });
  252. }
  253. break;
  254. case 'BETWEEN':
  255. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  256. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  257. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  258. });
  259. break;
  260. case 'NOT_BETWEEN':
  261. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  262. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  263. return $q->where(function($qq) use ($request){
  264. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  265. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  266. });
  267. });
  268. }
  269. break;
  270. }
  271. }
  272. switch($request->input('status')) {
  273. case 'ACTIVE':
  274. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  275. break;
  276. case 'AWAITING_VISIT':
  277. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  278. break;
  279. case 'INACTIVE':
  280. $patients->where('is_active', '<>', true);
  281. break;
  282. }
  283. $initiative = $request->input('initiative');
  284. if($initiative){
  285. $wildCardedInitiative = '%'.$initiative.'%';
  286. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  287. }
  288. $include_test_records = $request->input('include_test_records');
  289. if(!$include_test_records){
  290. $patients = $patients->where(function ($q) {
  291. return $q->whereNull('client_engagement_status_category')
  292. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  293. });
  294. }
  295. $with_claim_not_closed = $request->input('with_claim_not_closed');
  296. if($with_claim_not_closed){
  297. $patients = $patients->whereHas('notes', function ($q) {
  298. return $q->where('is_claim_closed', false)
  299. ->where('is_signed_by_hcp', true)
  300. ->where('is_cancelled', false);
  301. });
  302. }
  303. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  304. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  305. });
  306. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  307. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  308. }
  309. public function notes(Request $request)
  310. {
  311. $notes = Note::paginate(5);
  312. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  313. return view('app.mcp.notes', compact('notes'));
  314. }
  315. public function notes_pending_summary_suggestion(Request $request){
  316. $pro = $this->performer->pro;
  317. $data = [
  318. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  319. ];
  320. return view('app.admin.notes_pending_summary_suggestion', $data);
  321. }
  322. public function notes_rejected_summary_suggestion(Request $request){
  323. $pro = $this->performer->pro;
  324. $data = [
  325. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  326. ];
  327. return view('app.admin.notes_rejected_summary_suggestion', $data);
  328. }
  329. public function appointments(Request $request)
  330. {
  331. $appointments = Appointment::paginate(5);
  332. return view('app.mcp.appointments', compact('appointments'));
  333. }
  334. public function bills(Request $request)
  335. {
  336. $bills = Bill::paginate(5);
  337. return view('app.mcp.bills', compact('bills'));
  338. }
  339. public function erx_and_orders(Request $request)
  340. {
  341. $erxAndOrders = Erx::paginate(5);
  342. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  343. }
  344. public function reports(Request $request)
  345. {
  346. $data = [];
  347. return view('app.mcp.reports', $data);
  348. }
  349. public function supply_orders(Request $request)
  350. {
  351. $supplyOrders = SupplyOrder::paginate(5);
  352. return view('app.mcp.supply_orders', compact('supplyOrders'));
  353. }
  354. public function getCreateNewPatientScriptTemplate(Request $request){
  355. $template = $request->get('template');
  356. if(!$template) return $this->fail('No script template');
  357. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  358. if(!File::exists($path)) return $this->fail('Invalid script template');
  359. $templateContent = file_get_contents($path);
  360. return $this->pass($templateContent);
  361. }
  362. public function bdtDevices(Request $request)
  363. {
  364. $filters = $request->all();
  365. $bdtDevices = BDTDevice::query();
  366. $imei = $request->input('imei');
  367. if($imei){
  368. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  369. }
  370. $client = $request->input('client');
  371. if($client){
  372. $client = '%'.$client.'%';
  373. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  374. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  375. return $clientQuery->where(function($q) use ($client){
  376. return $q->where('name_first', 'ilike', $client)
  377. ->orWhere('name_last', 'ilike', $client)
  378. ->orWhere('cell_number', 'ilike', $client)
  379. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  380. });
  381. });
  382. });
  383. }
  384. $is_issued = $request->input('is_issued');
  385. if($is_issued){
  386. if($is_issued == 'YES'){
  387. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  388. }
  389. if($is_issued == 'NO'){
  390. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  391. }
  392. }
  393. $is_issued = $request->input('is_issued');
  394. if($is_issued){
  395. if($is_issued == 'YES'){
  396. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  397. }
  398. if($is_issued == 'NO'){
  399. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  400. }
  401. }
  402. $mcp = $request->input('mcp');
  403. if($mcp){
  404. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  405. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  406. $mcpPro = Pro::where('uid', $mcp)->first();
  407. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  408. });
  409. });
  410. }
  411. $bdtDevices = $bdtDevices->paginate(20);
  412. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  413. }
  414. public function patientsMissingDefasultSettings(Request $request){
  415. $filters = $request->all();
  416. $patients = Client::whereNull('shadow_pro_id');
  417. $patients = $patients->where(function($qry){
  418. return $qry->orWhereNull('mcp_pro_id')->orWhereNull('default_mcp_company_pro_id')->orWhereNull('default_mcp_company_pro_payer_id')->orWhereNull('default_mcp_company_location_id');
  419. });
  420. if ($request->input('name')) {
  421. $name = trim($request->input('name'));
  422. if ($name) {
  423. $patients = $patients->where(function ($q) use ($name) {
  424. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  425. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  426. });
  427. }
  428. }
  429. if ($request->input('mcp')) {
  430. if($request->input('mcp') == 'NO_MCP'){
  431. $patients = $patients->whereNull('mcp_pro_id');
  432. }else{
  433. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  434. if ($mcp) {
  435. $patients = $patients->where('mcp_pro_id', $mcp->id);
  436. }
  437. }
  438. }
  439. if ($request->input('chart_number')) {
  440. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  441. }
  442. $status = $request->input('status');
  443. if($status){
  444. if($status == 'ACTIVE'){
  445. $patients->where('is_active', true)->where(function($q) use ($status){
  446. return $q->where('client_engagement_status_category', $status)
  447. ->orWhereNull('client_engagement_status_category');
  448. });
  449. }else {
  450. $patients->where('client_engagement_status_category', $status);
  451. }
  452. }
  453. $insurance = $request->get('insurance');
  454. if($insurance){
  455. if($insurance === 'MEDICARE'){
  456. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  457. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  458. });
  459. }elseif($insurance === 'MEDICARE_PENDING'){
  460. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  461. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  462. });
  463. }elseif($insurance === 'NOT_COVERED'){
  464. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  465. return $cpcQuery->where('is_covered', '!=', 'YES');
  466. });
  467. }elseif($insurance === 'PENDING'){
  468. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  469. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  470. });
  471. }
  472. else{
  473. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  474. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  475. });
  476. }
  477. }
  478. $missing_default_settings = $request->get('missing_default_settings');
  479. if($missing_default_settings){
  480. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  481. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  482. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  483. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  484. }
  485. $care_plan = $request->get('care_plan');
  486. if($care_plan){
  487. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  488. $patients = $patients->whereHas('notes', function($noteQuery){
  489. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  490. });
  491. }
  492. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  493. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  494. }
  495. }
  496. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  497. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  498. }
  499. }