AdminController.php 26 KB

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