PracticeManagementController.php 59 KB

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