PracticeManagementController.php 14 KB

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