AdminController.php 32 KB

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