PracticeManagementController.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\ClaimEDI;
  6. use App\Models\Handout;
  7. use App\Models\Measurement;
  8. use App\Models\Bill;
  9. use App\Models\Claim;
  10. use App\Models\Client;
  11. use App\Models\McpRequest;
  12. use App\Models\Note;
  13. use App\Models\Pack;
  14. use App\Models\Pro;
  15. use App\Models\Product;
  16. use App\Models\ProFavorite;
  17. use App\Models\ProGeneralAvailability;
  18. use App\Models\ProProAccess;
  19. use App\Models\ProRate;
  20. use App\Models\ProSpecificAvailability;
  21. use App\Models\ProSpecificUnavailability;
  22. use App\Models\ProTextShortcut;
  23. use App\Models\ProTransaction;
  24. use App\Models\Shipment;
  25. use App\Models\SupplyOrder;
  26. use App\Models\Ticket;
  27. use App\Models\ClientMeasurementDaysPerMonth;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Support\Facades\Http;
  30. use PDF;
  31. use DateTime;
  32. use DateTimeZone;
  33. use Illuminate\Http\Request;
  34. class PracticeManagementController extends Controller
  35. {
  36. public function remoteMonitoringReport(Request $request)
  37. {
  38. $rows = null;
  39. $proID = $this->performer()->pro->id;
  40. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  41. $rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  42. return view ('app.practice-management.remote-monitoring-report', compact('rows'));
  43. }
  44. public function billingReport(Request $request)
  45. {
  46. $rows = BillingReport::paginate(50);
  47. return view('app.practice-management.billing-report', compact('rows'));
  48. }
  49. public function dashboard(Request $request)
  50. {
  51. return view('app.practice-management.dashboard');
  52. }
  53. public function rates(Request $request, $selectedProUid = 'all')
  54. {
  55. $proUid = $selectedProUid ? $selectedProUid : 'all';
  56. $rates = ProRate::where('is_active', true);
  57. if ($proUid !== 'all') {
  58. $selectedPro = Pro::where('uid', $proUid)->first();
  59. $rates = $rates->where('pro_id', $selectedPro->id);
  60. }
  61. $rates = $rates->orderBy('pro_id', 'asc')->get();
  62. $pros = $this->pros;
  63. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  64. }
  65. public function previousBills(Request $request)
  66. {
  67. return view('app.practice-management.previous-bills');
  68. }
  69. public function financialTransactions(Request $request)
  70. {
  71. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  72. return view('app.practice-management.financial-transactions', compact('transactions'));
  73. }
  74. public function pendingBillsToSign(Request $request)
  75. {
  76. return view('app.practice-management.pending-bills-to-sign');
  77. }
  78. public function HR(Request $request)
  79. {
  80. return view('app.practice-management.hr');
  81. }
  82. public function directDepositSettings(Request $request)
  83. {
  84. return view('app.practice-management.direct-deposit-settings');
  85. }
  86. public function w9(Request $request)
  87. {
  88. return view('app.practice-management.w9');
  89. }
  90. public function contract(Request $request)
  91. {
  92. return view('app.practice-management.contract');
  93. }
  94. public function notes(Request $request, $filter = '')
  95. {
  96. $proID = $this->performer()->pro->id;
  97. $query = Note::where('hcp_pro_id', $proID);
  98. switch ($filter) {
  99. case 'not-yet-signed':
  100. $query = $query->where('is_signed_by_hcp', false);
  101. break;
  102. // more cases can be added as needed
  103. default:
  104. break;
  105. }
  106. $notes = $query->orderBy('created_at', 'desc')->get();
  107. return view('app.practice-management.notes', compact('notes', 'filter'));
  108. }
  109. public function bills(Request $request, $filter = '')
  110. {
  111. $proID = $this->performer()->pro->id;
  112. $query = Bill::where('is_cancelled', false);
  113. switch ($filter) {
  114. case 'not-yet-signed':
  115. $query = $query
  116. ->where(function ($q) use ($proID) {
  117. $q->where(function ($q2) use ($proID) {
  118. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  119. })
  120. ->orWhere(function ($q2) use ($proID) {
  121. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  122. })
  123. ->orWhere(function ($q2) use ($proID) {
  124. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  125. })
  126. ->orWhere(function ($q2) use ($proID) {
  127. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  128. });
  129. });
  130. break;
  131. case 'previous':
  132. $query = $query
  133. ->where(function ($q) use ($proID) {
  134. $q->where(function ($q2) use ($proID) {
  135. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  136. })
  137. ->orWhere(function ($q2) use ($proID) {
  138. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  139. })
  140. ->orWhere(function ($q2) use ($proID) {
  141. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  142. })
  143. ->orWhere(function ($q2) use ($proID) {
  144. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  145. });
  146. });
  147. break;
  148. // more cases can be added as needed
  149. default:
  150. break;
  151. }
  152. $bills = $query->orderBy('created_at', 'desc')->get();
  153. return view('app.practice-management.bills', compact('bills', 'filter'));
  154. }
  155. public function unacknowledgedCancelledBills(Request $request)
  156. {
  157. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  158. ->where('is_cancelled', true)
  159. ->where('is_cancellation_acknowledged', false)
  160. ->orderBy('created_at', 'desc')
  161. ->get();
  162. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  163. }
  164. public function myTickets(Request $request, $filter = 'open')
  165. {
  166. $performer = $this->performer();
  167. $myTickets = Ticket::where(function ($q) use ($performer) {
  168. $q->where('assigned_pro_id', $performer->pro_id)
  169. ->orWhere('manager_pro_id', $performer->pro_id)
  170. ->orWhere('ordering_pro_id', $performer->pro_id)
  171. ->orWhere('initiating_pro_id', $performer->pro_id);
  172. });
  173. if ($filter === 'open') {
  174. $myTickets = $myTickets->where('is_open', true);
  175. } else if ($filter === 'closed') {
  176. $myTickets = $myTickets->where('is_open', false);
  177. }
  178. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  179. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  180. }
  181. public function myTextShortcuts(Request $request)
  182. {
  183. $myTextShortcuts = DB::table('pro_text_shortcut')
  184. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  185. ->select(
  186. 'pro_text_shortcut.uid',
  187. 'pro_text_shortcut.shortcut',
  188. 'pro_text_shortcut.text',
  189. 'pro.name_first',
  190. 'pro.name_last'
  191. )
  192. ->where('pro_text_shortcut.is_removed', false);
  193. if($this->performer()->pro->pro_type !== 'ADMIN') {
  194. $myTextShortcuts = $myTextShortcuts->where('pro_id', $this->performer()->pro_id);
  195. }
  196. $myTextShortcuts = $myTextShortcuts
  197. ->orderBy('pro.name_last')
  198. ->orderBy('pro.name_first')
  199. ->get();
  200. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  201. }
  202. public function myFavorites(Request $request, $filter = 'all')
  203. {
  204. $performer = $this->performer();
  205. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  206. ->where('is_removed', false);
  207. if ($filter !== 'all') {
  208. $myFavorites = $myFavorites->where('category', $filter);
  209. }
  210. $myFavorites = $myFavorites
  211. ->orderBy('category', 'asc')
  212. ->orderBy('position_index', 'asc')
  213. ->get();
  214. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  215. }
  216. public function proAvailability(Request $request, $proUid = null)
  217. {
  218. $performer = $this->performer();
  219. $pro = $performer->pro;
  220. if ($proUid) {
  221. $pro = Pro::where('uid', $proUid)->first();
  222. }
  223. if ($request->get('pro_uid')) {
  224. $proUid = $request->get('pro_uid');
  225. $pro = Pro::where('uid', $proUid)->first();
  226. }
  227. $selectedProUid = $pro->uid;
  228. $pros = $this->pros;
  229. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  230. $generalAvailabilities = [
  231. 'MONDAY' => [],
  232. 'TUESDAY' => [],
  233. 'WEDNESDAY' => [],
  234. 'THURSDAY' => [],
  235. 'FRIDAY' => [],
  236. 'SATURDAY' => [],
  237. 'SUNDAY' => [],
  238. ];
  239. foreach ($generalAvailabilitiesList as $ga) {
  240. if ($ga->day_of_week == 'MONDAY') {
  241. $generalAvailabilities['MONDAY'][] = $ga;
  242. }
  243. if ($ga->day_of_week == 'TUESDAY') {
  244. $generalAvailabilities['TUESDAY'][] = $ga;
  245. }
  246. if ($ga->day_of_week == 'WEDNESDAY') {
  247. $generalAvailabilities['WEDNESDAY'][] = $ga;
  248. }
  249. if ($ga->day_of_week == 'THURSDAY') {
  250. $generalAvailabilities['THURSDAY'][] = $ga;
  251. }
  252. if ($ga->day_of_week == 'FRIDAY') {
  253. $generalAvailabilities['FRIDAY'][] = $ga;
  254. }
  255. if ($ga->day_of_week == 'SATURDAY') {
  256. $generalAvailabilities['SATURDAY'][] = $ga;
  257. }
  258. if ($ga->day_of_week == 'SUNDAY') {
  259. $generalAvailabilities['SUNDAY'][] = $ga;
  260. }
  261. }
  262. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  263. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  264. //events for the calendar
  265. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  266. $endDateTime = new DateTime($startDate);
  267. $endDateTime->modify('+6 day');
  268. $endDate = $endDateTime->format("Y-m-d");
  269. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  270. $events = json_encode($eventsData);
  271. return view(
  272. 'app.practice-management.pro-availability',
  273. compact(
  274. 'pros',
  275. 'generalAvailabilities',
  276. 'specificAvailabilities',
  277. 'specificUnavailabilities',
  278. 'events',
  279. 'selectedProUid'
  280. )
  281. );
  282. }
  283. public function loadAvailability(Request $request, $proUid)
  284. {
  285. $performer = $this->performer();
  286. $pro = $performer->pro;
  287. $startDate = $request->get('start');
  288. $endDate = $request->get('end');
  289. $selectedPro = Pro::where('uid', $proUid)->first();
  290. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  291. }
  292. public function proAvailabilityFilter(Request $request)
  293. {
  294. $proUid = $request->get('proUid');
  295. return ['success' => true, 'data' => $proUid];
  296. }
  297. // video call page (RHS)
  298. // generic call handle (no uid)
  299. // specific call handle (uid of client)
  300. public function meet(Request $request, $uid = false)
  301. {
  302. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  303. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  304. if (!empty($client)) {
  305. return view('app.video.call-minimal', compact('session', 'client'));
  306. }
  307. return view('app.video.call-agora-v2', compact('session', 'client'));
  308. }
  309. // check video page
  310. public function checkVideo(Request $request, $uid)
  311. {
  312. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  313. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  314. $publish = false;
  315. return view('app.video.check-video-minimal', compact('session', 'client'));
  316. }
  317. public function getParticipantInfo(Request $request)
  318. {
  319. $sid = intval($request->get('uid')) - 1000000;
  320. $session = AppSession::where('id', $sid)->first();
  321. $result = [
  322. "type" => '',
  323. "name" => ''
  324. ];
  325. if ($session) {
  326. $result["type"] = $session->session_type;
  327. switch ($session->session_type) {
  328. case 'PRO':
  329. $pro = Pro::where('id', $session->pro_id)->first();
  330. $result["name"] = $pro->displayName();
  331. break;
  332. case 'CLIENT':
  333. $client = Client::where('id', $session->client_id)->first();
  334. $result["name"] = $client->displayName();
  335. break;
  336. }
  337. }
  338. return json_encode($result);
  339. }
  340. // ajax ep used by the video page
  341. // this is needed bcoz meet() is used not
  342. // just for the client passed to the view
  343. public function getOpentokSessionKey(Request $request, $uid)
  344. {
  345. $client = Client::where('uid', $uid)->first();
  346. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  347. }
  348. // poll to check if there are patients with active mcp requests
  349. public function getPatientsInQueue(Request $request)
  350. {
  351. $myInitiatives = $this->performer->pro->initiatives;
  352. if ($myInitiatives) {
  353. $myInitiatives = strtoupper($myInitiatives);
  354. }
  355. $myInitiativesList = explode('|', $myInitiatives);
  356. $myForeignLanguages = $this->performer->pro->foreign_languages;
  357. if ($myForeignLanguages) {
  358. $myForeignLanguages = strtoupper($myForeignLanguages);
  359. }
  360. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  361. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  362. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  363. })
  364. ->where(function ($query) use ($myForeignLanguagesList) {
  365. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  366. })->limit(3)->get();
  367. $results = [];
  368. foreach ($clients as $client) {
  369. $results[] = [
  370. 'clientUid' => $client->uid,
  371. 'name' => $client->displayName(),
  372. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  373. ];
  374. }
  375. return json_encode($results);
  376. }
  377. public function currentWork(Request $request)
  378. {
  379. return view('app/current-work');
  380. }
  381. public function calendar(Request $request, $proUid = null)
  382. {
  383. $pros = Pro::all();
  384. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  385. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  386. $accessibleProIds = [];
  387. foreach ($accessiblePros as $accessiblePro) {
  388. $accessibleProIds[] = $accessiblePro->id;
  389. }
  390. $accessibleProIds[] = $this->pro->id;
  391. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  392. }
  393. return view('app.practice-management.calendar', compact('pros'));
  394. }
  395. public function cellularDeviceManager(Request $request, $proUid = null)
  396. {
  397. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  398. $performerPro = $this->performer->pro;
  399. $targetPro = null;
  400. $allPros = [];
  401. $expectedForHcp = null;
  402. if ($performerPro->pro_type == 'ADMIN') {
  403. $allPros = Pro::all();
  404. $targetPro = Pro::where('uid', $proUid)->first();
  405. } else {
  406. $targetPro = $performerPro;
  407. }
  408. $clients = [];
  409. if ($targetPro) {
  410. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  411. } else {
  412. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  413. }
  414. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  415. }
  416. public function treatmentServiceUtil(Request $request)
  417. {
  418. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  419. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  420. $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"));
  421. return view('app.practice-management.treatment-services-util', compact(
  422. 'view_treatment_service_utilization_org',
  423. 'view_treatment_service_utilization',
  424. 'view_treatment_service_utilization_by_patient'));
  425. }
  426. public function processingBillMatrix(Request $request, $proUid = null)
  427. {
  428. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  429. $performerPro = $this->performer->pro;
  430. $targetPro = null;
  431. $allPros = [];
  432. if ($performerPro->pro_type == 'ADMIN') {
  433. $allPros = Pro::all();
  434. $targetPro = Pro::where('uid', $proUid)->first();
  435. } else {
  436. $targetPro = $performerPro;
  437. }
  438. $bills = [];
  439. if ($targetPro) {
  440. $bills = Bill::where('hcp_pro_id', $targetPro->id)->
  441. where('has_hcp_been_paid', false)->
  442. where('is_cancelled', false)->
  443. where('is_signed_by_hcp', true)->
  444. orderBy('effective_date', 'desc')->paginate();
  445. } else {
  446. $bills = Bill::where('has_hcp_been_paid', false)->
  447. where('is_cancelled', false)->
  448. where('is_signed_by_hcp', true)->
  449. orderBy('effective_date', 'desc')->
  450. paginate();
  451. }
  452. $viewData = [
  453. 'bills' => $bills,
  454. 'allPros' => $allPros,
  455. 'targetPro' => $targetPro,
  456. 'performerPro' => $performerPro,
  457. 'proUid' => $proUid
  458. ];
  459. return view('app.practice-management.processing-bill-matrix', $viewData);
  460. }
  461. public function hcpBillMatrix(Request $request, $proUid = null)
  462. {
  463. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  464. $performerPro = $this->performer->pro;
  465. $targetPro = null;
  466. $allPros = [];
  467. $expectedForHcp = null;
  468. if ($performerPro->pro_type == 'ADMIN') {
  469. $allPros = Pro::all();
  470. $targetPro = Pro::where('uid', $proUid)->first();
  471. } else {
  472. $targetPro = $performerPro;
  473. }
  474. $rows = [];
  475. if ($targetPro) {
  476. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  477. } else {
  478. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  479. }
  480. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  481. }
  482. public function billingManager(Request $request, $proUid = null)
  483. {
  484. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  485. $performerPro = $this->performer->pro;
  486. $targetPro = null;
  487. $allPros = [];
  488. $expectedForHcp = null;
  489. if ($performerPro->pro_type == 'ADMIN') {
  490. $allPros = Pro::all();
  491. $targetPro = Pro::where('uid', $proUid)->first();
  492. } else {
  493. $targetPro = $performerPro;
  494. }
  495. $notes = [];
  496. if ($targetPro) {
  497. $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;
  498. $notes = Note::where('hcp_pro_id', $targetPro->id);
  499. } else {
  500. $notes = Note::where('id', '>', 0);
  501. }
  502. if($request->input('date')) {
  503. $notes = $notes->where('effective_dateest', $request->input('date'));
  504. }
  505. $filters = [];
  506. $filters['bills_created'] = $request->input('bills_created');
  507. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  508. $filters['bills_resolved'] = $request->input('bills_resolved');
  509. $filters['bills_closed'] = $request->input('bills_closed');
  510. $filters['claims_created'] = $request->input('claims_created');
  511. $filters['claims_closed'] = $request->input('claims_closed');
  512. if ($filters['bills_created']) {
  513. $notes->where(
  514. 'bill_total_expected',
  515. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  516. 0);
  517. }
  518. if ($filters['is_billing_marked_done']) {
  519. $notes->where(
  520. 'is_billing_marked_done',
  521. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  522. true);
  523. }
  524. if ($filters['bills_resolved']) {
  525. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  526. if ($filters['bills_resolved'] === 'yes') {
  527. $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');
  528. } elseif ($filters['bills_resolved'] === 'no') {
  529. $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');
  530. }
  531. }
  532. if ($filters['bills_closed']) {
  533. $notes->where(
  534. 'is_bill_closed',
  535. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  536. true);
  537. }
  538. if ($filters['claims_created']) {
  539. $notes->where(
  540. 'claim_total_expected',
  541. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  542. 0);
  543. }
  544. if ($filters['claims_closed']) {
  545. $notes->where(
  546. 'is_claim_closed',
  547. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  548. true);
  549. }
  550. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  551. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  552. }
  553. public function billMatrix(Request $request)
  554. {
  555. $bClients = [];
  556. $bHCPPros = [];
  557. $bNAPros = [];
  558. $filters = [];
  559. $filters['client'] = $request->input('client');
  560. $filters['service'] = $request->input('service');
  561. $filters['hcp'] = $request->input('hcp');
  562. $filters['hcp_paid'] = $request->input('hcp_paid');
  563. $filters['expected_op'] = $request->input('expected_op');
  564. $filters['expected_value'] = $request->input('expected_value');
  565. $filters['paid_op'] = $request->input('paid_op');
  566. $filters['paid_value'] = $request->input('paid_value');
  567. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  568. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  569. $filters['hcp_sign'] = $request->input('hcp_sign');
  570. $filters['verified'] = $request->input('verified');
  571. $filters['cancelled'] = $request->input('cancelled');
  572. $bills = Bill::orderBy('effective_date')->paginate();
  573. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  574. }
  575. public function medicarePartBClaims(Request $request)
  576. {
  577. $medicarePartBOnly = $request->get("medicare_part_b");
  578. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  579. //Only medicare claims
  580. $claims = [];
  581. foreach ($allClaims as $claim) {
  582. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  583. $claims[] = $claim;
  584. }
  585. }
  586. $claimEDIs = ClaimEDI::all();
  587. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  588. }
  589. // Generate PDF
  590. public function downloadClaims()
  591. {
  592. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  593. view()->share('claims', $claims);
  594. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  595. return $pdf->download('pdf_file.pdf');
  596. }
  597. public function tickets(Request $request, $proUid = null)
  598. {
  599. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  600. return view('app.practice-management.tickets', compact('tickets'));
  601. }
  602. public function supplyOrders(Request $request)
  603. {
  604. // counts
  605. $counts = $this->getSupplyOrderCounts();
  606. // so clients
  607. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  608. $soClientIDs = array_map(function ($_x) {
  609. return $_x->client_id;
  610. }, $soClientIDs);
  611. $soClients = Client::whereIn('id', $soClientIDs)->get();
  612. // so products
  613. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  614. $soProductIDs = array_map(function ($_x) {
  615. return $_x->product_id;
  616. }, $soProductIDs);
  617. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  618. $filters = [];
  619. $filters['client'] = $request->input('client');
  620. $filters['product'] = $request->input('product');
  621. $filters['reason'] = $request->input('reason');
  622. $filters['cu_memo'] = $request->input('cu_memo');
  623. $filters['pro_sign'] = $request->input('pro_sign');
  624. $filters['client_sign'] = $request->input('client_sign');
  625. $filters['shipment'] = $request->input('shipment');
  626. $filters['lot_number'] = $request->input('lot_number');
  627. $filters['imei'] = $request->input('imei');
  628. $filters['cancelled'] = $request->input('cancelled');
  629. $supplyOrders = SupplyOrder::where('id', '>', 0);
  630. // apply filters
  631. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  632. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  633. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  634. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  635. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  636. if ($filters['client_sign']) {
  637. if ($filters['client_sign'] === 'signed')
  638. $supplyOrders->where('is_signed_by_client', true);
  639. elseif ($filters['client_sign'] === 'waived')
  640. $supplyOrders->where('is_client_signature_waived', true);
  641. else
  642. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  643. }
  644. if ($filters['shipment']) {
  645. if ($filters['shipment'] === 'not_cleared_for_shipment')
  646. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  647. elseif ($filters['shipment'] === 'cleared_for_shipment')
  648. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  649. else
  650. $supplyOrders
  651. ->whereNotNull('shipment_id')
  652. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  653. }
  654. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  655. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  656. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  657. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  658. return view('app.practice-management.supply-orders',
  659. compact('supplyOrders', 'filters',
  660. 'soClients', 'soProducts', 'counts'
  661. )
  662. );
  663. }
  664. public function shipments(Request $request, $filter = null)
  665. {
  666. // counts
  667. $counts = $this->getShipmentCounts();
  668. // so clients
  669. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  670. $shClientIDs = array_map(function ($_x) {
  671. return $_x->client_id;
  672. }, $shClientIDs);
  673. $shClients = Client::whereIn('id', $shClientIDs)->get();
  674. $shipments = Shipment::where('id', '>', 0);
  675. $filters = [];
  676. $filters['client'] = $request->input('client');
  677. $filters['courier'] = $request->input('courier');
  678. $filters['tracking_num'] = $request->input('tracking_num');
  679. $filters['label'] = $request->input('label');
  680. $filters['status'] = $request->input('status');
  681. $filters['cancelled'] = $request->input('cancelled');
  682. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  683. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  684. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  685. if ($filters['label']) {
  686. if ($filters['label'] === 'yes')
  687. $shipments->whereNotNull('label_system_file_id');
  688. else
  689. $shipments->whereNull('label_system_file_id');
  690. }
  691. if ($filters['status']) $shipments->where('status', $filters['status']);
  692. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  693. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  694. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  695. }
  696. public function cellularMeasurements(Request $request)
  697. {
  698. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  699. return view('app.practice-management.cellular-measurements', compact('measurements'));
  700. }
  701. // v2 supply-orders & shipments management (wh)
  702. public function supplyOrdersReadyToShip(Request $request)
  703. {
  704. $counts = $this->getSupplyOrderCounts();
  705. $supplyOrders = SupplyOrder
  706. ::where('is_cleared_for_shipment', true)
  707. ->where('is_cancelled', false)
  708. ->whereNull('shipment_id')
  709. ->join('client', 'client.id', '=', 'supply_order.client_id')
  710. ->orderBy('client.name_last', 'ASC')
  711. ->orderBy('client.name_first', 'ASC')
  712. ->orderBy('supply_order.client_id', 'ASC')
  713. ->orderBy('supply_order.mailing_address_full', 'ASC')
  714. ->orderBy('supply_order.created_at', 'ASC')
  715. ->select('supply_order.*')
  716. ->paginate();
  717. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  718. }
  719. public function supplyOrdersShipmentUnderway(Request $request)
  720. {
  721. $counts = $this->getSupplyOrderCounts();
  722. $supplyOrders = SupplyOrder
  723. ::where('is_cancelled', false)
  724. ->whereNotNull('shipment_id')
  725. ->orderBy('client_id', 'ASC')
  726. ->orderBy('mailing_address_full', 'ASC')
  727. ->orderBy('created_at', 'ASC')
  728. ->paginate();
  729. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  730. }
  731. public function supplyOrdersHanging(Request $request)
  732. {
  733. $counts = $this->getSupplyOrderCounts();
  734. $supplyOrders = SupplyOrder
  735. ::select('supply_order.*')
  736. ->leftJoin('shipment', function($join) {
  737. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  738. })
  739. ->where('shipment.status', 'CANCELLED')
  740. ->where('supply_order.is_cancelled', false)
  741. ->orderBy('supply_order.client_id', 'ASC')
  742. ->orderBy('supply_order.mailing_address_full', 'ASC')
  743. ->orderBy('supply_order.created_at', 'ASC')
  744. ->paginate();
  745. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  746. }
  747. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  748. {
  749. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  750. ->where('is_cancelled', true)
  751. ->where('is_cancellation_acknowledged', false)
  752. ->orderBy('created_at', 'desc')
  753. ->paginate();
  754. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  755. }
  756. public function supplyOrdersUnsigned(Request $request)
  757. {
  758. $supplyOrders = SupplyOrder
  759. ::where('is_cancelled', false)
  760. ->where('is_signed_by_pro', false)
  761. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  762. ->orderBy('created_at', 'desc')
  763. ->paginate();
  764. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  765. }
  766. private function getSupplyOrderCounts()
  767. {
  768. return [
  769. "supplyOrders" => SupplyOrder::count(),
  770. "supplyOrdersReadyToShip" => SupplyOrder
  771. ::where('is_cleared_for_shipment', true)
  772. ->where('is_cancelled', false)
  773. ->whereNull('shipment_id')->count(),
  774. "supplyOrdersShipmentUnderway" => SupplyOrder
  775. ::where('is_cancelled', false)
  776. ->whereNotNull('shipment_id')->count(),
  777. "supplyOrdersHanging" => SupplyOrder
  778. ::leftJoin('shipment', function($join) {
  779. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  780. })
  781. ->where('shipment.status', 'CANCELLED')
  782. ->where('supply_order.is_cancelled', false)
  783. ->count(),
  784. ];
  785. }
  786. public function shipmentsReadyToPrint(Request $request)
  787. {
  788. $counts = $this->getShipmentCounts();
  789. $shipments = Shipment
  790. ::where('is_cancelled', false)
  791. ->where('status', 'CREATED')
  792. ->orderBy('created_at', 'ASC')
  793. ->paginate();
  794. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  795. }
  796. public function shipmentsShipmentUnderway(Request $request)
  797. {
  798. $counts = $this->getShipmentCounts();
  799. $shipments = Shipment
  800. ::where('is_cancelled', false)
  801. ->where('status', 'PRINTED')
  802. ->orderBy('created_at', 'ASC')
  803. ->paginate();
  804. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  805. }
  806. private function getShipmentCounts()
  807. {
  808. return [
  809. "shipments" => Shipment::count(),
  810. "shipmentsReadyToPrint" => Shipment
  811. ::where('is_cancelled', false)
  812. ->where('status', 'CREATED')
  813. ->count(),
  814. "shipmentsWaitingForPicker" => Shipment
  815. ::where('is_cancelled', false)
  816. ->where('status', 'PRINTED')
  817. ->count()
  818. ];
  819. }
  820. public function shipment(Request $request, Shipment $shipment)
  821. {
  822. return view('app.practice-management.shipment', compact('shipment'));
  823. }
  824. public function shipmentsMultiPrint(Request $request, $ids)
  825. {
  826. $ids = array_map(function ($_x) {
  827. return intval($_x);
  828. }, explode("|", $ids));
  829. $shipments = Shipment::whereIn('id', $ids)->get();
  830. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  831. }
  832. public function patientClaimSummary(Request $request, $proUid = null)
  833. {
  834. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  835. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  836. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  837. $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;
  838. $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;
  839. $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;
  840. $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;
  841. $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;
  842. $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;
  843. $performerPro = $this->performer->pro;
  844. $allPros = [];
  845. if ($performerPro->pro_type == 'ADMIN') {
  846. $allPros = Pro::all();
  847. } else {
  848. $allPros = [$performerPro];
  849. }
  850. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  851. $patientsQuery = Client::where('is_dummy', '=', false)
  852. ->whereNull('shadow_pro_id')
  853. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  854. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  855. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  856. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  857. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  858. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  859. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  860. if ($proUid) {
  861. $mcpPro = Pro::where('uid', $proUid)->first();
  862. if ($mcpPro) {
  863. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  864. }
  865. }
  866. $patientsQuery->orderBy('notes_without_claiming_closed', 'DESC');
  867. $patients = $patientsQuery->paginate(50);
  868. $data = [
  869. 'patients' => $patients,
  870. 'proUid' => $proUid,
  871. 'allPros' => $allPros,
  872. 'notesTotal' => $notesTotal,
  873. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  874. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  875. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  876. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  877. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  878. 'patientsTotal' => $patientsTotal,
  879. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  880. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  881. ];
  882. return view('app.practice-management.patient-claim-summary', $data);
  883. }
  884. public function packsMultiPrint(Request $request) {
  885. $packs = Pack
  886. ::select('pack.*')
  887. ->leftJoin('shipment', function($join) {
  888. $join->on('pack.shipment_id', '=', 'shipment.id');
  889. })
  890. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  891. ->where(function ($query) {
  892. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  893. })
  894. ->whereNotNull('pack.label_system_file_id')
  895. ->orderBy('pack.created_at', 'ASC')
  896. ->get();
  897. return view('app.practice-management.packs-multi-print', compact('packs'));
  898. }
  899. public function packsMultiPDF(Request $request, $ids) {
  900. $ids = array_map(function ($_x) {
  901. return intval($_x);
  902. }, explode("|", $ids));
  903. $packs = Pack::whereIn('id', $ids)->get();
  904. }
  905. public function handouts(Request $request) {
  906. $handouts = Handout::orderBy('display_name')->get();
  907. return view('app.practice-management.handouts', compact('handouts'));
  908. }
  909. private function callJava($request, $endPoint, $data)
  910. {
  911. $url = config('stag.backendUrl') . $endPoint;
  912. $response = Http::asForm()
  913. ->withHeaders([
  914. 'sessionKey' => $request->cookie('sessionKey')
  915. ])
  916. ->post($url, $data)
  917. ->body();
  918. dd($response);
  919. return $response;
  920. }
  921. }