AdminController.php 39 KB

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