AdminController.php 24 KB

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