PracticeManagementController.php 75 KB

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