PracticeManagementController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\Measurement;
  5. use App\Models\Bill;
  6. use App\Models\Claim;
  7. use App\Models\Client;
  8. use App\Models\McpRequest;
  9. use App\Models\Note;
  10. use App\Models\Pro;
  11. use App\Models\ProFavorite;
  12. use App\Models\ProGeneralAvailability;
  13. use App\Models\ProProAccess;
  14. use App\Models\ProRate;
  15. use App\Models\ProSpecificAvailability;
  16. use App\Models\ProSpecificUnavailability;
  17. use App\Models\ProTextShortcut;
  18. use App\Models\ProTransaction;
  19. use App\Models\Ticket;
  20. use Illuminate\Support\Facades\DB;
  21. use PDF;
  22. use DateTime;
  23. use DateTimeZone;
  24. use Illuminate\Http\Request;
  25. class PracticeManagementController extends Controller
  26. {
  27. public function dashboard(Request $request)
  28. {
  29. return view('app.practice-management.dashboard');
  30. }
  31. public function rates(Request $request, $selectedProUid = 'all')
  32. {
  33. $proUid = $selectedProUid ? $selectedProUid : 'all';
  34. $rates = ProRate::where('is_active', true);
  35. if ($proUid !== 'all') {
  36. $selectedPro = Pro::where('uid', $proUid)->first();
  37. $rates = $rates->where('pro_id', $selectedPro->id);
  38. }
  39. $rates = $rates->orderBy('pro_id', 'asc')->get();
  40. $pros = $this->pros;
  41. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  42. }
  43. public function previousBills(Request $request)
  44. {
  45. return view('app.practice-management.previous-bills');
  46. }
  47. public function financialTransactions(Request $request)
  48. {
  49. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  50. return view('app.practice-management.financial-transactions', compact('transactions'));
  51. }
  52. public function pendingBillsToSign(Request $request)
  53. {
  54. return view('app.practice-management.pending-bills-to-sign');
  55. }
  56. public function HR(Request $request)
  57. {
  58. return view('app.practice-management.hr');
  59. }
  60. public function directDepositSettings(Request $request)
  61. {
  62. return view('app.practice-management.direct-deposit-settings');
  63. }
  64. public function w9(Request $request)
  65. {
  66. return view('app.practice-management.w9');
  67. }
  68. public function contract(Request $request)
  69. {
  70. return view('app.practice-management.contract');
  71. }
  72. public function notes(Request $request, $filter = '')
  73. {
  74. $proID = $this->performer()->pro->id;
  75. $query = Note::where('hcp_pro_id', $proID);
  76. switch ($filter) {
  77. case 'not-yet-signed':
  78. $query = $query->where('is_signed_by_hcp', false);
  79. break;
  80. // more cases can be added as needed
  81. default:
  82. break;
  83. }
  84. $notes = $query->orderBy('created_at', 'desc')->get();
  85. return view('app.practice-management.notes', compact('notes', 'filter'));
  86. }
  87. public function bills(Request $request, $filter = '')
  88. {
  89. $proID = $this->performer()->pro->id;
  90. $query = Bill::where('is_cancelled', false);
  91. switch ($filter) {
  92. case 'not-yet-signed':
  93. $query = $query
  94. ->where(function ($q) use ($proID) {
  95. $q->where(function ($q2) use ($proID) {
  96. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  97. })
  98. ->orWhere(function ($q2) use ($proID) {
  99. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  100. })
  101. ->orWhere(function ($q2) use ($proID) {
  102. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  103. })
  104. ->orWhere(function ($q2) use ($proID) {
  105. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  106. });
  107. });
  108. break;
  109. case 'previous':
  110. $query = $query
  111. ->where(function ($q) use ($proID) {
  112. $q->where(function ($q2) use ($proID) {
  113. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  114. })
  115. ->orWhere(function ($q2) use ($proID) {
  116. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  117. })
  118. ->orWhere(function ($q2) use ($proID) {
  119. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  120. })
  121. ->orWhere(function ($q2) use ($proID) {
  122. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  123. });
  124. });
  125. break;
  126. // more cases can be added as needed
  127. default:
  128. break;
  129. }
  130. $bills = $query->orderBy('created_at', 'desc')->get();
  131. return view('app.practice-management.bills', compact('bills', 'filter'));
  132. }
  133. public function myTickets(Request $request, $filter = 'open')
  134. {
  135. $performer = $this->performer();
  136. $myTickets = Ticket::where(function ($q) use ($performer) {
  137. $q->where('assigned_pro_id', $performer->pro_id)
  138. ->orWhere('manager_pro_id', $performer->pro_id)
  139. ->orWhere('ordering_pro_id', $performer->pro_id)
  140. ->orWhere('initiating_pro_id', $performer->pro_id);
  141. });
  142. if ($filter === 'open') {
  143. $myTickets = $myTickets->where('is_open', true);
  144. } else if ($filter === 'closed') {
  145. $myTickets = $myTickets->where('is_open', false);
  146. }
  147. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  148. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  149. }
  150. public function myTextShortcuts(Request $request)
  151. {
  152. $performer = $this->performer();
  153. $myTextShortcuts = ProTextShortcut::where('pro_id', $performer->pro_id)->where('is_removed', false)->get();
  154. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  155. }
  156. public function myFavorites(Request $request, $filter = 'all')
  157. {
  158. $performer = $this->performer();
  159. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  160. ->where('is_removed', false);
  161. if ($filter !== 'all') {
  162. $myFavorites = $myFavorites->where('category', $filter);
  163. }
  164. $myFavorites = $myFavorites
  165. ->orderBy('category', 'asc')
  166. ->orderBy('position_index', 'asc')
  167. ->get();
  168. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  169. }
  170. public function proAvailability(Request $request, $proUid = null)
  171. {
  172. $performer = $this->performer();
  173. $pro = $performer->pro;
  174. if ($proUid) {
  175. $pro = Pro::where('uid', $proUid)->first();
  176. }
  177. if ($request->get('pro_uid')) {
  178. $proUid = $request->get('pro_uid');
  179. $pro = Pro::where('uid', $proUid)->first();
  180. }
  181. $selectedProUid = $pro->uid;
  182. $pros = $this->pros;
  183. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  184. $generalAvailabilities = [
  185. 'MONDAY' => [],
  186. 'TUESDAY' => [],
  187. 'WEDNESDAY' => [],
  188. 'THURSDAY' => [],
  189. 'FRIDAY' => [],
  190. 'SATURDAY' => [],
  191. 'SUNDAY' => [],
  192. ];
  193. foreach ($generalAvailabilitiesList as $ga) {
  194. if ($ga->day_of_week == 'MONDAY') {
  195. $generalAvailabilities['MONDAY'][] = $ga;
  196. }
  197. if ($ga->day_of_week == 'TUESDAY') {
  198. $generalAvailabilities['TUESDAY'][] = $ga;
  199. }
  200. if ($ga->day_of_week == 'WEDNESDAY') {
  201. $generalAvailabilities['WEDNESDAY'][] = $ga;
  202. }
  203. if ($ga->day_of_week == 'THURSDAY') {
  204. $generalAvailabilities['THURSDAY'][] = $ga;
  205. }
  206. if ($ga->day_of_week == 'FRIDAY') {
  207. $generalAvailabilities['FRIDAY'][] = $ga;
  208. }
  209. if ($ga->day_of_week == 'SATURDAY') {
  210. $generalAvailabilities['SATURDAY'][] = $ga;
  211. }
  212. if ($ga->day_of_week == 'SUNDAY') {
  213. $generalAvailabilities['SUNDAY'][] = $ga;
  214. }
  215. }
  216. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  217. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  218. //events for the calendar
  219. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  220. $endDateTime = new DateTime($startDate);
  221. $endDateTime->modify('+6 day');
  222. $endDate = $endDateTime->format("Y-m-d");
  223. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  224. $events = json_encode($eventsData);
  225. return view(
  226. 'app.practice-management.pro-availability',
  227. compact(
  228. 'pros',
  229. 'generalAvailabilities',
  230. 'specificAvailabilities',
  231. 'specificUnavailabilities',
  232. 'events',
  233. 'selectedProUid'
  234. )
  235. );
  236. }
  237. public function loadAvailability(Request $request, $proUid)
  238. {
  239. $performer = $this->performer();
  240. $pro = $performer->pro;
  241. $startDate = $request->get('start');
  242. $endDate = $request->get('end');
  243. $selectedPro = Pro::where('uid', $proUid)->first();
  244. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  245. }
  246. public function proAvailabilityFilter(Request $request)
  247. {
  248. $proUid = $request->get('proUid');
  249. return ['success' => true, 'data' => $proUid];
  250. }
  251. // video call page (RHS)
  252. // generic call handle (no uid)
  253. // specific call handle (uid of client)
  254. public function meet(Request $request, $uid = false)
  255. {
  256. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  257. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  258. if (!empty($client)) {
  259. return view('app.video.call-minimal', compact('session', 'client'));
  260. }
  261. return view('app.video.call-agora-v2', compact('session', 'client'));
  262. }
  263. // check video page
  264. public function checkVideo(Request $request, $uid)
  265. {
  266. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  267. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  268. $publish = false;
  269. return view('app.video.check-video-minimal', compact('session', 'client'));
  270. }
  271. public function getParticipantInfo(Request $request)
  272. {
  273. $sid = intval($request->get('uid')) - 1000000;
  274. $session = AppSession::where('id', $sid)->first();
  275. $result = [
  276. "type" => '',
  277. "name" => ''
  278. ];
  279. if ($session) {
  280. $result["type"] = $session->session_type;
  281. switch ($session->session_type) {
  282. case 'PRO':
  283. $pro = Pro::where('id', $session->pro_id)->first();
  284. $result["name"] = $pro->displayName();
  285. break;
  286. case 'CLIENT':
  287. $client = Client::where('id', $session->client_id)->first();
  288. $result["name"] = $client->displayName();
  289. break;
  290. }
  291. }
  292. return json_encode($result);
  293. }
  294. // ajax ep used by the video page
  295. // this is needed bcoz meet() is used not
  296. // just for the client passed to the view
  297. public function getOpentokSessionKey(Request $request, $uid)
  298. {
  299. $client = Client::where('uid', $uid)->first();
  300. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  301. }
  302. // poll to check if there are patients with active mcp requests
  303. public function getPatientsInQueue(Request $request)
  304. {
  305. $requests = McpRequest::where('is_active', true)->where('was_claimed', false)->limit(3)->get();
  306. $results = [];
  307. if ($requests && count($requests)) {
  308. foreach ($requests as $mcpRequest) {
  309. $client = $mcpRequest->client;
  310. // if ($client->initiative) {
  311. // if (strpos($this->performer->pro->initiative, $client->initiative) !== false) {
  312. // $results[] = [
  313. // "clientUid" => $client->uid,
  314. // "name" => $client->displayName(),
  315. // "initials" => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  316. // ];
  317. // }
  318. // } else {
  319. $results[] = [
  320. "clientUid" => $client->uid,
  321. "name" => $client->displayName(),
  322. "initials" => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  323. ];
  324. //}
  325. }
  326. }
  327. return json_encode($results);
  328. }
  329. public function currentWork(Request $request)
  330. {
  331. return view('app/current-work');
  332. }
  333. public function calendar(Request $request, $proUid = null)
  334. {
  335. $pros = Pro::all();
  336. if($this->pro && $this->pro->pro_type != 'ADMIN'){
  337. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  338. $accessibleProIds = [];
  339. foreach($accessiblePros as $accessiblePro){
  340. $accessibleProIds[] = $accessiblePro->id;
  341. }
  342. $accessibleProIds[] = $this->pro->id;
  343. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  344. }
  345. return view('app.practice-management.calendar', compact('pros'));
  346. }
  347. public function cellularDeviceManager(Request $request, $proUid = null)
  348. {
  349. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  350. $performerPro = $this->performer->pro;
  351. $targetPro = null;
  352. $allPros = [];
  353. $expectedForHcp = null;
  354. if ($performerPro->pro_type == 'ADMIN') {
  355. $allPros = Pro::all();
  356. $targetPro = Pro::where('uid', $proUid)->first();
  357. } else {
  358. $targetPro = $performerPro;
  359. }
  360. $clients = [];
  361. if ($targetPro) {
  362. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  363. } else {
  364. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  365. }
  366. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  367. }
  368. public function treatmentServiceUtil(Request $request){
  369. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  370. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  371. $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"));
  372. return view('app.practice-management.treatment-services-util', compact(
  373. 'view_treatment_service_utilization_org',
  374. 'view_treatment_service_utilization',
  375. 'view_treatment_service_utilization_by_patient'));
  376. }
  377. public function hcpBillMatrix(Request $request, $proUid = null)
  378. {
  379. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  380. $performerPro = $this->performer->pro;
  381. $targetPro = null;
  382. $allPros = [];
  383. $expectedForHcp = null;
  384. if ($performerPro->pro_type == 'ADMIN') {
  385. $allPros = Pro::all();
  386. $targetPro = Pro::where('uid', $proUid)->first();
  387. } else {
  388. $targetPro = $performerPro;
  389. }
  390. $rows = [];
  391. if ($targetPro) {
  392. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  393. } else {
  394. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  395. }
  396. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  397. }
  398. public function billingManager(Request $request, $proUid = null)
  399. {
  400. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  401. $performerPro = $this->performer->pro;
  402. $targetPro = null;
  403. $allPros = [];
  404. $expectedForHcp = null;
  405. if ($performerPro->pro_type == 'ADMIN') {
  406. $allPros = Pro::all();
  407. $targetPro = Pro::where('uid', $proUid)->first();
  408. } else {
  409. $targetPro = $performerPro;
  410. }
  411. $notes = [];
  412. if ($targetPro) {
  413. $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;
  414. $notes = Note::where('hcp_pro_id', $targetPro->id)->orderBy('effective_dateest', 'desc')->paginate();
  415. } else {
  416. $notes = Note::orderBy('effective_dateest', 'desc')->paginate();
  417. }
  418. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  419. }
  420. public function claims(Request $request)
  421. {
  422. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->paginate();
  423. return view('app.practice-management.claims', compact('claims'));
  424. }
  425. // Generate PDF
  426. public function downloadClaims()
  427. {
  428. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  429. view()->share('claims', $claims);
  430. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  431. return $pdf->download('pdf_file.pdf');
  432. }
  433. public function tickets(Request $request, $proUid = null)
  434. {
  435. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  436. return view('app.practice-management.tickets', compact('tickets'));
  437. }
  438. public function cellularMeasurements(Request $request){
  439. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  440. return view('app.practice-management.cellular-measurements', compact('measurements'));
  441. }
  442. }