PracticeManagementController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. $myInitiatives = $this->performer->pro->initiatives;
  306. if($myInitiatives){
  307. $myInitiatives = strtoupper($myInitiatives);
  308. }
  309. $myInitiativesList = explode('|', $myInitiatives);
  310. $myForeignLanguages = $this->performer->pro->foreign_languages;
  311. if($myForeignLanguages){
  312. $myForeignLanguages = strtoupper($myForeignLanguages);
  313. }
  314. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  315. $clients = Client::whereNotNull('active_mcp_request_id')->where(function($query) use ($myInitiativesList){
  316. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  317. })
  318. ->where(function($query) use ($myForeignLanguagesList) {
  319. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  320. })->limit(3)->get();
  321. $results = [];
  322. foreach($clients as $client){
  323. $results[] = [
  324. 'clientUid' => $client->uid,
  325. 'name' => $client->displayName(),
  326. 'initials'=> substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  327. ];
  328. }
  329. return json_encode($results);
  330. }
  331. public function currentWork(Request $request)
  332. {
  333. return view('app/current-work');
  334. }
  335. public function calendar(Request $request, $proUid = null)
  336. {
  337. $pros = Pro::all();
  338. if($this->pro && $this->pro->pro_type != 'ADMIN'){
  339. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  340. $accessibleProIds = [];
  341. foreach($accessiblePros as $accessiblePro){
  342. $accessibleProIds[] = $accessiblePro->id;
  343. }
  344. $accessibleProIds[] = $this->pro->id;
  345. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  346. }
  347. return view('app.practice-management.calendar', compact('pros'));
  348. }
  349. public function cellularDeviceManager(Request $request, $proUid = null)
  350. {
  351. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  352. $performerPro = $this->performer->pro;
  353. $targetPro = null;
  354. $allPros = [];
  355. $expectedForHcp = null;
  356. if ($performerPro->pro_type == 'ADMIN') {
  357. $allPros = Pro::all();
  358. $targetPro = Pro::where('uid', $proUid)->first();
  359. } else {
  360. $targetPro = $performerPro;
  361. }
  362. $clients = [];
  363. if ($targetPro) {
  364. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  365. } else {
  366. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  367. }
  368. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  369. }
  370. public function treatmentServiceUtil(Request $request){
  371. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  372. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  373. $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"));
  374. return view('app.practice-management.treatment-services-util', compact(
  375. 'view_treatment_service_utilization_org',
  376. 'view_treatment_service_utilization',
  377. 'view_treatment_service_utilization_by_patient'));
  378. }
  379. public function hcpBillMatrix(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. $rows = [];
  393. if ($targetPro) {
  394. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  395. } else {
  396. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  397. }
  398. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  399. }
  400. public function billingManager(Request $request, $proUid = null)
  401. {
  402. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  403. $performerPro = $this->performer->pro;
  404. $targetPro = null;
  405. $allPros = [];
  406. $expectedForHcp = null;
  407. if ($performerPro->pro_type == 'ADMIN') {
  408. $allPros = Pro::all();
  409. $targetPro = Pro::where('uid', $proUid)->first();
  410. } else {
  411. $targetPro = $performerPro;
  412. }
  413. $notes = [];
  414. if ($targetPro) {
  415. $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;
  416. $notes = Note::where('hcp_pro_id', $targetPro->id)->orderBy('effective_dateest', 'desc')->paginate();
  417. } else {
  418. $notes = Note::orderBy('effective_dateest', 'desc')->paginate();
  419. }
  420. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  421. }
  422. public function claims(Request $request)
  423. {
  424. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->paginate();
  425. return view('app.practice-management.claims', compact('claims'));
  426. }
  427. // Generate PDF
  428. public function downloadClaims()
  429. {
  430. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  431. view()->share('claims', $claims);
  432. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  433. return $pdf->download('pdf_file.pdf');
  434. }
  435. public function tickets(Request $request, $proUid = null)
  436. {
  437. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  438. return view('app.practice-management.tickets', compact('tickets'));
  439. }
  440. public function cellularMeasurements(Request $request){
  441. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  442. return view('app.practice-management.cellular-measurements', compact('measurements'));
  443. }
  444. }