PracticeManagementController.php 64 KB

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