PracticeManagementController.php 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353
  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\ClientProChange;
  8. use App\Models\Company;
  9. use App\Models\Handout;
  10. use App\Models\MBClaim;
  11. use App\Models\Measurement;
  12. use App\Models\Bill;
  13. use App\Models\Claim;
  14. use App\Models\Client;
  15. use App\Models\McpRequest;
  16. use App\Models\McCodeCheck;
  17. use App\Models\Note;
  18. use App\Models\Pack;
  19. use App\Models\Pro;
  20. use App\Models\Product;
  21. use App\Models\ProFavorite;
  22. use App\Models\ProGeneralAvailability;
  23. use App\Models\ProProAccess;
  24. use App\Models\ProRate;
  25. use App\Models\ProSpecificAvailability;
  26. use App\Models\ProSpecificUnavailability;
  27. use App\Models\ProTeam;
  28. use App\Models\ProTextShortcut;
  29. use App\Models\ProTransaction;
  30. use App\Models\Shipment;
  31. use App\Models\SupplyOrder;
  32. use App\Models\Team;
  33. use App\Models\Ticket;
  34. use App\Models\AccountInvite;
  35. use App\Models\ClientMeasurementDaysPerMonth;
  36. use App\Models\ClientBDTDevice;
  37. use App\Models\ClientMemo;
  38. use Carbon\Carbon;
  39. use Illuminate\Pagination\LengthAwarePaginator;
  40. use Illuminate\Support\Facades\DB;
  41. use Illuminate\Support\Facades\Http;
  42. use PDF;
  43. use DateTime;
  44. use DateTimeZone;
  45. use Illuminate\Http\Request;
  46. use App\Models\SegmentTemplate;
  47. use App\Models\VisitTemplate;
  48. use App\Models\VisitTemplateSegmentTemplate;
  49. use App\Models\VisitTemplateAccess;
  50. class PracticeManagementController extends Controller
  51. {
  52. public function rpmMatrix(Request $request)
  53. {
  54. $proID = $this->performer()->pro->id;
  55. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  56. $query = Client::whereNull('shadow_pro_id');
  57. if(!$isAdmin) $query->where('mcp_pro_id', '=', $proID);
  58. $clients = $query->orderByRaw('most_recent_cellular_measurement_at desc nulls last')
  59. ->paginate(50);
  60. return view ('app.practice-management.rpm-matrix', compact('clients'));
  61. }
  62. public function mcCodeChecks(Request $request)
  63. {
  64. $checks = McCodeCheck::orderBy('next_eligible_date_professional', 'asc')->get();
  65. return view ('app.practice-management.mc-code-checks', compact('checks'));
  66. }
  67. public function remoteMonitoringReport(Request $request)
  68. {
  69. $rows = null;
  70. $proID = $this->performer()->pro->id;
  71. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  72. //$rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  73. $query = CareMonth::whereNotNull('mcp_pro_id')
  74. ->where('number_of_days_with_remote_measurements', '>=', 16)
  75. ->where('days_between_most_recent_mcp_note_date_and_end_of_care_month', '<=', 90);
  76. if(!$isAdmin) $query->where('mcp_pro_id', '=', $proID);
  77. if($request->get('show_billing_not_closed_only')){
  78. $query->where(function($q){
  79. return $q->where('is_bill_closed', false)->orWhereNull('is_bill_closed');
  80. });
  81. }
  82. $rows = $query->orderByRaw(DB::raw('start_date DESC'))->paginate(100) ;
  83. return view ('app.practice-management.remote-monitoring-report', compact('rows', 'isAdmin'));
  84. }
  85. public function dailyTreatmentServices(Request $request) {
  86. $type = $request->input('t');
  87. $fPro = null;
  88. if($type === 'pro') {
  89. $fPro = Pro::where('uid', $request->input('p'))->first();
  90. $rows = DB::select(DB::raw("
  91. SELECT effective_date, hcp_pro_id, p.name_first, p.name_last, count(*), sum(number_of_units) as units
  92. FROM bill JOIN pro p on p.id = bill.hcp_pro_id
  93. WHERE code = 'Treatment Services'
  94. GROUP BY effective_date, hcp_pro_id, p.name_first, p.name_last
  95. ORDER BY effective_date DESC, p.name_first ASC, p.name_last ASC
  96. "));
  97. }
  98. else {
  99. $rows = DB::select(DB::raw("
  100. SELECT effective_date, count(*), sum(number_of_units) as units FROM bill WHERE code = 'Treatment Services' GROUP BY effective_date ORDER BY effective_date DESC
  101. "));
  102. }
  103. return view ('app.practice-management.daily-treatment-services', compact('rows'));
  104. }
  105. public function clientProChanges(Request $request)
  106. {
  107. $filters = $request->all();
  108. $rows = ClientProChange::query();
  109. $associationTypes = DB::table('client_pro_change')->select('responsibility_type')->distinct()->pluck('responsibility_type')->toArray();
  110. $this->filterMultiQuery($request, $rows, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  111. $this->filterSimpleQuery($request, $rows, 'responsibility_type', 'responsibility_type');
  112. $oldProUid = $request->get('old_pro');
  113. if($oldProUid){
  114. $oldPro = Pro::where('uid', $oldProUid)->first();
  115. $rows = $rows->where('previous_pro_id',$oldPro->id);
  116. }
  117. $newProUid = $request->get('new_pro');
  118. if($newProUid){
  119. $newPro = Pro::where('uid', $newProUid)->first();
  120. $rows = $rows->where('new_pro_id',$newPro->id);
  121. }
  122. $rows = $rows->orderByRaw('created_at::date DESC')->orderBy('client_id', 'desc')->paginate(20);
  123. return view ('app.practice-management.client-pro-changes', compact('rows', 'filters', 'associationTypes'));
  124. }
  125. public function billingReport(Request $request)
  126. {
  127. $filters = $request->all();
  128. $claimStatus = $request->get('status');
  129. $rows = BillingReport::whereHas('client', function($clientQuery){
  130. return $clientQuery->where('client_engagement_status_category','<>' ,'DUMMY');
  131. })->orderBy('note_date', 'desc');
  132. $no_claims = $request->get('no_claims');
  133. $zero_deductible = $request->get('zero_deductible');
  134. $claim_status = $request->get('claim_status');
  135. $verified = $request->get('verified');
  136. if($no_claims){
  137. $rows = $rows->whereHas('note', function($noteQuery){
  138. return $noteQuery->has('claims', '=', 0);
  139. });
  140. }
  141. if($zero_deductible){
  142. $rows = $rows->whereHas('client', function($clientQuery){
  143. return $clientQuery->where('client_engagement_status_category','<>' ,'DUMMY')
  144. ->whereHas('latestClientPrimaryCoverage', function($lcpcQuery){
  145. return $lcpcQuery->where('auto_medicare_mpb_deductible', 0);
  146. });
  147. });
  148. }
  149. if($claim_status){
  150. $rows = $rows->whereHas('note', function($noteQuery) use ($claim_status){
  151. return $noteQuery->whereHas('claims', function($claimQuery) use ($claim_status) {
  152. return $claimQuery->where('status', $claim_status);
  153. });
  154. });
  155. }
  156. if($verified){
  157. if($verified == 'VERIFIED'){
  158. $rows = $rows->whereHas('note', function($noteQuery){
  159. return $noteQuery->whereHas('bills', function($billQuery) {
  160. return $billQuery->where('is_verified', true)->where('is_cancelled', false);
  161. });
  162. });
  163. }
  164. if($verified == 'UNVERIFIED'){
  165. $rows = $rows->whereHas('note', function($noteQuery){
  166. return $noteQuery->whereHas('bills', function($billQuery) {
  167. return $billQuery->where('is_verified', false)->where('is_cancelled', false);
  168. });
  169. });
  170. }
  171. }
  172. $rows = $rows->paginate(50);
  173. $claimStatuses = DB::select('SELECT distinct status FROM claim ORDER BY status DESC');
  174. return view('app.practice-management.billing-report', compact('rows', 'claimStatuses', 'filters'));
  175. }
  176. public function dashboard(Request $request)
  177. {
  178. return view('app.practice-management.dashboard');
  179. }
  180. public function rates(Request $request, $selectedProUid = 'all')
  181. {
  182. $proUid = $selectedProUid ? $selectedProUid : 'all';
  183. $rates = ProRate::where('is_active', true);
  184. if ($proUid !== 'all') {
  185. $selectedPro = Pro::where('uid', $proUid)->first();
  186. $rates = $rates->where('pro_id', $selectedPro->id);
  187. }
  188. $rates = $rates->orderBy('pro_id', 'asc')->get();
  189. $pros = $this->pros;
  190. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  191. }
  192. public function previousBills(Request $request)
  193. {
  194. return view('app.practice-management.previous-bills');
  195. }
  196. public function financialTransactions(Request $request)
  197. {
  198. $pro = $this->performer()->pro;
  199. $transactions = null;
  200. if($pro->pro_type === 'ADMIN') {
  201. $transactions = ProTransaction::whereNotNull('id');
  202. }
  203. else {
  204. $transactions = ProTransaction::where('pro_id', $pro->id);
  205. }
  206. $filter = $request->input('p');
  207. if ($filter) {
  208. $filterPro = Pro::where('uid', $filter)->first();
  209. if($filterPro) {
  210. $transactions = $transactions->where('pro_id', '=', $filterPro->id);
  211. }
  212. }
  213. $filter = $request->input('t');
  214. if ($filter) {
  215. $transactions = $transactions->where('plus_or_minus', '=', $filter);
  216. }
  217. $filter = $request->input('c');
  218. if ($filter) {
  219. $transactions = $transactions->where('company_id', '=', $filter);
  220. }
  221. $filter = $request->input('bs');
  222. if ($filter) {
  223. $transactions = $transactions->where('created_at', '>=', $filter);
  224. }
  225. $filter = $request->input('be');
  226. if ($filter) {
  227. $transactions = $transactions->where('created_at', '<=', $filter);
  228. }
  229. $transactions = $transactions->orderBy('created_at', 'desc')->paginate();
  230. $companies = Company::where('is_active', true)->orderBy('name')->get();
  231. return view('app.practice-management.financial-transactions', compact('transactions', 'companies'));
  232. }
  233. public function pendingBillsToSign(Request $request)
  234. {
  235. return view('app.practice-management.pending-bills-to-sign');
  236. }
  237. public function HR(Request $request)
  238. {
  239. return view('app.practice-management.hr');
  240. }
  241. public function directDepositSettings(Request $request)
  242. {
  243. return view('app.practice-management.direct-deposit-settings');
  244. }
  245. public function w9(Request $request)
  246. {
  247. return view('app.practice-management.w9');
  248. }
  249. public function contract(Request $request)
  250. {
  251. return view('app.practice-management.contract');
  252. }
  253. public function notes(Request $request, $filter = '')
  254. {
  255. $proID = $this->performer()->pro->id;
  256. $query = Note::where('hcp_pro_id', $proID);
  257. switch ($filter) {
  258. case 'not-yet-signed':
  259. $query = $query->where('is_signed_by_hcp', false);
  260. break;
  261. case 'not-yet-signed-but-ally-signed':
  262. $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
  263. break;
  264. case 'without-bills':
  265. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  266. break;
  267. // more cases can be added as needed
  268. default:
  269. break;
  270. }
  271. $notes = $query->orderBy('created_at', 'desc')->paginate(30);
  272. return view('app.practice-management.notes', compact('notes', 'filter'));
  273. }
  274. public function allNotes(Request $request)
  275. {
  276. $query = Note::query();
  277. $filters = $request->all();
  278. $this->filterMultiQuery($request, $query, 'created_at', 'created_at_category', 'created_at_value_1', 'created_at_value_2');
  279. $this->filterMultiQuery($request, $query, 'effective_dateest', 'effective_date_category', 'effective_date_value_1', 'effective_date_value_2');
  280. $filterByProsIDs = $request->get('pros');
  281. if($filterByProsIDs && count($filterByProsIDs)){
  282. $query = $query->whereIn('hcp_pro_id', $filterByProsIDs);
  283. }
  284. $filterByAllyProsIDs = $request->get('ally_pros');
  285. if($filterByAllyProsIDs && count($filterByAllyProsIDs)){
  286. $query = $query->whereIn('ally_pro_id', $filterByAllyProsIDs);
  287. }
  288. $filterByPatientsIDs = $request->get('patients');
  289. if($filterByPatientsIDs && count($filterByPatientsIDs)){
  290. $query = $query->whereIn('client_id', $filterByPatientsIDs);
  291. }
  292. $status = $request->get('status');
  293. if($status){
  294. if($status == 'CANCELLED') $query = $query->where('is_cancelled', true);
  295. if($status == 'SIGNED') $query = $query->where('is_signed_by_hcp', true);
  296. if($status == 'NOT_YET_SIGNED') $query = $query->where('is_signed_by_hcp', false);
  297. if($status == 'NOT_YET_SIGNED_BUT_ALLY_SIGNED') $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
  298. if($status == 'WITH_BILLS') $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereHas('bills');
  299. if($status == 'WITHOUT_BILLS') $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  300. }
  301. $allProsWithNotesIDs = DB::table('note')->pluck('hcp_pro_id')->toArray();
  302. $allProsWithNotes = Pro::whereIn('id', $allProsWithNotesIDs)->get();
  303. $allPatientsWithNotesIDs = DB::table('note')->pluck('client_id')->toArray();
  304. $allPatientsWithNotes = Client::whereIn('id', $allPatientsWithNotesIDs)->get();
  305. $allAllyProsIDs = DB::table('note')->pluck('ally_pro_id')->toArray();
  306. $allAllyPros = Pro::whereIn('id', $allAllyProsIDs)->get();
  307. $notes = $query->orderBy('created_at', 'desc')->paginate(30);
  308. return view('app.practice-management.all-notes', compact('notes', 'filters','allProsWithNotes', 'allPatientsWithNotes', 'allAllyPros'));
  309. }
  310. public function dnaNotesPendingMcpSign(Request $request)
  311. {
  312. $proID = $this->performer()->pro->id;
  313. $notes = Note::where('ally_pro_id', $proID)
  314. ->where('is_signed_by_hcp', false)
  315. ->where('is_cancelled', false)
  316. ->orderBy('created_at', 'desc')
  317. ->get();
  318. return view('app.practice-management.dna-notes-pending-mcp-sign', compact('notes'));
  319. }
  320. public function naBillableSignedNotes(Request $request)
  321. {
  322. $notes = Note
  323. ::where('is_signed_by_hcp', TRUE)
  324. ->where('ally_pro_id', $this->performer()->pro->id)
  325. ->where('is_cancelled', FALSE)
  326. ->whereRaw("
  327. (
  328. SELECT count(bill.id)
  329. FROM bill WHERE
  330. bill.is_cancelled = FALSE AND
  331. bill.generic_pro_id = {$this->performer()->pro->id} AND
  332. bill.note_id = note.id
  333. ) = 0
  334. ");
  335. $notes = $notes->orderBy('created_at', 'desc')->get();
  336. return view('app.practice-management.na-billable-signed-notes', compact('notes'));
  337. }
  338. public function bills(Request $request, $filter = '')
  339. {
  340. $proID = $this->performer()->pro->id;
  341. $query = Bill::where('is_cancelled', false)->where('bill_service_type', '<>', 'CARE_MONTH');
  342. switch ($filter) {
  343. case 'not-yet-signed':
  344. $query = $query
  345. ->where(function ($q) use ($proID) {
  346. $q->where(function ($q2) use ($proID) {
  347. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  348. })
  349. ->orWhere(function ($q2) use ($proID) {
  350. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  351. })
  352. ->orWhere(function ($q2) use ($proID) {
  353. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  354. })
  355. ->orWhere(function ($q2) use ($proID) {
  356. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  357. })
  358. ->orWhere(function ($q2) use ($proID) {
  359. $q2->where('generic_pro_id', $proID)->where('is_signed_by_generic_pro', false);
  360. });
  361. });
  362. break;
  363. case 'previous':
  364. $query = $query
  365. ->where(function ($q) use ($proID) {
  366. $q->where(function ($q2) use ($proID) {
  367. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  368. })
  369. ->orWhere(function ($q2) use ($proID) {
  370. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  371. })
  372. ->orWhere(function ($q2) use ($proID) {
  373. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  374. })
  375. ->orWhere(function ($q2) use ($proID) {
  376. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  377. });
  378. });
  379. break;
  380. // more cases can be added as needed
  381. default:
  382. $query = $query
  383. ->where(function ($q) use ($proID) {
  384. $q->where(function ($q2) use ($proID) {
  385. $q2->where('hcp_pro_id', $proID);
  386. })
  387. ->orWhere(function ($q2) use ($proID) {
  388. $q2->where('cm_pro_id', $proID);
  389. })
  390. ->orWhere(function ($q2) use ($proID) {
  391. $q2->where('rme_pro_id', $proID);
  392. })
  393. ->orWhere(function ($q2) use ($proID) {
  394. $q2->where('rmm_pro_id', $proID);
  395. });
  396. });
  397. break;
  398. }
  399. $bills = $query->orderBy('created_at', 'desc')->get();
  400. return view('app.practice-management.bills', compact('bills', 'filter'));
  401. }
  402. public function rmBillsToSign(Request $request)
  403. {
  404. $performerProID = $this->performer()->pro->id;
  405. $bills = Bill::where('is_cancelled', false)->where('cm_or_rm', 'RM')
  406. ->where(function ($q) use ($performerProID) {
  407. $q
  408. ->where(function ($q2) use ($performerProID) {
  409. $q2->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false);
  410. })
  411. ->orWhere(function ($q2) use ($performerProID) {
  412. $q2->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false);
  413. })
  414. ->orWhere(function ($q2) use ($performerProID) {
  415. $q2->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false);
  416. })
  417. ->orWhere(function ($q2) use ($performerProID) {
  418. $q2->where('generic_pro_id', $performerProID)->where('is_signed_by_generic_pro', false);
  419. });
  420. })
  421. ->orderBy('effective_date', 'desc')
  422. ->get();
  423. return view('app.practice-management.rm-bills-to-sign', compact('bills'));
  424. }
  425. public function unacknowledgedCancelledBills(Request $request)
  426. {
  427. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  428. ->where('is_cancelled', true)
  429. ->where('is_cancellation_acknowledged', false)
  430. ->orderBy('created_at', 'desc')
  431. ->get();
  432. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  433. }
  434. public function myTickets(Request $request, $filter = 'open')
  435. {
  436. $performer = $this->performer();
  437. $myTickets = Ticket::where(function ($q) use ($performer) {
  438. $q->where('assigned_pro_id', $performer->pro_id)
  439. ->orWhere('manager_pro_id', $performer->pro_id)
  440. ->orWhere('ordering_pro_id', $performer->pro_id)
  441. ->orWhere('initiating_pro_id', $performer->pro_id);
  442. })->where('category', 'other');
  443. if ($filter === 'open') {
  444. $myTickets = $myTickets->where('is_open', true);
  445. } else if ($filter === 'closed') {
  446. $myTickets = $myTickets->where('is_open', false);
  447. }
  448. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  449. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  450. }
  451. public function myTextShortcuts(Request $request)
  452. {
  453. $personalShortcuts = DB::table('pro_text_shortcut')
  454. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  455. ->select(
  456. 'pro_text_shortcut.uid',
  457. 'pro_text_shortcut.shortcut',
  458. 'pro_text_shortcut.text',
  459. 'pro.name_first',
  460. 'pro.name_last'
  461. )
  462. ->where('pro_text_shortcut.is_removed', false);
  463. if($this->performer()->pro->pro_type !== 'ADMIN') {
  464. $personalShortcuts = $personalShortcuts->where('pro_id', $this->performer()->pro_id);
  465. }
  466. $personalShortcuts = $personalShortcuts
  467. ->orderBy('pro.name_last')
  468. ->orderBy('pro.name_first')
  469. ->orderBy('pro_text_shortcut.shortcut')
  470. ->get();
  471. $globalTextShortcuts = DB::table('pro_text_shortcut')
  472. ->select(
  473. 'pro_text_shortcut.uid',
  474. 'pro_text_shortcut.shortcut',
  475. 'pro_text_shortcut.text'
  476. )
  477. ->whereNull('pro_id')
  478. ->where('pro_text_shortcut.is_removed', false)
  479. ->orderBy('pro_text_shortcut.shortcut')
  480. ->get();
  481. return view('app.practice-management.my-text-shortcuts', compact('personalShortcuts', 'globalTextShortcuts'));
  482. }
  483. public function myFavorites(Request $request, $filter = 'all')
  484. {
  485. $performer = $this->performer();
  486. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)->where('is_removed', false);
  487. if ($filter !== 'all') {
  488. $myFavorites = $myFavorites->where('category', $filter);
  489. }
  490. $myFavorites = $myFavorites
  491. ->whereIn('category', ['allergy', 'medication', 'problem'])
  492. ->orderBy('category', 'asc')
  493. ->orderBy('position_index', 'asc')
  494. ->get();
  495. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  496. }
  497. public function patientsWithoutCoverage(Request $request, $filter = 'all')
  498. {
  499. $performer = $this->performer();
  500. $sql = "SELECT cl.uid, cl.name_first, cl.name_last FROM client cl ";
  501. $joinClause = 'LEFT JOIN client_primary_coverage cpc ON cl.latest_client_primary_coverage_id = cpc.id ';
  502. $withoutCondition = "cl.latest_client_primary_coverage_id IS NULL";
  503. $pendingCondition = "
  504. (cl.latest_client_primary_coverage_id IS NOT NULL -- coverage exists, but status is null or unknown
  505. AND (
  506. (cpc.plan_type = 'MEDICARE' AND (cpc.is_partbprimary = 'UNKNOWN' OR cpc.is_partbprimary IS NULL))
  507. OR
  508. (cpc.plan_type != 'MEDICARE' AND
  509. (cpc.manual_determination_category = 'UNKNOWN' OR cpc.manual_determination_category IS NULL))
  510. ))";
  511. switch ($filter) {
  512. case 'without-coverage-information':
  513. $sql .= 'WHERE cl.shadow_pro_id IS NULL AND ';
  514. $sql .= $withoutCondition;
  515. break;
  516. case 'pending-coverage-verification':
  517. $sql .= $joinClause . 'WHERE cl.shadow_pro_id IS NULL AND ';
  518. $sql .= $pendingCondition;
  519. break;
  520. default:
  521. $sql .= $joinClause . 'WHERE cl.shadow_pro_id IS NULL AND ';
  522. $sql .= '(' . $withoutCondition . ' OR ' . $pendingCondition . ')';
  523. break;
  524. }
  525. $sql .= ' ORDER BY cl.name_first ASC';
  526. $pendingCoverage = DB::select(DB::raw($sql));
  527. return view('app.practice-management.patients-without-coverage', compact('pendingCoverage', 'filter'));
  528. }
  529. public function proAvailability(Request $request, $proUid = null)
  530. {
  531. $performer = $this->performer();
  532. $pro = $performer->pro;
  533. if ($proUid) {
  534. $pro = Pro::where('uid', $proUid)->first();
  535. }
  536. if ($request->get('pro_uid')) {
  537. $proUid = $request->get('pro_uid');
  538. $pro = Pro::where('uid', $proUid)->first();
  539. }
  540. $selectedProUid = $pro->uid;
  541. $pros = $this->pros;
  542. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  543. $generalAvailabilities = [
  544. 'MONDAY' => [],
  545. 'TUESDAY' => [],
  546. 'WEDNESDAY' => [],
  547. 'THURSDAY' => [],
  548. 'FRIDAY' => [],
  549. 'SATURDAY' => [],
  550. 'SUNDAY' => [],
  551. ];
  552. foreach ($generalAvailabilitiesList as $ga) {
  553. if ($ga->day_of_week == 'MONDAY') {
  554. $generalAvailabilities['MONDAY'][] = $ga;
  555. }
  556. if ($ga->day_of_week == 'TUESDAY') {
  557. $generalAvailabilities['TUESDAY'][] = $ga;
  558. }
  559. if ($ga->day_of_week == 'WEDNESDAY') {
  560. $generalAvailabilities['WEDNESDAY'][] = $ga;
  561. }
  562. if ($ga->day_of_week == 'THURSDAY') {
  563. $generalAvailabilities['THURSDAY'][] = $ga;
  564. }
  565. if ($ga->day_of_week == 'FRIDAY') {
  566. $generalAvailabilities['FRIDAY'][] = $ga;
  567. }
  568. if ($ga->day_of_week == 'SATURDAY') {
  569. $generalAvailabilities['SATURDAY'][] = $ga;
  570. }
  571. if ($ga->day_of_week == 'SUNDAY') {
  572. $generalAvailabilities['SUNDAY'][] = $ga;
  573. }
  574. }
  575. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  576. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  577. //events for the calendar
  578. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  579. $endDateTime = new DateTime($startDate);
  580. $endDateTime->modify('+6 day');
  581. $endDate = $endDateTime->format("Y-m-d");
  582. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  583. $events = json_encode($eventsData);
  584. return view(
  585. 'app.practice-management.pro-availability',
  586. compact(
  587. 'pros',
  588. 'generalAvailabilities',
  589. 'specificAvailabilities',
  590. 'specificUnavailabilities',
  591. 'events',
  592. 'selectedProUid'
  593. )
  594. );
  595. }
  596. public function loadAvailability(Request $request, $proUid)
  597. {
  598. $performer = $this->performer();
  599. $pro = $performer->pro;
  600. $startDate = $request->get('start');
  601. $endDate = $request->get('end');
  602. $selectedPro = Pro::where('uid', $proUid)->first();
  603. return $selectedPro->getAvailabilityEvents($startDate, $endDate, $request->input('tz') ? $request->input('tz') : 'EASTERN');
  604. }
  605. public function proAvailabilityFilter(Request $request)
  606. {
  607. $proUid = $request->get('proUid');
  608. return ['success' => true, 'data' => $proUid];
  609. }
  610. // video call page (RHS)
  611. // generic call handle (no uid)
  612. // specific call handle (uid of client)
  613. public function meet(Request $request, $uid = false)
  614. {
  615. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  616. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  617. if (!empty($client)) {
  618. return view('app.video.call-minimal', compact('session', 'client'));
  619. }
  620. return view('app.video.call-agora-v2', compact('session', 'client'));
  621. }
  622. // check video page
  623. public function checkVideo(Request $request, $uid)
  624. {
  625. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  626. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  627. $publish = false;
  628. return view('app.video.check-video-minimal', compact('session', 'client'));
  629. }
  630. public function getParticipantInfo(Request $request)
  631. {
  632. $sid = intval($request->get('uid')) - 1000000;
  633. $session = AppSession::where('id', $sid)->first();
  634. $result = [
  635. "type" => '',
  636. "name" => ''
  637. ];
  638. if ($session) {
  639. $result["type"] = $session->session_type;
  640. switch ($session->session_type) {
  641. case 'PRO':
  642. $pro = Pro::where('id', $session->pro_id)->first();
  643. $result["name"] = $pro->displayName();
  644. break;
  645. case 'CLIENT':
  646. $client = Client::where('id', $session->client_id)->first();
  647. $result["name"] = $client->displayName();
  648. break;
  649. }
  650. }
  651. return json_encode($result);
  652. }
  653. // ajax ep used by the video page
  654. // this is needed bcoz meet() is used not
  655. // just for the client passed to the view
  656. public function getOpentokSessionKey(Request $request, $uid)
  657. {
  658. $client = Client::where('uid', $uid)->first();
  659. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  660. }
  661. // poll to check if there are patients with active mcp requests
  662. public function getPatientsInQueue(Request $request)
  663. {
  664. $myInitiatives = $this->performer->pro->initiatives;
  665. if ($myInitiatives) {
  666. $myInitiatives = strtoupper($myInitiatives);
  667. }
  668. $myInitiativesList = explode('|', $myInitiatives);
  669. $myForeignLanguages = $this->performer->pro->foreign_languages;
  670. if ($myForeignLanguages) {
  671. $myForeignLanguages = strtoupper($myForeignLanguages);
  672. }
  673. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  674. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  675. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  676. })
  677. ->where(function ($query) use ($myForeignLanguagesList) {
  678. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  679. })->limit(3)->get();
  680. $results = [];
  681. foreach ($clients as $client) {
  682. $results[] = [
  683. 'clientUid' => $client->uid,
  684. 'name' => $client->displayName(),
  685. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  686. ];
  687. }
  688. return json_encode($results);
  689. }
  690. public function currentWork(Request $request)
  691. {
  692. return view('app/current-work');
  693. }
  694. public function calendar(Request $request, $proUid = null)
  695. {
  696. $pros = Pro::all();
  697. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  698. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  699. $accessibleProIds = [];
  700. foreach ($accessiblePros as $accessiblePro) {
  701. $accessibleProIds[] = $accessiblePro->id;
  702. }
  703. $accessibleProIds[] = $this->pro->id;
  704. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  705. }
  706. return view('app.practice-management.calendar', compact('pros'));
  707. }
  708. public function cellularDeviceManager(Request $request, $proUid = null)
  709. {
  710. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  711. $performerPro = $this->performer->pro;
  712. $targetPro = null;
  713. $expectedForHcp = null;
  714. if ($performerPro->pro_type == 'ADMIN') {
  715. $targetPro = Pro::where('uid', $proUid)->first();
  716. } else {
  717. $targetPro = $performerPro;
  718. }
  719. $clients = [];
  720. if ($targetPro) {
  721. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(20);
  722. } else {
  723. $clients = Client::orderBy('created_at', 'desc')->paginate(20);
  724. }
  725. return view('app.practice-management.cellular-device-manager', compact('clients', 'targetPro', 'proUid'));
  726. }
  727. public function treatmentServiceUtil(Request $request)
  728. {
  729. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  730. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  731. $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"));
  732. return view('app.practice-management.treatment-services-util', compact(
  733. 'view_treatment_service_utilization_org',
  734. 'view_treatment_service_utilization',
  735. 'view_treatment_service_utilization_by_patient'));
  736. }
  737. public function processingBillMatrix(Request $request, $proUid = null)
  738. {
  739. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  740. $performerPro = $this->performer->pro;
  741. $targetPro = null;
  742. if ($performerPro->pro_type == 'ADMIN') {
  743. $targetPro = Pro::where('uid', $proUid)->first();
  744. } else {
  745. $targetPro = $performerPro;
  746. }
  747. $bills = Bill::where('is_cancelled', false);
  748. if(!$request->input('t') || $request->input('t') === 'hcp') {
  749. $bills = $bills
  750. ->where('has_hcp_been_paid', false)
  751. ->where('hcp_expected_payment_amount', '>', 0)
  752. ->where('is_signed_by_hcp', true);
  753. if ($targetPro) {
  754. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  755. }
  756. if($request->input('c')) {
  757. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = hcp_company_pro_id) = ' . $request->input('c'));
  758. }
  759. }
  760. else if($request->input('t') === 'na') {
  761. $bills = $bills
  762. ->where('has_generic_pro_been_paid', false)
  763. ->where('generic_pro_expected_payment_amount', '>', 0)
  764. ->where('is_signed_by_generic_pro', true);
  765. if ($targetPro) {
  766. $bills = $bills->where('generic_pro_id', $targetPro->id);
  767. }
  768. if($request->input('c')) {
  769. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = generic_company_pro_id) = ' . $request->input('c'));
  770. }
  771. }
  772. $filter = $request->input('f');
  773. switch ($filter) {
  774. case 'verified':
  775. $bills = $bills->where('is_verified', true);
  776. break;
  777. case 'not-verified':
  778. $bills = $bills->where('is_verified', false);
  779. break;
  780. }
  781. $filter = $request->input('bs');
  782. if ($filter) {
  783. $bills = $bills->where('balance_post_date', '>=', $filter);
  784. }
  785. $filter = $request->input('be');
  786. if ($filter) {
  787. $bills = $bills->where('balance_post_date', '<=', $filter);
  788. }
  789. $filter = $request->input('s');
  790. if ($filter) {
  791. $bills = $bills->where('code', '=', $filter);
  792. }
  793. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  794. $companies = Company::where('is_active', true)->orderBy('name')->get();
  795. $codes = DB::select(DB::raw("SELECT code, count(*) as count FROM bill WHERE is_cancelled IS FALSE GROUP BY code ORDER BY code"));
  796. $viewData = [
  797. 'bills' => $bills,
  798. 'targetPro' => $targetPro,
  799. 'performerPro' => $performerPro,
  800. 'proUid' => $proUid,
  801. 'companies' => $companies,
  802. 'codes' => $codes
  803. ];
  804. return view('app.practice-management.processing-bill-matrix', $viewData);
  805. }
  806. public function hcpNoteActivity(Request $request)
  807. {
  808. $filters = $request->all();
  809. $from_date = $request->get('from_date');
  810. // if(!$from_date) {
  811. // $from_date = '2010-01-01';
  812. // }
  813. $to_date = $request->get('to_date');
  814. // if(!$to_date){
  815. // $to_date = '2900-12-31';
  816. // }
  817. //TODO sanitize inputs
  818. $proUid = $request->get('pro-uid');
  819. $now = Carbon::now();
  820. $startOfCurrentMonth = $now->startOfMonth('Y-m-d');
  821. $startOfCurrentMonth = $startOfCurrentMonth->toDateString();
  822. $endOfCurrentMonth = $now->endOfMonth()->format('Y-m-d');
  823. $rows = DB::table('pro')
  824. ->selectRaw(" *,
  825. (SELECT id FROM note WHERE note.hcp_pro_id = pro.id AND is_cancelled = false ORDER BY created_at DESC LIMIT 1) as last_note_id,
  826. (SELECT note.created_at FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false ORDER BY note.created_at DESC LIMIT 1) as last_note_created_at,
  827. (SELECT COUNT(*) FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false AND created_at::DATE >= '${startOfCurrentMonth}'::DATE AND created_at::DATE <= '${endOfCurrentMonth}'::DATE ) AS notes_this_month,
  828. (SELECT COUNT(*) FROM client WHERE client.mcp_pro_id = pro.id AND client.client_engagement_status_category <> 'DUMMY' AND client.client_engagement_status_category <> 'DUPLICATE') as total_client_count,
  829. (SELECT COUNT(*) FROM client WHERE client.mcp_pro_id = pro.id AND client.client_engagement_status_category <> 'DUMMY' AND client.client_engagement_status_category <> 'DUPLICATE' AND client.most_recent_completed_mcp_note_date >= (NOW() - interval '60 days')::DATE) as active_client_count
  830. " . (!!$from_date && !!$to_date ? ",(SELECT COUNT(*) FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false AND created_at::DATE >= '${from_date}'::DATE AND created_at::DATE <= '${to_date}'::DATE ) AS notes_this_period" : ""));
  831. $prosIDs = $request->get('pros_ids');
  832. if($prosIDs){
  833. $pros =
  834. $rows = $rows->whereIn('pro.id', $prosIDs);
  835. }
  836. $sortBy = $request->input('sort_by') ?: 'name_first';
  837. $sortDir = $request->input('sort_dir') ?: 'ASC';
  838. $rows = $rows->orderByRaw("$sortBy $sortDir NULLS LAST");
  839. $rows = $rows->paginate(50);
  840. return view('app.practice-management.hcp-note-activity', compact('rows', 'filters'));
  841. }
  842. public function proFinancials(Request $request, $proUid = null)
  843. {
  844. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  845. $performerPro = $this->performer->pro;
  846. $targetPro = null;
  847. if ($performerPro->pro_type == 'ADMIN') {
  848. $targetPro = Pro::where('uid', $proUid)->first();
  849. } else {
  850. $targetPro = $performerPro;
  851. }
  852. $fPros = Pro::whereNotNull('balance');
  853. if($targetPro) {
  854. $fPros = $fPros->where('uid', $targetPro->uid);
  855. }
  856. $fPros = $fPros
  857. ->where('balance', '>', 0)
  858. ->orderBy('name_first')
  859. ->orderBy('name_last')
  860. ->paginate();
  861. return view('app.practice-management.pro-financials', compact('fPros', 'targetPro'));
  862. }
  863. public function hcpBillMatrix(Request $request, $proUid = null)
  864. {
  865. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  866. $performerPro = $this->performer->pro;
  867. $targetPro = null;
  868. $allPros = [];
  869. $expectedForHcp = null;
  870. if ($performerPro->pro_type == 'ADMIN') {
  871. $allPros = Pro::all();
  872. $targetPro = Pro::where('uid', $proUid)->first();
  873. } else {
  874. $targetPro = $performerPro;
  875. }
  876. $rows = [];
  877. if ($targetPro) {
  878. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  879. } else {
  880. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  881. }
  882. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  883. }
  884. public function billingManager(Request $request, $proUid = null)
  885. {
  886. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  887. $performerPro = $this->performer->pro;
  888. $targetPro = null;
  889. $allPros = [];
  890. $expectedForHcp = null;
  891. if ($performerPro->pro_type == 'ADMIN') {
  892. $allPros = Pro::query()->orderBy('name_first', 'asc')->get();
  893. $targetPro = Pro::where('uid', $proUid)->first();
  894. } else {
  895. $targetPro = $performerPro;
  896. }
  897. $notes = [];
  898. if ($targetPro) {
  899. $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;
  900. $notes = Note::where('hcp_pro_id', $targetPro->id);
  901. } else {
  902. $notes = Note::where('id', '>', 0);
  903. }
  904. if($request->input('date')) {
  905. $notes = $notes->where('effective_dateest', $request->input('date'));
  906. }
  907. $filters = [];
  908. $filters['bills_created'] = $request->input('bills_created');
  909. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  910. $filters['bills_resolved'] = $request->input('bills_resolved');
  911. $filters['bills_closed'] = $request->input('bills_closed');
  912. $filters['claims_created'] = $request->input('claims_created');
  913. $filters['claims_closed'] = $request->input('claims_closed');
  914. if ($filters['bills_created']) {
  915. $notes->where(
  916. 'bill_total_expected',
  917. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  918. 0);
  919. }
  920. if ($filters['is_billing_marked_done']) {
  921. $notes->where(
  922. 'is_billing_marked_done',
  923. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  924. true);
  925. }
  926. if ($filters['bills_resolved']) {
  927. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  928. if ($filters['bills_resolved'] === 'yes') {
  929. $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');
  930. } elseif ($filters['bills_resolved'] === 'no') {
  931. $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');
  932. }
  933. }
  934. if ($filters['bills_closed']) {
  935. $notes->where(
  936. 'is_bill_closed',
  937. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  938. true);
  939. }
  940. if ($filters['claims_created']) {
  941. $notes->where(
  942. 'claim_total_expected',
  943. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  944. 0);
  945. }
  946. if ($filters['claims_closed']) {
  947. $notes->where(
  948. 'is_claim_closed',
  949. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  950. true);
  951. }
  952. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(20);
  953. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  954. }
  955. public function remoteMonitoring(Request $request) {
  956. $performer = $this->performer();
  957. // abort_if($performer->pro->pro_type !== 'ADMIN' && !$performer->pro->can_view_rm_matrix, 403);
  958. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  959. $careMonthStart = date($ym . '-01');
  960. $rc = $request->input('rc') ?: 1;
  961. $rc2 = $request->input('rc2') ?: 2;
  962. $conditions = [];// $this->rpmConditions($performer, $rc, $rc2);
  963. $sortBy = $request->input('sort_by') ?: 'name_first';
  964. $sortDir = $request->input('sort_dir') ?: 'ASC';
  965. $orderBy = "ORDER BY $sortBy $sortDir NULLS LAST";
  966. $query = "
  967. SELECT client.name_first, client.name_last,
  968. (client.name_first || ' ' || client.name_last) as client_name,
  969. client.uid as client_uid,
  970. client.dob,
  971. client.is_enrolled_in_rm,
  972. client.most_recent_completed_mcp_note_date,
  973. client.most_recent_completed_mcp_note_id,
  974. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  975. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  976. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  977. client.cell_number,
  978. client.is_assigned_cellular_bp_device,
  979. client.is_assigned_cellular_weight_scale_device,
  980. care_month.uid as care_month_uid,
  981. care_month.id as care_month_id,
  982. care_month.start_date,
  983. care_month.rm_total_time_in_seconds_by_mcp,
  984. care_month.rm_total_time_in_seconds_by_rmm_pro,
  985. care_month.number_of_days_with_remote_measurements,
  986. care_month.has_mcp_interacted_with_client_about_rm,
  987. care_month.rm_num_measurements_not_stamped_by_mcp,
  988. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  989. care_month.rm_num_measurements_not_stamped_by_rmm,
  990. care_month.rm_num_measurements_not_stamped_by_rme,
  991. care_month.mcp_pro_id as care_month_mcp_pro_id,
  992. care_month.rmm_pro_id as care_month_rmm_pro_id,
  993. client.mcp_pro_id,
  994. client.default_na_pro_id,
  995. client.rmm_pro_id,
  996. client.rme_pro_id,
  997. client.cell_number,
  998. care_month.most_recent_cellular_bp_dbp_mm_hg,
  999. care_month.most_recent_cellular_bp_sbp_mm_hg,
  1000. care_month.most_recent_cellular_bp_measurement_at,
  1001. care_month.most_recent_cellular_weight_value,
  1002. care_month.most_recent_cellular_weight_measurement_at
  1003. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  1004. left join note mrmnote on mrmnote.id = (
  1005. select max(n.id) from note n
  1006. where
  1007. n.client_id = client.id AND
  1008. n.is_cancelled = FALSE AND
  1009. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  1010. n.effective_dateest::date >= care_month.start_date::date AND
  1011. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  1012. )
  1013. WHERE
  1014. (care_month.mcp_pro_id = {$performer->pro->id} OR care_month.rmm_pro_id = {$performer->pro->id})
  1015. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1016. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1017. " . (count($conditions) > 0 ? 'AND ' . implode(" AND ", $conditions) : '') . "
  1018. $orderBy
  1019. ";
  1020. $patients = DB::select($query);
  1021. $timestamp = strtotime(date('Y-m-d'));
  1022. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  1023. return view('app.practice-management.remote-monitoring', compact('patients', 'daysRemaining', 'careMonthStart'));
  1024. }
  1025. public function remoteMonitoring_Row(Request $request) {
  1026. $clientUid = $request->input('clientUid');
  1027. $careMonthUid = $request->input('careMonthUid');
  1028. $query = "
  1029. SELECT client.name_first, client.name_last,
  1030. (client.name_first || ' ' || client.name_last) as client_name,
  1031. client.uid as client_uid,
  1032. client.dob,
  1033. client.is_enrolled_in_rm,
  1034. client.most_recent_completed_mcp_note_date,
  1035. client.most_recent_completed_mcp_note_id,
  1036. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  1037. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  1038. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  1039. client.cell_number,
  1040. client.is_assigned_cellular_bp_device,
  1041. client.is_assigned_cellular_weight_scale_device,
  1042. care_month.uid as care_month_uid,
  1043. care_month.id as care_month_id,
  1044. care_month.start_date,
  1045. care_month.rm_total_time_in_seconds_by_mcp,
  1046. care_month.rm_total_time_in_seconds_by_rmm_pro,
  1047. care_month.number_of_days_with_remote_measurements,
  1048. care_month.has_mcp_interacted_with_client_about_rm,
  1049. care_month.rm_num_measurements_not_stamped_by_mcp,
  1050. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  1051. care_month.rm_num_measurements_not_stamped_by_rmm,
  1052. care_month.rm_num_measurements_not_stamped_by_rme,
  1053. care_month.mcp_pro_id as care_month_mcp_pro_id,
  1054. care_month.rmm_pro_id as care_month_rmm_pro_id,
  1055. client.mcp_pro_id,
  1056. client.default_na_pro_id,
  1057. client.rmm_pro_id,
  1058. client.rme_pro_id,
  1059. client.cell_number,
  1060. care_month.most_recent_cellular_bp_dbp_mm_hg,
  1061. care_month.most_recent_cellular_bp_sbp_mm_hg,
  1062. care_month.most_recent_cellular_bp_measurement_at,
  1063. care_month.most_recent_cellular_weight_value,
  1064. care_month.most_recent_cellular_weight_measurement_at
  1065. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  1066. left join note mrmnote on mrmnote.id = (
  1067. select max(n.id) from note n
  1068. where
  1069. n.client_id = client.id AND
  1070. n.is_cancelled = FALSE AND
  1071. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  1072. n.effective_dateest::date >= care_month.start_date::date AND
  1073. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  1074. )
  1075. WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
  1076. ";
  1077. $patients = DB::select($query);
  1078. return view('app.practice-management.remote-monitoring-row', ['iPatient' => $patients[0], 'trIndex' => $request->input('trIndex')]);
  1079. }
  1080. private function remoteMonitoring_RowByProType(Request $request, $mode) {
  1081. $clientUid = $request->input('clientUid');
  1082. $careMonthUid = $request->input('careMonthUid');
  1083. $query = "
  1084. SELECT client.name_first, client.name_last,
  1085. (client.name_first || ' ' || client.name_last) as client_name,
  1086. client.uid as client_uid,
  1087. client.dob,
  1088. client.is_enrolled_in_rm,
  1089. client.most_recent_completed_mcp_note_date,
  1090. client.most_recent_completed_mcp_note_id,
  1091. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  1092. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  1093. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  1094. client.cell_number,
  1095. client.is_assigned_cellular_bp_device,
  1096. client.is_assigned_cellular_weight_scale_device,
  1097. care_month.uid as care_month_uid,
  1098. care_month.id as care_month_id,
  1099. care_month.start_date,
  1100. care_month.rm_total_time_in_seconds_by_mcp,
  1101. care_month.rm_total_time_in_seconds_by_rmm_pro,
  1102. care_month.number_of_days_with_remote_measurements,
  1103. care_month.has_mcp_interacted_with_client_about_rm,
  1104. care_month.rm_num_measurements_not_stamped_by_mcp,
  1105. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  1106. care_month.rm_num_measurements_not_stamped_by_rmm,
  1107. care_month.rm_num_measurements_not_stamped_by_rme,
  1108. care_month.mcp_pro_id as care_month_mcp_pro_id,
  1109. care_month.rmm_pro_id as care_month_rmm_pro_id,
  1110. client.mcp_pro_id,
  1111. client.default_na_pro_id,
  1112. client.rmm_pro_id,
  1113. client.rme_pro_id,
  1114. client.cell_number,
  1115. care_month.most_recent_cellular_bp_dbp_mm_hg,
  1116. care_month.most_recent_cellular_bp_sbp_mm_hg,
  1117. care_month.most_recent_cellular_bp_measurement_at,
  1118. care_month.most_recent_cellular_weight_value,
  1119. care_month.most_recent_cellular_weight_measurement_at
  1120. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  1121. left join note mrmnote on mrmnote.id = (
  1122. select max(n.id) from note n
  1123. where
  1124. n.client_id = client.id AND
  1125. n.is_cancelled = FALSE AND
  1126. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  1127. n.effective_dateest::date >= care_month.start_date::date AND
  1128. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  1129. )
  1130. WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
  1131. ";
  1132. $patients = DB::select($query);
  1133. return view('app.practice-management.remote-monitoring-row-by-pro-type', ['iPatient' => $patients[0], 'trIndex' => $request->input('trIndex'), 'mode' => $mode]);
  1134. }
  1135. private function rpmMatrixByProType(Request $request, $mode) {
  1136. $performer = $this->performer();
  1137. // abort_if($performer->pro->pro_type !== 'ADMIN' && !$performer->pro->can_view_rm_matrix, 403);
  1138. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  1139. $careMonthStart = date($ym . '-01');
  1140. $rc = $request->input('rc') ?: 1;
  1141. $rc2 = $request->input('rc2') ?: 2;
  1142. $conditions = [];// $this->rpmConditions($performer, $rc, $rc2);
  1143. $sortBy = $request->input('sort_by') ?: 'name_first';
  1144. $sortDir = $request->input('sort_dir') ?: 'ASC';
  1145. $orderBy = "ORDER BY $sortBy $sortDir NULLS LAST";
  1146. // special case of if-bill-exists
  1147. if($sortBy === 'mcp_rm_generic_bill_id' || $sortBy === 'rmm_rm_generic_bill_id') {
  1148. if($sortDir === 'ASC') {
  1149. $orderBy = "ORDER BY $sortBy $sortDir NULLS FIRST";
  1150. }
  1151. else {
  1152. $orderBy = "ORDER BY $sortBy $sortDir NULLS LAST";
  1153. }
  1154. }
  1155. // filters from the UI
  1156. if(trim($request->input('f_name'))) {
  1157. $v = trim($request->input('f_name'));
  1158. $conditions[] = "(client.name_first ILIKE '%{$v}%' OR client.name_last ILIKE '%{$v}%')";
  1159. }
  1160. if(trim($request->input('f_dob_op')) && trim($request->input('f_dob'))) {
  1161. $o = trim($request->input('f_dob_op'));
  1162. $v = trim($request->input('f_dob'));
  1163. $conditions[] = "(client.dob {$o} '{$v}')";
  1164. }
  1165. if(trim($request->input('f_rpm'))) {
  1166. $v = trim($request->input('f_rpm'));
  1167. if($v === 'yes') {
  1168. $conditions[] = "(client.is_enrolled_in_rm IS NOT NULL AND client.is_enrolled_in_rm = 'YES')";
  1169. }
  1170. elseif($v === 'no') {
  1171. $conditions[] = "(client.is_enrolled_in_rm IS NULL OR client.is_enrolled_in_rm != 'YES')";
  1172. }
  1173. }
  1174. if(trim($request->input('f_cell_bp'))) {
  1175. $v = trim($request->input('f_cell_bp'));
  1176. if($v === 'yes') {
  1177. $conditions[] = "(client.is_assigned_cellular_bp_device IS NOT NULL AND client.is_assigned_cellular_bp_device = TRUE)";
  1178. }
  1179. elseif($v === 'no') {
  1180. $conditions[] = "(client.is_assigned_cellular_bp_device IS NULL OR client.is_assigned_cellular_bp_device = FALSE)";
  1181. }
  1182. }
  1183. if(trim($request->input('f_cell_wt'))) {
  1184. $v = trim($request->input('f_cell_wt'));
  1185. if($v === 'yes') {
  1186. $conditions[] = "(client.is_assigned_cellular_weight_scale_device IS NOT NULL AND client.is_assigned_cellular_weight_scale_device = TRUE)";
  1187. }
  1188. elseif($v === 'no') {
  1189. $conditions[] = "(client.is_assigned_cellular_weight_scale_device IS NULL OR client.is_assigned_cellular_weight_scale_device = FALSE)";
  1190. }
  1191. }
  1192. if(trim($request->input('f_comm'))) {
  1193. $v = trim($request->input('f_comm'));
  1194. if($v === 'yes') {
  1195. $conditions[] = "(care_month.has_mcp_interacted_with_client_about_rm IS NOT NULL AND care_month.has_mcp_interacted_with_client_about_rm = TRUE)";
  1196. }
  1197. elseif($v === 'no') {
  1198. $conditions[] = "(care_month.has_mcp_interacted_with_client_about_rm IS NULL OR care_month.has_mcp_interacted_with_client_about_rm = FALSE)";
  1199. }
  1200. }
  1201. if($mode === 'mcp' || $mode === 'rmm') {
  1202. if(trim($request->input('f_billable'))) {
  1203. $v = trim($request->input('f_billable'));
  1204. if($v === 'yes') {
  1205. $conditions[] = "(care_month.{$mode}_rm_generic_bill_id IS NOT NULL)";
  1206. }
  1207. elseif($v === 'no') {
  1208. $conditions[] = "(care_month.{$mode}_rm_generic_bill_id IS NULL)";
  1209. }
  1210. }
  1211. }
  1212. if(trim($request->input('f_md_op')) && trim($request->input('f_md')) !== '') {
  1213. $o = trim($request->input('f_md_op'));
  1214. $v = trim($request->input('f_md'));
  1215. $conditions[] = "(care_month.number_of_days_with_remote_measurements {$o} {$v})";
  1216. }
  1217. if(trim($request->input('f_unst_op')) && trim($request->input('f_unst')) !== '') {
  1218. $o = trim($request->input('f_unst_op'));
  1219. $v = trim($request->input('f_unst'));
  1220. $conditions[] = "(care_month.rm_num_measurements_not_stamped_by_mcp {$o} {$v})";
  1221. }
  1222. if(trim($request->input('f_mins_op')) && trim($request->input('f_mins')) !== '') {
  1223. $o = trim($request->input('f_mins_op'));
  1224. $v = intval(trim($request->input('f_mins'))) * 60;
  1225. $field = 'rm_total_time_in_seconds_by_mcp';
  1226. switch($mode) {
  1227. case 'mcp':
  1228. $field = 'rm_total_time_in_seconds_by_mcp';
  1229. break;
  1230. case 'rmm':
  1231. $field = 'rm_total_time_in_seconds_by_rmm_pro';
  1232. break;
  1233. break;
  1234. }
  1235. $conditions[] = "(care_month.{$field} {$o} {$v})";
  1236. }
  1237. $proTypeCondition = '';
  1238. $genericBillIdColumns = '';
  1239. $genericBillJoinClause = '';
  1240. switch($mode) {
  1241. case 'mcp':
  1242. $proTypeCondition = "care_month.mcp_pro_id = {$performer->pro->id}";
  1243. $genericBillIdColumns = "care_month.mcp_rm_generic_bill_id,
  1244. bill.uid as mcp_rm_generic_bill_uid,
  1245. bill.generic_pro_id as mcp_rm_generic_bill_generic_pro_id,
  1246. bill.generic_pro_expected_payment_amount as mcp_rm_generic_bill_expected_payment_amount,
  1247. bill.is_signed_by_generic_pro as mcp_rm_generic_bill_signed";
  1248. $genericBillJoinClause = 'left join bill on care_month.mcp_rm_generic_bill_id = bill.id';
  1249. break;
  1250. case 'rmm':
  1251. $proTypeCondition = "care_month.rmm_pro_id = {$performer->pro->id}";
  1252. $genericBillIdColumns = "care_month.rmm_rm_generic_bill_id,
  1253. bill.uid as rmm_rm_generic_bill_uid,
  1254. bill.generic_pro_id as rmm_rm_generic_bill_generic_pro_id,
  1255. bill.generic_pro_expected_payment_amount as rmm_rm_generic_bill_expected_payment_amount,
  1256. bill.is_signed_by_generic_pro as rmm_rm_generic_bill_signed";
  1257. $genericBillJoinClause = 'left join bill on care_month.rmm_rm_generic_bill_id = bill.id';
  1258. break;
  1259. case 'rme':
  1260. $proTypeCondition = "left join bill on care_month.rme_pro_id = {$performer->pro->id}";
  1261. break;
  1262. }
  1263. $query = "
  1264. SELECT client.name_first, client.name_last,
  1265. (client.name_first || ' ' || client.name_last) as client_name,
  1266. client.uid as client_uid,
  1267. client.dob,
  1268. client.is_enrolled_in_rm,
  1269. client.most_recent_completed_mcp_note_date,
  1270. client.most_recent_completed_mcp_note_id,
  1271. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  1272. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  1273. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  1274. client.cell_number,
  1275. client.is_assigned_cellular_bp_device,
  1276. client.is_assigned_cellular_weight_scale_device,
  1277. care_month.uid as care_month_uid,
  1278. care_month.id as care_month_id,
  1279. care_month.start_date,
  1280. care_month.rm_total_time_in_seconds_by_mcp,
  1281. care_month.rm_total_time_in_seconds_by_rmm_pro,
  1282. care_month.number_of_days_with_remote_measurements,
  1283. care_month.has_mcp_interacted_with_client_about_rm,
  1284. care_month.rm_num_measurements_not_stamped_by_mcp,
  1285. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  1286. care_month.rm_num_measurements_not_stamped_by_rmm,
  1287. care_month.rm_num_measurements_not_stamped_by_rme,
  1288. care_month.mcp_pro_id as care_month_mcp_pro_id,
  1289. care_month.rmm_pro_id as care_month_rmm_pro_id,
  1290. client.mcp_pro_id,
  1291. client.default_na_pro_id,
  1292. client.rmm_pro_id,
  1293. client.rme_pro_id,
  1294. client.cell_number,
  1295. care_month.most_recent_cellular_bp_dbp_mm_hg,
  1296. care_month.most_recent_cellular_bp_sbp_mm_hg,
  1297. care_month.most_recent_cellular_bp_measurement_at,
  1298. care_month.most_recent_cellular_weight_value,
  1299. care_month.most_recent_cellular_weight_measurement_at,
  1300. {$genericBillIdColumns}
  1301. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  1302. {$genericBillJoinClause}
  1303. left join note mrmnote on mrmnote.id = (
  1304. select max(n.id) from note n
  1305. where
  1306. n.client_id = client.id AND
  1307. n.is_cancelled = FALSE AND
  1308. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  1309. n.effective_dateest::date >= care_month.start_date::date AND
  1310. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  1311. )
  1312. WHERE
  1313. $proTypeCondition
  1314. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1315. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1316. " . (count($conditions) > 0 ? 'AND ' . implode(" AND ", $conditions) : '') . "
  1317. $orderBy
  1318. ";
  1319. // dd($query);
  1320. $patients = DB::select($query);
  1321. $timestamp = strtotime(date('Y-m-d'));
  1322. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  1323. return view('app.practice-management.rpm-matrix-by-pro-type', compact('patients', 'daysRemaining', 'careMonthStart', 'mode'));
  1324. }
  1325. public function remoteMonitoringMCP(Request $request) {
  1326. return $this->rpmMatrixByProType($request, 'mcp');
  1327. }
  1328. public function remoteMonitoringRMM(Request $request) {
  1329. return $this->rpmMatrixByProType($request, 'rmm');
  1330. }
  1331. public function remoteMonitoringRME(Request $request) {
  1332. return $this->rpmMatrixByProType($request, 'rme');
  1333. }
  1334. public function remoteMonitoring_RowMCP(Request $request) {
  1335. return $this->remoteMonitoring_RowByProType($request, 'mcp');
  1336. }
  1337. public function remoteMonitoring_RowRMM(Request $request) {
  1338. return $this->remoteMonitoring_RowByProType($request, 'rmm');
  1339. }
  1340. public function remoteMonitoring_RowRME(Request $request) {
  1341. return $this->remoteMonitoring_RowByProType($request, 'rme');
  1342. }
  1343. public function rpm_work_matrix(Request $request) {
  1344. // get the patient having most recent unstamped measurement
  1345. $performer = $this->performer();
  1346. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  1347. $careMonthStart = date($ym . '-01');
  1348. $patient = null;
  1349. $careMonth = null;
  1350. if($request->input('patientUid') && $request->input('careMonthUid')) {
  1351. $patient = Client::where('uid', $request->input('patientUid'))->first();
  1352. $careMonth = CareMonth::where('uid', $request->input('careMonthUid'))->first();
  1353. }
  1354. if(!$patient && !$careMonth) {
  1355. $query = "
  1356. SELECT client.name_first, client.name_last,
  1357. (client.name_first || ' ' || client.name_last) as client_name,
  1358. client.uid as client_uid,
  1359. client.dob,
  1360. client.is_enrolled_in_rm,
  1361. client.most_recent_completed_mcp_note_date,
  1362. care_month.uid as care_month_uid,
  1363. care_month.id as care_month_id,
  1364. care_month.start_date,
  1365. care_month.rm_total_time_in_seconds_by_mcp,
  1366. care_month.number_of_days_with_remote_measurements,
  1367. care_month.rm_num_measurements_not_stamped_by_mcp,
  1368. care_month.rm_num_measurements_not_stamped_by_rmm,
  1369. client.mcp_pro_id,
  1370. client.default_na_pro_id,
  1371. client.rmm_pro_id,
  1372. client.rme_pro_id,
  1373. client.cell_number,
  1374. client.most_recent_cellular_bp_dbp_mm_hg,
  1375. client.most_recent_cellular_bp_sbp_mm_hg,
  1376. client.most_recent_cellular_bp_measurement_at,
  1377. client.most_recent_cellular_weight_value,
  1378. client.most_recent_cellular_weight_measurement_at,
  1379. GREATEST(client.most_recent_cellular_bp_measurement_at, client.most_recent_cellular_weight_measurement_at) as latest_measurement_at
  1380. FROM care_month join client on care_month.client_id = client.id
  1381. WHERE
  1382. (
  1383. (care_month.mcp_pro_id = {$performer->pro->id} AND care_month.rm_num_measurements_not_stamped_by_mcp > 0) OR
  1384. (care_month.rmm_pro_id = {$performer->pro->id} AND care_month.rm_num_measurements_not_stamped_by_rmm > 0)
  1385. )
  1386. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1387. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1388. ORDER BY latest_measurement_at DESC
  1389. LIMIT 1
  1390. ";
  1391. $patients = DB::select($query);
  1392. if (count($patients)) {
  1393. $patient = Client::where('uid', $patients[0]->client_uid)->first();
  1394. $careMonth = CareMonth::where('uid', $patients[0]->care_month_uid)->first();
  1395. }
  1396. }
  1397. return view('app.practice-management.rpm_work_matrix', compact('patient', 'careMonth'));
  1398. }
  1399. public function remoteMonitoringCount(Request $request) {
  1400. $performer = $this->performer();
  1401. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  1402. $careMonthStart = date($ym . '-01');
  1403. $rc = $request->input('rc') ?: 1;
  1404. $rc2 = $request->input('rc2') ?: 2;
  1405. $conditions = $this->rpmConditions($performer, $rc, $rc2);
  1406. $count = DB::select(
  1407. DB::raw(
  1408. "
  1409. SELECT count(*)
  1410. FROM care_month join client on care_month.client_id = client.id
  1411. WHERE
  1412. client.mcp_pro_id = {$performer->pro->id}
  1413. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1414. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1415. " . (count($conditions) > 0 ? 'AND ' . implode(" AND ", $conditions) : '')
  1416. )
  1417. );
  1418. return $count[0]->count;
  1419. }
  1420. public function remoteMonitoringAdmin(Request $request) {
  1421. $performer = $this->performer();
  1422. // abort_if($performer->pro->pro_type !== 'ADMIN' && !$performer->pro->can_view_rm_matrix, 403);
  1423. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  1424. $careMonthStart = date($ym . '-01');
  1425. $rc = $request->input('rc') ?: 1;
  1426. $rc2 = $request->input('rc2') ?: 2;
  1427. $conditions = [];// $this->rpmConditions($performer, $rc, $rc2);
  1428. $sortBy = $request->input('sort_by') ?: 'name_first';
  1429. $sortDir = $request->input('sort_dir') ?: 'ASC';
  1430. $orderBy = "ORDER BY $sortBy $sortDir NULLS LAST";
  1431. $query = "
  1432. SELECT client.name_first, client.name_last,
  1433. (client.name_first || ' ' || client.name_last) as client_name,
  1434. (mcp_pro.name_first || ' ' || mcp_pro.name_last) as mcp_pro_name,
  1435. (rmm_pro.name_first || ' ' || rmm_pro.name_last) as rmm_pro_name,
  1436. client.uid as client_uid,
  1437. client.chart_number,
  1438. client.dob,
  1439. client.is_enrolled_in_rm,
  1440. client.most_recent_completed_mcp_note_date,
  1441. client.most_recent_completed_mcp_note_id,
  1442. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  1443. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  1444. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  1445. client.cell_number,
  1446. client.is_assigned_cellular_bp_device,
  1447. client.is_assigned_cellular_weight_scale_device,
  1448. care_month.uid as care_month_uid,
  1449. care_month.id as care_month_id,
  1450. care_month.start_date,
  1451. care_month.rm_total_time_in_seconds_by_mcp,
  1452. care_month.rm_total_time_in_seconds_by_rmm_pro,
  1453. care_month.number_of_days_with_remote_measurements,
  1454. care_month.has_anyone_interacted_with_client_about_rm,
  1455. care_month.has_mcp_interacted_with_client_about_rm,
  1456. care_month.rm_num_measurements_not_stamped_by_mcp,
  1457. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  1458. care_month.rm_num_measurements_not_stamped_by_rmm,
  1459. care_month.rm_num_measurements_not_stamped_by_rme,
  1460. care_month.mcp_pro_id as care_month_mcp_pro_id,
  1461. care_month.rmm_pro_id as care_month_rmm_pro_id,
  1462. client.mcp_pro_id,
  1463. client.default_na_pro_id,
  1464. client.rmm_pro_id,
  1465. client.rme_pro_id,
  1466. client.cell_number,
  1467. care_month.most_recent_cellular_bp_dbp_mm_hg,
  1468. care_month.most_recent_cellular_bp_sbp_mm_hg,
  1469. care_month.most_recent_cellular_bp_measurement_at,
  1470. care_month.most_recent_cellular_weight_value,
  1471. care_month.most_recent_cellular_weight_measurement_at
  1472. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  1473. left join note mrmnote on mrmnote.id = (
  1474. select max(n.id) from note n
  1475. where
  1476. n.client_id = client.id AND
  1477. n.is_cancelled = FALSE AND
  1478. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  1479. n.effective_dateest::date >= care_month.start_date::date AND
  1480. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  1481. )
  1482. left join pro mcp_pro on care_month.mcp_pro_id = mcp_pro.id
  1483. left join pro rmm_pro on care_month.rmm_pro_id = rmm_pro.id
  1484. WHERE
  1485. EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1486. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1487. " . (count($conditions) > 0 ? 'AND ' . implode(" AND ", $conditions) : '') . "
  1488. $orderBy
  1489. ";
  1490. $patients = DB::select($query);
  1491. $timestamp = strtotime(date('Y-m-d'));
  1492. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  1493. return view('app.practice-management.remote-monitoring-admin', compact('patients', 'daysRemaining', 'careMonthStart'));
  1494. }
  1495. public function remoteMonitoringAdminCount(Request $request) {
  1496. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  1497. $careMonthStart = date($ym . '-01');
  1498. $rc = $request->input('rc') ?: 1;
  1499. $rc2 = $request->input('rc2') ?: 2;
  1500. $conditions = $this->rpmConditionsAdmin($this->performer(), $rc, $rc2);
  1501. $count = DB::select(
  1502. DB::raw(
  1503. "
  1504. SELECT count(*)
  1505. FROM care_month join client on care_month.client_id = client.id
  1506. WHERE
  1507. client.shadow_pro_id is null AND client.is_enrolled_in_rm = 'YES'
  1508. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  1509. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  1510. " . (count($conditions) > 0 ? 'AND ' . implode(" AND ", $conditions) : '')
  1511. )
  1512. );
  1513. return $count[0]->count;
  1514. }
  1515. private function rpmConditions($performer, $rc, $rc2) {
  1516. $conditions = [];
  1517. $c_isMCP = "client.mcp_pro_id = {$performer->pro->id}";
  1518. $c_enrolledInRPM = "client.is_enrolled_in_rm = 'YES'";
  1519. $c_hasDevice = "(SELECT COUNT(client_bdt_device.id) FROM client_bdt_device JOIN bdt_device bd on client_bdt_device.device_id = bd.id WHERE client_bdt_device.client_id = client.id) > 0";
  1520. $c_lastVisitBefore90Days = "DATE_PART('day', care_month.start_date::timestamp - client.most_recent_completed_mcp_note_date::timestamp) > 90";
  1521. $c_lastVisitWithin90Days = "DATE_PART('day', care_month.start_date::timestamp - client.most_recent_completed_mcp_note_date::timestamp) <= 90";
  1522. $c_notSpokenToThisMonth = "(care_month.has_anyone_interacted_with_client_about_rm_outside_note IS NULL OR care_month.has_anyone_interacted_with_client_about_rm_outside_note = FALSE)";
  1523. $c_spokenToThisMonth = "care_month.has_anyone_interacted_with_client_about_rm_outside_note = TRUE";
  1524. $c_hasUnstamped = "care_month.rm_num_measurements_not_stamped_by_mcp > 0";
  1525. $c_hasNoUnstamped = "care_month.rm_num_measurements_not_stamped_by_mcp = 0";
  1526. $c_lt16MeasurementDays = "care_month.number_of_days_with_remote_measurements < 16";
  1527. $c_gte16MeasurementDays = "care_month.number_of_days_with_remote_measurements >= 16";
  1528. $c_subscribedToSMS = "client.send_sms_on_bdt_measurement = TRUE";
  1529. $c_deviceUsed = "(client.most_recent_cellular_bp_measurement_at IS NOT NULL OR most_recent_cellular_weight_measurement_at IS NOT NULL)";
  1530. $c_lt20BillingMinutes = "care_month.rm_total_time_in_seconds_by_mcp < 1200";
  1531. $c_gte20BillingMinutes = "care_month.rm_total_time_in_seconds_by_mcp >= 1200";
  1532. switch ($rc) {
  1533. case 1:
  1534. $conditions = [$c_isMCP];
  1535. break;
  1536. case 2:
  1537. $conditions = [$c_isMCP, $c_enrolledInRPM];
  1538. break;
  1539. case 3:
  1540. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice];
  1541. break;
  1542. case 4:
  1543. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice, ($rc2 == 1 ? $c_lastVisitBefore90Days : $c_lastVisitWithin90Days)];
  1544. break;
  1545. case 5:
  1546. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, ($rc2 == 1 ? $c_notSpokenToThisMonth : $c_spokenToThisMonth)];
  1547. break;
  1548. case 6:
  1549. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, ($rc2 == 1 ? $c_hasUnstamped : $c_hasNoUnstamped)];
  1550. break;
  1551. case 7:
  1552. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, ($rc2 == 1 ? $c_lt16MeasurementDays : $c_gte16MeasurementDays)];
  1553. break;
  1554. case 10:
  1555. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, $c_gte16MeasurementDays, ($rc2 == 1 ? $c_lt20BillingMinutes : $c_gte20BillingMinutes)];
  1556. break;
  1557. case 8:
  1558. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_subscribedToSMS];
  1559. break;
  1560. case 9:
  1561. $conditions = [$c_isMCP, $c_enrolledInRPM, $c_deviceUsed];
  1562. break;
  1563. }
  1564. return $conditions;
  1565. }
  1566. private function rpmConditionsAdmin($performer, $rc, $rc2) {
  1567. $conditions = [];
  1568. $c_enrolledInRPM = "client.is_enrolled_in_rm = 'YES'";
  1569. $c_hasDevice = "(SELECT COUNT(client_bdt_device.id) FROM client_bdt_device JOIN bdt_device bd on client_bdt_device.device_id = bd.id WHERE client_bdt_device.client_id = client.id) > 0";
  1570. $c_lastVisitBefore90Days = "DATE_PART('day', care_month.start_date::timestamp - client.most_recent_completed_mcp_note_date::timestamp) > 90";
  1571. $c_lastVisitWithin90Days = "DATE_PART('day', care_month.start_date::timestamp - client.most_recent_completed_mcp_note_date::timestamp) <= 90";
  1572. $c_notSpokenToThisMonth = "(care_month.has_anyone_interacted_with_client_about_rm_outside_note IS NULL OR care_month.has_anyone_interacted_with_client_about_rm_outside_note = FALSE)";
  1573. $c_spokenToThisMonth = "care_month.has_anyone_interacted_with_client_about_rm_outside_note = TRUE";
  1574. $c_hasUnstamped = "care_month.rm_num_measurements_not_stamped_by_mcp > 0";
  1575. $c_hasNoUnstamped = "care_month.rm_num_measurements_not_stamped_by_mcp = 0";
  1576. $c_lt16MeasurementDays = "care_month.number_of_days_with_remote_measurements < 16";
  1577. $c_gte16MeasurementDays = "care_month.number_of_days_with_remote_measurements >= 16";
  1578. $c_subscribedToSMS = "client.send_sms_on_bdt_measurement = TRUE";
  1579. $c_deviceUsed = "(client.most_recent_cellular_bp_measurement_at IS NOT NULL OR client.most_recent_cellular_weight_measurement_at IS NOT NULL)";
  1580. $c_lt20BillingMinutes = "care_month.rm_total_time_in_seconds_by_mcp < 1200";
  1581. $c_gte20BillingMinutes = "care_month.rm_total_time_in_seconds_by_mcp >= 1200";
  1582. switch ($rc) {
  1583. case 2:
  1584. $conditions = [$c_enrolledInRPM];
  1585. break;
  1586. case 3:
  1587. $conditions = [$c_enrolledInRPM, $c_hasDevice];
  1588. break;
  1589. case 4:
  1590. $conditions = [$c_enrolledInRPM, $c_hasDevice, ($rc2 == 1 ? $c_lastVisitBefore90Days : $c_lastVisitWithin90Days)];
  1591. break;
  1592. case 5:
  1593. $conditions = [$c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, ($rc2 == 1 ? $c_notSpokenToThisMonth : $c_spokenToThisMonth)];
  1594. break;
  1595. case 6:
  1596. $conditions = [$c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, ($rc2 == 1 ? $c_hasUnstamped : $c_hasNoUnstamped)];
  1597. break;
  1598. case 7:
  1599. $conditions = [$c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, ($rc2 == 1 ? $c_lt16MeasurementDays : $c_gte16MeasurementDays)];
  1600. break;
  1601. case 10:
  1602. $conditions = [$c_enrolledInRPM, $c_hasDevice, $c_lastVisitWithin90Days, $c_spokenToThisMonth, $c_gte16MeasurementDays, ($rc2 == 1 ? $c_lt20BillingMinutes : $c_gte20BillingMinutes)];
  1603. break;
  1604. case 8:
  1605. $conditions = [$c_enrolledInRPM, $c_subscribedToSMS];
  1606. break;
  1607. case 9:
  1608. $conditions = [$c_enrolledInRPM, $c_deviceUsed];
  1609. break;
  1610. }
  1611. return $conditions;
  1612. }
  1613. public function remoteMonitoringMeasurements(Request $request, CareMonth $careMonth) {
  1614. $performer = $this->performer();
  1615. $measurements = DB::select(
  1616. DB::raw(
  1617. "
  1618. SELECT measurement.label,
  1619. measurement.ts,
  1620. measurement.effective_date,
  1621. measurement.sbp_mm_hg,
  1622. measurement.dbp_mm_hg,
  1623. measurement.value_pulse,
  1624. measurement.value_irregular,
  1625. measurement.numeric_value,
  1626. measurement.value,
  1627. measurement.uid,
  1628. client.name_first,
  1629. client.name_last,
  1630. client.mcp_pro_id,
  1631. client.default_na_pro_id,
  1632. client.rmm_pro_id,
  1633. client.rme_pro_id
  1634. FROM measurement RIGHT JOIN client on measurement.client_id = client.id
  1635. WHERE
  1636. client.id = {$careMonth->client_id}
  1637. AND measurement.label IS NOT NULL
  1638. AND measurement.label NOT IN ('SBP', 'DBP')
  1639. AND (measurement.is_cellular_zero = FALSE or measurement.is_cellular_zero IS NULL)
  1640. AND measurement.is_removed IS FALSE
  1641. AND measurement.client_bdt_measurement_id IS NOT NULL
  1642. AND measurement.care_month_id = {$careMonth->id}
  1643. AND (
  1644. (client.mcp_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_mcp IS FALSE)
  1645. OR (client.rmm_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rmm IS FALSE)
  1646. OR (client.rme_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rme IS FALSE)
  1647. OR (client.default_na_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_non_hcp IS FALSE)
  1648. )
  1649. ORDER BY ts DESC
  1650. "
  1651. )
  1652. );
  1653. return view('app.practice-management.remote-monitoring-measurements', compact('careMonth', 'measurements'));
  1654. }
  1655. public function billMatrix(Request $request)
  1656. {
  1657. $bClients = [];
  1658. $bHCPPros = [];
  1659. $bNAPros = [];
  1660. $filters = [];
  1661. $filters['client'] = $request->input('client');
  1662. $filters['service'] = $request->input('service');
  1663. $filters['hcp'] = $request->input('hcp');
  1664. $filters['hcp_paid'] = $request->input('hcp_paid');
  1665. $filters['expected_op'] = $request->input('expected_op');
  1666. $filters['expected_value'] = $request->input('expected_value');
  1667. $filters['paid_op'] = $request->input('paid_op');
  1668. $filters['paid_value'] = $request->input('paid_value');
  1669. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  1670. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  1671. $filters['hcp_sign'] = $request->input('hcp_sign');
  1672. $filters['verified'] = $request->input('verified');
  1673. $filters['cancelled'] = $request->input('cancelled');
  1674. $bills = Bill::orderBy('effective_date')->paginate();
  1675. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  1676. }
  1677. public function medicarePartBClaims(Request $request)
  1678. {
  1679. $medicarePartBOnly = $request->get("medicare_part_b");
  1680. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  1681. //Only medicare claims
  1682. $claims = [];
  1683. foreach ($allClaims as $claim) {
  1684. if ($claim->client != null && $claim->client->getPrimaryCoverageStatus() == 'YES' && !$claim->edi) {
  1685. $claims[] = $claim;
  1686. }
  1687. }
  1688. $claimEDIs = ClaimEDI::all();
  1689. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  1690. }
  1691. // Generate PDF
  1692. public function downloadClaims()
  1693. {
  1694. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  1695. view()->share('claims', $claims);
  1696. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  1697. return $pdf->download('pdf_file.pdf');
  1698. }
  1699. public function tickets(Request $request, $proUid = null)
  1700. {
  1701. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  1702. return view('app.practice-management.tickets', compact('tickets'));
  1703. }
  1704. public function supplyOrders(Request $request)
  1705. {
  1706. // counts
  1707. $counts = $this->getSupplyOrderCounts();
  1708. // so clients
  1709. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  1710. $soClientIDs = array_map(function ($_x) {
  1711. return $_x->client_id;
  1712. }, $soClientIDs);
  1713. $soClients = Client::whereIn('id', $soClientIDs)->get();
  1714. // so products
  1715. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  1716. $soProductIDs = array_map(function ($_x) {
  1717. return $_x->product_id;
  1718. }, $soProductIDs);
  1719. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  1720. $filters = [];
  1721. $filters['client'] = $request->input('client');
  1722. $filters['product'] = $request->input('product');
  1723. $filters['reason'] = $request->input('reason');
  1724. $filters['cu_memo'] = $request->input('cu_memo');
  1725. $filters['pro_sign'] = $request->input('pro_sign');
  1726. $filters['client_sign'] = $request->input('client_sign');
  1727. $filters['shipment'] = $request->input('shipment');
  1728. $filters['lot_number'] = $request->input('lot_number');
  1729. $filters['imei'] = $request->input('imei');
  1730. $filters['cancelled'] = $request->input('cancelled');
  1731. $supplyOrders = SupplyOrder::where('id', '>', 0);
  1732. // apply filters
  1733. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  1734. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  1735. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  1736. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  1737. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  1738. if ($filters['client_sign']) {
  1739. if ($filters['client_sign'] === 'signed')
  1740. $supplyOrders->where('is_signed_by_client', true);
  1741. elseif ($filters['client_sign'] === 'waived')
  1742. $supplyOrders->where('is_client_signature_waived', true);
  1743. else
  1744. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  1745. }
  1746. if ($filters['shipment']) {
  1747. if ($filters['shipment'] === 'not_cleared_for_shipment')
  1748. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  1749. elseif ($filters['shipment'] === 'cleared_for_shipment')
  1750. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  1751. else
  1752. $supplyOrders
  1753. ->whereNotNull('shipment_id')
  1754. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  1755. }
  1756. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  1757. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  1758. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  1759. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  1760. return view('app.practice-management.supply-orders',
  1761. compact('supplyOrders', 'filters',
  1762. 'soClients', 'soProducts', 'counts'
  1763. )
  1764. );
  1765. }
  1766. public function shipments(Request $request, $filter = null)
  1767. {
  1768. // counts
  1769. $counts = $this->getShipmentCounts();
  1770. // so clients
  1771. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  1772. $shClientIDs = array_map(function ($_x) {
  1773. return $_x->client_id;
  1774. }, $shClientIDs);
  1775. $shClients = Client::whereIn('id', $shClientIDs)->get();
  1776. $shipments = Shipment::where('id', '>', 0);
  1777. $filters = [];
  1778. $filters['client'] = $request->input('client');
  1779. $filters['courier'] = $request->input('courier');
  1780. $filters['tracking_num'] = $request->input('tracking_num');
  1781. $filters['label'] = $request->input('label');
  1782. $filters['status'] = $request->input('status');
  1783. $filters['cancelled'] = $request->input('cancelled');
  1784. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  1785. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  1786. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  1787. if ($filters['label']) {
  1788. if ($filters['label'] === 'yes')
  1789. $shipments->whereNotNull('label_system_file_id');
  1790. else
  1791. $shipments->whereNull('label_system_file_id');
  1792. }
  1793. if ($filters['status']) $shipments->where('status', $filters['status']);
  1794. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  1795. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  1796. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  1797. }
  1798. public function cellularMeasurements(Request $request)
  1799. {
  1800. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  1801. return view('app.practice-management.cellular-measurements', compact('measurements'));
  1802. }
  1803. // v2 supply-orders & shipments management (wh)
  1804. public function supplyOrdersReadyToShip(Request $request)
  1805. {
  1806. $counts = $this->getSupplyOrderCounts();
  1807. $supplyOrders = SupplyOrder
  1808. ::where('is_cleared_for_shipment', true)
  1809. ->where('is_cancelled', false)
  1810. ->whereNull('shipment_id')
  1811. ->join('client', 'client.id', '=', 'supply_order.client_id')
  1812. ->orderBy('client.name_last', 'ASC')
  1813. ->orderBy('client.name_first', 'ASC')
  1814. ->orderBy('supply_order.client_id', 'ASC')
  1815. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1816. ->orderBy('supply_order.created_at', 'ASC')
  1817. ->select('supply_order.*')
  1818. ->paginate();
  1819. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  1820. }
  1821. public function supplyOrdersShipmentUnderway(Request $request)
  1822. {
  1823. $counts = $this->getSupplyOrderCounts();
  1824. $supplyOrders = SupplyOrder
  1825. ::where('is_cancelled', false)
  1826. ->whereNotNull('shipment_id')
  1827. ->orderBy('client_id', 'ASC')
  1828. ->orderBy('mailing_address_full', 'ASC')
  1829. ->orderBy('created_at', 'ASC')
  1830. ->paginate();
  1831. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  1832. }
  1833. public function supplyOrdersHanging(Request $request)
  1834. {
  1835. $counts = $this->getSupplyOrderCounts();
  1836. $supplyOrders = SupplyOrder
  1837. ::select('supply_order.*')
  1838. ->leftJoin('shipment', function($join) {
  1839. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1840. })
  1841. ->where('shipment.status', 'CANCELLED')
  1842. ->where('supply_order.is_cancelled', false)
  1843. ->orderBy('supply_order.client_id', 'ASC')
  1844. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1845. ->orderBy('supply_order.created_at', 'ASC')
  1846. ->paginate();
  1847. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  1848. }
  1849. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  1850. {
  1851. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  1852. ->where('is_cancelled', true)
  1853. ->where('is_cancellation_acknowledged', false)
  1854. ->orderBy('created_at', 'desc')
  1855. ->paginate();
  1856. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  1857. }
  1858. public function supplyOrdersUnsigned(Request $request)
  1859. {
  1860. $supplyOrders = SupplyOrder
  1861. ::where('is_cancelled', false)
  1862. ->where('is_signed_by_pro', false)
  1863. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  1864. ->orderBy('created_at', 'desc')
  1865. ->paginate();
  1866. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  1867. }
  1868. private function getSupplyOrderCounts()
  1869. {
  1870. return [
  1871. "supplyOrders" => SupplyOrder::count(),
  1872. "supplyOrdersReadyToShip" => SupplyOrder
  1873. ::where('is_cleared_for_shipment', true)
  1874. ->where('is_cancelled', false)
  1875. ->whereNull('shipment_id')->count(),
  1876. "supplyOrdersShipmentUnderway" => SupplyOrder
  1877. ::where('is_cancelled', false)
  1878. ->whereNotNull('shipment_id')->count(),
  1879. "supplyOrdersHanging" => SupplyOrder
  1880. ::leftJoin('shipment', function($join) {
  1881. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1882. })
  1883. ->where('shipment.status', 'CANCELLED')
  1884. ->where('supply_order.is_cancelled', false)
  1885. ->count(),
  1886. ];
  1887. }
  1888. public function shipmentsReadyToPrint(Request $request)
  1889. {
  1890. $counts = $this->getShipmentCounts();
  1891. $shipments = Shipment
  1892. ::where('is_cancelled', false)
  1893. ->where('status', 'CREATED')
  1894. ->orderBy('created_at', 'ASC')
  1895. ->paginate();
  1896. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  1897. }
  1898. public function shipmentsShipmentUnderway(Request $request)
  1899. {
  1900. $counts = $this->getShipmentCounts();
  1901. $shipments = Shipment
  1902. ::where('is_cancelled', false)
  1903. ->where('status', 'PRINTED')
  1904. ->orderBy('created_at', 'ASC')
  1905. ->paginate();
  1906. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  1907. }
  1908. private function getShipmentCounts()
  1909. {
  1910. return [
  1911. "shipments" => Shipment::count(),
  1912. "shipmentsReadyToPrint" => Shipment
  1913. ::where('is_cancelled', false)
  1914. ->where('status', 'CREATED')
  1915. ->count(),
  1916. "shipmentsWaitingForPicker" => Shipment
  1917. ::where('is_cancelled', false)
  1918. ->where('status', 'PRINTED')
  1919. ->count()
  1920. ];
  1921. }
  1922. public function shipment(Request $request, Shipment $shipment)
  1923. {
  1924. return view('app.practice-management.shipment', compact('shipment'));
  1925. }
  1926. public function shipmentsMultiPrint(Request $request, $ids)
  1927. {
  1928. $ids = array_map(function ($_x) {
  1929. return intval($_x);
  1930. }, explode("|", $ids));
  1931. $shipments = Shipment::whereIn('id', $ids)->get();
  1932. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  1933. }
  1934. public function patientClaimSummary(Request $request, $proUid = null)
  1935. {
  1936. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  1937. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  1938. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  1939. $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;
  1940. $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;
  1941. $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;
  1942. $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;
  1943. $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;
  1944. $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;
  1945. $performerPro = $this->performer->pro;
  1946. $allPros = [];
  1947. if ($performerPro->pro_type == 'ADMIN') {
  1948. $allPros = Pro::all();
  1949. } else {
  1950. $allPros = [$performerPro];
  1951. }
  1952. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  1953. $patientsQuery = Client::where('is_dummy', '=', false)
  1954. ->whereNull('shadow_pro_id')
  1955. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  1956. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  1957. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  1958. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  1959. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  1960. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  1961. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  1962. if ($proUid) {
  1963. $mcpPro = Pro::where('uid', $proUid)->first();
  1964. if ($mcpPro) {
  1965. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  1966. }
  1967. }
  1968. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  1969. $patientsQuery->orderBy('notes_total', 'DESC');
  1970. $patients = $patientsQuery->paginate(50);
  1971. $data = [
  1972. 'patients' => $patients,
  1973. 'proUid' => $proUid,
  1974. 'allPros' => $allPros,
  1975. 'notesTotal' => $notesTotal,
  1976. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  1977. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  1978. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  1979. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  1980. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  1981. 'patientsTotal' => $patientsTotal,
  1982. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  1983. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  1984. ];
  1985. return view('app.practice-management.patient-claim-summary', $data);
  1986. }
  1987. public function claims(Request $request){
  1988. $status = $request->get('status');
  1989. $claims = [];
  1990. if(!$status){
  1991. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  1992. }else{
  1993. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  1994. }
  1995. return view('app.practice-management.claims', compact('claims', 'status'));
  1996. }
  1997. public function processClaims(Request $request) {
  1998. $status = '';
  1999. $q = $request->input('q') ? $request->input('q') : '';
  2000. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  2001. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  2002. $params = [
  2003. 'q' => '%' . $q . '%',
  2004. 'from' => $from,
  2005. 'to' => $to
  2006. ];
  2007. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  2008. if($hcpPro) {
  2009. $params['hcp'] = $hcpPro->id;
  2010. }
  2011. $claims = DB::select(DB::raw("
  2012. SELECT claim.uid AS uid,
  2013. DATE(claim.created_at) AS created,
  2014. claim.status,
  2015. client.uid AS client_uid,
  2016. client.cell_number AS client_phone,
  2017. client.phone_home AS client_phone_home,
  2018. (client.name_last || ' ' || client.name_first) AS client ,
  2019. client.chart_number AS client_chart_number,
  2020. cp.id AS claim_pro_id,
  2021. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  2022. sp.id AS status_pro_id,
  2023. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  2024. note.uid AS note_uid,
  2025. note.method,
  2026. note.new_or_fu_or_na,
  2027. care_month.uid AS care_month_uid,
  2028. care_month.start_date as care_month_start__date,
  2029. -- claim.status_updated_at,
  2030. (DATE(claim.status_updated_at) || ' ' ||
  2031. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  2032. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  2033. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  2034. (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,
  2035. ROUND(claim.expected_total, 3) as expected_total
  2036. FROM claim
  2037. join client on claim.client_id = client.id
  2038. join pro cp on claim.pro_id = cp.id
  2039. left join note on claim.note_id = note.id
  2040. left join care_month on claim.care_month_id = care_month.id
  2041. left join app_session on claim.status_updated_by_session_id = app_session.id
  2042. left join pro sp on app_session.pro_id = sp.id
  2043. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  2044. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'" . ($request->input('show-submitted') ? "" : ", 'SUBMITTED'") . "))
  2045. -- AND claim.current_version_id IS NOT NULL
  2046. AND (client.name_first ILIKE :q OR
  2047. client.name_last ILIKE :q OR
  2048. client.chart_number ILIKE :q OR
  2049. client.mcn ILIKE :q)
  2050. AND (claim.created_at >= :from AND claim.created_at <= :to)
  2051. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  2052. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  2053. ORDER BY claim.created_at DESC
  2054. --OFFSET 0 LIMIT 15
  2055. "), $params);
  2056. if($request->input('json')) {
  2057. return json_encode($claims);
  2058. }
  2059. return view('app.practice-management.process-claims', compact('claims', 'status'));
  2060. }
  2061. public function rmLaunchAndClean(Request $request) {
  2062. $keyNumbers = [];
  2063. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_40'] = CareMonth::where('is_bill_closed', false)
  2064. ->whereNotNull('company_pro_id')
  2065. ->whereNotNull('company_pro_payer_id')
  2066. ->whereNotNull('company_location_id')
  2067. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2068. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  2069. ->where('rm_total_time_in_seconds', '>=', 4200) // at 4200 (70 minutes, it becomes eligible for plus40)
  2070. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2071. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  2072. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS TRUE OR has_mcp_rm_interacted_by_note IS TRUE )')
  2073. ->count();
  2074. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_40_ifHadInteraction'] = CareMonth::where('is_bill_closed', false)
  2075. ->whereNotNull('company_pro_id')
  2076. ->whereNotNull('company_pro_payer_id')
  2077. ->whereNotNull('company_location_id')
  2078. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2079. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  2080. ->where('rm_total_time_in_seconds', '>=', 4200) // at 4200 (70 minutes, it becomes eligible for plus40)
  2081. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2082. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  2083. ->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 )')
  2084. ->count();
  2085. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_20'] = CareMonth::where('is_bill_closed', false)
  2086. ->whereNotNull('company_pro_id')
  2087. ->whereNotNull('company_pro_payer_id')
  2088. ->whereNotNull('company_location_id')
  2089. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2090. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  2091. ->where('rm_total_time_in_seconds', '<', 4200)
  2092. ->where('rm_total_time_in_seconds', '>=', 3000) // at 3000 (50 minutes, it becomes eligible for plus20)
  2093. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2094. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  2095. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS TRUE OR has_mcp_rm_interacted_by_note IS TRUE)')
  2096. ->count();
  2097. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_20_ifHadInteraction'] = CareMonth::where('is_bill_closed', false)
  2098. ->whereNotNull('company_pro_id')
  2099. ->whereNotNull('company_pro_payer_id')
  2100. ->whereNotNull('company_location_id')
  2101. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2102. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  2103. ->where('rm_total_time_in_seconds', '<', 4200)
  2104. ->where('rm_total_time_in_seconds', '>=', 3000) // at 3000 (50 minutes, it becomes eligible for plus20)
  2105. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2106. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  2107. ->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 )')
  2108. ->count();
  2109. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP'] = CareMonth::where('is_bill_closed', false)
  2110. ->whereNotNull('company_pro_id')
  2111. ->whereNotNull('company_pro_payer_id')
  2112. ->whereNotNull('company_location_id')
  2113. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2114. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  2115. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  2116. ->whereRaw('(rm_total_time_in_seconds < 3000 OR (rm_total_time_in_seconds >= 3000 AND is_rm_interaction_waived IS TRUE))')
  2117. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2118. ->count();
  2119. $keyNumbers['careMonthsEligibleForBillGeneration_RMB'] = CareMonth::where('is_bill_closed', false)
  2120. ->whereNotNull('company_pro_id')
  2121. ->whereNotNull('company_pro_payer_id')
  2122. ->whereNotNull('company_location_id')
  2123. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  2124. ->whereRaw('is_rm_time_waived IS TRUE')
  2125. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2126. ->count();
  2127. $keyNumbers['careMonthsWith16OrMoreMeasurementDays'] = CareMonth::where('is_bill_closed', false)
  2128. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  2129. ->count();
  2130. $careMonthsWith16PlusMeasurements = CareMonth::where('is_bill_closed', false)
  2131. ->where('number_of_days_with_remote_measurements', '>=', 16 )->get();
  2132. return view('app.practice-management.rm-launch-and-clean', compact('keyNumbers', 'careMonthsWith16PlusMeasurements'));
  2133. }
  2134. public function processNotes(Request $request) {
  2135. $mode = $request->input('mode') ? $request->input('mode') : '1';
  2136. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  2137. DB::enableQueryLog(); // Enable query log
  2138. $test = Note::where('is_cancelled', false)
  2139. ->where('is_signed_by_hcp', true)
  2140. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  2141. ->whereNull('current_note_pickup_for_processing_id')
  2142. ->where('is_billing_marked_done', true)
  2143. ->where('is_bill_closed', true)
  2144. ->where('is_claim_closed', true)
  2145. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  2146. ->count();
  2147. // var_dump($test);
  2148. // dd(DB::getQueryLog()); // Show results of log
  2149. // exit;
  2150. $counts = [
  2151. "picked" => Note::where('is_cancelled', false)
  2152. ->whereNotNull('current_note_pickup_for_processing_id')
  2153. ->count(),
  2154. "bad" => Note::where('is_cancelled', false)
  2155. ->whereRaw("((detail_json)::json->>'isBad' = 'true')")
  2156. ->count(),
  2157. // not yet signed
  2158. "mode-1" => Note::where('is_cancelled', false)
  2159. ->where('is_signed_by_hcp', false)
  2160. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2161. // ->whereNull('current_note_pickup_for_processing_id')
  2162. ->count(),
  2163. // billing not marked done
  2164. "mode-2" => Note::where('is_cancelled', false)
  2165. ->where('is_signed_by_hcp', true)
  2166. //->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2167. // ->whereNull('current_note_pickup_for_processing_id')
  2168. ->where('is_billing_marked_done', false)
  2169. ->count(),
  2170. // billing not closed
  2171. "mode-3" => Note::where('is_cancelled', false)
  2172. ->where('is_signed_by_hcp', true)
  2173. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2174. // ->whereNull('current_note_pickup_for_processing_id')
  2175. ->where('is_billing_marked_done', true)
  2176. ->where('is_bill_closed', false)
  2177. ->count(),
  2178. // claiming not closed
  2179. "mode-4" => Note::where('is_cancelled', false)
  2180. ->where('is_signed_by_hcp', true)
  2181. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2182. // ->whereNull('current_note_pickup_for_processing_id')
  2183. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  2184. ->where('is_billing_marked_done', true)
  2185. ->where('is_bill_closed', true)
  2186. ->where('is_claim_closed', false)
  2187. ->count(),
  2188. // has unsubmitted claims
  2189. "mode-5" => Note::where('is_cancelled', false)
  2190. /*->where('is_signed_by_hcp', true)
  2191. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2192. ->whereNull('current_note_pickup_for_processing_id')
  2193. ->where('is_billing_marked_done', true)
  2194. ->where('is_bill_closed', true)
  2195. ->where('is_claim_closed', true)
  2196. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  2197. */
  2198. ->where('is_signed_by_hcp', true)
  2199. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2200. // ->whereNull('current_note_pickup_for_processing_id')
  2201. ->where('is_billing_marked_done', true)
  2202. ->where('is_bill_closed', true)
  2203. // ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true')")
  2204. ->where('is_claim_closed', true)
  2205. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0)")
  2206. ->count(),
  2207. // has unsubmitted claims MARKED GOOD!
  2208. "mode-8" => Note::where('is_cancelled', false)
  2209. ->where('is_signed_by_hcp', true)
  2210. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  2211. ->whereNull('current_note_pickup_for_processing_id')
  2212. ->where('is_billing_marked_done', true)
  2213. ->where('is_bill_closed', true)
  2214. ->where('is_claim_closed', false)
  2215. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  2216. ->count(),
  2217. // all good
  2218. "mode-6" => Note::where('is_cancelled', false)
  2219. ->where('is_signed_by_hcp', true)
  2220. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2221. // ->whereNull('current_note_pickup_for_processing_id')
  2222. ->where('is_billing_marked_done', true)
  2223. ->where('is_bill_closed', true)
  2224. ->where('is_claim_closed', true)
  2225. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id) > 0)")
  2226. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  2227. ->count(),
  2228. "mode-7" => Note::where('is_cancelled', false)
  2229. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  2230. ->count(),
  2231. ];
  2232. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  2233. }
  2234. public function notesProcessingCenter(Request $request) {
  2235. $notes = Note::where('is_cancelled', false);
  2236. // filters
  2237. $proUid = $request->input('proUid');
  2238. if($proUid) {
  2239. $fPro = Pro::where('uid', $proUid)->first();
  2240. if($fPro) {
  2241. $notes = $notes->where('hcp_pro_id', $fPro->id);
  2242. }
  2243. }
  2244. $proSigned = $request->input('proSigned');
  2245. switch($proSigned) {
  2246. case 'yes': $notes = $notes->where('is_signed_by_hcp', true); break;
  2247. case 'no': $notes = $notes->where('is_signed_by_hcp', '!=', true); break;
  2248. }
  2249. $billingMarkedDone = $request->input('billingMarkedDone');
  2250. switch($billingMarkedDone) {
  2251. case 'yes': $notes = $notes->where('is_billing_marked_done', true); break;
  2252. case 'no': $notes = $notes->where('is_billing_marked_done', '!=', true); break;
  2253. }
  2254. $billingClosed = $request->input('billingClosed');
  2255. switch($billingClosed) {
  2256. case 'yes': $notes = $notes->where('is_bill_closed', true); break;
  2257. case 'no': $notes = $notes->where('is_bill_closed', '!=', true); break;
  2258. }
  2259. $claimingClosed = $request->input('claimingClosed');
  2260. switch($claimingClosed) {
  2261. case 'yes': $notes = $notes->where('is_claim_closed', true); break;
  2262. case 'no': $notes = $notes->where('is_claim_closed', '!=', true); break;
  2263. }
  2264. $allClaimsSubmitted = $request->input('allClaimsSubmitted');
  2265. if($allClaimsSubmitted) {
  2266. // TODO
  2267. }
  2268. $goodBad = $request->input('goodBad');
  2269. switch($goodBad) {
  2270. case 'good': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' = 'false'"); break;
  2271. case 'bad': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' = 'true'"); break;
  2272. case 'unclassified': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' is null"); break;
  2273. }
  2274. $startDate = $request->input('startDate');
  2275. if($startDate) {
  2276. $notes = $notes->where('effective_dateest', '>=', $startDate);
  2277. }
  2278. $endDate = $request->input('endDate');
  2279. if($endDate) {
  2280. $notes = $notes->where('effective_dateest', '<=', $endDate);
  2281. }
  2282. $mcPartB = $request->input('mcPartB');
  2283. if($mcPartB) {
  2284. // TODO
  2285. }
  2286. $notes = $notes->orderBy('effective_dateest')->paginate(10);
  2287. return view('app.practice-management.notes-processing-center', compact('notes'));
  2288. }
  2289. public function getNextNote(Request $request, $mode)
  2290. {
  2291. $note = null;
  2292. switch (+$mode) {
  2293. case 1:
  2294. $note = Note::where('is_cancelled', false)
  2295. ->where('is_signed_by_hcp', false)
  2296. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2297. ->whereNull('current_note_pickup_for_processing_id')
  2298. ->orderBy('effective_dateest', 'DESC')
  2299. ->first();
  2300. break;
  2301. case 2:
  2302. $note = Note::where('is_cancelled', false)
  2303. ->where('is_signed_by_hcp', true)
  2304. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2305. ->whereNull('current_note_pickup_for_processing_id')
  2306. ->where('is_billing_marked_done', false)
  2307. ->orderBy('effective_dateest', 'DESC')
  2308. ->first();
  2309. break;
  2310. case 3:
  2311. $note = Note::where('is_cancelled', false)
  2312. ->where('is_signed_by_hcp', true)
  2313. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2314. ->whereNull('current_note_pickup_for_processing_id')
  2315. ->where('is_billing_marked_done', true)
  2316. ->where('is_bill_closed', false)
  2317. ->orderBy('effective_dateest', 'DESC')
  2318. ->first();
  2319. break;
  2320. case 4: // claiming not closed
  2321. DB::enableQueryLog(); // Enable query log
  2322. $note = Note::where('is_cancelled', false)
  2323. ->where('is_signed_by_hcp', true)
  2324. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2325. ->whereNull('current_note_pickup_for_processing_id')
  2326. ->where('is_billing_marked_done', true)
  2327. ->where('is_bill_closed', true)
  2328. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  2329. ->where('is_claim_closed', false)
  2330. ->orderBy('effective_dateest', 'DESC')
  2331. ->first();
  2332. // DB::enableQueryLog(); // Enable query log
  2333. // Your Eloquent query executed by using get()
  2334. // dd(DB::getQueryLog()); // Show results of log
  2335. break;
  2336. case 5:
  2337. $note = Note::where('is_cancelled', false)
  2338. ->where('is_signed_by_hcp', true)
  2339. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2340. ->whereNull('current_note_pickup_for_processing_id')
  2341. ->where('is_billing_marked_done', true)
  2342. ->where('is_bill_closed', true)
  2343. ->where('is_claim_closed', true)
  2344. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  2345. ->orderBy('effective_dateest', 'DESC')
  2346. ->first();
  2347. break;
  2348. case 8:
  2349. $note = Note::where('is_cancelled', false)
  2350. ->where('is_signed_by_hcp', true)
  2351. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  2352. ->whereNull('current_note_pickup_for_processing_id')
  2353. ->where('is_billing_marked_done', true)
  2354. ->where('is_bill_closed', true)
  2355. ->where('is_claim_closed', false)
  2356. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  2357. ->orderBy('effective_dateest', 'DESC')
  2358. ->first();
  2359. break;
  2360. }
  2361. if($note) {
  2362. $note->client_uid = $note->client->uid;
  2363. }
  2364. return json_encode($note);
  2365. }
  2366. public function pickedNotes(Request $request) {
  2367. $counts = [
  2368. "unpicked" => Note::where('is_cancelled', false)
  2369. ->whereNull('current_note_pickup_for_processing_id')
  2370. ->count(),
  2371. ];
  2372. $notes = Note::where('is_cancelled', false)
  2373. ->whereNotNull('current_note_pickup_for_processing_id')
  2374. ->orderBy('effective_dateest', 'ASC')
  2375. ->paginate();
  2376. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  2377. }
  2378. public function badNotes(Request $request) {
  2379. $counts = [
  2380. "unpicked" => Note::where('is_cancelled', false)
  2381. ->whereNull('current_note_pickup_for_processing_id')
  2382. ->count(),
  2383. ];
  2384. $notes = Note::where('is_cancelled', false)
  2385. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  2386. ->orderBy('effective_dateest', 'ASC')
  2387. ->paginate();
  2388. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  2389. }
  2390. public function doneNotes(Request $request) {
  2391. $counts = [
  2392. "unpicked" => Note::where('is_cancelled', false)
  2393. ->whereNull('current_note_pickup_for_processing_id')
  2394. ->count(),
  2395. ];
  2396. $notes = Note::where('is_cancelled', false)
  2397. ->where('is_signed_by_hcp', true)
  2398. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  2399. ->whereNull('current_note_pickup_for_processing_id')
  2400. ->where('is_billing_marked_done', true)
  2401. ->where('is_bill_closed', true)
  2402. ->where('is_claim_closed', true)
  2403. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  2404. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  2405. ->orderBy('effective_dateest', 'ASC')
  2406. ->paginate();
  2407. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  2408. }
  2409. public function currentMbClaim(Request $request, $claimUid) {
  2410. $claim = Claim::where('uid', $claimUid)->first();
  2411. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->where('status', '!=', 'CANCELLED')->first());
  2412. }
  2413. public function currentClaimLines(Request $request, $claimUid) {
  2414. $claim = Claim::where('uid', $claimUid)->first();
  2415. if($request->input('json')) {
  2416. foreach ($claim->lines as $line) {
  2417. $line->expected_total = round($line->expected_total, 3);
  2418. $x = $line->claimLineIcds;
  2419. }
  2420. return json_encode($claim->lines);
  2421. }
  2422. return view('app.practice-management._claim-lines', compact('claim'));
  2423. }
  2424. public function packsMultiPrint(Request $request) {
  2425. $packs = Pack
  2426. ::select('pack.*')
  2427. ->leftJoin('shipment', function($join) {
  2428. $join->on('pack.shipment_id', '=', 'shipment.id');
  2429. })
  2430. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  2431. ->where(function ($query) {
  2432. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  2433. })
  2434. ->whereNotNull('pack.label_system_file_id')
  2435. ->orderBy('pack.created_at', 'ASC')
  2436. ->get();
  2437. return view('app.practice-management.packs-multi-print', compact('packs'));
  2438. }
  2439. public function packsMultiPDF(Request $request, $ids) {
  2440. $ids = array_map(function ($_x) {
  2441. return intval($_x);
  2442. }, explode("|", $ids));
  2443. $packs = Pack::whereIn('id', $ids)->get();
  2444. }
  2445. public function handouts(Request $request) {
  2446. $handouts = Handout::orderBy('display_name')->get();
  2447. return view('app.practice-management.handouts', compact('handouts'));
  2448. }
  2449. private function callJava($request, $endPoint, $data)
  2450. {
  2451. $url = config('stag.backendUrl') . $endPoint;
  2452. $response = Http::asForm()
  2453. ->withHeaders([
  2454. 'sessionKey' => $request->cookie('sessionKey')
  2455. ])
  2456. ->post($url, $data)
  2457. ->body();
  2458. dd($response);
  2459. return $response;
  2460. }
  2461. public function genericBills(Request $request)
  2462. {
  2463. return view('app.practice-management.generic-bills');
  2464. }
  2465. public function billsUnderProcessing(Request $request)
  2466. {
  2467. $bills = Bill::where('is_cancelled', false)
  2468. ->where(function ($query) { // mcp of any client program and program OB pending
  2469. $query
  2470. ->where(function ($_query) {
  2471. $_query->where('hcp_pro_id', $this->pro->id)
  2472. ->where('hcp_expected_payment_amount', '>', 0)
  2473. ->where('has_hcp_been_paid', false)
  2474. ->where('is_signed_by_hcp', true);
  2475. })
  2476. ->orWhere(function ($_query) {
  2477. $_query->where('cm_pro_id', $this->pro->id)
  2478. ->where('cm_expected_payment_amount', '>', 0)
  2479. ->where('has_cm_been_paid', false)
  2480. ->where('is_signed_by_cm', true);
  2481. })
  2482. ->orWhere(function ($_query) {
  2483. $_query->where('rme_pro_id', $this->pro->id)
  2484. ->where('rme_expected_payment_amount', '>', 0)
  2485. ->where('has_rme_been_paid', false)
  2486. ->where('is_signed_by_rme', true);
  2487. })
  2488. ->orWhere(function ($_query) {
  2489. $_query->where('rmm_pro_id', $this->pro->id)
  2490. ->where('rmm_expected_payment_amount', '>', 0)
  2491. ->where('has_rmm_been_paid', false)
  2492. ->where('is_signed_by_rmm', true);
  2493. })
  2494. ->orWhere(function ($_query) {
  2495. $_query->where('generic_pro_id', $this->pro->id)
  2496. ->where('generic_pro_expected_payment_amount', '>', 0)
  2497. ->where('has_generic_pro_been_paid', false)
  2498. ->where('is_signed_by_generic_pro', true);
  2499. });
  2500. })
  2501. ->orderBy('created_at', 'DESC')
  2502. ->paginate();
  2503. return view('app.practice-management.bills-under-processing', compact('bills'));
  2504. }
  2505. public function careMonthReport(Request $request) {
  2506. $m = $request->input('m');
  2507. $y = $request->input('y');
  2508. if(!$m || !$y) {
  2509. $date = date('Y-m-01');
  2510. }
  2511. else {
  2512. $date = date('Y-m-d', strtotime("$m/1/$y"));
  2513. }
  2514. $pro = $this->pro;
  2515. $records = DB::select("
  2516. SELECT cm.id as care_month_id,
  2517. cm.uid as care_month_uid,
  2518. c.id as client_id,
  2519. c.uid as client_uid,
  2520. cm.number_of_days_with_remote_measurements,
  2521. cm.rm_total_time_in_seconds_by_mcp,
  2522. cm.rm_total_time_in_seconds_by_rme_pro,
  2523. cm.rm_total_time_in_seconds_by_rmm_pro,
  2524. cm.mcp_pro_id,
  2525. cm.rme_pro_id,
  2526. cm.rmm_pro_id,
  2527. (c.name_first || ' ' || c.name_last) as client_name
  2528. FROM care_month cm
  2529. JOIN client c on cm.client_id = c.id
  2530. WHERE cm.start_date = :startDate
  2531. AND (cm.mcp_pro_id = :proID OR
  2532. cm.rme_pro_id = :proID OR
  2533. cm.rmm_pro_id = :proID)
  2534. ORDER BY c.name_last, c.name_first
  2535. ",
  2536. ['startDate' => $date, 'proID' => $pro->id]
  2537. );
  2538. return view('app.practice-management.care-month-report', compact('records', 'date'));
  2539. }
  2540. public function myTeams(Request $request) {
  2541. $pro = $this->pro;
  2542. // get all teams where the authed-pro is the assistant pro
  2543. $teams = ProTeam::where('assistant_pro_id', $pro->id)
  2544. ->where('is_active', true)
  2545. ->orderBy('slug')
  2546. ->get();
  2547. return view('app.practice-management.my-teams', compact('teams'));
  2548. }
  2549. public function patientsAccountsInvites(Request $request){
  2550. $filters = $request->all();
  2551. $accountInvites = AccountInvite::select('account_invite.*')
  2552. ->join('client', 'client.id', '=', 'account_invite.for_client_id');
  2553. if($this->performer->pro->pro_type !== 'ADMIN'){
  2554. $accountInvites = $accountInvites->where('client.mcp_pro_id', $this->performer->pro->id);
  2555. }
  2556. $this->filterMultiQuery($request, $accountInvites, 'account_invite.created_at', 'date_category', 'date_value_1', 'date_value_2');
  2557. $this->filterSimpleQuery($request, $accountInvites, 'account_invite.status', 'status');
  2558. $accountInvites = $accountInvites->orderBy('created_at', 'DESC')->paginate(20);
  2559. return view('app.practice-management.patients-accounts-invites', compact('accountInvites','filters'));
  2560. }
  2561. public function clientsBdtDevices(Request $request){
  2562. $filters = $request->all();
  2563. $devices = ClientBDTDevice::select(
  2564. 'client_bdt_device.*',
  2565. DB::raw('(SELECT COUNT(*) FROM bdt_measurement WHERE bdt_measurement.bdt_device_id = client_bdt_device.device_id AND is_cellular_zero IS FALSE) as num_of_measurements'))
  2566. // DB::raw('(SELECT ts_date_time FROM bdt_measurement WHERE bdt_measurement.bdt_device_id = client_bdt_device.device_id AND is_cellular_zero IS FALSE ORDER BY ts_date_time DESC LIMIT 1) as most_recent_none_zero_measurement_at'))
  2567. ->join('client', 'client.id', '=', 'client_bdt_device.client_id');
  2568. if($this->performer->pro->pro_type !== 'ADMIN'){
  2569. $devices = $devices->where('client.mcp_pro_id', $this->performer->pro->id);
  2570. }
  2571. $this->filterMultiQuery($request, $devices, 'client_bdt_device.most_recent_measurement_at', 'date_category', 'date_value_1', 'date_value_2');
  2572. // $dateKeyName = $request->get('date_category');
  2573. // $date1 = $request->get('date_value_1');
  2574. // $date2 = $request->get('date_value_2');
  2575. // $dateColumnName = "(SELECT (MAX(ts_date_time))::DATE FROM bdt_measurement WHERE bdt_measurement.bdt_device_id = client_bdt_device.device_id AND is_cellular_zero IS FALSE)";
  2576. // switch($dateKeyName) {
  2577. // case 'EXACTLY':
  2578. // if($date1) {
  2579. // $devices->whereRaw($dateColumnName." = '$date1'::DATE");
  2580. // }
  2581. // break;
  2582. // case 'LESS_THAN':
  2583. // if($date1) {
  2584. // $devices->whereRaw($dateColumnName. "< '$date1'::DATE" );
  2585. // }
  2586. // break;
  2587. // case 'GREATER_THAN':
  2588. // if($date1) {
  2589. // $devices->whereRaw($dateColumnName. "> '$date1'::DATE");
  2590. // }
  2591. // break;
  2592. // case 'BETWEEN':
  2593. // if($date1 && $date2) {
  2594. // $devices
  2595. // ->whereRaw($dateColumnName. ">= '$date1'::DATE")
  2596. // ->whereRaw($dateColumnName. "<= '$date2'::DATE");
  2597. // }
  2598. // break;
  2599. // case 'NOT_BETWEEN':
  2600. // if($date2 && $date1) {
  2601. // $devices
  2602. // ->where(function ($q) use ($request, $dateColumnName, $date1, $date2) {
  2603. // $q->whereRaw($dateColumnName. "< '$date1'::DATE")
  2604. // ->orWhereRaw($dateColumnName. "> '$date2'::DATE");
  2605. // });
  2606. // }
  2607. // break;
  2608. // }
  2609. $keyName = $request->get('measurement_count_category');
  2610. $value1 = $request->get('measurement_count_value_1');
  2611. $value2 = $request->get('measurement_count_value_2');
  2612. $columnName = "(SELECT COUNT(*) FROM bdt_measurement WHERE bdt_measurement.bdt_device_id = client_bdt_device.device_id AND is_cellular_zero IS FALSE)";
  2613. switch($keyName) {
  2614. case 'EXACTLY':
  2615. if($value1) {
  2616. $devices->whereRaw($columnName.'='.$value1);
  2617. }
  2618. break;
  2619. case 'LESS_THAN':
  2620. if($value1) {
  2621. $devices->whereRaw($columnName. '<' .$value1);
  2622. }
  2623. break;
  2624. case 'GREATER_THAN':
  2625. if($value1) {
  2626. $devices->whereRaw($columnName. '>'. $value1);
  2627. }
  2628. break;
  2629. case 'BETWEEN':
  2630. if($value1 && $value2) {
  2631. $devices
  2632. ->whereRaw($columnName. '>=' . $value1)
  2633. ->whereRaw($columnName. '<=' . $value1);
  2634. }
  2635. break;
  2636. case 'NOT_BETWEEN':
  2637. if($value1 && $value2) {
  2638. $devices
  2639. ->where(function ($q) use ($request, $columnName, $value1, $value2) {
  2640. $q->whereRaw($columnName. '<'. $value1)
  2641. ->orWhereRaw($columnName. '>'.$value2);
  2642. });
  2643. }
  2644. break;
  2645. }
  2646. $status = $request->get('status');
  2647. if($status){
  2648. if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
  2649. if($status === 'DEACTIVATED') $devices = $devices->where('client_bdt_device.is_active', false);
  2650. }
  2651. $devices = $devices->orderBy('num_of_measurements', 'DESC')->paginate(20);
  2652. return view('app.practice-management.clients_bdt_devices', compact('devices','filters'));
  2653. }
  2654. public function memos(Request $request){
  2655. $filters = $request->all();
  2656. $memos = ClientMemo::select('client_memo.*')
  2657. ->join('client', 'client.id', '=', 'client_memo.client_id');
  2658. if($this->performer->pro->pro_type !== 'ADMIN'){
  2659. $memos = $memos->where('client.mcp_pro_id', $this->performer->pro->id);
  2660. }
  2661. $this->filterMultiQuery($request, $memos, 'client_memo.created_at', 'date_category', 'date_value_1', 'date_value_2');
  2662. $this->filterSimpleQuery($request, $memos, 'category', 'category');
  2663. $memos = $memos->orderBy('created_at', 'DESC')->paginate(20);
  2664. return view('app.practice-management.memos', compact('memos', 'filters'));
  2665. }
  2666. public function segmentTemplates(Request $request){
  2667. $segmentTemplates = SegmentTemplate::query();
  2668. $segmentTemplates = $segmentTemplates->orderBy('created_at', 'DESC');
  2669. $responseType = $request->get('response_type');
  2670. if($responseType && $responseType == 'json'){
  2671. $segmentTemplates = $segmentTemplates->where('is_active', true)->get();
  2672. return $this->pass($segmentTemplates);
  2673. }
  2674. $segmentTemplates = $segmentTemplates->paginate(30);
  2675. return view('app.practice-management.segment-templates.index', compact('segmentTemplates'));
  2676. }
  2677. public function visitTemplates(Request $request){
  2678. $visitTemplates = VisitTemplate::query();
  2679. $visitTemplates = $visitTemplates->orderBy('created_at', 'DESC');
  2680. $responseType = $request->get('response_type');
  2681. if($responseType && $responseType == 'json'){
  2682. $visitTemplates = $visitTemplates->where('is_active', true)->get();
  2683. return $this->pass($visitTemplates);
  2684. }
  2685. $visitTemplates = $visitTemplates->paginate(30);
  2686. return view('app.practice-management.visit-templates.index', compact('visitTemplates'));
  2687. }
  2688. public function visitTemplate(Request $request, VisitTemplate $visitTemplate){
  2689. $visitTemplateSegmentTemplates = VisitTemplateSegmentTemplate::query();
  2690. $visitTemplateSegmentTemplates = $visitTemplateSegmentTemplates->where('visit_template_id', $visitTemplate->id);
  2691. $visitTemplateSegmentTemplates = $visitTemplateSegmentTemplates->orderBy('position_index', 'ASC');
  2692. $visitTemplateSegmentTemplates = $visitTemplateSegmentTemplates->paginate(50);
  2693. return view('app.practice-management.visit-templates.visit-template-segment-templates.index', compact('visitTemplate','visitTemplateSegmentTemplates'));
  2694. }
  2695. public function visitTemplateAccess(Request $request, VisitTemplate $visitTemplate){
  2696. $visitTemplateAccesses = VisitTemplateAccess::where('visit_template_id', $visitTemplate->id)->paginate(50);
  2697. return view('app.practice-management.visit-templates.visit-template-accesses.index', compact('visitTemplate','visitTemplateAccesses'));
  2698. }
  2699. public function clientCcmRmStatus(Request $request){
  2700. $filters = $request->all();
  2701. $patients = Client::whereNull('shadow_pro_id');
  2702. $pro = $this->performer()->pro;
  2703. if($pro->pro_type !== 'ADMIN') {
  2704. $patients = $patients->where('mcp_pro_id', $this->performer()->pro->id);
  2705. }
  2706. // filters
  2707. /*
  2708. array:18 [▼
  2709. "age_category" => "LESS_THAN"
  2710. "age_value_1" => "34"
  2711. "age_value_2" => null
  2712. "sex" => "M"
  2713. "bmi_category" => "BETWEEN"
  2714. "bmi_value_1" => "20"
  2715. "bmi_value_2" => "25"
  2716. "last_visit_category" => "LESS_THAN"
  2717. "last_visit_value_1" => "2021-10-14"
  2718. "last_visit_value_2" => null
  2719. "next_appointment_category" => "LESS_THAN"
  2720. "next_appointment_value_1" => "2021-10-15"
  2721. "status" => "ACTIVE"
  2722. "last_weighed_in_category" => "EXACTLY"
  2723. "last_weighed_in_value_1" => "2021-10-07"
  2724. "last_bp_category" => "BETWEEN"
  2725. "last_bp_value_1" => "2021-10-01"
  2726. "last_bp_value_2" => "2021-10-31"
  2727. ]
  2728. */
  2729. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
  2730. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  2731. $this->filterMultiQuery($request, $patients, 'usual_bmi', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  2732. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  2733. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  2734. switch($request->input('status')) {
  2735. case 'ACTIVE':
  2736. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  2737. break;
  2738. case 'AWAITING_VISIT':
  2739. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  2740. break;
  2741. case 'INACTIVE':
  2742. $patients->where('is_active', '<>', true);
  2743. break;
  2744. }
  2745. if($request->input('is_eligible_for_cm')){
  2746. $patients->where('is_eligible_for_cm', '=', $request->input('is_eligible_for_cm'));
  2747. }
  2748. if($request->input('is_enrolled_in_cm')){
  2749. $patients->where('is_enrolled_in_cm', '=', $request->input('is_enrolled_in_cm'));
  2750. }
  2751. if($request->input('has_cm_setup_been_performed')){
  2752. $patients->where('has_cm_setup_been_performed', '=', $request->input('has_cm_setup_been_performed')=='YES'? true : false);
  2753. }
  2754. if($request->input('is_eligible_for_rm')){
  2755. $patients->where('is_eligible_for_rm', '=', $request->input('is_eligible_for_rm'));
  2756. }
  2757. if($request->input('is_enrolled_in_rm')){
  2758. $patients->where('is_enrolled_in_rm', '=', $request->input('is_enrolled_in_rm'));
  2759. }
  2760. if($request->input('has_rm_setup_been_performed')){
  2761. $patients->where('has_rm_setup_been_performed', '=', $request->input('has_rm_setup_been_performed') =='YES'? true : false);
  2762. }
  2763. $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
  2764. return view('app.admin.client-ccm-rm-status', compact('patients', 'filters'));
  2765. }
  2766. public function rmActionReport(Request $request){
  2767. $filters = $request->all();
  2768. $careMonthStartDate = $request->get('care_month_start_date');
  2769. $careMonthYear = $request->get('care_month_year');
  2770. if(!$careMonthStartDate){
  2771. $careMonthStartDate = '2022-01-01';
  2772. }
  2773. if($careMonthYear){
  2774. $careMonthStartDate = $careMonthYear . "-01";
  2775. }
  2776. $lastDateOfMonth = new DateTime(date("Y-m-t", strtotime(get_current_date())));
  2777. $today = new DateTime(get_current_date());
  2778. $diff = $lastDateOfMonth->diff($today);
  2779. $daysBetweenNowAndEndmonth = $diff->days;
  2780. $minRequiredMeasurements = 16 - $daysBetweenNowAndEndmonth -1;
  2781. $numOfMeasurements = $request->get('num_of_measurements'); //16_or_more, 12_or_more
  2782. $hasRecentVisit = $request->get('has_recent_visit'); //yes no
  2783. $hasBeenSpokenToThisMonth = $request->get('has_been_spoken_to'); //yes no
  2784. $cmQuery = CareMonth::where('start_date', $careMonthStartDate);
  2785. //remove dummies
  2786. $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
  2787. return $clientQuery->where('client_engagement_status_category','<>', 'DUMMY')
  2788. ->orWhere('client_engagement_status_category', '=', null);
  2789. });
  2790. if($numOfMeasurements){
  2791. if($numOfMeasurements == '16_or_more'){
  2792. $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '>=', 16);
  2793. }
  2794. if($numOfMeasurements == 'min_or_more'){
  2795. $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '>=', $minRequiredMeasurements)
  2796. ->where('number_of_days_with_remote_measurements', '<', 16);
  2797. }
  2798. if($numOfMeasurements == 'less_than_min'){
  2799. $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '<', $minRequiredMeasurements);
  2800. }
  2801. }
  2802. if($hasRecentVisit){
  2803. if($hasRecentVisit == 'YES'){
  2804. $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
  2805. return $clientQuery->whereRaw("most_recent_completed_mcp_note_date::DATE >= ((NOW() - interval '90 days')::DATE)");
  2806. });
  2807. }else{
  2808. $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
  2809. return $clientQuery->whereRaw("most_recent_completed_mcp_note_date::DATE < ((NOW() - interval '90 days')::DATE)");
  2810. });
  2811. }
  2812. }
  2813. if($hasBeenSpokenToThisMonth){
  2814. if($hasBeenSpokenToThisMonth == 'YES'){
  2815. $cmQuery = $cmQuery->where('has_anyone_interacted_with_client_about_rm_outside_note', '=', true);
  2816. }else{
  2817. $cmQuery = $cmQuery->where('has_anyone_interacted_with_client_about_rm_outside_note', '=', false);
  2818. }
  2819. }
  2820. $claimingClosed = $request->get('claiming_closed');
  2821. if($claimingClosed){
  2822. if($claimingClosed === 'YES') $cmQuery = $cmQuery->where('is_claim_closed', true);
  2823. if($claimingClosed === 'NO') $cmQuery = $cmQuery->where('is_claim_closed', false);
  2824. }
  2825. $cmQuery = $cmQuery->orderBy('start_date', 'DESC')->orderBy('client_id', 'ASC');
  2826. $rows =$cmQuery->paginate(15);
  2827. return view('app.practice-management.rm-action-report', compact('rows', 'filters', 'minRequiredMeasurements'));
  2828. }
  2829. public function myFlyers(Request $request){
  2830. $pro = $this->performer->pro;
  2831. $slug = $pro->slug ?? $pro->uid;
  2832. $url = config('app.stagfe6_url') . '/flyers/json-list?slug=' . $slug;
  2833. $arrContextOptions=array(
  2834. "ssl"=>array(
  2835. "verify_peer"=>false,
  2836. "verify_peer_name"=>false,
  2837. ),
  2838. );
  2839. $response = @file_get_contents($url, false, stream_context_create($arrContextOptions));
  2840. $response = @json_decode($response);
  2841. $flyerTemplates = [];
  2842. if(isset($response->data)){
  2843. $flyerTemplates = $response->data;
  2844. }
  2845. return view('app.my-flyers', compact('pro','flyerTemplates'));
  2846. }
  2847. public function notesResolutionCenter(Request $request) {
  2848. $columns = "(c.name_first || ' ' || c.name_last) as client_name,
  2849. (hcp.name_first || ' ' || hcp.name_last) as hcp_name,
  2850. (na.name_first || ' ' || na.name_last) as na_name,
  2851. c.chart_number,
  2852. c.uid as client_uid,
  2853. n.effective_dateest,
  2854. n.uid,
  2855. n.detail_json
  2856. ";
  2857. $from = "FROM note AS n
  2858. JOIN pro AS hcp ON n.hcp_pro_id = hcp.id
  2859. JOIN pro AS na ON n.ally_pro_id = na.id
  2860. JOIN client AS c ON n.client_id = c.id
  2861. ";
  2862. $where = "WHERE
  2863. n.is_signed_by_hcp IS TRUE AND
  2864. n.is_claim_closed IS NOT TRUE AND
  2865. n.is_cancelled IS NOT TRUE AND
  2866. n.created_at::DATE >= '2022-01-01'::DATE AND
  2867. c.client_engagement_status_category <> 'DUMMY' AND
  2868. c.name_first NOT ILIKE '%test%' AND
  2869. c.name_last NOT ILIKE '%test%' AND
  2870. n.id IN (SELECT note_id FROM bill WHERE code ILIKE '%treatment%' AND bill.is_cancelled IS NOT TRUE AND note_id IS NOT NULL) AND
  2871. n.id NOT IN (SELECT note_id FROM claim WHERE note_id IS NOT NULL) AND
  2872. c.is_part_b_primary = 'YES' AND
  2873. c.latest_eligible_refresh_at::DATE >= '2022-01-01' AND
  2874. c.mpb_remaining = 0
  2875. ";
  2876. $filters = [];
  2877. if($request->input('f')) {
  2878. $filters[] = "(n.detail_json IS NOT NULL AND ((n.detail_json)::json->'farah_decision')::text = '\"" . $request->input('f') . "\"')";
  2879. }
  2880. if($request->input('s')) {
  2881. $filters[] = "(n.detail_json IS NOT NULL AND ((n.detail_json)::json->'shawn_decision')::text = '\"" . $request->input('s') . "\"')";
  2882. }
  2883. if(count($filters)) {
  2884. $filters = 'AND ' . implode(' AND ', $filters);
  2885. }
  2886. else {
  2887. $filters = '';
  2888. }
  2889. // $filters = '';
  2890. $orderBy = "ORDER BY c.chart_number ASC, n.effective_dateest ASC";
  2891. $countQuery = "SELECT count(*) {$from} {$where} {$filters}";
  2892. // dd($countQuery);
  2893. $countResult = DB::select($countQuery);
  2894. $total = $countResult[0]->count;
  2895. $page = $request->input('page') ?: 1;
  2896. $perPage = $request->input('per_page') ?: 20;
  2897. $offset = ($page - 1) * $perPage;
  2898. $dataQuery = "SELECT {$columns} {$from} {$where} {$filters} {$orderBy} OFFSET {$offset} LIMIT {$perPage}";
  2899. $rows = DB::select($dataQuery);
  2900. $paginator = new LengthAwarePaginator($rows, $total, $request->input('per_page') ?: 20, $request->input('page') ?: 1);
  2901. $paginator->setPath(route('practice-management.notes-resolution-center'));
  2902. return view('app.practice-management.notes-resolution-center', compact('rows', 'paginator'));
  2903. }
  2904. }