AdminController.php 24 KB

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