PracticeManagementController.php 127 KB

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