PracticeManagementController.php 96 KB

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