PracticeManagementController.php 83 KB

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