PracticeManagementController.php 47 KB

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