AdminController.php 36 KB

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