PracticeManagementController.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\ClaimEDI;
  6. use App\Models\Measurement;
  7. use App\Models\Bill;
  8. use App\Models\Claim;
  9. use App\Models\Client;
  10. use App\Models\McpRequest;
  11. use App\Models\Note;
  12. use App\Models\Pack;
  13. use App\Models\Pro;
  14. use App\Models\Product;
  15. use App\Models\ProFavorite;
  16. use App\Models\ProGeneralAvailability;
  17. use App\Models\ProProAccess;
  18. use App\Models\ProRate;
  19. use App\Models\ProSpecificAvailability;
  20. use App\Models\ProSpecificUnavailability;
  21. use App\Models\ProTextShortcut;
  22. use App\Models\ProTransaction;
  23. use App\Models\Shipment;
  24. use App\Models\SupplyOrder;
  25. use App\Models\Ticket;
  26. use Illuminate\Support\Facades\DB;
  27. use Illuminate\Support\Facades\Http;
  28. use PDF;
  29. use DateTime;
  30. use DateTimeZone;
  31. use Illuminate\Http\Request;
  32. class PracticeManagementController extends Controller
  33. {
  34. public function billingReport(Request $request)
  35. {
  36. $rows = BillingReport::paginate(50);
  37. return view('app.practice-management.billing-report', compact('rows'));
  38. }
  39. public function dashboard(Request $request)
  40. {
  41. return view('app.practice-management.dashboard');
  42. }
  43. public function rates(Request $request, $selectedProUid = 'all')
  44. {
  45. $proUid = $selectedProUid ? $selectedProUid : 'all';
  46. $rates = ProRate::where('is_active', true);
  47. if ($proUid !== 'all') {
  48. $selectedPro = Pro::where('uid', $proUid)->first();
  49. $rates = $rates->where('pro_id', $selectedPro->id);
  50. }
  51. $rates = $rates->orderBy('pro_id', 'asc')->get();
  52. $pros = $this->pros;
  53. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  54. }
  55. public function previousBills(Request $request)
  56. {
  57. return view('app.practice-management.previous-bills');
  58. }
  59. public function financialTransactions(Request $request)
  60. {
  61. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  62. return view('app.practice-management.financial-transactions', compact('transactions'));
  63. }
  64. public function pendingBillsToSign(Request $request)
  65. {
  66. return view('app.practice-management.pending-bills-to-sign');
  67. }
  68. public function HR(Request $request)
  69. {
  70. return view('app.practice-management.hr');
  71. }
  72. public function directDepositSettings(Request $request)
  73. {
  74. return view('app.practice-management.direct-deposit-settings');
  75. }
  76. public function w9(Request $request)
  77. {
  78. return view('app.practice-management.w9');
  79. }
  80. public function contract(Request $request)
  81. {
  82. return view('app.practice-management.contract');
  83. }
  84. public function notes(Request $request, $filter = '')
  85. {
  86. $proID = $this->performer()->pro->id;
  87. $query = Note::where('hcp_pro_id', $proID);
  88. switch ($filter) {
  89. case 'not-yet-signed':
  90. $query = $query->where('is_signed_by_hcp', false);
  91. break;
  92. // more cases can be added as needed
  93. default:
  94. break;
  95. }
  96. $notes = $query->orderBy('created_at', 'desc')->get();
  97. return view('app.practice-management.notes', compact('notes', 'filter'));
  98. }
  99. public function bills(Request $request, $filter = '')
  100. {
  101. $proID = $this->performer()->pro->id;
  102. $query = Bill::where('is_cancelled', false);
  103. switch ($filter) {
  104. case 'not-yet-signed':
  105. $query = $query
  106. ->where(function ($q) use ($proID) {
  107. $q->where(function ($q2) use ($proID) {
  108. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  109. })
  110. ->orWhere(function ($q2) use ($proID) {
  111. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  112. })
  113. ->orWhere(function ($q2) use ($proID) {
  114. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  115. })
  116. ->orWhere(function ($q2) use ($proID) {
  117. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  118. });
  119. });
  120. break;
  121. case 'previous':
  122. $query = $query
  123. ->where(function ($q) use ($proID) {
  124. $q->where(function ($q2) use ($proID) {
  125. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  126. })
  127. ->orWhere(function ($q2) use ($proID) {
  128. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  129. })
  130. ->orWhere(function ($q2) use ($proID) {
  131. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  132. })
  133. ->orWhere(function ($q2) use ($proID) {
  134. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  135. });
  136. });
  137. break;
  138. // more cases can be added as needed
  139. default:
  140. break;
  141. }
  142. $bills = $query->orderBy('created_at', 'desc')->get();
  143. return view('app.practice-management.bills', compact('bills', 'filter'));
  144. }
  145. public function unacknowledgedCancelledBills(Request $request)
  146. {
  147. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  148. ->where('is_cancelled', true)
  149. ->where('is_cancellation_acknowledged', false)
  150. ->orderBy('created_at', 'desc')
  151. ->get();
  152. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  153. }
  154. public function myTickets(Request $request, $filter = 'open')
  155. {
  156. $performer = $this->performer();
  157. $myTickets = Ticket::where(function ($q) use ($performer) {
  158. $q->where('assigned_pro_id', $performer->pro_id)
  159. ->orWhere('manager_pro_id', $performer->pro_id)
  160. ->orWhere('ordering_pro_id', $performer->pro_id)
  161. ->orWhere('initiating_pro_id', $performer->pro_id);
  162. });
  163. if ($filter === 'open') {
  164. $myTickets = $myTickets->where('is_open', true);
  165. } else if ($filter === 'closed') {
  166. $myTickets = $myTickets->where('is_open', false);
  167. }
  168. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  169. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  170. }
  171. public function myTextShortcuts(Request $request)
  172. {
  173. $performer = $this->performer();
  174. $myTextShortcuts = ProTextShortcut::where('pro_id', $performer->pro_id)->where('is_removed', false)->get();
  175. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  176. }
  177. public function myFavorites(Request $request, $filter = 'all')
  178. {
  179. $performer = $this->performer();
  180. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  181. ->where('is_removed', false);
  182. if ($filter !== 'all') {
  183. $myFavorites = $myFavorites->where('category', $filter);
  184. }
  185. $myFavorites = $myFavorites
  186. ->orderBy('category', 'asc')
  187. ->orderBy('position_index', 'asc')
  188. ->get();
  189. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  190. }
  191. public function proAvailability(Request $request, $proUid = null)
  192. {
  193. $performer = $this->performer();
  194. $pro = $performer->pro;
  195. if ($proUid) {
  196. $pro = Pro::where('uid', $proUid)->first();
  197. }
  198. if ($request->get('pro_uid')) {
  199. $proUid = $request->get('pro_uid');
  200. $pro = Pro::where('uid', $proUid)->first();
  201. }
  202. $selectedProUid = $pro->uid;
  203. $pros = $this->pros;
  204. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  205. $generalAvailabilities = [
  206. 'MONDAY' => [],
  207. 'TUESDAY' => [],
  208. 'WEDNESDAY' => [],
  209. 'THURSDAY' => [],
  210. 'FRIDAY' => [],
  211. 'SATURDAY' => [],
  212. 'SUNDAY' => [],
  213. ];
  214. foreach ($generalAvailabilitiesList as $ga) {
  215. if ($ga->day_of_week == 'MONDAY') {
  216. $generalAvailabilities['MONDAY'][] = $ga;
  217. }
  218. if ($ga->day_of_week == 'TUESDAY') {
  219. $generalAvailabilities['TUESDAY'][] = $ga;
  220. }
  221. if ($ga->day_of_week == 'WEDNESDAY') {
  222. $generalAvailabilities['WEDNESDAY'][] = $ga;
  223. }
  224. if ($ga->day_of_week == 'THURSDAY') {
  225. $generalAvailabilities['THURSDAY'][] = $ga;
  226. }
  227. if ($ga->day_of_week == 'FRIDAY') {
  228. $generalAvailabilities['FRIDAY'][] = $ga;
  229. }
  230. if ($ga->day_of_week == 'SATURDAY') {
  231. $generalAvailabilities['SATURDAY'][] = $ga;
  232. }
  233. if ($ga->day_of_week == 'SUNDAY') {
  234. $generalAvailabilities['SUNDAY'][] = $ga;
  235. }
  236. }
  237. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  238. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  239. //events for the calendar
  240. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  241. $endDateTime = new DateTime($startDate);
  242. $endDateTime->modify('+6 day');
  243. $endDate = $endDateTime->format("Y-m-d");
  244. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  245. $events = json_encode($eventsData);
  246. return view(
  247. 'app.practice-management.pro-availability',
  248. compact(
  249. 'pros',
  250. 'generalAvailabilities',
  251. 'specificAvailabilities',
  252. 'specificUnavailabilities',
  253. 'events',
  254. 'selectedProUid'
  255. )
  256. );
  257. }
  258. public function loadAvailability(Request $request, $proUid)
  259. {
  260. $performer = $this->performer();
  261. $pro = $performer->pro;
  262. $startDate = $request->get('start');
  263. $endDate = $request->get('end');
  264. $selectedPro = Pro::where('uid', $proUid)->first();
  265. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  266. }
  267. public function proAvailabilityFilter(Request $request)
  268. {
  269. $proUid = $request->get('proUid');
  270. return ['success' => true, 'data' => $proUid];
  271. }
  272. // video call page (RHS)
  273. // generic call handle (no uid)
  274. // specific call handle (uid of client)
  275. public function meet(Request $request, $uid = false)
  276. {
  277. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  278. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  279. if (!empty($client)) {
  280. return view('app.video.call-minimal', compact('session', 'client'));
  281. }
  282. return view('app.video.call-agora-v2', compact('session', 'client'));
  283. }
  284. // check video page
  285. public function checkVideo(Request $request, $uid)
  286. {
  287. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  288. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  289. $publish = false;
  290. return view('app.video.check-video-minimal', compact('session', 'client'));
  291. }
  292. public function getParticipantInfo(Request $request)
  293. {
  294. $sid = intval($request->get('uid')) - 1000000;
  295. $session = AppSession::where('id', $sid)->first();
  296. $result = [
  297. "type" => '',
  298. "name" => ''
  299. ];
  300. if ($session) {
  301. $result["type"] = $session->session_type;
  302. switch ($session->session_type) {
  303. case 'PRO':
  304. $pro = Pro::where('id', $session->pro_id)->first();
  305. $result["name"] = $pro->displayName();
  306. break;
  307. case 'CLIENT':
  308. $client = Client::where('id', $session->client_id)->first();
  309. $result["name"] = $client->displayName();
  310. break;
  311. }
  312. }
  313. return json_encode($result);
  314. }
  315. // ajax ep used by the video page
  316. // this is needed bcoz meet() is used not
  317. // just for the client passed to the view
  318. public function getOpentokSessionKey(Request $request, $uid)
  319. {
  320. $client = Client::where('uid', $uid)->first();
  321. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  322. }
  323. // poll to check if there are patients with active mcp requests
  324. public function getPatientsInQueue(Request $request)
  325. {
  326. $myInitiatives = $this->performer->pro->initiatives;
  327. if ($myInitiatives) {
  328. $myInitiatives = strtoupper($myInitiatives);
  329. }
  330. $myInitiativesList = explode('|', $myInitiatives);
  331. $myForeignLanguages = $this->performer->pro->foreign_languages;
  332. if ($myForeignLanguages) {
  333. $myForeignLanguages = strtoupper($myForeignLanguages);
  334. }
  335. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  336. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  337. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  338. })
  339. ->where(function ($query) use ($myForeignLanguagesList) {
  340. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  341. })->limit(3)->get();
  342. $results = [];
  343. foreach ($clients as $client) {
  344. $results[] = [
  345. 'clientUid' => $client->uid,
  346. 'name' => $client->displayName(),
  347. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  348. ];
  349. }
  350. return json_encode($results);
  351. }
  352. public function currentWork(Request $request)
  353. {
  354. return view('app/current-work');
  355. }
  356. public function calendar(Request $request, $proUid = null)
  357. {
  358. $pros = Pro::all();
  359. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  360. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  361. $accessibleProIds = [];
  362. foreach ($accessiblePros as $accessiblePro) {
  363. $accessibleProIds[] = $accessiblePro->id;
  364. }
  365. $accessibleProIds[] = $this->pro->id;
  366. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  367. }
  368. return view('app.practice-management.calendar', compact('pros'));
  369. }
  370. public function cellularDeviceManager(Request $request, $proUid = null)
  371. {
  372. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  373. $performerPro = $this->performer->pro;
  374. $targetPro = null;
  375. $allPros = [];
  376. $expectedForHcp = null;
  377. if ($performerPro->pro_type == 'ADMIN') {
  378. $allPros = Pro::all();
  379. $targetPro = Pro::where('uid', $proUid)->first();
  380. } else {
  381. $targetPro = $performerPro;
  382. }
  383. $clients = [];
  384. if ($targetPro) {
  385. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  386. } else {
  387. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  388. }
  389. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  390. }
  391. public function treatmentServiceUtil(Request $request)
  392. {
  393. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  394. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  395. $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"));
  396. return view('app.practice-management.treatment-services-util', compact(
  397. 'view_treatment_service_utilization_org',
  398. 'view_treatment_service_utilization',
  399. 'view_treatment_service_utilization_by_patient'));
  400. }
  401. public function processingBillMatrix(Request $request, $proUid = null)
  402. {
  403. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  404. $performerPro = $this->performer->pro;
  405. $targetPro = null;
  406. $allPros = [];
  407. if ($performerPro->pro_type == 'ADMIN') {
  408. $allPros = Pro::all();
  409. $targetPro = Pro::where('uid', $proUid)->first();
  410. } else {
  411. $targetPro = $performerPro;
  412. }
  413. $bills = [];
  414. if ($targetPro) {
  415. $bills = Bill::where('hcp_pro_id', $targetPro->id)->
  416. where('has_hcp_been_paid', false)->
  417. where('is_cancelled', false)->
  418. where('is_signed_by_hcp', true)->
  419. orderBy('effective_date', 'desc')->paginate();
  420. } else {
  421. $bills = Bill::where('has_hcp_been_paid', false)->
  422. where('is_cancelled', false)->
  423. where('is_signed_by_hcp', true)->
  424. orderBy('effective_date', 'desc')->
  425. paginate();
  426. }
  427. $viewData = [
  428. 'bills' => $bills,
  429. 'allPros' => $allPros,
  430. 'targetPro' => $targetPro,
  431. 'performerPro' => $performerPro,
  432. 'proUid' => $proUid
  433. ];
  434. return view('app.practice-management.processing-bill-matrix', $viewData);
  435. }
  436. public function hcpBillMatrix(Request $request, $proUid = null)
  437. {
  438. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  439. $performerPro = $this->performer->pro;
  440. $targetPro = null;
  441. $allPros = [];
  442. $expectedForHcp = null;
  443. if ($performerPro->pro_type == 'ADMIN') {
  444. $allPros = Pro::all();
  445. $targetPro = Pro::where('uid', $proUid)->first();
  446. } else {
  447. $targetPro = $performerPro;
  448. }
  449. $rows = [];
  450. if ($targetPro) {
  451. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  452. } else {
  453. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  454. }
  455. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  456. }
  457. public function billingManager(Request $request, $proUid = null)
  458. {
  459. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  460. $performerPro = $this->performer->pro;
  461. $targetPro = null;
  462. $allPros = [];
  463. $expectedForHcp = null;
  464. if ($performerPro->pro_type == 'ADMIN') {
  465. $allPros = Pro::all();
  466. $targetPro = Pro::where('uid', $proUid)->first();
  467. } else {
  468. $targetPro = $performerPro;
  469. }
  470. $notes = [];
  471. if ($targetPro) {
  472. $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;
  473. $notes = Note::where('hcp_pro_id', $targetPro->id);
  474. } else {
  475. $notes = Note::where('id', '>', 0);
  476. }
  477. if($request->input('date')) {
  478. $notes = $notes->where('effective_dateest', $request->input('date'));
  479. }
  480. $filters = [];
  481. $filters['bills_created'] = $request->input('bills_created');
  482. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  483. $filters['bills_resolved'] = $request->input('bills_resolved');
  484. $filters['bills_closed'] = $request->input('bills_closed');
  485. $filters['claims_created'] = $request->input('claims_created');
  486. $filters['claims_closed'] = $request->input('claims_closed');
  487. if ($filters['bills_created']) {
  488. $notes->where(
  489. 'bill_total_expected',
  490. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  491. 0);
  492. }
  493. if ($filters['is_billing_marked_done']) {
  494. $notes->where(
  495. 'is_billing_marked_done',
  496. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  497. true);
  498. }
  499. if ($filters['bills_resolved']) {
  500. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  501. if ($filters['bills_resolved'] === 'yes') {
  502. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND (is_cancelled = false AND is_verified = false) OR (is_cancelled = TRUE AND is_cancellation_acknowledged = FALSE)) > 0');
  503. } elseif ($filters['bills_resolved'] === 'no') {
  504. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND ((is_cancelled = true AND is_cancellation_acknowledged = true) OR is_verified = true)) = 0');
  505. }
  506. }
  507. if ($filters['bills_closed']) {
  508. $notes->where(
  509. 'is_bill_closed',
  510. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  511. true);
  512. }
  513. if ($filters['claims_created']) {
  514. $notes->where(
  515. 'claim_total_expected',
  516. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  517. 0);
  518. }
  519. if ($filters['claims_closed']) {
  520. $notes->where(
  521. 'is_claim_closed',
  522. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  523. true);
  524. }
  525. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  526. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  527. }
  528. public function billMatrix(Request $request)
  529. {
  530. $bClients = [];
  531. $bHCPPros = [];
  532. $bNAPros = [];
  533. $filters = [];
  534. $filters['client'] = $request->input('client');
  535. $filters['service'] = $request->input('service');
  536. $filters['hcp'] = $request->input('hcp');
  537. $filters['hcp_paid'] = $request->input('hcp_paid');
  538. $filters['expected_op'] = $request->input('expected_op');
  539. $filters['expected_value'] = $request->input('expected_value');
  540. $filters['paid_op'] = $request->input('paid_op');
  541. $filters['paid_value'] = $request->input('paid_value');
  542. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  543. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  544. $filters['hcp_sign'] = $request->input('hcp_sign');
  545. $filters['verified'] = $request->input('verified');
  546. $filters['cancelled'] = $request->input('cancelled');
  547. $bills = Bill::orderBy('effective_date')->paginate();
  548. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  549. }
  550. public function medicarePartBClaims(Request $request)
  551. {
  552. $medicarePartBOnly = $request->get("medicare_part_b");
  553. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  554. //Only medicare claims
  555. $claims = [];
  556. foreach ($allClaims as $claim) {
  557. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  558. $claims[] = $claim;
  559. }
  560. }
  561. $claimEDIs = ClaimEDI::all();
  562. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  563. }
  564. // Generate PDF
  565. public function downloadClaims()
  566. {
  567. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  568. view()->share('claims', $claims);
  569. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  570. return $pdf->download('pdf_file.pdf');
  571. }
  572. public function tickets(Request $request, $proUid = null)
  573. {
  574. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  575. return view('app.practice-management.tickets', compact('tickets'));
  576. }
  577. public function supplyOrders(Request $request)
  578. {
  579. // counts
  580. $counts = $this->getSupplyOrderCounts();
  581. // so clients
  582. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  583. $soClientIDs = array_map(function ($_x) {
  584. return $_x->client_id;
  585. }, $soClientIDs);
  586. $soClients = Client::whereIn('id', $soClientIDs)->get();
  587. // so products
  588. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  589. $soProductIDs = array_map(function ($_x) {
  590. return $_x->product_id;
  591. }, $soProductIDs);
  592. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  593. $filters = [];
  594. $filters['client'] = $request->input('client');
  595. $filters['product'] = $request->input('product');
  596. $filters['reason'] = $request->input('reason');
  597. $filters['cu_memo'] = $request->input('cu_memo');
  598. $filters['pro_sign'] = $request->input('pro_sign');
  599. $filters['client_sign'] = $request->input('client_sign');
  600. $filters['shipment'] = $request->input('shipment');
  601. $filters['lot_number'] = $request->input('lot_number');
  602. $filters['imei'] = $request->input('imei');
  603. $filters['cancelled'] = $request->input('cancelled');
  604. $supplyOrders = SupplyOrder::where('id', '>', 0);
  605. // apply filters
  606. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  607. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  608. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  609. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  610. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  611. if ($filters['client_sign']) {
  612. if ($filters['client_sign'] === 'signed')
  613. $supplyOrders->where('is_signed_by_client', true);
  614. elseif ($filters['client_sign'] === 'waived')
  615. $supplyOrders->where('is_client_signature_waived', true);
  616. else
  617. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  618. }
  619. if ($filters['shipment']) {
  620. if ($filters['shipment'] === 'not_cleared_for_shipment')
  621. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  622. elseif ($filters['shipment'] === 'cleared_for_shipment')
  623. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  624. else
  625. $supplyOrders
  626. ->whereNotNull('shipment_id')
  627. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  628. }
  629. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  630. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  631. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  632. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  633. return view('app.practice-management.supply-orders',
  634. compact('supplyOrders', 'filters',
  635. 'soClients', 'soProducts', 'counts'
  636. )
  637. );
  638. }
  639. public function shipments(Request $request, $filter = null)
  640. {
  641. // counts
  642. $counts = $this->getShipmentCounts();
  643. // so clients
  644. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  645. $shClientIDs = array_map(function ($_x) {
  646. return $_x->client_id;
  647. }, $shClientIDs);
  648. $shClients = Client::whereIn('id', $shClientIDs)->get();
  649. $shipments = Shipment::where('id', '>', 0);
  650. $filters = [];
  651. $filters['client'] = $request->input('client');
  652. $filters['courier'] = $request->input('courier');
  653. $filters['tracking_num'] = $request->input('tracking_num');
  654. $filters['label'] = $request->input('label');
  655. $filters['status'] = $request->input('status');
  656. $filters['cancelled'] = $request->input('cancelled');
  657. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  658. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  659. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  660. if ($filters['label']) {
  661. if ($filters['label'] === 'yes')
  662. $shipments->whereNotNull('label_system_file_id');
  663. else
  664. $shipments->whereNull('label_system_file_id');
  665. }
  666. if ($filters['status']) $shipments->where('status', $filters['status']);
  667. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  668. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  669. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  670. }
  671. public function cellularMeasurements(Request $request)
  672. {
  673. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  674. return view('app.practice-management.cellular-measurements', compact('measurements'));
  675. }
  676. // v2 supply-orders & shipments management (wh)
  677. public function supplyOrdersReadyToShip(Request $request)
  678. {
  679. $counts = $this->getSupplyOrderCounts();
  680. $supplyOrders = SupplyOrder
  681. ::where('is_cleared_for_shipment', true)
  682. ->where('is_cancelled', false)
  683. ->whereNull('shipment_id')
  684. ->join('client', 'client.id', '=', 'supply_order.client_id')
  685. ->orderBy('client.name_last', 'ASC')
  686. ->orderBy('client.name_first', 'ASC')
  687. ->orderBy('supply_order.client_id', 'ASC')
  688. ->orderBy('supply_order.mailing_address_full', 'ASC')
  689. ->orderBy('supply_order.created_at', 'ASC')
  690. ->select('supply_order.*')
  691. ->paginate();
  692. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  693. }
  694. public function supplyOrdersShipmentUnderway(Request $request)
  695. {
  696. $counts = $this->getSupplyOrderCounts();
  697. $supplyOrders = SupplyOrder
  698. ::where('is_cancelled', false)
  699. ->whereNotNull('shipment_id')
  700. ->orderBy('client_id', 'ASC')
  701. ->orderBy('mailing_address_full', 'ASC')
  702. ->orderBy('created_at', 'ASC')
  703. ->paginate();
  704. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  705. }
  706. public function supplyOrdersHanging(Request $request)
  707. {
  708. $counts = $this->getSupplyOrderCounts();
  709. $supplyOrders = SupplyOrder
  710. ::select('supply_order.*')
  711. ->leftJoin('shipment', function($join) {
  712. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  713. })
  714. ->where('shipment.status', 'CANCELLED')
  715. ->where('supply_order.is_cancelled', false)
  716. ->orderBy('supply_order.client_id', 'ASC')
  717. ->orderBy('supply_order.mailing_address_full', 'ASC')
  718. ->orderBy('supply_order.created_at', 'ASC')
  719. ->paginate();
  720. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  721. }
  722. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  723. {
  724. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  725. ->where('is_cancelled', true)
  726. ->where('is_cancellation_acknowledged', false)
  727. ->paginate();
  728. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  729. }
  730. private function getSupplyOrderCounts()
  731. {
  732. return [
  733. "supplyOrders" => SupplyOrder::count(),
  734. "supplyOrdersReadyToShip" => SupplyOrder
  735. ::where('is_cleared_for_shipment', true)
  736. ->where('is_cancelled', false)
  737. ->whereNull('shipment_id')->count(),
  738. "supplyOrdersShipmentUnderway" => SupplyOrder
  739. ::where('is_cancelled', false)
  740. ->whereNotNull('shipment_id')->count(),
  741. "supplyOrdersHanging" => SupplyOrder
  742. ::leftJoin('shipment', function($join) {
  743. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  744. })
  745. ->where('shipment.status', 'CANCELLED')
  746. ->where('supply_order.is_cancelled', false)
  747. ->count(),
  748. ];
  749. }
  750. public function shipmentsReadyToPrint(Request $request)
  751. {
  752. $counts = $this->getShipmentCounts();
  753. $shipments = Shipment
  754. ::where('is_cancelled', false)
  755. ->where('status', 'CREATED')
  756. ->orderBy('created_at', 'ASC')
  757. ->paginate();
  758. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  759. }
  760. public function shipmentsShipmentUnderway(Request $request)
  761. {
  762. $counts = $this->getShipmentCounts();
  763. $shipments = Shipment
  764. ::where('is_cancelled', false)
  765. ->where('status', 'PRINTED')
  766. ->orderBy('created_at', 'ASC')
  767. ->paginate();
  768. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  769. }
  770. private function getShipmentCounts()
  771. {
  772. return [
  773. "shipments" => Shipment::count(),
  774. "shipmentsReadyToPrint" => Shipment
  775. ::where('is_cancelled', false)
  776. ->where('status', 'CREATED')
  777. ->count(),
  778. "shipmentsWaitingForPicker" => Shipment
  779. ::where('is_cancelled', false)
  780. ->where('status', 'PRINTED')
  781. ->count()
  782. ];
  783. }
  784. public function shipment(Request $request, Shipment $shipment)
  785. {
  786. return view('app.practice-management.shipment', compact('shipment'));
  787. }
  788. public function shipmentsMultiPrint(Request $request, $ids)
  789. {
  790. $ids = array_map(function ($_x) {
  791. return intval($_x);
  792. }, explode("|", $ids));
  793. $shipments = Shipment::whereIn('id', $ids)->get();
  794. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  795. }
  796. public function patientClaimSummary(Request $request, $proUid = null)
  797. {
  798. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  799. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  800. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  801. $notes3rdPartyTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  802. $notes3rdPartyTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_bill_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  803. $notes3rdPartyTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_claim_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  804. $patientsTotal = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  805. $patientsTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  806. $patientsTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  807. $performerPro = $this->performer->pro;
  808. $allPros = [];
  809. if ($performerPro->pro_type == 'ADMIN') {
  810. $allPros = Pro::all();
  811. } else {
  812. $allPros = [$performerPro];
  813. }
  814. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  815. $patientsQuery = Client::where('is_dummy', '=', false)
  816. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  817. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  818. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  819. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  820. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  821. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  822. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  823. if ($proUid) {
  824. $mcpPro = Pro::where('uid', $proUid)->first();
  825. if ($mcpPro) {
  826. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  827. }
  828. }
  829. $patients = $patientsQuery->paginate(50);
  830. $data = [
  831. 'patients' => $patients,
  832. 'proUid' => $proUid,
  833. 'allPros' => $allPros,
  834. 'notesTotal' => $notesTotal,
  835. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  836. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  837. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  838. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  839. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  840. 'patientsTotal' => $patientsTotal,
  841. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  842. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  843. ];
  844. return view('app.practice-management.patient-claim-summary', $data);
  845. }
  846. public function packsMultiPrint(Request $request) {
  847. $packs = Pack
  848. ::select('pack.*')
  849. ->leftJoin('shipment', function($join) {
  850. $join->on('pack.shipment_id', '=', 'shipment.id');
  851. })
  852. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  853. ->where(function ($query) {
  854. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  855. })
  856. ->whereNotNull('pack.label_system_file_id')
  857. ->orderBy('pack.created_at', 'ASC')
  858. ->get();
  859. return view('app.practice-management.packs-multi-print', compact('packs'));
  860. }
  861. public function packsMultiPDF(Request $request, $ids) {
  862. $ids = array_map(function ($_x) {
  863. return intval($_x);
  864. }, explode("|", $ids));
  865. $packs = Pack::whereIn('id', $ids)->get();
  866. }
  867. private function callJava($request, $endPoint, $data)
  868. {
  869. $url = config('stag.backendUrl') . $endPoint;
  870. $response = Http::asForm()
  871. ->withHeaders([
  872. 'sessionKey' => $request->cookie('sessionKey')
  873. ])
  874. ->post($url, $data)
  875. ->body();
  876. dd($response);
  877. return $response;
  878. }
  879. }