AdminController.php 37 KB

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