AdminController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. $fVal = $request->input('has_email');
  103. if($fVal) {
  104. if($fVal === '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. $fVal = $request->input('has_account');
  112. if($fVal) {
  113. if($fVal === '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. $fVal = $request->input('has_default_mcp_company_pro');
  121. if($fVal) {
  122. if($fVal === 'YES') {
  123. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NOT NULL)");
  124. }
  125. else {
  126. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NULL)");
  127. }
  128. }
  129. $fVal = $request->input('has_default_mcp_company_pro_payer');
  130. if($fVal) {
  131. if($fVal === 'YES') {
  132. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NOT NULL)");
  133. }
  134. else {
  135. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NULL)");
  136. }
  137. }
  138. $fVal = $request->input('has_default_mcp_company_location');
  139. if($fVal) {
  140. if($fVal === 'YES') {
  141. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NOT NULL)");
  142. }
  143. else {
  144. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NULL)");
  145. }
  146. }
  147. $fVal = $request->input('has_bp_device');
  148. if($fVal) {
  149. if($fVal === 'YES') {
  150. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  151. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  152. "WHERE so.product_id = 1 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) > 0)");
  153. }
  154. else {
  155. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  156. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  157. "WHERE so.product_id = 1 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) = 0)");
  158. }
  159. }
  160. $fVal = $request->input('has_weight_scale');
  161. if($fVal) {
  162. if($fVal === 'YES') {
  163. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  164. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  165. "WHERE so.product_id = 2 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) > 0)");
  166. }
  167. else {
  168. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  169. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  170. "WHERE so.product_id = 2 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) = 0)");
  171. }
  172. }
  173. $fVal = $request->input('has_pulse_ox');
  174. if($fVal) {
  175. if($fVal === 'YES') {
  176. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  177. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  178. "WHERE so.product_id = 3 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) > 0)");
  179. }
  180. else {
  181. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  182. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  183. "WHERE so.product_id = 3 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) = 0)");
  184. }
  185. }
  186. $fVal = $request->input('has_temp_fun');
  187. if($fVal) {
  188. if($fVal === 'YES') {
  189. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  190. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  191. "WHERE so.product_id = 4 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) > 0)");
  192. }
  193. else {
  194. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  195. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  196. "WHERE so.product_id = 4 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = client.id) = 0)");
  197. }
  198. }
  199. if($request->input('number_of_measurements')){
  200. $keyName = $request->input('number_of_measurements');
  201. $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)';
  202. switch($keyName) {
  203. case 'EXACTLY':
  204. if($request->input('number_of_measurements_value_1')) {
  205. $patients->whereRaw($measurementCountQuery . '='.$request->input('number_of_measurements_value_1'));
  206. }
  207. break;
  208. case 'LESS_THAN':
  209. if($request->input('number_of_measurements_value_1')) {
  210. $patients->whereRaw($measurementCountQuery . '<='.$request->input('number_of_measurements_value_1'));
  211. }
  212. break;
  213. case 'GREATER_THAN':
  214. if($request->input('number_of_measurements_value_1')) {
  215. $patients->whereRaw($measurementCountQuery . '>='.$request->input('number_of_measurements_value_1'));
  216. }
  217. break;
  218. case 'BETWEEN':
  219. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  220. $patients->whereRaw($measurementCountQuery.'>='.$request->input('number_of_measurements_value_1') .' AND '. $measurementCountQuery . '<='.$request->input('number_of_measurements_value_2'));
  221. }
  222. break;
  223. case 'NOT_BETWEEN':
  224. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  225. $patients->where(function ($q) use ($request, $measurementCountQuery) {
  226. $q->whereRaw($measurementCountQuery . '<'.$request->input('number_of_measurements_value_1') .' OR '. $measurementCountQuery . '>'.$request->input('number_of_measurements_value_2'));
  227. });
  228. }
  229. break;
  230. }
  231. }
  232. $status = $request->input('status');
  233. if($status){
  234. if($status === 'ACTIVE'){
  235. $patients->where('is_active', true)->where(function($q) use ($status){
  236. return $q->where('client_engagement_status_category', $status)
  237. ->orWhereNull('client_engagement_status_category');
  238. });
  239. }elseif($status === 'NONE'){
  240. $patients->whereNull('client_engagement_status_category');
  241. }else {
  242. $patients->where('client_engagement_status_category', $status);
  243. }
  244. }
  245. $initiative = $request->input('initiative');
  246. if($initiative){
  247. $wildCardedInitiative = '%'.$initiative.'%';
  248. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  249. }
  250. $include_test_records = $request->input('include_test_records');
  251. if(!$include_test_records && $status != 'DUMMY'){
  252. $patients = $patients->where(function ($q) {
  253. $q->whereNull('client_engagement_status_category')
  254. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  255. });
  256. }
  257. $zero_deductible = $request->input('zero_deductible');
  258. if($zero_deductible){
  259. $patients = $patients->where(function ($q) {
  260. $q->where('mpb_remaining', 0);
  261. });
  262. }
  263. $insurance = $request->get('insurance');
  264. if($insurance){
  265. if($insurance === 'MEDICARE'){
  266. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) {
  267. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  268. });
  269. }else{
  270. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) use ($insurance){
  271. return $cpcQuery->where('commercial_payer_id', '=', $insurance);
  272. });
  273. }
  274. }
  275. $sortBy = $request->input('sort_by') ?: 'name_first';
  276. $sortDir = $request->input('sort_dir') ?: 'ASC';
  277. $sortBySQL = "$sortBy $sortDir NULLS LAST";
  278. if($sortBy !== 'client_engagement_status_category' && $request->input('status')) {
  279. $sortBySQL = "client_engagement_status_category DESC NULLS LAST";
  280. }
  281. $patients = $patients->orderByRaw($sortBySQL)->paginate(25);
  282. $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');
  283. return view('app.admin.patients', compact('patients', 'filters', 'insurances'));
  284. }
  285. public function partBPatients(Request $request){
  286. $filters = $request->all();
  287. $patients = Client::whereNull('shadow_pro_id');
  288. if ($request->input('name')) {
  289. $name = trim($request->input('name'));
  290. if ($name) {
  291. $patients = $patients->where(function ($q) use ($name) {
  292. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  293. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  294. });
  295. }
  296. }
  297. if ($request->input('mcp')) {
  298. if($request->input('mcp') == 'NO_MCP'){
  299. $patients = $patients->whereNull('mcp_pro_id');
  300. }else{
  301. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  302. if ($mcp) {
  303. $patients = $patients->where('mcp_pro_id', $mcp->id);
  304. }
  305. }
  306. }
  307. if ($request->input('na')) {
  308. if($request->input('na') == 'NO_NA'){
  309. $patients = $patients->whereNull('default_na_pro_id');
  310. }else{
  311. $na = Pro::where('uid', trim($request->input('na')))->first();
  312. if ($na) {
  313. $patients = $patients->where('default_na_pro_id', $na->id);
  314. }
  315. }
  316. }
  317. if ($request->input('next_appointment_category')) {
  318. if($request->input('next_appointment_category') == 'NONE'){
  319. $patients = $patients->whereNull('next_mcp_appointment_id');
  320. }
  321. }
  322. if ($request->input('chart_number')) {
  323. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  324. }
  325. if ($request->input('home_address_state')) {
  326. if($request->input('home_address_state') == 'NONE'){
  327. $patients = $patients->whereNull('mailing_address_state');
  328. }else if($request->input('home_address_state') == 'NOT_MD'){
  329. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  330. }else{
  331. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  332. }
  333. }
  334. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  335. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  336. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  337. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  338. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  339. if($request->input('deductible')){
  340. $keyName = $request->input('deductible');
  341. switch($keyName) {
  342. case 'EXACTLY':
  343. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  344. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  345. });
  346. break;
  347. case 'LESS_THAN':
  348. if($request->input('deductible_value_1')) {
  349. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  350. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  351. });
  352. }
  353. break;
  354. case 'GREATER_THAN':
  355. if($request->input('deductible_value_1')) {
  356. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  357. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  358. });
  359. }
  360. break;
  361. case 'BETWEEN':
  362. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  363. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  364. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  365. });
  366. break;
  367. case 'NOT_BETWEEN':
  368. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  369. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  370. return $q->where(function($qq) use ($request){
  371. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  372. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  373. });
  374. });
  375. }
  376. break;
  377. }
  378. }
  379. switch($request->input('status')) {
  380. case 'ACTIVE':
  381. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  382. break;
  383. case 'AWAITING_VISIT':
  384. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  385. break;
  386. case 'INACTIVE':
  387. $patients->where('is_active', '<>', true);
  388. break;
  389. }
  390. $initiative = $request->input('initiative');
  391. if($initiative){
  392. $wildCardedInitiative = '%'.$initiative.'%';
  393. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  394. }
  395. $include_test_records = $request->input('include_test_records');
  396. if(!$include_test_records){
  397. $patients = $patients->where(function ($q) {
  398. return $q->whereNull('client_engagement_status_category')
  399. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  400. });
  401. }
  402. $with_claim_not_closed = $request->input('with_claim_not_closed');
  403. if($with_claim_not_closed){
  404. $patients = $patients->whereHas('notes', function ($q) {
  405. return $q->where('is_claim_closed', false)
  406. ->where('is_signed_by_hcp', true)
  407. ->where('is_cancelled', false);
  408. });
  409. }
  410. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  411. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  412. });
  413. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  414. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  415. }
  416. public function notes(Request $request)
  417. {
  418. $notes = Note::paginate(5);
  419. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  420. return view('app.mcp.notes', compact('notes'));
  421. }
  422. public function notes_pending_summary_suggestion(Request $request){
  423. $pro = $this->performer->pro;
  424. $data = [
  425. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  426. ];
  427. return view('app.admin.notes_pending_summary_suggestion', $data);
  428. }
  429. public function notes_rejected_summary_suggestion(Request $request){
  430. $pro = $this->performer->pro;
  431. $data = [
  432. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  433. ];
  434. return view('app.admin.notes_rejected_summary_suggestion', $data);
  435. }
  436. public function appointments(Request $request)
  437. {
  438. $appointments = Appointment::paginate(5);
  439. return view('app.mcp.appointments', compact('appointments'));
  440. }
  441. public function bills(Request $request)
  442. {
  443. $bills = Bill::paginate(5);
  444. return view('app.mcp.bills', compact('bills'));
  445. }
  446. public function erx_and_orders(Request $request)
  447. {
  448. $erxAndOrders = Erx::paginate(5);
  449. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  450. }
  451. public function reports(Request $request)
  452. {
  453. $data = [];
  454. return view('app.mcp.reports', $data);
  455. }
  456. public function supply_orders(Request $request)
  457. {
  458. $supplyOrders = SupplyOrder::paginate(5);
  459. return view('app.mcp.supply_orders', compact('supplyOrders'));
  460. }
  461. public function getCreateNewPatientScriptTemplate(Request $request){
  462. $template = $request->get('template');
  463. if(!$template) return $this->fail('No script template');
  464. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  465. if(!File::exists($path)) return $this->fail('Invalid script template');
  466. $templateContent = file_get_contents($path);
  467. return $this->pass($templateContent);
  468. }
  469. public function bdtDevices(Request $request)
  470. {
  471. $filters = $request->all();
  472. $bdtDevices = BDTDevice::query();
  473. $imei = $request->input('imei');
  474. if($imei){
  475. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  476. }
  477. $client = $request->input('client');
  478. if($client){
  479. $client = '%'.$client.'%';
  480. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  481. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  482. return $clientQuery->where(function($q) use ($client){
  483. return $q->where('name_first', 'ilike', $client)
  484. ->orWhere('name_last', 'ilike', $client)
  485. ->orWhere('cell_number', 'ilike', $client)
  486. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  487. });
  488. });
  489. });
  490. }
  491. $is_issued = $request->input('is_issued');
  492. if($is_issued){
  493. if($is_issued == 'YES'){
  494. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  495. }
  496. if($is_issued == 'NO'){
  497. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  498. }
  499. }
  500. $is_issued = $request->input('is_issued');
  501. if($is_issued){
  502. if($is_issued == 'YES'){
  503. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  504. }
  505. if($is_issued == 'NO'){
  506. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  507. }
  508. }
  509. $mcp = $request->input('mcp');
  510. if($mcp){
  511. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  512. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  513. $mcpPro = Pro::where('uid', $mcp)->first();
  514. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  515. });
  516. });
  517. }
  518. $bdtDevices = $bdtDevices->paginate(20);
  519. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  520. }
  521. public function patientsMissingDefasultSettings(Request $request){
  522. $filters = $request->all();
  523. $patients = Client::whereNull('shadow_pro_id');
  524. $patients = $patients->where(function($qry){
  525. 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');
  526. });
  527. if ($request->input('name')) {
  528. $name = trim($request->input('name'));
  529. if ($name) {
  530. $patients = $patients->where(function ($q) use ($name) {
  531. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  532. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  533. });
  534. }
  535. }
  536. if ($request->input('mcp')) {
  537. if($request->input('mcp') == 'NO_MCP'){
  538. $patients = $patients->whereNull('mcp_pro_id');
  539. }else{
  540. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  541. if ($mcp) {
  542. $patients = $patients->where('mcp_pro_id', $mcp->id);
  543. }
  544. }
  545. }
  546. if ($request->input('chart_number')) {
  547. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  548. }
  549. $status = $request->input('status');
  550. if($status){
  551. if($status == 'ACTIVE'){
  552. $patients->where('is_active', true)->where(function($q) use ($status){
  553. return $q->where('client_engagement_status_category', $status)
  554. ->orWhereNull('client_engagement_status_category');
  555. });
  556. }else {
  557. $patients->where('client_engagement_status_category', $status);
  558. }
  559. }
  560. $insurance = $request->get('insurance');
  561. if($insurance){
  562. if($insurance === 'MEDICARE'){
  563. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  564. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  565. });
  566. }elseif($insurance === 'MEDICARE_PENDING'){
  567. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  568. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  569. });
  570. }elseif($insurance === 'NOT_COVERED'){
  571. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  572. return $cpcQuery->where('is_covered', '!=', 'YES');
  573. });
  574. }elseif($insurance === 'PENDING'){
  575. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  576. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  577. });
  578. }
  579. else{
  580. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  581. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  582. });
  583. }
  584. }
  585. $missing_default_settings = $request->get('missing_default_settings');
  586. if($missing_default_settings){
  587. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  588. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  589. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  590. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  591. }
  592. $care_plan = $request->get('care_plan');
  593. if($care_plan){
  594. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  595. $patients = $patients->whereHas('notes', function($noteQuery){
  596. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  597. });
  598. }
  599. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  600. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  601. }
  602. }
  603. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  604. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  605. }
  606. }