AdminController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. $sortBy = $request->input('sort_by') ?: 'name_first';
  283. $sortDir = $request->input('sort_dir') ?: 'ASC';
  284. $sortBySQL = "$sortBy $sortDir NULLS LAST";
  285. if($sortBy !== 'client_engagement_status_category' && $request->input('status')) {
  286. $sortBySQL = "client_engagement_status_category DESC NULLS LAST";
  287. }
  288. if(@$filters['mapView'] == 1){
  289. $patients = $patients->orderByRaw($sortBySQL)->paginate(100);
  290. }else{
  291. $patients = $patients->orderByRaw($sortBySQL)->paginate(25);
  292. }
  293. $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');
  294. return view('app.admin.patients', compact('patients', 'filters', 'insurances'));
  295. }
  296. public function partBPatients(Request $request){
  297. $filters = $request->all();
  298. $patients = Client::whereNull('shadow_pro_id');
  299. if ($request->input('name')) {
  300. $name = trim($request->input('name'));
  301. if ($name) {
  302. $patients = $patients->where(function ($q) use ($name) {
  303. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  304. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  305. });
  306. }
  307. }
  308. if ($request->input('mcp')) {
  309. if($request->input('mcp') == 'NO_MCP'){
  310. $patients = $patients->whereNull('mcp_pro_id');
  311. }else{
  312. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  313. if ($mcp) {
  314. $patients = $patients->where('mcp_pro_id', $mcp->id);
  315. }
  316. }
  317. }
  318. if ($request->input('na')) {
  319. if($request->input('na') == 'NO_NA'){
  320. $patients = $patients->whereNull('default_na_pro_id');
  321. }else{
  322. $na = Pro::where('uid', trim($request->input('na')))->first();
  323. if ($na) {
  324. $patients = $patients->where('default_na_pro_id', $na->id);
  325. }
  326. }
  327. }
  328. if ($request->input('next_appointment_category')) {
  329. if($request->input('next_appointment_category') == 'NONE'){
  330. $patients = $patients->whereNull('next_mcp_appointment_id');
  331. }
  332. }
  333. if ($request->input('chart_number')) {
  334. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  335. }
  336. if ($request->input('home_address_state')) {
  337. if($request->input('home_address_state') == 'NONE'){
  338. $patients = $patients->whereNull('mailing_address_state');
  339. }else if($request->input('home_address_state') == 'NOT_MD'){
  340. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  341. }else{
  342. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  343. }
  344. }
  345. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  346. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  347. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  348. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  349. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  350. if($request->input('deductible')){
  351. $keyName = $request->input('deductible');
  352. switch($keyName) {
  353. case 'EXACTLY':
  354. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  355. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  356. });
  357. break;
  358. case 'LESS_THAN':
  359. if($request->input('deductible_value_1')) {
  360. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  361. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  362. });
  363. }
  364. break;
  365. case 'GREATER_THAN':
  366. if($request->input('deductible_value_1')) {
  367. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  368. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  369. });
  370. }
  371. break;
  372. case 'BETWEEN':
  373. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  374. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  375. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  376. });
  377. break;
  378. case 'NOT_BETWEEN':
  379. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  380. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  381. return $q->where(function($qq) use ($request){
  382. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  383. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  384. });
  385. });
  386. }
  387. break;
  388. }
  389. }
  390. switch($request->input('status')) {
  391. case 'ACTIVE':
  392. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  393. break;
  394. case 'AWAITING_VISIT':
  395. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  396. break;
  397. case 'INACTIVE':
  398. $patients->where('is_active', '<>', true);
  399. break;
  400. }
  401. $initiative = $request->input('initiative');
  402. if($initiative){
  403. $wildCardedInitiative = '%'.$initiative.'%';
  404. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  405. }
  406. $include_test_records = $request->input('include_test_records');
  407. if(!$include_test_records){
  408. $patients = $patients->where(function ($q) {
  409. return $q->whereNull('client_engagement_status_category')
  410. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  411. });
  412. }
  413. $with_claim_not_closed = $request->input('with_claim_not_closed');
  414. if($with_claim_not_closed){
  415. $patients = $patients->whereHas('notes', function ($q) {
  416. return $q->where('is_claim_closed', false)
  417. ->where('is_signed_by_hcp', true)
  418. ->where('is_cancelled', false);
  419. });
  420. }
  421. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  422. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  423. });
  424. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  425. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  426. }
  427. public function notes(Request $request)
  428. {
  429. $notes = Note::paginate(5);
  430. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  431. return view('app.mcp.notes', compact('notes'));
  432. }
  433. public function notes_pending_summary_suggestion(Request $request){
  434. $pro = $this->performer->pro;
  435. $data = [
  436. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  437. ];
  438. return view('app.admin.notes_pending_summary_suggestion', $data);
  439. }
  440. public function notes_rejected_summary_suggestion(Request $request){
  441. $pro = $this->performer->pro;
  442. $data = [
  443. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  444. ];
  445. return view('app.admin.notes_rejected_summary_suggestion', $data);
  446. }
  447. public function appointments(Request $request)
  448. {
  449. $appointments = Appointment::paginate(5);
  450. return view('app.mcp.appointments', compact('appointments'));
  451. }
  452. public function bills(Request $request)
  453. {
  454. $bills = Bill::paginate(5);
  455. return view('app.mcp.bills', compact('bills'));
  456. }
  457. public function erx_and_orders(Request $request)
  458. {
  459. $erxAndOrders = Erx::paginate(5);
  460. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  461. }
  462. public function reports(Request $request)
  463. {
  464. $data = [];
  465. return view('app.mcp.reports', $data);
  466. }
  467. public function supply_orders(Request $request)
  468. {
  469. $supplyOrders = SupplyOrder::paginate(5);
  470. return view('app.mcp.supply_orders', compact('supplyOrders'));
  471. }
  472. public function getCreateNewPatientScriptTemplate(Request $request){
  473. $template = $request->get('template');
  474. if(!$template) return $this->fail('No script template');
  475. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  476. if(!File::exists($path)) return $this->fail('Invalid script template');
  477. $templateContent = file_get_contents($path);
  478. return $this->pass($templateContent);
  479. }
  480. public function bdtDevices(Request $request)
  481. {
  482. $filters = $request->all();
  483. $bdtDevices = BDTDevice::query();
  484. $imei = $request->input('imei');
  485. if($imei){
  486. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  487. }
  488. $client = $request->input('client');
  489. if($client){
  490. $client = '%'.$client.'%';
  491. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  492. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  493. return $clientQuery->where(function($q) use ($client){
  494. return $q->where('name_first', 'ilike', $client)
  495. ->orWhere('name_last', 'ilike', $client)
  496. ->orWhere('cell_number', 'ilike', $client)
  497. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  498. });
  499. });
  500. });
  501. }
  502. $is_issued = $request->input('is_issued');
  503. if($is_issued){
  504. if($is_issued == 'YES'){
  505. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  506. }
  507. if($is_issued == 'NO'){
  508. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  509. }
  510. }
  511. $is_issued = $request->input('is_issued');
  512. if($is_issued){
  513. if($is_issued == 'YES'){
  514. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  515. }
  516. if($is_issued == 'NO'){
  517. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  518. }
  519. }
  520. $mcp = $request->input('mcp');
  521. if($mcp){
  522. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  523. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  524. $mcpPro = Pro::where('uid', $mcp)->first();
  525. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  526. });
  527. });
  528. }
  529. $bdtDevices = $bdtDevices->paginate(20);
  530. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  531. }
  532. public function patientsMissingDefasultSettings(Request $request){
  533. $filters = $request->all();
  534. $patients = Client::whereNull('shadow_pro_id');
  535. $patients = $patients->where(function($qry){
  536. 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');
  537. });
  538. if ($request->input('name')) {
  539. $name = trim($request->input('name'));
  540. if ($name) {
  541. $patients = $patients->where(function ($q) use ($name) {
  542. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  543. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  544. });
  545. }
  546. }
  547. if ($request->input('mcp')) {
  548. if($request->input('mcp') == 'NO_MCP'){
  549. $patients = $patients->whereNull('mcp_pro_id');
  550. }else{
  551. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  552. if ($mcp) {
  553. $patients = $patients->where('mcp_pro_id', $mcp->id);
  554. }
  555. }
  556. }
  557. if ($request->input('chart_number')) {
  558. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  559. }
  560. $status = $request->input('status');
  561. if($status){
  562. if($status == 'ACTIVE'){
  563. $patients->where('is_active', true)->where(function($q) use ($status){
  564. return $q->where('client_engagement_status_category', $status)
  565. ->orWhereNull('client_engagement_status_category');
  566. });
  567. }else {
  568. $patients->where('client_engagement_status_category', $status);
  569. }
  570. }
  571. $insurance = $request->get('insurance');
  572. if($insurance){
  573. if($insurance === 'MEDICARE'){
  574. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  575. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  576. });
  577. }elseif($insurance === 'MEDICARE_PENDING'){
  578. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  579. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  580. });
  581. }elseif($insurance === 'NOT_COVERED'){
  582. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  583. return $cpcQuery->where('is_covered', '!=', 'YES');
  584. });
  585. }elseif($insurance === 'PENDING'){
  586. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  587. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  588. });
  589. }
  590. else{
  591. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  592. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  593. });
  594. }
  595. }
  596. $missing_default_settings = $request->get('missing_default_settings');
  597. if($missing_default_settings){
  598. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  599. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  600. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  601. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  602. }
  603. $care_plan = $request->get('care_plan');
  604. if($care_plan){
  605. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  606. $patients = $patients->whereHas('notes', function($noteQuery){
  607. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  608. });
  609. }
  610. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  611. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  612. }
  613. }
  614. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  615. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  616. }
  617. public function points(Request $request){
  618. $filters = $request->all();
  619. $points = Point::query();
  620. $points = $points->where('is_removed','!=', true);
  621. if ($request->input('name')) {
  622. $name = trim($request->input('name'));
  623. if ($name) {
  624. $points = $points->whereHas('client', function ($q) use ($name) {
  625. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  626. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  627. });
  628. }
  629. }
  630. if ($request->input('mcp')) {
  631. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  632. if ($mcp) {
  633. $points = $points->where('created_by_pro_id', $mcp->id);
  634. }
  635. }
  636. $points = $points->paginate(30);
  637. $_point = new Point;
  638. $tableName = $_point->getTable();
  639. $columns = Schema::getColumnListing($tableName);
  640. return view('app.admin.points.index', compact('points', 'filters', 'columns'));
  641. }
  642. }