PracticeManagementController.php 16 KB

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