AdminController.php 37 KB

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