PracticeManagementController.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\CareMonth;
  6. use App\Models\ClaimEDI;
  7. use App\Models\Handout;
  8. use App\Models\MBClaim;
  9. use App\Models\Measurement;
  10. use App\Models\Bill;
  11. use App\Models\Claim;
  12. use App\Models\Client;
  13. use App\Models\McpRequest;
  14. use App\Models\Note;
  15. use App\Models\Pack;
  16. use App\Models\Pro;
  17. use App\Models\Product;
  18. use App\Models\ProFavorite;
  19. use App\Models\ProGeneralAvailability;
  20. use App\Models\ProProAccess;
  21. use App\Models\ProRate;
  22. use App\Models\ProSpecificAvailability;
  23. use App\Models\ProSpecificUnavailability;
  24. use App\Models\ProTextShortcut;
  25. use App\Models\ProTransaction;
  26. use App\Models\Shipment;
  27. use App\Models\SupplyOrder;
  28. use App\Models\Ticket;
  29. use App\Models\ClientMeasurementDaysPerMonth;
  30. use Illuminate\Support\Facades\DB;
  31. use Illuminate\Support\Facades\Http;
  32. use PDF;
  33. use DateTime;
  34. use DateTimeZone;
  35. use Illuminate\Http\Request;
  36. class PracticeManagementController extends Controller
  37. {
  38. public function remoteMonitoringReport(Request $request)
  39. {
  40. $rows = null;
  41. $proID = $this->performer()->pro->id;
  42. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  43. $rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  44. return view ('app.practice-management.remote-monitoring-report', compact('rows'));
  45. }
  46. public function billingReport(Request $request)
  47. {
  48. $rows = BillingReport::paginate(50);
  49. return view('app.practice-management.billing-report', compact('rows'));
  50. }
  51. public function dashboard(Request $request)
  52. {
  53. return view('app.practice-management.dashboard');
  54. }
  55. public function rates(Request $request, $selectedProUid = 'all')
  56. {
  57. $proUid = $selectedProUid ? $selectedProUid : 'all';
  58. $rates = ProRate::where('is_active', true);
  59. if ($proUid !== 'all') {
  60. $selectedPro = Pro::where('uid', $proUid)->first();
  61. $rates = $rates->where('pro_id', $selectedPro->id);
  62. }
  63. $rates = $rates->orderBy('pro_id', 'asc')->get();
  64. $pros = $this->pros;
  65. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  66. }
  67. public function previousBills(Request $request)
  68. {
  69. return view('app.practice-management.previous-bills');
  70. }
  71. public function financialTransactions(Request $request)
  72. {
  73. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  74. return view('app.practice-management.financial-transactions', compact('transactions'));
  75. }
  76. public function pendingBillsToSign(Request $request)
  77. {
  78. return view('app.practice-management.pending-bills-to-sign');
  79. }
  80. public function HR(Request $request)
  81. {
  82. return view('app.practice-management.hr');
  83. }
  84. public function directDepositSettings(Request $request)
  85. {
  86. return view('app.practice-management.direct-deposit-settings');
  87. }
  88. public function w9(Request $request)
  89. {
  90. return view('app.practice-management.w9');
  91. }
  92. public function contract(Request $request)
  93. {
  94. return view('app.practice-management.contract');
  95. }
  96. public function notes(Request $request, $filter = '')
  97. {
  98. $proID = $this->performer()->pro->id;
  99. $query = Note::where('hcp_pro_id', $proID);
  100. switch ($filter) {
  101. case 'not-yet-signed':
  102. $query = $query->where('is_signed_by_hcp', false);
  103. break;
  104. case 'not-yet-signed-but-ally-signed':
  105. $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
  106. break;
  107. case 'without-bills':
  108. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  109. break;
  110. // more cases can be added as needed
  111. default:
  112. break;
  113. }
  114. $notes = $query->orderBy('created_at', 'desc')->get();
  115. return view('app.practice-management.notes', compact('notes', 'filter'));
  116. }
  117. public function naBillableSignedNotes(Request $request)
  118. {
  119. $notes = Note
  120. ::where('is_signed_by_hcp', TRUE)
  121. ->where('ally_pro_id', $this->performer()->pro->id)
  122. ->where('is_cancelled', FALSE)
  123. ->whereRaw("
  124. (
  125. SELECT count(bill.id)
  126. FROM bill WHERE
  127. bill.is_cancelled = FALSE AND
  128. bill.generic_pro_id = {$this->performer()->pro->id} AND
  129. bill.note_id = note.id
  130. ) = 0
  131. ");
  132. $notes = $notes->orderBy('created_at', 'desc')->get();
  133. return view('app.practice-management.na-billable-signed-notes', compact('notes'));
  134. }
  135. public function bills(Request $request, $filter = '')
  136. {
  137. $proID = $this->performer()->pro->id;
  138. $query = Bill::where('is_cancelled', false)->where('bill_service_type', '<>', 'CARE_MONTH');
  139. switch ($filter) {
  140. case 'not-yet-signed':
  141. $query = $query
  142. ->where(function ($q) use ($proID) {
  143. $q->where(function ($q2) use ($proID) {
  144. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  145. })
  146. ->orWhere(function ($q2) use ($proID) {
  147. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  148. })
  149. ->orWhere(function ($q2) use ($proID) {
  150. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  151. })
  152. ->orWhere(function ($q2) use ($proID) {
  153. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  154. });
  155. });
  156. break;
  157. case 'previous':
  158. $query = $query
  159. ->where(function ($q) use ($proID) {
  160. $q->where(function ($q2) use ($proID) {
  161. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  162. })
  163. ->orWhere(function ($q2) use ($proID) {
  164. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  165. })
  166. ->orWhere(function ($q2) use ($proID) {
  167. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  168. })
  169. ->orWhere(function ($q2) use ($proID) {
  170. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  171. });
  172. });
  173. break;
  174. // more cases can be added as needed
  175. default:
  176. break;
  177. }
  178. $bills = $query->orderBy('created_at', 'desc')->get();
  179. return view('app.practice-management.bills', compact('bills', 'filter'));
  180. }
  181. public function rmBillsToSign(Request $request)
  182. {
  183. $performerProID = $this->performer()->pro->id;
  184. $bills = Bill::where('is_cancelled', false)->where('cm_or_rm', 'RM')
  185. ->where(function ($q) use ($performerProID) {
  186. $q
  187. ->where(function ($q2) use ($performerProID) {
  188. $q2->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false);
  189. })
  190. ->orWhere(function ($q2) use ($performerProID) {
  191. $q2->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false);
  192. })
  193. ->orWhere(function ($q2) use ($performerProID) {
  194. $q2->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false);
  195. })
  196. ->orWhere(function ($q2) use ($performerProID) {
  197. $q2->where('generic_pro_id', $performerProID)->where('is_signed_by_generic_pro', false);
  198. });
  199. })
  200. ->orderBy('effective_date', 'desc')
  201. ->get();
  202. return view('app.practice-management.rm-bills-to-sign', compact('bills'));
  203. }
  204. public function unacknowledgedCancelledBills(Request $request)
  205. {
  206. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  207. ->where('is_cancelled', true)
  208. ->where('is_cancellation_acknowledged', false)
  209. ->orderBy('created_at', 'desc')
  210. ->get();
  211. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  212. }
  213. public function myTickets(Request $request, $filter = 'open')
  214. {
  215. $performer = $this->performer();
  216. $myTickets = Ticket::where(function ($q) use ($performer) {
  217. $q->where('assigned_pro_id', $performer->pro_id)
  218. ->orWhere('manager_pro_id', $performer->pro_id)
  219. ->orWhere('ordering_pro_id', $performer->pro_id)
  220. ->orWhere('initiating_pro_id', $performer->pro_id);
  221. });
  222. if ($filter === 'open') {
  223. $myTickets = $myTickets->where('is_open', true);
  224. } else if ($filter === 'closed') {
  225. $myTickets = $myTickets->where('is_open', false);
  226. }
  227. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  228. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  229. }
  230. public function myTextShortcuts(Request $request)
  231. {
  232. $myTextShortcuts = DB::table('pro_text_shortcut')
  233. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  234. ->select(
  235. 'pro_text_shortcut.uid',
  236. 'pro_text_shortcut.shortcut',
  237. 'pro_text_shortcut.text',
  238. 'pro.name_first',
  239. 'pro.name_last'
  240. )
  241. ->where('pro_text_shortcut.is_removed', false);
  242. if($this->performer()->pro->pro_type !== 'ADMIN') {
  243. $myTextShortcuts = $myTextShortcuts->where('pro_id', $this->performer()->pro_id);
  244. }
  245. $myTextShortcuts = $myTextShortcuts
  246. ->orderBy('pro.name_last')
  247. ->orderBy('pro.name_first')
  248. ->get();
  249. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  250. }
  251. public function myFavorites(Request $request, $filter = 'all')
  252. {
  253. $performer = $this->performer();
  254. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  255. ->where('is_removed', false);
  256. if ($filter !== 'all') {
  257. $myFavorites = $myFavorites->where('category', $filter);
  258. }
  259. $myFavorites = $myFavorites
  260. ->orderBy('category', 'asc')
  261. ->orderBy('position_index', 'asc')
  262. ->get();
  263. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  264. }
  265. public function proAvailability(Request $request, $proUid = null)
  266. {
  267. $performer = $this->performer();
  268. $pro = $performer->pro;
  269. if ($proUid) {
  270. $pro = Pro::where('uid', $proUid)->first();
  271. }
  272. if ($request->get('pro_uid')) {
  273. $proUid = $request->get('pro_uid');
  274. $pro = Pro::where('uid', $proUid)->first();
  275. }
  276. $selectedProUid = $pro->uid;
  277. $pros = $this->pros;
  278. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  279. $generalAvailabilities = [
  280. 'MONDAY' => [],
  281. 'TUESDAY' => [],
  282. 'WEDNESDAY' => [],
  283. 'THURSDAY' => [],
  284. 'FRIDAY' => [],
  285. 'SATURDAY' => [],
  286. 'SUNDAY' => [],
  287. ];
  288. foreach ($generalAvailabilitiesList as $ga) {
  289. if ($ga->day_of_week == 'MONDAY') {
  290. $generalAvailabilities['MONDAY'][] = $ga;
  291. }
  292. if ($ga->day_of_week == 'TUESDAY') {
  293. $generalAvailabilities['TUESDAY'][] = $ga;
  294. }
  295. if ($ga->day_of_week == 'WEDNESDAY') {
  296. $generalAvailabilities['WEDNESDAY'][] = $ga;
  297. }
  298. if ($ga->day_of_week == 'THURSDAY') {
  299. $generalAvailabilities['THURSDAY'][] = $ga;
  300. }
  301. if ($ga->day_of_week == 'FRIDAY') {
  302. $generalAvailabilities['FRIDAY'][] = $ga;
  303. }
  304. if ($ga->day_of_week == 'SATURDAY') {
  305. $generalAvailabilities['SATURDAY'][] = $ga;
  306. }
  307. if ($ga->day_of_week == 'SUNDAY') {
  308. $generalAvailabilities['SUNDAY'][] = $ga;
  309. }
  310. }
  311. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  312. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  313. //events for the calendar
  314. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  315. $endDateTime = new DateTime($startDate);
  316. $endDateTime->modify('+6 day');
  317. $endDate = $endDateTime->format("Y-m-d");
  318. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  319. $events = json_encode($eventsData);
  320. return view(
  321. 'app.practice-management.pro-availability',
  322. compact(
  323. 'pros',
  324. 'generalAvailabilities',
  325. 'specificAvailabilities',
  326. 'specificUnavailabilities',
  327. 'events',
  328. 'selectedProUid'
  329. )
  330. );
  331. }
  332. public function loadAvailability(Request $request, $proUid)
  333. {
  334. $performer = $this->performer();
  335. $pro = $performer->pro;
  336. $startDate = $request->get('start');
  337. $endDate = $request->get('end');
  338. $selectedPro = Pro::where('uid', $proUid)->first();
  339. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  340. }
  341. public function proAvailabilityFilter(Request $request)
  342. {
  343. $proUid = $request->get('proUid');
  344. return ['success' => true, 'data' => $proUid];
  345. }
  346. // video call page (RHS)
  347. // generic call handle (no uid)
  348. // specific call handle (uid of client)
  349. public function meet(Request $request, $uid = false)
  350. {
  351. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  352. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  353. if (!empty($client)) {
  354. return view('app.video.call-minimal', compact('session', 'client'));
  355. }
  356. return view('app.video.call-agora-v2', compact('session', 'client'));
  357. }
  358. // check video page
  359. public function checkVideo(Request $request, $uid)
  360. {
  361. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  362. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  363. $publish = false;
  364. return view('app.video.check-video-minimal', compact('session', 'client'));
  365. }
  366. public function getParticipantInfo(Request $request)
  367. {
  368. $sid = intval($request->get('uid')) - 1000000;
  369. $session = AppSession::where('id', $sid)->first();
  370. $result = [
  371. "type" => '',
  372. "name" => ''
  373. ];
  374. if ($session) {
  375. $result["type"] = $session->session_type;
  376. switch ($session->session_type) {
  377. case 'PRO':
  378. $pro = Pro::where('id', $session->pro_id)->first();
  379. $result["name"] = $pro->displayName();
  380. break;
  381. case 'CLIENT':
  382. $client = Client::where('id', $session->client_id)->first();
  383. $result["name"] = $client->displayName();
  384. break;
  385. }
  386. }
  387. return json_encode($result);
  388. }
  389. // ajax ep used by the video page
  390. // this is needed bcoz meet() is used not
  391. // just for the client passed to the view
  392. public function getOpentokSessionKey(Request $request, $uid)
  393. {
  394. $client = Client::where('uid', $uid)->first();
  395. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  396. }
  397. // poll to check if there are patients with active mcp requests
  398. public function getPatientsInQueue(Request $request)
  399. {
  400. $myInitiatives = $this->performer->pro->initiatives;
  401. if ($myInitiatives) {
  402. $myInitiatives = strtoupper($myInitiatives);
  403. }
  404. $myInitiativesList = explode('|', $myInitiatives);
  405. $myForeignLanguages = $this->performer->pro->foreign_languages;
  406. if ($myForeignLanguages) {
  407. $myForeignLanguages = strtoupper($myForeignLanguages);
  408. }
  409. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  410. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  411. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  412. })
  413. ->where(function ($query) use ($myForeignLanguagesList) {
  414. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  415. })->limit(3)->get();
  416. $results = [];
  417. foreach ($clients as $client) {
  418. $results[] = [
  419. 'clientUid' => $client->uid,
  420. 'name' => $client->displayName(),
  421. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  422. ];
  423. }
  424. return json_encode($results);
  425. }
  426. public function currentWork(Request $request)
  427. {
  428. return view('app/current-work');
  429. }
  430. public function calendar(Request $request, $proUid = null)
  431. {
  432. $pros = Pro::all();
  433. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  434. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  435. $accessibleProIds = [];
  436. foreach ($accessiblePros as $accessiblePro) {
  437. $accessibleProIds[] = $accessiblePro->id;
  438. }
  439. $accessibleProIds[] = $this->pro->id;
  440. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  441. }
  442. return view('app.practice-management.calendar', compact('pros'));
  443. }
  444. public function cellularDeviceManager(Request $request, $proUid = null)
  445. {
  446. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  447. $performerPro = $this->performer->pro;
  448. $targetPro = null;
  449. $allPros = [];
  450. $expectedForHcp = null;
  451. if ($performerPro->pro_type == 'ADMIN') {
  452. $allPros = Pro::all();
  453. $targetPro = Pro::where('uid', $proUid)->first();
  454. } else {
  455. $targetPro = $performerPro;
  456. }
  457. $clients = [];
  458. if ($targetPro) {
  459. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  460. } else {
  461. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  462. }
  463. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  464. }
  465. public function treatmentServiceUtil(Request $request)
  466. {
  467. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  468. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  469. $view_treatment_service_utilization_by_patient = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_by_patient ORDER BY pro_lname ASC, pro_fname ASC, hcp_pro_id ASC, total_hrs DESC"));
  470. return view('app.practice-management.treatment-services-util', compact(
  471. 'view_treatment_service_utilization_org',
  472. 'view_treatment_service_utilization',
  473. 'view_treatment_service_utilization_by_patient'));
  474. }
  475. public function processingBillMatrix(Request $request, $proUid = null)
  476. {
  477. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  478. $performerPro = $this->performer->pro;
  479. $targetPro = null;
  480. if ($performerPro->pro_type == 'ADMIN') {
  481. $targetPro = Pro::where('uid', $proUid)->first();
  482. } else {
  483. $targetPro = $performerPro;
  484. }
  485. $bills = Bill::where('is_cancelled', false);
  486. if(!$request->input('t') || $request->input('t') === 'hcp') {
  487. $bills = $bills
  488. ->where('has_hcp_been_paid', false)
  489. ->where('is_signed_by_hcp', true);
  490. if ($targetPro) {
  491. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  492. }
  493. }
  494. else if($request->input('t') === 'na') {
  495. $bills = $bills
  496. ->where('has_generic_pro_been_paid', false)
  497. ->where('is_signed_by_generic_pro', true);
  498. if ($targetPro) {
  499. $bills = $bills->where('generic_pro_id', $targetPro->id);
  500. }
  501. }
  502. $filter = $request->input('f');
  503. switch ($filter) {
  504. case 'verified':
  505. $bills = $bills->where('is_verified', true);
  506. break;
  507. case 'not-verified':
  508. $bills = $bills->where('is_verified', false);
  509. break;
  510. }
  511. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  512. $viewData = [
  513. 'bills' => $bills,
  514. 'targetPro' => $targetPro,
  515. 'performerPro' => $performerPro,
  516. 'proUid' => $proUid
  517. ];
  518. return view('app.practice-management.processing-bill-matrix', $viewData);
  519. }
  520. public function hcpBillMatrix(Request $request, $proUid = null)
  521. {
  522. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  523. $performerPro = $this->performer->pro;
  524. $targetPro = null;
  525. $allPros = [];
  526. $expectedForHcp = null;
  527. if ($performerPro->pro_type == 'ADMIN') {
  528. $allPros = Pro::all();
  529. $targetPro = Pro::where('uid', $proUid)->first();
  530. } else {
  531. $targetPro = $performerPro;
  532. }
  533. $rows = [];
  534. if ($targetPro) {
  535. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  536. } else {
  537. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  538. }
  539. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  540. }
  541. public function billingManager(Request $request, $proUid = null)
  542. {
  543. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  544. $performerPro = $this->performer->pro;
  545. $targetPro = null;
  546. $allPros = [];
  547. $expectedForHcp = null;
  548. if ($performerPro->pro_type == 'ADMIN') {
  549. $allPros = Pro::all();
  550. $targetPro = Pro::where('uid', $proUid)->first();
  551. } else {
  552. $targetPro = $performerPro;
  553. }
  554. $notes = [];
  555. if ($targetPro) {
  556. $expectedForHcp = DB::select(DB::raw("SELECT coalesce(SUM(hcp_expected_payment_amount),0) as expected_pay FROM bill WHERE hcp_pro_id = :targetProID AND is_signed_by_hcp IS TRUE AND is_cancelled = false"), ['targetProID' => $targetPro->id])[0]->expected_pay;
  557. $notes = Note::where('hcp_pro_id', $targetPro->id);
  558. } else {
  559. $notes = Note::where('id', '>', 0);
  560. }
  561. if($request->input('date')) {
  562. $notes = $notes->where('effective_dateest', $request->input('date'));
  563. }
  564. $filters = [];
  565. $filters['bills_created'] = $request->input('bills_created');
  566. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  567. $filters['bills_resolved'] = $request->input('bills_resolved');
  568. $filters['bills_closed'] = $request->input('bills_closed');
  569. $filters['claims_created'] = $request->input('claims_created');
  570. $filters['claims_closed'] = $request->input('claims_closed');
  571. if ($filters['bills_created']) {
  572. $notes->where(
  573. 'bill_total_expected',
  574. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  575. 0);
  576. }
  577. if ($filters['is_billing_marked_done']) {
  578. $notes->where(
  579. 'is_billing_marked_done',
  580. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  581. true);
  582. }
  583. if ($filters['bills_resolved']) {
  584. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  585. if ($filters['bills_resolved'] === 'yes') {
  586. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND (is_cancelled = false AND is_verified = false) OR (is_cancelled = TRUE AND is_cancellation_acknowledged = FALSE)) > 0');
  587. } elseif ($filters['bills_resolved'] === 'no') {
  588. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND ((is_cancelled = true AND is_cancellation_acknowledged = true) OR is_verified = true)) = 0');
  589. }
  590. }
  591. if ($filters['bills_closed']) {
  592. $notes->where(
  593. 'is_bill_closed',
  594. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  595. true);
  596. }
  597. if ($filters['claims_created']) {
  598. $notes->where(
  599. 'claim_total_expected',
  600. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  601. 0);
  602. }
  603. if ($filters['claims_closed']) {
  604. $notes->where(
  605. 'is_claim_closed',
  606. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  607. true);
  608. }
  609. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  610. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  611. }
  612. public function remoteMonitoring(Request $request) {
  613. $performer = $this->performer();
  614. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  615. $careMonthStart = date($ym . '-01');
  616. // filters
  617. $filters = '';
  618. $fmd = $request->input('fmd');
  619. if($fmd && $fmd !== 'all') {
  620. switch($fmd) {
  621. case 'lt16':
  622. $filters .= ' AND (care_month.number_of_days_with_remote_measurements < 16 OR care_month.number_of_days_with_remote_measurements IS NULL) ';
  623. break;
  624. case 'gte16':
  625. $filters .= ' AND (care_month.number_of_days_with_remote_measurements >= 16 AND care_month.number_of_days_with_remote_measurements IS NOT NULL) ';
  626. break;
  627. }
  628. }
  629. $fcomm = $request->input('fcomm');
  630. if($fcomm && $fcomm !== 'all') {
  631. switch($fcomm) {
  632. case '0':
  633. $filters .= ' AND care_month.has_non_hcp_communicated_to_patient_about_rm = FALSE ';
  634. break;
  635. case '1':
  636. $filters .= ' AND care_month.has_non_hcp_communicated_to_patient_about_rm = TRUE ';
  637. break;
  638. }
  639. }
  640. $c = $request->input('c');
  641. if($c) {
  642. $filters .= ' AND client.uid = \'' . $request->input('c') . '\' ';
  643. }
  644. $patients = DB::select(
  645. DB::raw(
  646. "
  647. SELECT client.name_first, client.name_last, client.uid as client_uid,
  648. care_month.uid as care_month_uid,
  649. care_month.id as care_month_id,
  650. care_month.start_date,
  651. care_month.number_of_days_with_remote_measurements,
  652. care_month.has_non_hcp_communicated_to_patient_about_rm,
  653. care_month.rm_num_measurements_not_stamped_by_mcp,
  654. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  655. care_month.rm_num_measurements_not_stamped_by_rmm,
  656. care_month.rm_num_measurements_not_stamped_by_rme,
  657. client.mcp_pro_id,
  658. client.default_na_pro_id,
  659. client.rmm_pro_id,
  660. client.rme_pro_id,
  661. client.cell_number
  662. FROM care_month join client on care_month.client_id = client.id
  663. WHERE (
  664. (client.mcp_pro_id = {$performer->pro->id})
  665. OR (client.rmm_pro_id = {$performer->pro->id})
  666. OR (client.rme_pro_id = {$performer->pro->id})
  667. OR (client.default_na_pro_id = {$performer->pro->id})
  668. )
  669. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  670. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  671. {$filters}
  672. ORDER BY care_month.number_of_days_with_remote_measurements DESC NULLS LAST, client.name_first ASC, client.name_last ASC
  673. "
  674. )
  675. );
  676. $timestamp = strtotime(date('Y-m-d'));
  677. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  678. return view('app.practice-management.remote-monitoring', compact('patients', 'daysRemaining', 'careMonthStart'));
  679. }
  680. public function remoteMonitoringMeasurements(Request $request, CareMonth $careMonth) {
  681. $performer = $this->performer();
  682. $measurements = DB::select(
  683. DB::raw(
  684. "
  685. SELECT measurement.label,
  686. measurement.ts,
  687. measurement.effective_date,
  688. measurement.sbp_mm_hg,
  689. measurement.dbp_mm_hg,
  690. measurement.value_pulse,
  691. measurement.value_irregular,
  692. measurement.numeric_value,
  693. measurement.value,
  694. measurement.uid,
  695. client.name_first,
  696. client.name_last,
  697. client.mcp_pro_id,
  698. client.default_na_pro_id,
  699. client.rmm_pro_id,
  700. client.rme_pro_id
  701. FROM measurement RIGHT JOIN client on measurement.client_id = client.id
  702. WHERE
  703. client.id = {$careMonth->client_id}
  704. AND measurement.label IS NOT NULL
  705. AND measurement.label NOT IN ('SBP', 'DBP')
  706. AND (measurement.is_cellular_zero = FALSE or measurement.is_cellular_zero IS NULL)
  707. AND measurement.is_removed IS FALSE
  708. AND measurement.client_bdt_measurement_id IS NOT NULL
  709. AND measurement.care_month_id = {$careMonth->id}
  710. AND (
  711. (client.mcp_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_mcp IS FALSE)
  712. OR (client.rmm_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rmm IS FALSE)
  713. OR (client.rme_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rme IS FALSE)
  714. OR (client.default_na_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_non_hcp IS FALSE)
  715. )
  716. ORDER BY ts DESC
  717. "
  718. )
  719. );
  720. return view('app.practice-management.remote-monitoring-measurements', compact('careMonth', 'measurements'));
  721. }
  722. public function billMatrix(Request $request)
  723. {
  724. $bClients = [];
  725. $bHCPPros = [];
  726. $bNAPros = [];
  727. $filters = [];
  728. $filters['client'] = $request->input('client');
  729. $filters['service'] = $request->input('service');
  730. $filters['hcp'] = $request->input('hcp');
  731. $filters['hcp_paid'] = $request->input('hcp_paid');
  732. $filters['expected_op'] = $request->input('expected_op');
  733. $filters['expected_value'] = $request->input('expected_value');
  734. $filters['paid_op'] = $request->input('paid_op');
  735. $filters['paid_value'] = $request->input('paid_value');
  736. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  737. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  738. $filters['hcp_sign'] = $request->input('hcp_sign');
  739. $filters['verified'] = $request->input('verified');
  740. $filters['cancelled'] = $request->input('cancelled');
  741. $bills = Bill::orderBy('effective_date')->paginate();
  742. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  743. }
  744. public function medicarePartBClaims(Request $request)
  745. {
  746. $medicarePartBOnly = $request->get("medicare_part_b");
  747. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  748. //Only medicare claims
  749. $claims = [];
  750. foreach ($allClaims as $claim) {
  751. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  752. $claims[] = $claim;
  753. }
  754. }
  755. $claimEDIs = ClaimEDI::all();
  756. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  757. }
  758. // Generate PDF
  759. public function downloadClaims()
  760. {
  761. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  762. view()->share('claims', $claims);
  763. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  764. return $pdf->download('pdf_file.pdf');
  765. }
  766. public function tickets(Request $request, $proUid = null)
  767. {
  768. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  769. return view('app.practice-management.tickets', compact('tickets'));
  770. }
  771. public function supplyOrders(Request $request)
  772. {
  773. // counts
  774. $counts = $this->getSupplyOrderCounts();
  775. // so clients
  776. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  777. $soClientIDs = array_map(function ($_x) {
  778. return $_x->client_id;
  779. }, $soClientIDs);
  780. $soClients = Client::whereIn('id', $soClientIDs)->get();
  781. // so products
  782. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  783. $soProductIDs = array_map(function ($_x) {
  784. return $_x->product_id;
  785. }, $soProductIDs);
  786. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  787. $filters = [];
  788. $filters['client'] = $request->input('client');
  789. $filters['product'] = $request->input('product');
  790. $filters['reason'] = $request->input('reason');
  791. $filters['cu_memo'] = $request->input('cu_memo');
  792. $filters['pro_sign'] = $request->input('pro_sign');
  793. $filters['client_sign'] = $request->input('client_sign');
  794. $filters['shipment'] = $request->input('shipment');
  795. $filters['lot_number'] = $request->input('lot_number');
  796. $filters['imei'] = $request->input('imei');
  797. $filters['cancelled'] = $request->input('cancelled');
  798. $supplyOrders = SupplyOrder::where('id', '>', 0);
  799. // apply filters
  800. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  801. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  802. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  803. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  804. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  805. if ($filters['client_sign']) {
  806. if ($filters['client_sign'] === 'signed')
  807. $supplyOrders->where('is_signed_by_client', true);
  808. elseif ($filters['client_sign'] === 'waived')
  809. $supplyOrders->where('is_client_signature_waived', true);
  810. else
  811. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  812. }
  813. if ($filters['shipment']) {
  814. if ($filters['shipment'] === 'not_cleared_for_shipment')
  815. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  816. elseif ($filters['shipment'] === 'cleared_for_shipment')
  817. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  818. else
  819. $supplyOrders
  820. ->whereNotNull('shipment_id')
  821. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  822. }
  823. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  824. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  825. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  826. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  827. return view('app.practice-management.supply-orders',
  828. compact('supplyOrders', 'filters',
  829. 'soClients', 'soProducts', 'counts'
  830. )
  831. );
  832. }
  833. public function shipments(Request $request, $filter = null)
  834. {
  835. // counts
  836. $counts = $this->getShipmentCounts();
  837. // so clients
  838. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  839. $shClientIDs = array_map(function ($_x) {
  840. return $_x->client_id;
  841. }, $shClientIDs);
  842. $shClients = Client::whereIn('id', $shClientIDs)->get();
  843. $shipments = Shipment::where('id', '>', 0);
  844. $filters = [];
  845. $filters['client'] = $request->input('client');
  846. $filters['courier'] = $request->input('courier');
  847. $filters['tracking_num'] = $request->input('tracking_num');
  848. $filters['label'] = $request->input('label');
  849. $filters['status'] = $request->input('status');
  850. $filters['cancelled'] = $request->input('cancelled');
  851. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  852. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  853. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  854. if ($filters['label']) {
  855. if ($filters['label'] === 'yes')
  856. $shipments->whereNotNull('label_system_file_id');
  857. else
  858. $shipments->whereNull('label_system_file_id');
  859. }
  860. if ($filters['status']) $shipments->where('status', $filters['status']);
  861. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  862. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  863. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  864. }
  865. public function cellularMeasurements(Request $request)
  866. {
  867. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  868. return view('app.practice-management.cellular-measurements', compact('measurements'));
  869. }
  870. // v2 supply-orders & shipments management (wh)
  871. public function supplyOrdersReadyToShip(Request $request)
  872. {
  873. $counts = $this->getSupplyOrderCounts();
  874. $supplyOrders = SupplyOrder
  875. ::where('is_cleared_for_shipment', true)
  876. ->where('is_cancelled', false)
  877. ->whereNull('shipment_id')
  878. ->join('client', 'client.id', '=', 'supply_order.client_id')
  879. ->orderBy('client.name_last', 'ASC')
  880. ->orderBy('client.name_first', 'ASC')
  881. ->orderBy('supply_order.client_id', 'ASC')
  882. ->orderBy('supply_order.mailing_address_full', 'ASC')
  883. ->orderBy('supply_order.created_at', 'ASC')
  884. ->select('supply_order.*')
  885. ->paginate();
  886. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  887. }
  888. public function supplyOrdersShipmentUnderway(Request $request)
  889. {
  890. $counts = $this->getSupplyOrderCounts();
  891. $supplyOrders = SupplyOrder
  892. ::where('is_cancelled', false)
  893. ->whereNotNull('shipment_id')
  894. ->orderBy('client_id', 'ASC')
  895. ->orderBy('mailing_address_full', 'ASC')
  896. ->orderBy('created_at', 'ASC')
  897. ->paginate();
  898. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  899. }
  900. public function supplyOrdersHanging(Request $request)
  901. {
  902. $counts = $this->getSupplyOrderCounts();
  903. $supplyOrders = SupplyOrder
  904. ::select('supply_order.*')
  905. ->leftJoin('shipment', function($join) {
  906. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  907. })
  908. ->where('shipment.status', 'CANCELLED')
  909. ->where('supply_order.is_cancelled', false)
  910. ->orderBy('supply_order.client_id', 'ASC')
  911. ->orderBy('supply_order.mailing_address_full', 'ASC')
  912. ->orderBy('supply_order.created_at', 'ASC')
  913. ->paginate();
  914. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  915. }
  916. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  917. {
  918. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  919. ->where('is_cancelled', true)
  920. ->where('is_cancellation_acknowledged', false)
  921. ->orderBy('created_at', 'desc')
  922. ->paginate();
  923. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  924. }
  925. public function supplyOrdersUnsigned(Request $request)
  926. {
  927. $supplyOrders = SupplyOrder
  928. ::where('is_cancelled', false)
  929. ->where('is_signed_by_pro', false)
  930. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  931. ->orderBy('created_at', 'desc')
  932. ->paginate();
  933. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  934. }
  935. private function getSupplyOrderCounts()
  936. {
  937. return [
  938. "supplyOrders" => SupplyOrder::count(),
  939. "supplyOrdersReadyToShip" => SupplyOrder
  940. ::where('is_cleared_for_shipment', true)
  941. ->where('is_cancelled', false)
  942. ->whereNull('shipment_id')->count(),
  943. "supplyOrdersShipmentUnderway" => SupplyOrder
  944. ::where('is_cancelled', false)
  945. ->whereNotNull('shipment_id')->count(),
  946. "supplyOrdersHanging" => SupplyOrder
  947. ::leftJoin('shipment', function($join) {
  948. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  949. })
  950. ->where('shipment.status', 'CANCELLED')
  951. ->where('supply_order.is_cancelled', false)
  952. ->count(),
  953. ];
  954. }
  955. public function shipmentsReadyToPrint(Request $request)
  956. {
  957. $counts = $this->getShipmentCounts();
  958. $shipments = Shipment
  959. ::where('is_cancelled', false)
  960. ->where('status', 'CREATED')
  961. ->orderBy('created_at', 'ASC')
  962. ->paginate();
  963. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  964. }
  965. public function shipmentsShipmentUnderway(Request $request)
  966. {
  967. $counts = $this->getShipmentCounts();
  968. $shipments = Shipment
  969. ::where('is_cancelled', false)
  970. ->where('status', 'PRINTED')
  971. ->orderBy('created_at', 'ASC')
  972. ->paginate();
  973. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  974. }
  975. private function getShipmentCounts()
  976. {
  977. return [
  978. "shipments" => Shipment::count(),
  979. "shipmentsReadyToPrint" => Shipment
  980. ::where('is_cancelled', false)
  981. ->where('status', 'CREATED')
  982. ->count(),
  983. "shipmentsWaitingForPicker" => Shipment
  984. ::where('is_cancelled', false)
  985. ->where('status', 'PRINTED')
  986. ->count()
  987. ];
  988. }
  989. public function shipment(Request $request, Shipment $shipment)
  990. {
  991. return view('app.practice-management.shipment', compact('shipment'));
  992. }
  993. public function shipmentsMultiPrint(Request $request, $ids)
  994. {
  995. $ids = array_map(function ($_x) {
  996. return intval($_x);
  997. }, explode("|", $ids));
  998. $shipments = Shipment::whereIn('id', $ids)->get();
  999. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  1000. }
  1001. public function patientClaimSummary(Request $request, $proUid = null)
  1002. {
  1003. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  1004. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  1005. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  1006. $notes3rdPartyTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  1007. $notes3rdPartyTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_bill_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  1008. $notes3rdPartyTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_claim_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  1009. $patientsTotal = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  1010. $patientsTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  1011. $patientsTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  1012. $performerPro = $this->performer->pro;
  1013. $allPros = [];
  1014. if ($performerPro->pro_type == 'ADMIN') {
  1015. $allPros = Pro::all();
  1016. } else {
  1017. $allPros = [$performerPro];
  1018. }
  1019. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  1020. $patientsQuery = Client::where('is_dummy', '=', false)
  1021. ->whereNull('shadow_pro_id')
  1022. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  1023. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  1024. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  1025. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  1026. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  1027. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  1028. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  1029. if ($proUid) {
  1030. $mcpPro = Pro::where('uid', $proUid)->first();
  1031. if ($mcpPro) {
  1032. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  1033. }
  1034. }
  1035. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  1036. $patientsQuery->orderBy('notes_total', 'DESC');
  1037. $patients = $patientsQuery->paginate(50);
  1038. $data = [
  1039. 'patients' => $patients,
  1040. 'proUid' => $proUid,
  1041. 'allPros' => $allPros,
  1042. 'notesTotal' => $notesTotal,
  1043. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  1044. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  1045. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  1046. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  1047. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  1048. 'patientsTotal' => $patientsTotal,
  1049. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  1050. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  1051. ];
  1052. return view('app.practice-management.patient-claim-summary', $data);
  1053. }
  1054. public function claims(Request $request){
  1055. $status = $request->get('status');
  1056. $claims = [];
  1057. if(!$status){
  1058. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  1059. }else{
  1060. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  1061. }
  1062. return view('app.practice-management.claims', compact('claims', 'status'));
  1063. }
  1064. public function processClaims(Request $request) {
  1065. $status = '';
  1066. $q = $request->input('q') ? $request->input('q') : '';
  1067. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  1068. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  1069. $params = [
  1070. 'q' => '%' . $q . '%',
  1071. 'from' => $from,
  1072. 'to' => $to
  1073. ];
  1074. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  1075. if($hcpPro) {
  1076. $params['hcp'] = $hcpPro->id;
  1077. }
  1078. $claims = DB::select(DB::raw("
  1079. SELECT claim.uid AS uid,
  1080. DATE(claim.created_at) AS created,
  1081. claim.status,
  1082. client.uid AS client_uid,
  1083. client.cell_number AS client_phone,
  1084. (client.name_last || ' ' || client.name_first) AS client ,
  1085. client.chart_number AS client_chart_number,
  1086. cp.id AS claim_pro_id,
  1087. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  1088. sp.id AS status_pro_id,
  1089. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  1090. note.uid AS note_uid,
  1091. note.method,
  1092. note.new_or_fu_or_na,
  1093. -- claim.status_updated_at,
  1094. (DATE(claim.status_updated_at) || ' ' ||
  1095. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  1096. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  1097. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  1098. (SELECT COUNT(claim_line_icd.id) FROM claim_line_icd WHERE claim_line_id IN (SELECT id FROM claim_line WHERE claim_id = claim.id)) AS icds,
  1099. ROUND(claim.expected_total, 3) as expected_total
  1100. FROM claim
  1101. join client on claim.client_id = client.id
  1102. join pro cp on claim.pro_id = cp.id
  1103. left join note on claim.note_id = note.id
  1104. left join app_session on claim.status_updated_by_session_id = app_session.id
  1105. left join pro sp on app_session.pro_id = sp.id
  1106. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  1107. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'))
  1108. -- AND claim.current_version_id IS NOT NULL
  1109. AND (client.name_first ILIKE :q OR
  1110. client.name_last ILIKE :q OR
  1111. client.chart_number ILIKE :q OR
  1112. client.mcn ILIKE :q)
  1113. AND (claim.created_at >= :from AND claim.created_at <= :to)
  1114. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  1115. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  1116. ORDER BY claim.created_at ASC
  1117. --OFFSET 0 LIMIT 15
  1118. "), $params);
  1119. if($request->input('json')) {
  1120. return json_encode($claims);
  1121. }
  1122. return view('app.practice-management.process-claims', compact('claims', 'status'));
  1123. }
  1124. public function processNotes(Request $request) {
  1125. $mode = $request->input('mode') ? $request->input('mode') : '1';
  1126. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  1127. $counts = [
  1128. "picked" => Note::where('is_cancelled', false)
  1129. ->whereNotNull('current_note_pickup_for_processing_id')
  1130. ->count(),
  1131. "bad" => Note::where('is_cancelled', false)
  1132. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1133. ->count(),
  1134. // not yet signed
  1135. "mode-1" => Note::where('is_cancelled', false)
  1136. ->where('is_signed_by_hcp', false)
  1137. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1138. // ->whereNull('current_note_pickup_for_processing_id')
  1139. ->count(),
  1140. // billing not marked done
  1141. "mode-2" => Note::where('is_cancelled', false)
  1142. ->where('is_signed_by_hcp', true)
  1143. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1144. // ->whereNull('current_note_pickup_for_processing_id')
  1145. ->where('is_billing_marked_done', false)
  1146. ->count(),
  1147. // billing not closed
  1148. "mode-3" => Note::where('is_cancelled', false)
  1149. ->where('is_signed_by_hcp', true)
  1150. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1151. // ->whereNull('current_note_pickup_for_processing_id')
  1152. ->where('is_billing_marked_done', true)
  1153. ->where('is_bill_closed', false)
  1154. ->count(),
  1155. // claiming not closed
  1156. "mode-4" => Note::where('is_cancelled', false)
  1157. ->where('is_signed_by_hcp', true)
  1158. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1159. // ->whereNull('current_note_pickup_for_processing_id')
  1160. ->where('is_billing_marked_done', true)
  1161. ->where('is_bill_closed', true)
  1162. ->where('is_claim_closed', false)
  1163. ->count(),
  1164. // has unsubmitted claims
  1165. "mode-5" => Note::where('is_cancelled', false)
  1166. ->where('is_signed_by_hcp', true)
  1167. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1168. // ->whereNull('current_note_pickup_for_processing_id')
  1169. ->where('is_billing_marked_done', true)
  1170. ->where('is_bill_closed', true)
  1171. ->where('is_claim_closed', true)
  1172. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1173. ->count(),
  1174. // has unsubmitted claims MARKED GOOD!
  1175. "mode-8" => Note::where('is_cancelled', false)
  1176. ->where('is_signed_by_hcp', true)
  1177. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1178. // ->whereNull('current_note_pickup_for_processing_id')
  1179. ->where('is_billing_marked_done', true)
  1180. ->where('is_bill_closed', true)
  1181. // ->where('is_claim_closed', true)
  1182. // ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1183. ->count(),
  1184. // all good
  1185. "mode-6" => Note::where('is_cancelled', false)
  1186. ->where('is_signed_by_hcp', true)
  1187. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1188. // ->whereNull('current_note_pickup_for_processing_id')
  1189. ->where('is_billing_marked_done', true)
  1190. ->where('is_bill_closed', true)
  1191. ->where('is_claim_closed', true)
  1192. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1193. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1194. ->count(),
  1195. "mode-7" => Note::where('is_cancelled', false)
  1196. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1197. ->count(),
  1198. ];
  1199. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1200. }
  1201. public function getNextNote(Request $request, $mode)
  1202. {
  1203. $note = null;
  1204. switch (+$mode) {
  1205. case 1:
  1206. $note = Note::where('is_cancelled', false)
  1207. ->where('is_signed_by_hcp', false)
  1208. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1209. ->whereNull('current_note_pickup_for_processing_id')
  1210. ->orderBy('effective_dateest', 'DESC')
  1211. ->first();
  1212. break;
  1213. case 2:
  1214. $note = Note::where('is_cancelled', false)
  1215. ->where('is_signed_by_hcp', true)
  1216. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1217. ->whereNull('current_note_pickup_for_processing_id')
  1218. ->where('is_billing_marked_done', false)
  1219. ->orderBy('effective_dateest', 'DESC')
  1220. ->first();
  1221. break;
  1222. case 3:
  1223. $note = Note::where('is_cancelled', false)
  1224. ->where('is_signed_by_hcp', true)
  1225. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1226. ->whereNull('current_note_pickup_for_processing_id')
  1227. ->where('is_billing_marked_done', true)
  1228. ->where('is_bill_closed', false)
  1229. ->orderBy('effective_dateest', 'DESC')
  1230. ->first();
  1231. break;
  1232. case 4:
  1233. $note = Note::where('is_cancelled', false)
  1234. ->where('is_signed_by_hcp', true)
  1235. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1236. ->whereNull('current_note_pickup_for_processing_id')
  1237. ->where('is_billing_marked_done', true)
  1238. ->where('is_bill_closed', true)
  1239. ->where('is_claim_closed', false)
  1240. ->orderBy('effective_dateest', 'DESC')
  1241. ->first();
  1242. break;
  1243. case 5:
  1244. $note = Note::where('is_cancelled', false)
  1245. ->where('is_signed_by_hcp', true)
  1246. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1247. ->whereNull('current_note_pickup_for_processing_id')
  1248. ->where('is_billing_marked_done', true)
  1249. ->where('is_bill_closed', true)
  1250. ->where('is_claim_closed', true)
  1251. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1252. ->orderBy('effective_dateest', 'DESC')
  1253. ->first();
  1254. break;
  1255. case 8:
  1256. $note = Note::where('is_cancelled', false)
  1257. ->where('is_signed_by_hcp', true)
  1258. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1259. // ->whereNull('current_note_pickup_for_processing_id')
  1260. ->where('is_billing_marked_done', true)
  1261. ->where('is_bill_closed', true)
  1262. // ->where('is_claim_closed', true)
  1263. // ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1264. ->orderBy('effective_dateest', 'DESC')
  1265. ->first();
  1266. break;
  1267. }
  1268. if($note) {
  1269. $note->client_uid = $note->client->uid;
  1270. }
  1271. return json_encode($note);
  1272. }
  1273. public function pickedNotes(Request $request) {
  1274. $counts = [
  1275. "unpicked" => Note::where('is_cancelled', false)
  1276. ->whereNull('current_note_pickup_for_processing_id')
  1277. ->count(),
  1278. ];
  1279. $notes = Note::where('is_cancelled', false)
  1280. ->whereNotNull('current_note_pickup_for_processing_id')
  1281. ->orderBy('effective_dateest', 'ASC')
  1282. ->paginate();
  1283. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1284. }
  1285. public function badNotes(Request $request) {
  1286. $counts = [
  1287. "unpicked" => Note::where('is_cancelled', false)
  1288. ->whereNull('current_note_pickup_for_processing_id')
  1289. ->count(),
  1290. ];
  1291. $notes = Note::where('is_cancelled', false)
  1292. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1293. ->orderBy('effective_dateest', 'ASC')
  1294. ->paginate();
  1295. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1296. }
  1297. public function doneNotes(Request $request) {
  1298. $counts = [
  1299. "unpicked" => Note::where('is_cancelled', false)
  1300. ->whereNull('current_note_pickup_for_processing_id')
  1301. ->count(),
  1302. ];
  1303. $notes = Note::where('is_cancelled', false)
  1304. ->where('is_signed_by_hcp', true)
  1305. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1306. ->whereNull('current_note_pickup_for_processing_id')
  1307. ->where('is_billing_marked_done', true)
  1308. ->where('is_bill_closed', true)
  1309. ->where('is_claim_closed', true)
  1310. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1311. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1312. ->orderBy('effective_dateest', 'ASC')
  1313. ->paginate();
  1314. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1315. }
  1316. public function currentMbClaim(Request $request, $claimUid) {
  1317. $claim = Claim::where('uid', $claimUid)->first();
  1318. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1319. }
  1320. public function currentClaimLines(Request $request, $claimUid) {
  1321. $claim = Claim::where('uid', $claimUid)->first();
  1322. if($request->input('json')) {
  1323. foreach ($claim->lines as $line) {
  1324. $line->expected_total = round($line->expected_total, 3);
  1325. $x = $line->claimLineIcds;
  1326. }
  1327. return json_encode($claim->lines);
  1328. }
  1329. return view('app.practice-management._claim-lines', compact('claim'));
  1330. }
  1331. public function packsMultiPrint(Request $request) {
  1332. $packs = Pack
  1333. ::select('pack.*')
  1334. ->leftJoin('shipment', function($join) {
  1335. $join->on('pack.shipment_id', '=', 'shipment.id');
  1336. })
  1337. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1338. ->where(function ($query) {
  1339. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1340. })
  1341. ->whereNotNull('pack.label_system_file_id')
  1342. ->orderBy('pack.created_at', 'ASC')
  1343. ->get();
  1344. return view('app.practice-management.packs-multi-print', compact('packs'));
  1345. }
  1346. public function packsMultiPDF(Request $request, $ids) {
  1347. $ids = array_map(function ($_x) {
  1348. return intval($_x);
  1349. }, explode("|", $ids));
  1350. $packs = Pack::whereIn('id', $ids)->get();
  1351. }
  1352. public function handouts(Request $request) {
  1353. $handouts = Handout::orderBy('display_name')->get();
  1354. return view('app.practice-management.handouts', compact('handouts'));
  1355. }
  1356. private function callJava($request, $endPoint, $data)
  1357. {
  1358. $url = config('stag.backendUrl') . $endPoint;
  1359. $response = Http::asForm()
  1360. ->withHeaders([
  1361. 'sessionKey' => $request->cookie('sessionKey')
  1362. ])
  1363. ->post($url, $data)
  1364. ->body();
  1365. dd($response);
  1366. return $response;
  1367. }
  1368. public function genericBills(Request $request)
  1369. {
  1370. return view('app.practice-management.generic-bills');
  1371. }
  1372. }