AdminController.php 39 KB

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