PracticeManagementController.php 41 KB

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