AdminController.php 23 KB

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