AdminController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. $sortBy = $request->input('sort_by') ?: 'name_first';
  298. $sortDir = $request->input('sort_dir') ?: 'ASC';
  299. $sortBySQL = "$sortBy $sortDir NULLS LAST";
  300. if($sortBy !== 'client_engagement_status_category' && $request->input('status')) {
  301. $sortBySQL = "client_engagement_status_category DESC NULLS LAST";
  302. }
  303. if(@$filters['mapView'] == 1){
  304. $patients = $patients->orderByRaw($sortBySQL)->paginate(100);
  305. }else{
  306. $patients = $patients->orderByRaw($sortBySQL)->paginate(25);
  307. }
  308. $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');
  309. return view('app.admin.patients', compact('patients', 'filters', 'insurances'));
  310. }
  311. public function partBPatients(Request $request){
  312. $filters = $request->all();
  313. $patients = Client::whereNull('shadow_pro_id');
  314. if ($request->input('name')) {
  315. $name = trim($request->input('name'));
  316. if ($name) {
  317. $patients = $patients->where(function ($q) use ($name) {
  318. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  319. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  320. });
  321. }
  322. }
  323. if ($request->input('mcp')) {
  324. if($request->input('mcp') == 'NO_MCP'){
  325. $patients = $patients->whereNull('mcp_pro_id');
  326. }else{
  327. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  328. if ($mcp) {
  329. $patients = $patients->where('mcp_pro_id', $mcp->id);
  330. }
  331. }
  332. }
  333. if ($request->input('na')) {
  334. if($request->input('na') == 'NO_NA'){
  335. $patients = $patients->whereNull('default_na_pro_id');
  336. }else{
  337. $na = Pro::where('uid', trim($request->input('na')))->first();
  338. if ($na) {
  339. $patients = $patients->where('default_na_pro_id', $na->id);
  340. }
  341. }
  342. }
  343. if ($request->input('next_appointment_category')) {
  344. if($request->input('next_appointment_category') == 'NONE'){
  345. $patients = $patients->whereNull('next_mcp_appointment_id');
  346. }
  347. }
  348. if ($request->input('chart_number')) {
  349. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  350. }
  351. if ($request->input('home_address_state')) {
  352. if($request->input('home_address_state') == 'NONE'){
  353. $patients = $patients->whereNull('mailing_address_state');
  354. }else if($request->input('home_address_state') == 'NOT_MD'){
  355. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  356. }else{
  357. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  358. }
  359. }
  360. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  361. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  362. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  363. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  364. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  365. if($request->input('deductible')){
  366. $keyName = $request->input('deductible');
  367. switch($keyName) {
  368. case 'EXACTLY':
  369. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  370. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  371. });
  372. break;
  373. case 'LESS_THAN':
  374. if($request->input('deductible_value_1')) {
  375. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  376. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  377. });
  378. }
  379. break;
  380. case 'GREATER_THAN':
  381. if($request->input('deductible_value_1')) {
  382. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  383. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  384. });
  385. }
  386. break;
  387. case 'BETWEEN':
  388. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  389. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  390. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  391. });
  392. break;
  393. case 'NOT_BETWEEN':
  394. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  395. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  396. return $q->where(function($qq) use ($request){
  397. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  398. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  399. });
  400. });
  401. }
  402. break;
  403. }
  404. }
  405. switch($request->input('status')) {
  406. case 'ACTIVE':
  407. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  408. break;
  409. case 'AWAITING_VISIT':
  410. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  411. break;
  412. case 'INACTIVE':
  413. $patients->where('is_active', '<>', true);
  414. break;
  415. }
  416. $initiative = $request->input('initiative');
  417. if($initiative){
  418. $wildCardedInitiative = '%'.$initiative.'%';
  419. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  420. }
  421. $include_test_records = $request->input('include_test_records');
  422. if(!$include_test_records){
  423. $patients = $patients->where(function ($q) {
  424. return $q->whereNull('client_engagement_status_category')
  425. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  426. });
  427. }
  428. $with_claim_not_closed = $request->input('with_claim_not_closed');
  429. if($with_claim_not_closed){
  430. $patients = $patients->whereHas('notes', function ($q) {
  431. return $q->where('is_claim_closed', false)
  432. ->where('is_signed_by_hcp', true)
  433. ->where('is_cancelled', false);
  434. });
  435. }
  436. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  437. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  438. });
  439. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  440. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  441. }
  442. public function notes(Request $request)
  443. {
  444. $notes = Note::paginate(5);
  445. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  446. return view('app.mcp.notes', compact('notes'));
  447. }
  448. public function notes_pending_summary_suggestion(Request $request){
  449. $pro = $this->performer->pro;
  450. $data = [
  451. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  452. ];
  453. return view('app.admin.notes_pending_summary_suggestion', $data);
  454. }
  455. public function notes_rejected_summary_suggestion(Request $request){
  456. $pro = $this->performer->pro;
  457. $data = [
  458. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  459. ];
  460. return view('app.admin.notes_rejected_summary_suggestion', $data);
  461. }
  462. public function appointments(Request $request)
  463. {
  464. $appointments = Appointment::paginate(5);
  465. return view('app.mcp.appointments', compact('appointments'));
  466. }
  467. public function bills(Request $request)
  468. {
  469. $bills = Bill::paginate(5);
  470. return view('app.mcp.bills', compact('bills'));
  471. }
  472. public function erx_and_orders(Request $request)
  473. {
  474. $erxAndOrders = Erx::paginate(5);
  475. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  476. }
  477. public function reports(Request $request)
  478. {
  479. $data = [];
  480. return view('app.mcp.reports', $data);
  481. }
  482. public function supply_orders(Request $request)
  483. {
  484. $supplyOrders = SupplyOrderView::paginate(5);
  485. return view('app.mcp.supply_orders', compact('supplyOrders'));
  486. }
  487. public function getCreateNewPatientScriptTemplate(Request $request){
  488. $template = $request->get('template');
  489. if(!$template) return $this->fail('No script template');
  490. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  491. if(!File::exists($path)) return $this->fail('Invalid script template');
  492. $templateContent = file_get_contents($path);
  493. return $this->pass($templateContent);
  494. }
  495. public function bdtDevices(Request $request)
  496. {
  497. $filters = $request->all();
  498. $bdtDevices = BDTDevice::query();
  499. $imei = $request->input('imei');
  500. if($imei){
  501. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  502. }
  503. $client = $request->input('client');
  504. if($client){
  505. $client = '%'.$client.'%';
  506. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  507. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  508. return $clientQuery->where(function($q) use ($client){
  509. return $q->where('name_first', 'ilike', $client)
  510. ->orWhere('name_last', 'ilike', $client)
  511. ->orWhere('cell_number', 'ilike', $client)
  512. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  513. });
  514. });
  515. });
  516. }
  517. $is_issued = $request->input('is_issued');
  518. if($is_issued){
  519. if($is_issued == 'YES'){
  520. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  521. }
  522. if($is_issued == 'NO'){
  523. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  524. }
  525. }
  526. $is_issued = $request->input('is_issued');
  527. if($is_issued){
  528. if($is_issued == 'YES'){
  529. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  530. }
  531. if($is_issued == 'NO'){
  532. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  533. }
  534. }
  535. $mcp = $request->input('mcp');
  536. if($mcp){
  537. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  538. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  539. $mcpPro = Pro::where('uid', $mcp)->first();
  540. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  541. });
  542. });
  543. }
  544. $bdtDevices = $bdtDevices->paginate(20);
  545. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  546. }
  547. public function patientsMissingDefasultSettings(Request $request){
  548. $filters = $request->all();
  549. $patients = Client::whereNull('shadow_pro_id');
  550. $patients = $patients->where(function($qry){
  551. 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');
  552. });
  553. if ($request->input('name')) {
  554. $name = trim($request->input('name'));
  555. if ($name) {
  556. $patients = $patients->where(function ($q) use ($name) {
  557. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  558. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  559. });
  560. }
  561. }
  562. if ($request->input('mcp')) {
  563. if($request->input('mcp') == 'NO_MCP'){
  564. $patients = $patients->whereNull('mcp_pro_id');
  565. }else{
  566. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  567. if ($mcp) {
  568. $patients = $patients->where('mcp_pro_id', $mcp->id);
  569. }
  570. }
  571. }
  572. if ($request->input('chart_number')) {
  573. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  574. }
  575. $status = $request->input('status');
  576. if($status){
  577. if($status == 'ACTIVE'){
  578. $patients->where('is_active', true)->where(function($q) use ($status){
  579. return $q->where('client_engagement_status_category', $status)
  580. ->orWhereNull('client_engagement_status_category');
  581. });
  582. }else {
  583. $patients->where('client_engagement_status_category', $status);
  584. }
  585. }
  586. $insurance = $request->get('insurance');
  587. if($insurance){
  588. if($insurance === 'MEDICARE'){
  589. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  590. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  591. });
  592. }elseif($insurance === 'MEDICARE_PENDING'){
  593. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  594. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  595. });
  596. }elseif($insurance === 'NOT_COVERED'){
  597. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  598. return $cpcQuery->where('is_covered', '!=', 'YES');
  599. });
  600. }elseif($insurance === 'PENDING'){
  601. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  602. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  603. });
  604. }
  605. else{
  606. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  607. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  608. });
  609. }
  610. }
  611. $missing_default_settings = $request->get('missing_default_settings');
  612. if($missing_default_settings){
  613. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  614. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  615. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  616. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  617. }
  618. $care_plan = $request->get('care_plan');
  619. if($care_plan){
  620. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  621. $patients = $patients->whereHas('notes', function($noteQuery){
  622. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  623. });
  624. }
  625. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  626. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  627. }
  628. }
  629. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  630. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  631. }
  632. public function points(Request $request)
  633. {
  634. $filters = $request->all();
  635. $points = Point::query();
  636. $points = $points->where('is_removed', '!=', true)
  637. ->where('category', '!=', 'REVIEW')
  638. ->where('category', '!=', 'PLAN');
  639. $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");
  640. $categories = [];
  641. $names = [];
  642. if ($request->input('mcp')) {
  643. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  644. if ($mcp) {
  645. // $points = $points->where('created_by_pro_id', $mcp->id);
  646. }
  647. }
  648. $implodedIntentions = null;
  649. if ($request->input('intentions')) {
  650. $points = $points->whereIn('intention', $request->input('intentions'));
  651. $implodedIntentions = join("','", $request->input('intentions'));
  652. $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");
  653. }
  654. if ($request->input('categories')) {
  655. $points = $points->whereIn('category', $request->input('categories'));
  656. $implodedCategories = join("','", $request->input('categories'));
  657. $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");
  658. }
  659. if ($request->input('names')){
  660. $implodedNames = join("','", $request->input('names'));
  661. $points = $points->whereRaw("(data::json)->>'name' in ('".$implodedNames."')");
  662. }
  663. $points = $points->paginate(30);
  664. $_point = new Point;
  665. $tableName = $_point->getTable();
  666. $columns = Schema::getColumnListing($tableName);
  667. return view('app.admin.points.index', compact('points', 'filters', 'columns', 'intentions', 'categories', 'names'));
  668. }
  669. public function pointDetails(Request $request, $uid){
  670. $point = Point::where('uid', $uid)->first();
  671. $tableName = $point->getTable();
  672. $columns = Schema::getColumnListing($tableName);
  673. return view('app.admin.points.record-details', compact('point', 'columns'));
  674. }
  675. public function messages(Request $request)
  676. {
  677. $messages = InternalMessage::orderBy('created_at', 'desc')->paginate(50);
  678. return view('app.admin.messages', compact('messages'));
  679. }
  680. public function patientsNotesPointsFilter(Request $request){
  681. $filters = $request->all();
  682. $searchArrayStrings = null;
  683. $searchString = $request->get('string');
  684. if($searchString){
  685. $searchStrings = explode(',', $searchString);
  686. $searchArray = [];
  687. foreach($searchStrings as $string){
  688. $s = "'" . '%'.strtolower(trim($string)).'%'. "'";
  689. array_push($searchArray, $s);
  690. }
  691. $searchArrayStrings = implode(',', $searchArray);
  692. }
  693. $qry = "
  694. SELECT
  695. DISTINCT p.client_id,
  696. p.id,
  697. p.uid,
  698. c.uid as client_uid,
  699. c.chart_number,
  700. c.cell_number,
  701. c.phone_home,
  702. c.phone_mobile,
  703. c.phone_work,
  704. c.email_address,
  705. c.dob,
  706. (c.name_first ||' '||c.name_last) as patient_name,
  707. (mcp.name_first ||' '||mcp.name_last) as mcp_name,
  708. c.most_recent_completed_mcp_note_date as last_visit_date,
  709. (cover.plan_type) as cover
  710. FROM
  711. point p
  712. LEFT JOIN client c on c.id = p.client_id
  713. LEFT JOIN pro mcp on mcp.id = c.mcp_pro_id
  714. LEFT JOIN client_primary_coverage cover on cover.client_id = c.id
  715. ";
  716. if($searchArrayStrings){
  717. $qry = $qry . "WHERE lower(p.data) ILIKE any (array[".$searchArrayStrings."])";
  718. }
  719. $records = DB::select($qry);
  720. $page = $request->get('page', 1);
  721. $size = 20;
  722. $collect = collect($records);
  723. $records = new LengthAwarePaginator(
  724. $collect->forPage($page, $size),
  725. $collect->count(),
  726. $size,
  727. $page
  728. );
  729. $records->setPath(route('admin.patients-notes-points-filter'));
  730. return view('app.admin.patients-notes-points-filter', compact('records', 'filters'));
  731. }
  732. }