PracticeManagementController.php 90 KB

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