PracticeManagementController.php 85 KB

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