PracticeManagementController.php 92 KB

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