PracticeManagementController.php 85 KB

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