PracticeManagementController.php 75 KB

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