PracticeManagementController.php 15 KB

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