PracticeManagementController.php 42 KB

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