PracticeManagementController.php 95 KB

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