PracticeManagementController.php 83 KB

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