AdminController.php 30 KB

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