AdminController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Lead;
  4. use App\Models\Appointment;
  5. use App\Models\BDTDevice;
  6. use App\Models\CareMonth;
  7. use App\Models\Client;
  8. use App\Models\ClientBDTDevice;
  9. use App\Models\ClientInfoLine;
  10. use App\Models\Erx;
  11. use App\Models\Facility;
  12. use App\Models\Handout;
  13. use App\Models\IncomingReport;
  14. use App\Models\InternalMessage;
  15. use App\Models\MBClaim;
  16. use App\Models\MBPayer;
  17. use App\Models\Note;
  18. use App\Models\NoteTemplate;
  19. use App\Models\Pro;
  20. use App\Models\Product;
  21. use App\Models\ProProAccess;
  22. use App\Models\SectionTemplate;
  23. use App\Models\Shipment;
  24. use App\Models\SupplyOrder;
  25. use App\Models\Ticket;
  26. use Illuminate\Http\Request;
  27. use Illuminate\Support\Facades\DB;
  28. use Illuminate\Support\Facades\File;
  29. use App\Models\Bill;
  30. use App\Models\ClientSMS;
  31. use App\Models\Point;
  32. use Illuminate\Support\Facades\Http;
  33. use PDF;
  34. use Illuminate\Support\Facades\Schema;
  35. use App\Models\AdminPatient;
  36. use App\Models\SupplyOrderView;
  37. use Illuminate\Pagination\LengthAwarePaginator;
  38. class AdminController extends Controller
  39. {
  40. public function patients(Request $request)
  41. {
  42. // DB::enableQueryLog();
  43. $filters = $request->all();
  44. $patients = AdminPatient::whereNull('shadow_pro_id');
  45. if ($request->input('name')) {
  46. $name = trim($request->input('name'));
  47. if ($name) {
  48. $patients = $patients->where(function ($q) use ($name) {
  49. $q->where('display_name', 'ILIKE', '%' . $name . '%');
  50. });
  51. }
  52. }
  53. if ($request->input('mcp')) {
  54. if($request->input('mcp') == 'NO_MCP'){
  55. $patients = $patients->whereNull('mcp_pro_id');
  56. }else{
  57. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  58. if ($mcp) {
  59. $patients = $patients->where('mcp_pro_id', $mcp->id);
  60. }
  61. }
  62. }
  63. if ($request->input('na')) {
  64. if($request->input('na') == 'NO_NA'){
  65. $patients = $patients->whereNull('default_na_pro_id');
  66. }else{
  67. $na = Pro::where('uid', trim($request->input('na')))->first();
  68. if ($na) {
  69. $patients = $patients->where('default_na_pro_id', $na->id);
  70. }
  71. }
  72. }
  73. if ($request->input('ob')) {
  74. if ($request->input('ob') == 'yes') {
  75. $patients = $patients->where('has_mcp_done_onboarding_visit', 'YES');
  76. } else {
  77. $patients = $patients->where('has_mcp_done_onboarding_visit', '!=', 'YES');
  78. }
  79. }
  80. if ($request->input('next_appointment_category')) {
  81. if($request->input('next_appointment_category') == 'NONE'){
  82. $patients = $patients->whereNull('next_mcp_appointment_id');
  83. }else{
  84. $self = $this;
  85. $patients = $patients->whereHas('nextMcpAppointment', function($pQry) use ($request, $self){
  86. return $self->filterMultiQuery($request, $pQry, 'raw_date', 'next_appointment_category', 'next_appointment_value_1', 'next_appointment_value_2');
  87. });
  88. }
  89. }
  90. if ($request->input('chart_number')) {
  91. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  92. }
  93. if ($request->input('home_address_state')) {
  94. if($request->input('home_address_state') == 'NONE'){
  95. $patients = $patients->whereRaw("mailing_address_state IS NULL OR TRIM(BOTH FROM mailing_address_state = ''");
  96. }else if($request->input('home_address_state') == 'NOT_MD'){
  97. $patients = $patients->whereRaw("(TRIM(BOTH FROM mailing_address_state) NOT ILIKE 'MD' AND TRIM(BOTH FROM mailing_address_state) NOT ILIKE 'MARYLAND')");
  98. }else{
  99. $patients = $patients->whereRaw("TRIM(BOTH FROM mailing_address_state) = '" . $request->input('home_address_state') . "'");
  100. }
  101. }
  102. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  103. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  104. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  105. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  106. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  107. $this->filterMultiQuery($request, $patients, 'created_at', 'created_at', 'created_at_value_1', 'created_at_value_2');
  108. $this->filterMultiQuery($request, $patients, 'most_recent_completed_mcp_note_date', 'last_visit_category', 'last_visit_value_1', 'last_visit_value_2');
  109. $fVal = $request->input('has_email');
  110. if($fVal) {
  111. if($fVal === 'YES') {
  112. $patients = $patients->whereRaw("(email_address IS NOT NULL AND TRIM(email_address) != '')");
  113. }
  114. else {
  115. $patients = $patients->whereRaw("(email_address IS NULL OR TRIM(email_address) = '')");
  116. }
  117. }
  118. $fVal = $request->input('has_account');
  119. if($fVal) {
  120. if($fVal === 'YES') {
  121. $patients = $patients->whereRaw("((SELECT COUNT(ac.id) FROM account_client ac WHERE ac.client_id = admin_patient_list.id) > 0)");
  122. }
  123. else {
  124. $patients = $patients->whereRaw("((SELECT COUNT(ac.id) FROM account_client ac WHERE ac.client_id = admin_patient_list.id) = 0)");
  125. }
  126. }
  127. $fVal = $request->input('has_default_mcp_company_pro');
  128. if($fVal) {
  129. if($fVal === 'YES') {
  130. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NOT NULL)");
  131. }
  132. else {
  133. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NULL)");
  134. }
  135. }
  136. $fVal = $request->input('has_default_mcp_company_pro_payer');
  137. if($fVal) {
  138. if($fVal === 'YES') {
  139. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NOT NULL)");
  140. }
  141. else {
  142. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NULL)");
  143. }
  144. }
  145. $fVal = $request->input('has_default_mcp_company_location');
  146. if($fVal) {
  147. if($fVal === 'YES') {
  148. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NOT NULL)");
  149. }
  150. else {
  151. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NULL)");
  152. }
  153. }
  154. $fVal = $request->input('has_bp_device');
  155. if($fVal) {
  156. if($fVal === 'YES') {
  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 = admin_patient_list.id) > 0)");
  160. }
  161. else {
  162. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  163. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  164. "WHERE so.product_id = 1 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  165. }
  166. }
  167. $fVal = $request->input('has_weight_scale');
  168. if($fVal) {
  169. if($fVal === 'YES') {
  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 = admin_patient_list.id) > 0)");
  173. }
  174. else {
  175. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  176. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  177. "WHERE so.product_id = 2 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  178. }
  179. }
  180. $fVal = $request->input('has_pulse_ox');
  181. if($fVal) {
  182. if($fVal === 'YES') {
  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 = admin_patient_list.id) > 0)");
  186. }
  187. else {
  188. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  189. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  190. "WHERE so.product_id = 3 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  191. }
  192. }
  193. $fVal = $request->input('has_temp_fun');
  194. if($fVal) {
  195. if($fVal === 'YES') {
  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 = admin_patient_list.id) > 0)");
  199. }
  200. else {
  201. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  202. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  203. "WHERE so.product_id = 4 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  204. }
  205. }
  206. $fVal = $request->input('imei');
  207. if($fVal) {
  208. $patients = $patients->whereRaw("((SELECT count(cbd.id) FROM client_bdt_device cbd
  209. WHERE cbd.client_id = admin_patient_list.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)");
  210. }
  211. if($request->input('number_of_measurements')){
  212. $keyName = $request->input('number_of_measurements');
  213. $measurementCountQuery = '(SELECT COUNT(*) FROM measurement WHERE measurement.client_id = admin_patient_list.id AND is_active IS TRUE AND is_cellular IS TRUE AND is_cellular_zero IS NOT TRUE)';
  214. switch($keyName) {
  215. case 'EXACTLY':
  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 'LESS_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 'GREATER_THAN':
  226. if($request->input('number_of_measurements_value_1')) {
  227. $patients->whereRaw($measurementCountQuery . '>='.$request->input('number_of_measurements_value_1'));
  228. }
  229. break;
  230. case 'BETWEEN':
  231. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  232. $patients->whereRaw($measurementCountQuery.'>='.$request->input('number_of_measurements_value_1') .' AND '. $measurementCountQuery . '<='.$request->input('number_of_measurements_value_2'));
  233. }
  234. break;
  235. case 'NOT_BETWEEN':
  236. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  237. $patients->where(function ($q) use ($request, $measurementCountQuery) {
  238. $q->whereRaw($measurementCountQuery . '<'.$request->input('number_of_measurements_value_1') .' OR '. $measurementCountQuery . '>'.$request->input('number_of_measurements_value_2'));
  239. });
  240. }
  241. break;
  242. }
  243. }
  244. $status = $request->input('status');
  245. if($status){
  246. if($status === 'ACTIVE'){
  247. $patients->where('is_active', true)->where(function($q) use ($status){
  248. return $q->where('client_engagement_status_category', $status)
  249. ->orWhereNull('client_engagement_status_category');
  250. });
  251. }elseif($status === 'NONE'){
  252. $patients->whereNull('client_engagement_status_category');
  253. }else {
  254. $patients->where('client_engagement_status_category', $status);
  255. }
  256. }
  257. $initiative = $request->input('initiative');
  258. if($initiative){
  259. $wildCardedInitiative = '%'.$initiative.'%';
  260. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  261. }
  262. $include_test_records = $request->input('include_test_records');
  263. if(!$include_test_records && $status != 'DUMMY'){
  264. $patients = $patients->where(function ($q) {
  265. $q->whereNull('client_engagement_status_category')
  266. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  267. });
  268. }
  269. $zero_deductible = $request->input('zero_deductible');
  270. if($zero_deductible){
  271. $patients = $patients->where(function ($q) {
  272. $q->where('mpb_remaining', 0);
  273. });
  274. }
  275. $insurance = $request->get('insurance');
  276. if($insurance){
  277. if(strpos($insurance, '_new_|') === 0){
  278. $trimmed = trim(str_replace('_new_|', '', $insurance));
  279. $condSql = "EXISTS(SELECT 1 FROM insurance_card WHERE client_id = admin_patient_list.id AND is_active = TRUE AND (carrier_category ILIKE '%{$trimmed}%' OR carrier_name ILIKE '%{$trimmed}%'))";
  280. $patients = $patients->whereRaw($condSql);
  281. }
  282. else {
  283. if($insurance === 'MEDICARE'){
  284. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) {
  285. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  286. });
  287. }else{
  288. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) use ($insurance) {
  289. return $cpcQuery->where('commercial_payer_id', '=', $insurance);
  290. });
  291. }
  292. }
  293. }
  294. $companyID = $request->get('company');
  295. if($companyID){
  296. if($companyID == 'NONE'){
  297. $patients = $patients->doesntHave('companyClients');
  298. }else{
  299. $patients = $patients->whereHas('companyClients', function($qry)use($companyID){
  300. if($companyID != 'ANY'){
  301. return $qry->where('company_id', $companyID);
  302. }
  303. });
  304. }
  305. }
  306. // search by tag
  307. if ($request->input('tags')) {
  308. $tags = trim($request->input('tags'));
  309. if ($tags) {
  310. try {
  311. $condSql = "((SELECT COUNT(ct.id) FROM client_tag ct WHERE ct.client_id = admin_patient_list.id AND UPPER(ct.tag) LIKE '%" . strtoupper($tags) . "%') > 0)";
  312. $patients = $patients->whereRaw($condSql);
  313. }
  314. catch (\Exception $e) {
  315. dd($e->getMessage());
  316. }
  317. }
  318. }
  319. $sortBy = $request->input('sort_by') ?: 'name_first';
  320. $sortDir = $request->input('sort_dir') ?: 'ASC';
  321. $sortBySQL = "$sortBy $sortDir NULLS LAST";
  322. if($sortBy !== 'client_engagement_status_category' && $request->input('status')) {
  323. $sortBySQL = "client_engagement_status_category DESC NULLS LAST";
  324. }
  325. if(@$filters['mapView'] == 1){
  326. $patients = $patients->orderByRaw($sortBySQL)->paginate(100);
  327. }else{
  328. $patients = $patients->orderByRaw($sortBySQL)->paginate(25);
  329. }
  330. $oldInsurances = 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');
  331. $newInsurances = DB::select("select distinct COALESCE(ic.carrier_name, ic.carrier_category) as payer_name from insurance_card ic");
  332. // $qLog = DB::getQueryLog();
  333. // dd($qLog);
  334. return view('app.admin.patients', compact('patients', 'filters', 'oldInsurances', 'newInsurances'));
  335. }
  336. public function partBPatients(Request $request){
  337. $filters = $request->all();
  338. $patients = Client::whereNull('shadow_pro_id');
  339. if ($request->input('name')) {
  340. $name = trim($request->input('name'));
  341. if ($name) {
  342. $patients = $patients->where(function ($q) use ($name) {
  343. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  344. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  345. });
  346. }
  347. }
  348. if ($request->input('mcp')) {
  349. if($request->input('mcp') == 'NO_MCP'){
  350. $patients = $patients->whereNull('mcp_pro_id');
  351. }else{
  352. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  353. if ($mcp) {
  354. $patients = $patients->where('mcp_pro_id', $mcp->id);
  355. }
  356. }
  357. }
  358. if ($request->input('na')) {
  359. if($request->input('na') == 'NO_NA'){
  360. $patients = $patients->whereNull('default_na_pro_id');
  361. }else{
  362. $na = Pro::where('uid', trim($request->input('na')))->first();
  363. if ($na) {
  364. $patients = $patients->where('default_na_pro_id', $na->id);
  365. }
  366. }
  367. }
  368. if ($request->input('next_appointment_category')) {
  369. if($request->input('next_appointment_category') == 'NONE'){
  370. $patients = $patients->whereNull('next_mcp_appointment_id');
  371. }
  372. }
  373. if ($request->input('chart_number')) {
  374. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  375. }
  376. if ($request->input('home_address_state')) {
  377. if($request->input('home_address_state') == 'NONE'){
  378. $patients = $patients->whereNull('mailing_address_state');
  379. }else if($request->input('home_address_state') == 'NOT_MD'){
  380. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  381. }else{
  382. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  383. }
  384. }
  385. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  386. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  387. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  388. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  389. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  390. if($request->input('deductible')){
  391. $keyName = $request->input('deductible');
  392. switch($keyName) {
  393. case 'EXACTLY':
  394. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  395. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  396. });
  397. break;
  398. case 'LESS_THAN':
  399. if($request->input('deductible_value_1')) {
  400. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  401. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  402. });
  403. }
  404. break;
  405. case 'GREATER_THAN':
  406. if($request->input('deductible_value_1')) {
  407. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  408. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  409. });
  410. }
  411. break;
  412. case 'BETWEEN':
  413. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  414. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  415. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  416. });
  417. break;
  418. case 'NOT_BETWEEN':
  419. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  420. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  421. return $q->where(function($qq) use ($request){
  422. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  423. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  424. });
  425. });
  426. }
  427. break;
  428. }
  429. }
  430. switch($request->input('status')) {
  431. case 'ACTIVE':
  432. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  433. break;
  434. case 'AWAITING_VISIT':
  435. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  436. break;
  437. case 'INACTIVE':
  438. $patients->where('is_active', '<>', true);
  439. break;
  440. }
  441. $initiative = $request->input('initiative');
  442. if($initiative){
  443. $wildCardedInitiative = '%'.$initiative.'%';
  444. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  445. }
  446. $include_test_records = $request->input('include_test_records');
  447. if(!$include_test_records){
  448. $patients = $patients->where(function ($q) {
  449. return $q->whereNull('client_engagement_status_category')
  450. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  451. });
  452. }
  453. $with_claim_not_closed = $request->input('with_claim_not_closed');
  454. if($with_claim_not_closed){
  455. $patients = $patients->whereHas('notes', function ($q) {
  456. return $q->where('is_claim_closed', false)
  457. ->where('is_signed_by_hcp', true)
  458. ->where('is_cancelled', false);
  459. });
  460. }
  461. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  462. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  463. });
  464. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  465. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  466. }
  467. public function leads(Request $request){
  468. $leads = Lead::whereNotNull('created_at');
  469. $leads = $leads->orderBy('created_at', 'DESC')->paginate(25);
  470. return view('app.admin.leads', compact('leads'));
  471. }
  472. public function notes(Request $request)
  473. {
  474. $notes = Note::paginate(5);
  475. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  476. return view('app.mcp.notes', compact('notes'));
  477. }
  478. public function notes_pending_summary_suggestion(Request $request){
  479. $pro = $this->performer->pro;
  480. $data = [
  481. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  482. ];
  483. return view('app.admin.notes_pending_summary_suggestion', $data);
  484. }
  485. public function notes_rejected_summary_suggestion(Request $request){
  486. $pro = $this->performer->pro;
  487. $data = [
  488. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  489. ];
  490. return view('app.admin.notes_rejected_summary_suggestion', $data);
  491. }
  492. public function appointments(Request $request)
  493. {
  494. $appointments = Appointment::paginate(5);
  495. return view('app.mcp.appointments', compact('appointments'));
  496. }
  497. public function bills(Request $request)
  498. {
  499. $bills = Bill::paginate(5);
  500. return view('app.mcp.bills', compact('bills'));
  501. }
  502. public function erx_and_orders(Request $request)
  503. {
  504. $erxAndOrders = Erx::paginate(5);
  505. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  506. }
  507. public function reports(Request $request)
  508. {
  509. $data = [];
  510. return view('app.mcp.reports', $data);
  511. }
  512. public function supply_orders(Request $request)
  513. {
  514. $supplyOrders = SupplyOrderView::paginate(5);
  515. return view('app.mcp.supply_orders', compact('supplyOrders'));
  516. }
  517. public function getCreateNewPatientScriptTemplate(Request $request){
  518. $template = $request->get('template');
  519. if(!$template) return $this->fail('No script template');
  520. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  521. if(!File::exists($path)) return $this->fail('Invalid script template');
  522. $templateContent = file_get_contents($path);
  523. return $this->pass($templateContent);
  524. }
  525. public function bdtDevices(Request $request)
  526. {
  527. $filters = $request->all();
  528. $bdtDevices = BDTDevice::query();
  529. $imei = $request->input('imei');
  530. if($imei){
  531. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  532. }
  533. $client = $request->input('client');
  534. if($client){
  535. $client = '%'.$client.'%';
  536. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  537. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  538. return $clientQuery->where(function($q) use ($client){
  539. return $q->where('name_first', 'ilike', $client)
  540. ->orWhere('name_last', 'ilike', $client)
  541. ->orWhere('cell_number', 'ilike', $client)
  542. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  543. });
  544. });
  545. });
  546. }
  547. $is_issued = $request->input('is_issued');
  548. if($is_issued){
  549. if($is_issued == 'YES'){
  550. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  551. }
  552. if($is_issued == 'NO'){
  553. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  554. }
  555. }
  556. $is_issued = $request->input('is_issued');
  557. if($is_issued){
  558. if($is_issued == 'YES'){
  559. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  560. }
  561. if($is_issued == 'NO'){
  562. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  563. }
  564. }
  565. $mcp = $request->input('mcp');
  566. if($mcp){
  567. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  568. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  569. $mcpPro = Pro::where('uid', $mcp)->first();
  570. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  571. });
  572. });
  573. }
  574. $bdtDevices = $bdtDevices->paginate(20);
  575. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  576. }
  577. public function patientsMissingDefasultSettings(Request $request){
  578. $filters = $request->all();
  579. $patients = Client::whereNull('shadow_pro_id');
  580. $patients = $patients->where(function($qry){
  581. 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');
  582. });
  583. if ($request->input('name')) {
  584. $name = trim($request->input('name'));
  585. if ($name) {
  586. $patients = $patients->where(function ($q) use ($name) {
  587. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  588. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  589. });
  590. }
  591. }
  592. if ($request->input('mcp')) {
  593. if($request->input('mcp') == 'NO_MCP'){
  594. $patients = $patients->whereNull('mcp_pro_id');
  595. }else{
  596. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  597. if ($mcp) {
  598. $patients = $patients->where('mcp_pro_id', $mcp->id);
  599. }
  600. }
  601. }
  602. if ($request->input('chart_number')) {
  603. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  604. }
  605. $status = $request->input('status');
  606. if($status){
  607. if($status == 'ACTIVE'){
  608. $patients->where('is_active', true)->where(function($q) use ($status){
  609. return $q->where('client_engagement_status_category', $status)
  610. ->orWhereNull('client_engagement_status_category');
  611. });
  612. }else {
  613. $patients->where('client_engagement_status_category', $status);
  614. }
  615. }
  616. $insurance = $request->get('insurance');
  617. if($insurance){
  618. if($insurance === 'MEDICARE'){
  619. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  620. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  621. });
  622. }elseif($insurance === 'MEDICARE_PENDING'){
  623. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  624. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  625. });
  626. }elseif($insurance === 'NOT_COVERED'){
  627. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  628. return $cpcQuery->where('is_covered', '!=', 'YES');
  629. });
  630. }elseif($insurance === 'PENDING'){
  631. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  632. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  633. });
  634. }
  635. else{
  636. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  637. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  638. });
  639. }
  640. }
  641. $missing_default_settings = $request->get('missing_default_settings');
  642. if($missing_default_settings){
  643. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  644. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  645. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  646. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  647. }
  648. $care_plan = $request->get('care_plan');
  649. if($care_plan){
  650. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  651. $patients = $patients->whereHas('notes', function($noteQuery){
  652. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  653. });
  654. }
  655. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  656. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  657. }
  658. }
  659. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  660. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  661. }
  662. public function points(Request $request)
  663. {
  664. $filters = $request->all();
  665. $points = Point::query();
  666. $points = $points->where('is_removed', '!=', true)
  667. ->where('category', '!=', 'REVIEW')
  668. ->where('category', '!=', 'PLAN');
  669. $intentions = DB::select("SELECT intention, COUNT(intention) as count FROM point WHERE intention is not null and is_removed is not true and category != 'REVIEW' and category != 'PLAN' GROUP BY intention ORDER BY intention ASC");
  670. $categories = [];
  671. $names = [];
  672. if ($request->input('mcp')) {
  673. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  674. if ($mcp) {
  675. // $points = $points->where('created_by_pro_id', $mcp->id);
  676. }
  677. }
  678. $implodedIntentions = null;
  679. if ($request->input('intentions')) {
  680. $points = $points->whereIn('intention', $request->input('intentions'));
  681. $implodedIntentions = join("','", $request->input('intentions'));
  682. $categories = DB::select("SELECT category, COUNT(id) as count FROM point WHERE intention in ('" . $implodedIntentions . "') and is_removed is not true and category != 'REVIEW' and category != 'PLAN' GROUP BY category ORDER BY category ASC");
  683. }
  684. if ($request->input('categories')) {
  685. $points = $points->whereIn('category', $request->input('categories'));
  686. $implodedCategories = join("','", $request->input('categories'));
  687. $names = DB::select("select (data::json)->>'name' as name, count(id) as count from point where category in ('".$implodedCategories."') and is_removed is not true and intention in ('".$implodedIntentions."') and category != 'REVIEW' and category != 'PLAN' group by 1 order by 1 asc");
  688. }
  689. if ($request->input('names')){
  690. $implodedNames = join("','", $request->input('names'));
  691. $points = $points->whereRaw("(data::json)->>'name' in ('".$implodedNames."')");
  692. }
  693. $points = $points->paginate(30);
  694. $_point = new Point;
  695. $tableName = $_point->getTable();
  696. $columns = Schema::getColumnListing($tableName);
  697. return view('app.admin.points.index', compact('points', 'filters', 'columns', 'intentions', 'categories', 'names'));
  698. }
  699. public function pointDetails(Request $request, $uid){
  700. $point = Point::where('uid', $uid)->first();
  701. $tableName = $point->getTable();
  702. $columns = Schema::getColumnListing($tableName);
  703. return view('app.admin.points.record-details', compact('point', 'columns'));
  704. }
  705. public function messages(Request $request)
  706. {
  707. $messages = InternalMessage::orderBy('created_at', 'desc')->paginate(50);
  708. return view('app.admin.messages', compact('messages'));
  709. }
  710. public function patientsNotesPointsFilter(Request $request){
  711. $filters = $request->all();
  712. $searchArrayStrings = null;
  713. $searchString = $request->get('string');
  714. if($searchString){
  715. $searchStrings = explode(',', $searchString);
  716. $searchArray = [];
  717. foreach($searchStrings as $string){
  718. $s = "'" . '%'.strtolower(trim($string)).'%'. "'";
  719. array_push($searchArray, $s);
  720. }
  721. $searchArrayStrings = implode(',', $searchArray);
  722. }
  723. //Query distinct clients who have points that contain specific substrings in the point.data
  724. $qry = "
  725. SELECT
  726. DISTINCT ON (p.client_id)
  727. p.client_id,
  728. p.id,
  729. p.uid,
  730. c.uid as client_uid,
  731. c.chart_number,
  732. c.cell_number,
  733. c.phone_home,
  734. c.phone_mobile,
  735. c.phone_work,
  736. c.email_address,
  737. c.dob,
  738. (c.name_first ||' '||c.name_last) as patient_name,
  739. (mcp.name_first ||' '||mcp.name_last) as mcp_name,
  740. c.most_recent_completed_mcp_note_date as last_visit_date,
  741. (cover.plan_type) as cover
  742. FROM
  743. point p
  744. LEFT JOIN client c on c.id = p.client_id
  745. LEFT JOIN pro mcp on mcp.id = c.mcp_pro_id
  746. LEFT JOIN client_primary_coverage cover on cover.client_id = c.id
  747. ";
  748. if($searchArrayStrings){
  749. $qry = $qry . " WHERE lower(p.data) ILIKE any (array[".$searchArrayStrings."])";
  750. }
  751. $qry = $qry . " ORDER BY p.client_id DESC, p.created_at DESC";
  752. $records = null;
  753. if($searchArrayStrings){
  754. $records = DB::select($qry);
  755. $page = $request->get('page', 1);
  756. $size = 20;
  757. $collect = collect($records);
  758. $records = new LengthAwarePaginator(
  759. $collect->forPage($page, $size),
  760. $collect->count(),
  761. $size,
  762. $page
  763. );
  764. $records->setPath(route('admin.patients-notes-points-filter'));
  765. }
  766. return view('app.admin.patients-notes-points-filter', compact('records', 'filters'));
  767. }
  768. }