PracticeManagementController.php 19 KB

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