PracticeManagementController.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\CareMonth;
  6. use App\Models\ClaimEDI;
  7. use App\Models\Company;
  8. use App\Models\Handout;
  9. use App\Models\MBClaim;
  10. use App\Models\Measurement;
  11. use App\Models\Bill;
  12. use App\Models\Claim;
  13. use App\Models\Client;
  14. use App\Models\McpRequest;
  15. use App\Models\Note;
  16. use App\Models\Pack;
  17. use App\Models\Pro;
  18. use App\Models\Product;
  19. use App\Models\ProFavorite;
  20. use App\Models\ProGeneralAvailability;
  21. use App\Models\ProProAccess;
  22. use App\Models\ProRate;
  23. use App\Models\ProSpecificAvailability;
  24. use App\Models\ProSpecificUnavailability;
  25. use App\Models\ProTextShortcut;
  26. use App\Models\ProTransaction;
  27. use App\Models\Shipment;
  28. use App\Models\SupplyOrder;
  29. use App\Models\Ticket;
  30. use App\Models\ClientMeasurementDaysPerMonth;
  31. use Illuminate\Support\Facades\DB;
  32. use Illuminate\Support\Facades\Http;
  33. use PDF;
  34. use DateTime;
  35. use DateTimeZone;
  36. use Illuminate\Http\Request;
  37. class PracticeManagementController extends Controller
  38. {
  39. public function remoteMonitoringReport(Request $request)
  40. {
  41. $rows = null;
  42. $proID = $this->performer()->pro->id;
  43. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  44. $rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  45. return view ('app.practice-management.remote-monitoring-report', compact('rows'));
  46. }
  47. public function billingReport(Request $request)
  48. {
  49. $rows = BillingReport::paginate(50);
  50. return view('app.practice-management.billing-report', compact('rows'));
  51. }
  52. public function dashboard(Request $request)
  53. {
  54. return view('app.practice-management.dashboard');
  55. }
  56. public function rates(Request $request, $selectedProUid = 'all')
  57. {
  58. $proUid = $selectedProUid ? $selectedProUid : 'all';
  59. $rates = ProRate::where('is_active', true);
  60. if ($proUid !== 'all') {
  61. $selectedPro = Pro::where('uid', $proUid)->first();
  62. $rates = $rates->where('pro_id', $selectedPro->id);
  63. }
  64. $rates = $rates->orderBy('pro_id', 'asc')->get();
  65. $pros = $this->pros;
  66. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  67. }
  68. public function previousBills(Request $request)
  69. {
  70. return view('app.practice-management.previous-bills');
  71. }
  72. public function financialTransactions(Request $request)
  73. {
  74. $pro = $this->performer()->pro;
  75. $transactions = null;
  76. if($pro->pro_type === 'ADMIN') {
  77. $transactions = ProTransaction::whereNotNull('id');
  78. }
  79. else {
  80. $transactions = ProTransaction::where('pro_id', $pro->id);
  81. }
  82. $filter = $request->input('p');
  83. if ($filter) {
  84. $filterPro = Pro::where('uid', $filter)->first();
  85. if($filterPro) {
  86. $transactions = $transactions->where('pro_id', '=', $filterPro->id);
  87. }
  88. }
  89. $filter = $request->input('t');
  90. if ($filter) {
  91. $transactions = $transactions->where('plus_or_minus', '=', $filter);
  92. }
  93. $filter = $request->input('c');
  94. if ($filter) {
  95. $transactions = $transactions->where('company_id', '=', $filter);
  96. }
  97. $filter = $request->input('bs');
  98. if ($filter) {
  99. $transactions = $transactions->where('created_at', '>=', $filter);
  100. }
  101. $filter = $request->input('be');
  102. if ($filter) {
  103. $transactions = $transactions->where('created_at', '<=', $filter);
  104. }
  105. $transactions = $transactions->orderBy('created_at', 'desc')->paginate();
  106. $companies = Company::where('is_active', true)->orderBy('name')->get();
  107. return view('app.practice-management.financial-transactions', compact('transactions', 'companies'));
  108. }
  109. public function pendingBillsToSign(Request $request)
  110. {
  111. return view('app.practice-management.pending-bills-to-sign');
  112. }
  113. public function HR(Request $request)
  114. {
  115. return view('app.practice-management.hr');
  116. }
  117. public function directDepositSettings(Request $request)
  118. {
  119. return view('app.practice-management.direct-deposit-settings');
  120. }
  121. public function w9(Request $request)
  122. {
  123. return view('app.practice-management.w9');
  124. }
  125. public function contract(Request $request)
  126. {
  127. return view('app.practice-management.contract');
  128. }
  129. public function notes(Request $request, $filter = '')
  130. {
  131. $proID = $this->performer()->pro->id;
  132. $query = Note::where('hcp_pro_id', $proID);
  133. switch ($filter) {
  134. case 'not-yet-signed':
  135. $query = $query->where('is_signed_by_hcp', false);
  136. break;
  137. case 'not-yet-signed-but-ally-signed':
  138. $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
  139. break;
  140. case 'without-bills':
  141. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  142. break;
  143. // more cases can be added as needed
  144. default:
  145. break;
  146. }
  147. $notes = $query->orderBy('created_at', 'desc')->get();
  148. return view('app.practice-management.notes', compact('notes', 'filter'));
  149. }
  150. public function naBillableSignedNotes(Request $request)
  151. {
  152. $notes = Note
  153. ::where('is_signed_by_hcp', TRUE)
  154. ->where('ally_pro_id', $this->performer()->pro->id)
  155. ->where('is_cancelled', FALSE)
  156. ->whereRaw("
  157. (
  158. SELECT count(bill.id)
  159. FROM bill WHERE
  160. bill.is_cancelled = FALSE AND
  161. bill.generic_pro_id = {$this->performer()->pro->id} AND
  162. bill.note_id = note.id
  163. ) = 0
  164. ");
  165. $notes = $notes->orderBy('created_at', 'desc')->get();
  166. return view('app.practice-management.na-billable-signed-notes', compact('notes'));
  167. }
  168. public function bills(Request $request, $filter = '')
  169. {
  170. $proID = $this->performer()->pro->id;
  171. $query = Bill::where('is_cancelled', false)->where('bill_service_type', '<>', 'CARE_MONTH');
  172. switch ($filter) {
  173. case 'not-yet-signed':
  174. $query = $query
  175. ->where(function ($q) use ($proID) {
  176. $q->where(function ($q2) use ($proID) {
  177. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  178. })
  179. ->orWhere(function ($q2) use ($proID) {
  180. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  181. })
  182. ->orWhere(function ($q2) use ($proID) {
  183. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  184. })
  185. ->orWhere(function ($q2) use ($proID) {
  186. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  187. });
  188. });
  189. break;
  190. case 'previous':
  191. $query = $query
  192. ->where(function ($q) use ($proID) {
  193. $q->where(function ($q2) use ($proID) {
  194. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  195. })
  196. ->orWhere(function ($q2) use ($proID) {
  197. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  198. })
  199. ->orWhere(function ($q2) use ($proID) {
  200. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  201. })
  202. ->orWhere(function ($q2) use ($proID) {
  203. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  204. });
  205. });
  206. break;
  207. // more cases can be added as needed
  208. default:
  209. $query = $query
  210. ->where(function ($q) use ($proID) {
  211. $q->where(function ($q2) use ($proID) {
  212. $q2->where('hcp_pro_id', $proID);
  213. })
  214. ->orWhere(function ($q2) use ($proID) {
  215. $q2->where('cm_pro_id', $proID);
  216. })
  217. ->orWhere(function ($q2) use ($proID) {
  218. $q2->where('rme_pro_id', $proID);
  219. })
  220. ->orWhere(function ($q2) use ($proID) {
  221. $q2->where('rmm_pro_id', $proID);
  222. });
  223. });
  224. break;
  225. }
  226. $bills = $query->orderBy('created_at', 'desc')->get();
  227. return view('app.practice-management.bills', compact('bills', 'filter'));
  228. }
  229. public function rmBillsToSign(Request $request)
  230. {
  231. $performerProID = $this->performer()->pro->id;
  232. $bills = Bill::where('is_cancelled', false)->where('cm_or_rm', 'RM')
  233. ->where(function ($q) use ($performerProID) {
  234. $q
  235. ->where(function ($q2) use ($performerProID) {
  236. $q2->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false);
  237. })
  238. ->orWhere(function ($q2) use ($performerProID) {
  239. $q2->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false);
  240. })
  241. ->orWhere(function ($q2) use ($performerProID) {
  242. $q2->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false);
  243. })
  244. ->orWhere(function ($q2) use ($performerProID) {
  245. $q2->where('generic_pro_id', $performerProID)->where('is_signed_by_generic_pro', false);
  246. });
  247. })
  248. ->orderBy('effective_date', 'desc')
  249. ->get();
  250. return view('app.practice-management.rm-bills-to-sign', compact('bills'));
  251. }
  252. public function unacknowledgedCancelledBills(Request $request)
  253. {
  254. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  255. ->where('is_cancelled', true)
  256. ->where('is_cancellation_acknowledged', false)
  257. ->orderBy('created_at', 'desc')
  258. ->get();
  259. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  260. }
  261. public function myTickets(Request $request, $filter = 'open')
  262. {
  263. $performer = $this->performer();
  264. $myTickets = Ticket::where(function ($q) use ($performer) {
  265. $q->where('assigned_pro_id', $performer->pro_id)
  266. ->orWhere('manager_pro_id', $performer->pro_id)
  267. ->orWhere('ordering_pro_id', $performer->pro_id)
  268. ->orWhere('initiating_pro_id', $performer->pro_id);
  269. });
  270. if ($filter === 'open') {
  271. $myTickets = $myTickets->where('is_open', true);
  272. } else if ($filter === 'closed') {
  273. $myTickets = $myTickets->where('is_open', false);
  274. }
  275. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  276. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  277. }
  278. public function myTextShortcuts(Request $request)
  279. {
  280. $myTextShortcuts = DB::table('pro_text_shortcut')
  281. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  282. ->select(
  283. 'pro_text_shortcut.uid',
  284. 'pro_text_shortcut.shortcut',
  285. 'pro_text_shortcut.text',
  286. 'pro.name_first',
  287. 'pro.name_last'
  288. )
  289. ->where('pro_text_shortcut.is_removed', false);
  290. if($this->performer()->pro->pro_type !== 'ADMIN') {
  291. $myTextShortcuts = $myTextShortcuts->where('pro_id', $this->performer()->pro_id);
  292. }
  293. $myTextShortcuts = $myTextShortcuts
  294. ->orderBy('pro.name_last')
  295. ->orderBy('pro.name_first')
  296. ->get();
  297. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  298. }
  299. public function myFavorites(Request $request, $filter = 'all')
  300. {
  301. $performer = $this->performer();
  302. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  303. ->where('is_removed', false);
  304. if ($filter !== 'all') {
  305. $myFavorites = $myFavorites->where('category', $filter);
  306. }
  307. $myFavorites = $myFavorites
  308. ->orderBy('category', 'asc')
  309. ->orderBy('position_index', 'asc')
  310. ->get();
  311. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  312. }
  313. public function proAvailability(Request $request, $proUid = null)
  314. {
  315. $performer = $this->performer();
  316. $pro = $performer->pro;
  317. if ($proUid) {
  318. $pro = Pro::where('uid', $proUid)->first();
  319. }
  320. if ($request->get('pro_uid')) {
  321. $proUid = $request->get('pro_uid');
  322. $pro = Pro::where('uid', $proUid)->first();
  323. }
  324. $selectedProUid = $pro->uid;
  325. $pros = $this->pros;
  326. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  327. $generalAvailabilities = [
  328. 'MONDAY' => [],
  329. 'TUESDAY' => [],
  330. 'WEDNESDAY' => [],
  331. 'THURSDAY' => [],
  332. 'FRIDAY' => [],
  333. 'SATURDAY' => [],
  334. 'SUNDAY' => [],
  335. ];
  336. foreach ($generalAvailabilitiesList as $ga) {
  337. if ($ga->day_of_week == 'MONDAY') {
  338. $generalAvailabilities['MONDAY'][] = $ga;
  339. }
  340. if ($ga->day_of_week == 'TUESDAY') {
  341. $generalAvailabilities['TUESDAY'][] = $ga;
  342. }
  343. if ($ga->day_of_week == 'WEDNESDAY') {
  344. $generalAvailabilities['WEDNESDAY'][] = $ga;
  345. }
  346. if ($ga->day_of_week == 'THURSDAY') {
  347. $generalAvailabilities['THURSDAY'][] = $ga;
  348. }
  349. if ($ga->day_of_week == 'FRIDAY') {
  350. $generalAvailabilities['FRIDAY'][] = $ga;
  351. }
  352. if ($ga->day_of_week == 'SATURDAY') {
  353. $generalAvailabilities['SATURDAY'][] = $ga;
  354. }
  355. if ($ga->day_of_week == 'SUNDAY') {
  356. $generalAvailabilities['SUNDAY'][] = $ga;
  357. }
  358. }
  359. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  360. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  361. //events for the calendar
  362. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  363. $endDateTime = new DateTime($startDate);
  364. $endDateTime->modify('+6 day');
  365. $endDate = $endDateTime->format("Y-m-d");
  366. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  367. $events = json_encode($eventsData);
  368. return view(
  369. 'app.practice-management.pro-availability',
  370. compact(
  371. 'pros',
  372. 'generalAvailabilities',
  373. 'specificAvailabilities',
  374. 'specificUnavailabilities',
  375. 'events',
  376. 'selectedProUid'
  377. )
  378. );
  379. }
  380. public function loadAvailability(Request $request, $proUid)
  381. {
  382. $performer = $this->performer();
  383. $pro = $performer->pro;
  384. $startDate = $request->get('start');
  385. $endDate = $request->get('end');
  386. $selectedPro = Pro::where('uid', $proUid)->first();
  387. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  388. }
  389. public function proAvailabilityFilter(Request $request)
  390. {
  391. $proUid = $request->get('proUid');
  392. return ['success' => true, 'data' => $proUid];
  393. }
  394. // video call page (RHS)
  395. // generic call handle (no uid)
  396. // specific call handle (uid of client)
  397. public function meet(Request $request, $uid = false)
  398. {
  399. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  400. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  401. if (!empty($client)) {
  402. return view('app.video.call-minimal', compact('session', 'client'));
  403. }
  404. return view('app.video.call-agora-v2', compact('session', 'client'));
  405. }
  406. // check video page
  407. public function checkVideo(Request $request, $uid)
  408. {
  409. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  410. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  411. $publish = false;
  412. return view('app.video.check-video-minimal', compact('session', 'client'));
  413. }
  414. public function getParticipantInfo(Request $request)
  415. {
  416. $sid = intval($request->get('uid')) - 1000000;
  417. $session = AppSession::where('id', $sid)->first();
  418. $result = [
  419. "type" => '',
  420. "name" => ''
  421. ];
  422. if ($session) {
  423. $result["type"] = $session->session_type;
  424. switch ($session->session_type) {
  425. case 'PRO':
  426. $pro = Pro::where('id', $session->pro_id)->first();
  427. $result["name"] = $pro->displayName();
  428. break;
  429. case 'CLIENT':
  430. $client = Client::where('id', $session->client_id)->first();
  431. $result["name"] = $client->displayName();
  432. break;
  433. }
  434. }
  435. return json_encode($result);
  436. }
  437. // ajax ep used by the video page
  438. // this is needed bcoz meet() is used not
  439. // just for the client passed to the view
  440. public function getOpentokSessionKey(Request $request, $uid)
  441. {
  442. $client = Client::where('uid', $uid)->first();
  443. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  444. }
  445. // poll to check if there are patients with active mcp requests
  446. public function getPatientsInQueue(Request $request)
  447. {
  448. $myInitiatives = $this->performer->pro->initiatives;
  449. if ($myInitiatives) {
  450. $myInitiatives = strtoupper($myInitiatives);
  451. }
  452. $myInitiativesList = explode('|', $myInitiatives);
  453. $myForeignLanguages = $this->performer->pro->foreign_languages;
  454. if ($myForeignLanguages) {
  455. $myForeignLanguages = strtoupper($myForeignLanguages);
  456. }
  457. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  458. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  459. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  460. })
  461. ->where(function ($query) use ($myForeignLanguagesList) {
  462. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  463. })->limit(3)->get();
  464. $results = [];
  465. foreach ($clients as $client) {
  466. $results[] = [
  467. 'clientUid' => $client->uid,
  468. 'name' => $client->displayName(),
  469. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  470. ];
  471. }
  472. return json_encode($results);
  473. }
  474. public function currentWork(Request $request)
  475. {
  476. return view('app/current-work');
  477. }
  478. public function calendar(Request $request, $proUid = null)
  479. {
  480. $pros = Pro::all();
  481. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  482. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  483. $accessibleProIds = [];
  484. foreach ($accessiblePros as $accessiblePro) {
  485. $accessibleProIds[] = $accessiblePro->id;
  486. }
  487. $accessibleProIds[] = $this->pro->id;
  488. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  489. }
  490. return view('app.practice-management.calendar', compact('pros'));
  491. }
  492. public function cellularDeviceManager(Request $request, $proUid = null)
  493. {
  494. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  495. $performerPro = $this->performer->pro;
  496. $targetPro = null;
  497. $allPros = [];
  498. $expectedForHcp = null;
  499. if ($performerPro->pro_type == 'ADMIN') {
  500. $allPros = Pro::all();
  501. $targetPro = Pro::where('uid', $proUid)->first();
  502. } else {
  503. $targetPro = $performerPro;
  504. }
  505. $clients = [];
  506. if ($targetPro) {
  507. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  508. } else {
  509. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  510. }
  511. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  512. }
  513. public function treatmentServiceUtil(Request $request)
  514. {
  515. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  516. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  517. $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"));
  518. return view('app.practice-management.treatment-services-util', compact(
  519. 'view_treatment_service_utilization_org',
  520. 'view_treatment_service_utilization',
  521. 'view_treatment_service_utilization_by_patient'));
  522. }
  523. public function processingBillMatrix(Request $request, $proUid = null)
  524. {
  525. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  526. $performerPro = $this->performer->pro;
  527. $targetPro = null;
  528. if ($performerPro->pro_type == 'ADMIN') {
  529. $targetPro = Pro::where('uid', $proUid)->first();
  530. } else {
  531. $targetPro = $performerPro;
  532. }
  533. $bills = Bill::where('is_cancelled', false);
  534. if(!$request->input('t') || $request->input('t') === 'hcp') {
  535. $bills = $bills
  536. ->where('has_hcp_been_paid', false)
  537. ->where('is_signed_by_hcp', true);
  538. if ($targetPro) {
  539. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  540. }
  541. if($request->input('c')) {
  542. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = hcp_company_pro_id) = ' . $request->input('c'));
  543. }
  544. }
  545. else if($request->input('t') === 'na') {
  546. $bills = $bills
  547. ->where('has_generic_pro_been_paid', false)
  548. ->where('is_signed_by_generic_pro', true);
  549. if ($targetPro) {
  550. $bills = $bills->where('generic_pro_id', $targetPro->id);
  551. }
  552. if($request->input('c')) {
  553. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = generic_company_pro_id) = ' . $request->input('c'));
  554. }
  555. }
  556. $filter = $request->input('f');
  557. switch ($filter) {
  558. case 'verified':
  559. $bills = $bills->where('is_verified', true);
  560. break;
  561. case 'not-verified':
  562. $bills = $bills->where('is_verified', false);
  563. break;
  564. }
  565. $filter = $request->input('bs');
  566. if ($filter) {
  567. $bills = $bills->where('balance_post_date', '>=', $filter);
  568. }
  569. $filter = $request->input('be');
  570. if ($filter) {
  571. $bills = $bills->where('balance_post_date', '<=', $filter);
  572. }
  573. $filter = $request->input('s');
  574. if ($filter) {
  575. $bills = $bills->where('code', '=', $filter);
  576. }
  577. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  578. $companies = Company::where('is_active', true)->orderBy('name')->get();
  579. $codes = DB::select(DB::raw("SELECT code, count(*) as count FROM bill WHERE is_cancelled IS FALSE GROUP BY code ORDER BY code"));
  580. $viewData = [
  581. 'bills' => $bills,
  582. 'targetPro' => $targetPro,
  583. 'performerPro' => $performerPro,
  584. 'proUid' => $proUid,
  585. 'companies' => $companies,
  586. 'codes' => $codes
  587. ];
  588. return view('app.practice-management.processing-bill-matrix', $viewData);
  589. }
  590. public function proFinancials(Request $request, $proUid = null)
  591. {
  592. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  593. $performerPro = $this->performer->pro;
  594. $targetPro = null;
  595. if ($performerPro->pro_type == 'ADMIN') {
  596. $targetPro = Pro::where('uid', $proUid)->first();
  597. } else {
  598. $targetPro = $performerPro;
  599. }
  600. $fPros = Pro::whereNotNull('balance');
  601. if($targetPro) {
  602. $fPros = $fPros->where('uid', $targetPro->uid);
  603. }
  604. $fPros = $fPros
  605. ->where('balance', '>', 0)
  606. ->orderBy('name_last')
  607. ->orderBy('name_first')
  608. ->paginate();
  609. return view('app.practice-management.pro-financials', compact('fPros', 'targetPro'));
  610. }
  611. public function hcpBillMatrix(Request $request, $proUid = null)
  612. {
  613. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  614. $performerPro = $this->performer->pro;
  615. $targetPro = null;
  616. $allPros = [];
  617. $expectedForHcp = null;
  618. if ($performerPro->pro_type == 'ADMIN') {
  619. $allPros = Pro::all();
  620. $targetPro = Pro::where('uid', $proUid)->first();
  621. } else {
  622. $targetPro = $performerPro;
  623. }
  624. $rows = [];
  625. if ($targetPro) {
  626. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  627. } else {
  628. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  629. }
  630. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  631. }
  632. public function billingManager(Request $request, $proUid = null)
  633. {
  634. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  635. $performerPro = $this->performer->pro;
  636. $targetPro = null;
  637. $allPros = [];
  638. $expectedForHcp = null;
  639. if ($performerPro->pro_type == 'ADMIN') {
  640. $allPros = Pro::all();
  641. $targetPro = Pro::where('uid', $proUid)->first();
  642. } else {
  643. $targetPro = $performerPro;
  644. }
  645. $notes = [];
  646. if ($targetPro) {
  647. $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;
  648. $notes = Note::where('hcp_pro_id', $targetPro->id);
  649. } else {
  650. $notes = Note::where('id', '>', 0);
  651. }
  652. if($request->input('date')) {
  653. $notes = $notes->where('effective_dateest', $request->input('date'));
  654. }
  655. $filters = [];
  656. $filters['bills_created'] = $request->input('bills_created');
  657. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  658. $filters['bills_resolved'] = $request->input('bills_resolved');
  659. $filters['bills_closed'] = $request->input('bills_closed');
  660. $filters['claims_created'] = $request->input('claims_created');
  661. $filters['claims_closed'] = $request->input('claims_closed');
  662. if ($filters['bills_created']) {
  663. $notes->where(
  664. 'bill_total_expected',
  665. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  666. 0);
  667. }
  668. if ($filters['is_billing_marked_done']) {
  669. $notes->where(
  670. 'is_billing_marked_done',
  671. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  672. true);
  673. }
  674. if ($filters['bills_resolved']) {
  675. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  676. if ($filters['bills_resolved'] === 'yes') {
  677. $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');
  678. } elseif ($filters['bills_resolved'] === 'no') {
  679. $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');
  680. }
  681. }
  682. if ($filters['bills_closed']) {
  683. $notes->where(
  684. 'is_bill_closed',
  685. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  686. true);
  687. }
  688. if ($filters['claims_created']) {
  689. $notes->where(
  690. 'claim_total_expected',
  691. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  692. 0);
  693. }
  694. if ($filters['claims_closed']) {
  695. $notes->where(
  696. 'is_claim_closed',
  697. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  698. true);
  699. }
  700. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  701. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  702. }
  703. public function remoteMonitoring(Request $request) {
  704. $performer = $this->performer();
  705. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  706. $careMonthStart = date($ym . '-01');
  707. // filters
  708. $filters = '';
  709. $fmd = $request->input('fmd');
  710. if($fmd && $fmd !== 'all') {
  711. switch($fmd) {
  712. case 'lt16':
  713. $filters .= ' AND (care_month.number_of_days_with_remote_measurements < 16 OR care_month.number_of_days_with_remote_measurements IS NULL) ';
  714. break;
  715. case 'gte16':
  716. $filters .= ' AND (care_month.number_of_days_with_remote_measurements >= 16 AND care_month.number_of_days_with_remote_measurements IS NOT NULL) ';
  717. break;
  718. }
  719. }
  720. $fcomm = $request->input('fcomm');
  721. if($fcomm && $fcomm !== 'all') {
  722. switch($fcomm) {
  723. case 'not-done':
  724. $filters .= ' AND (care_month.has_non_hcp_communicated_to_patient_about_rm = FALSE OR care_month.has_non_hcp_communicated_to_patient_about_rm IS NULL) ';
  725. break;
  726. case 'done':
  727. $filters .= ' AND (care_month.has_non_hcp_communicated_to_patient_about_rm = TRUE AND care_month.has_non_hcp_communicated_to_patient_about_rm IS NOT NULL) ';
  728. break;
  729. }
  730. }
  731. $c = $request->input('c');
  732. if($c) {
  733. $filters .= ' AND client.uid = \'' . $request->input('c') . '\' ';
  734. }
  735. $patients = DB::select(
  736. DB::raw(
  737. "
  738. SELECT client.name_first, client.name_last, client.uid as client_uid,
  739. care_month.uid as care_month_uid,
  740. care_month.id as care_month_id,
  741. care_month.start_date,
  742. care_month.number_of_days_with_remote_measurements,
  743. care_month.has_non_hcp_communicated_to_patient_about_rm,
  744. care_month.rm_num_measurements_not_stamped_by_mcp,
  745. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  746. care_month.rm_num_measurements_not_stamped_by_rmm,
  747. care_month.rm_num_measurements_not_stamped_by_rme,
  748. client.mcp_pro_id,
  749. client.default_na_pro_id,
  750. client.rmm_pro_id,
  751. client.rme_pro_id,
  752. client.cell_number
  753. FROM care_month join client on care_month.client_id = client.id
  754. WHERE (
  755. (client.mcp_pro_id = {$performer->pro->id})
  756. OR (client.rmm_pro_id = {$performer->pro->id})
  757. OR (client.rme_pro_id = {$performer->pro->id})
  758. OR (client.default_na_pro_id = {$performer->pro->id})
  759. )
  760. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  761. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  762. {$filters}
  763. ORDER BY care_month.number_of_days_with_remote_measurements DESC NULLS LAST, client.name_first ASC, client.name_last ASC
  764. "
  765. )
  766. );
  767. $timestamp = strtotime(date('Y-m-d'));
  768. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  769. return view('app.practice-management.remote-monitoring', compact('patients', 'daysRemaining', 'careMonthStart'));
  770. }
  771. public function remoteMonitoringMeasurements(Request $request, CareMonth $careMonth) {
  772. $performer = $this->performer();
  773. $measurements = DB::select(
  774. DB::raw(
  775. "
  776. SELECT measurement.label,
  777. measurement.ts,
  778. measurement.effective_date,
  779. measurement.sbp_mm_hg,
  780. measurement.dbp_mm_hg,
  781. measurement.value_pulse,
  782. measurement.value_irregular,
  783. measurement.numeric_value,
  784. measurement.value,
  785. measurement.uid,
  786. client.name_first,
  787. client.name_last,
  788. client.mcp_pro_id,
  789. client.default_na_pro_id,
  790. client.rmm_pro_id,
  791. client.rme_pro_id
  792. FROM measurement RIGHT JOIN client on measurement.client_id = client.id
  793. WHERE
  794. client.id = {$careMonth->client_id}
  795. AND measurement.label IS NOT NULL
  796. AND measurement.label NOT IN ('SBP', 'DBP')
  797. AND (measurement.is_cellular_zero = FALSE or measurement.is_cellular_zero IS NULL)
  798. AND measurement.is_removed IS FALSE
  799. AND measurement.client_bdt_measurement_id IS NOT NULL
  800. AND measurement.care_month_id = {$careMonth->id}
  801. AND (
  802. (client.mcp_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_mcp IS FALSE)
  803. OR (client.rmm_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rmm IS FALSE)
  804. OR (client.rme_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rme IS FALSE)
  805. OR (client.default_na_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_non_hcp IS FALSE)
  806. )
  807. ORDER BY ts DESC
  808. "
  809. )
  810. );
  811. return view('app.practice-management.remote-monitoring-measurements', compact('careMonth', 'measurements'));
  812. }
  813. public function billMatrix(Request $request)
  814. {
  815. $bClients = [];
  816. $bHCPPros = [];
  817. $bNAPros = [];
  818. $filters = [];
  819. $filters['client'] = $request->input('client');
  820. $filters['service'] = $request->input('service');
  821. $filters['hcp'] = $request->input('hcp');
  822. $filters['hcp_paid'] = $request->input('hcp_paid');
  823. $filters['expected_op'] = $request->input('expected_op');
  824. $filters['expected_value'] = $request->input('expected_value');
  825. $filters['paid_op'] = $request->input('paid_op');
  826. $filters['paid_value'] = $request->input('paid_value');
  827. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  828. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  829. $filters['hcp_sign'] = $request->input('hcp_sign');
  830. $filters['verified'] = $request->input('verified');
  831. $filters['cancelled'] = $request->input('cancelled');
  832. $bills = Bill::orderBy('effective_date')->paginate();
  833. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  834. }
  835. public function medicarePartBClaims(Request $request)
  836. {
  837. $medicarePartBOnly = $request->get("medicare_part_b");
  838. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  839. //Only medicare claims
  840. $claims = [];
  841. foreach ($allClaims as $claim) {
  842. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  843. $claims[] = $claim;
  844. }
  845. }
  846. $claimEDIs = ClaimEDI::all();
  847. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  848. }
  849. // Generate PDF
  850. public function downloadClaims()
  851. {
  852. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  853. view()->share('claims', $claims);
  854. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  855. return $pdf->download('pdf_file.pdf');
  856. }
  857. public function tickets(Request $request, $proUid = null)
  858. {
  859. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  860. return view('app.practice-management.tickets', compact('tickets'));
  861. }
  862. public function supplyOrders(Request $request)
  863. {
  864. // counts
  865. $counts = $this->getSupplyOrderCounts();
  866. // so clients
  867. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  868. $soClientIDs = array_map(function ($_x) {
  869. return $_x->client_id;
  870. }, $soClientIDs);
  871. $soClients = Client::whereIn('id', $soClientIDs)->get();
  872. // so products
  873. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  874. $soProductIDs = array_map(function ($_x) {
  875. return $_x->product_id;
  876. }, $soProductIDs);
  877. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  878. $filters = [];
  879. $filters['client'] = $request->input('client');
  880. $filters['product'] = $request->input('product');
  881. $filters['reason'] = $request->input('reason');
  882. $filters['cu_memo'] = $request->input('cu_memo');
  883. $filters['pro_sign'] = $request->input('pro_sign');
  884. $filters['client_sign'] = $request->input('client_sign');
  885. $filters['shipment'] = $request->input('shipment');
  886. $filters['lot_number'] = $request->input('lot_number');
  887. $filters['imei'] = $request->input('imei');
  888. $filters['cancelled'] = $request->input('cancelled');
  889. $supplyOrders = SupplyOrder::where('id', '>', 0);
  890. // apply filters
  891. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  892. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  893. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  894. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  895. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  896. if ($filters['client_sign']) {
  897. if ($filters['client_sign'] === 'signed')
  898. $supplyOrders->where('is_signed_by_client', true);
  899. elseif ($filters['client_sign'] === 'waived')
  900. $supplyOrders->where('is_client_signature_waived', true);
  901. else
  902. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  903. }
  904. if ($filters['shipment']) {
  905. if ($filters['shipment'] === 'not_cleared_for_shipment')
  906. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  907. elseif ($filters['shipment'] === 'cleared_for_shipment')
  908. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  909. else
  910. $supplyOrders
  911. ->whereNotNull('shipment_id')
  912. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  913. }
  914. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  915. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  916. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  917. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  918. return view('app.practice-management.supply-orders',
  919. compact('supplyOrders', 'filters',
  920. 'soClients', 'soProducts', 'counts'
  921. )
  922. );
  923. }
  924. public function shipments(Request $request, $filter = null)
  925. {
  926. // counts
  927. $counts = $this->getShipmentCounts();
  928. // so clients
  929. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  930. $shClientIDs = array_map(function ($_x) {
  931. return $_x->client_id;
  932. }, $shClientIDs);
  933. $shClients = Client::whereIn('id', $shClientIDs)->get();
  934. $shipments = Shipment::where('id', '>', 0);
  935. $filters = [];
  936. $filters['client'] = $request->input('client');
  937. $filters['courier'] = $request->input('courier');
  938. $filters['tracking_num'] = $request->input('tracking_num');
  939. $filters['label'] = $request->input('label');
  940. $filters['status'] = $request->input('status');
  941. $filters['cancelled'] = $request->input('cancelled');
  942. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  943. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  944. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  945. if ($filters['label']) {
  946. if ($filters['label'] === 'yes')
  947. $shipments->whereNotNull('label_system_file_id');
  948. else
  949. $shipments->whereNull('label_system_file_id');
  950. }
  951. if ($filters['status']) $shipments->where('status', $filters['status']);
  952. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  953. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  954. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  955. }
  956. public function cellularMeasurements(Request $request)
  957. {
  958. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  959. return view('app.practice-management.cellular-measurements', compact('measurements'));
  960. }
  961. // v2 supply-orders & shipments management (wh)
  962. public function supplyOrdersReadyToShip(Request $request)
  963. {
  964. $counts = $this->getSupplyOrderCounts();
  965. $supplyOrders = SupplyOrder
  966. ::where('is_cleared_for_shipment', true)
  967. ->where('is_cancelled', false)
  968. ->whereNull('shipment_id')
  969. ->join('client', 'client.id', '=', 'supply_order.client_id')
  970. ->orderBy('client.name_last', 'ASC')
  971. ->orderBy('client.name_first', 'ASC')
  972. ->orderBy('supply_order.client_id', 'ASC')
  973. ->orderBy('supply_order.mailing_address_full', 'ASC')
  974. ->orderBy('supply_order.created_at', 'ASC')
  975. ->select('supply_order.*')
  976. ->paginate();
  977. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  978. }
  979. public function supplyOrdersShipmentUnderway(Request $request)
  980. {
  981. $counts = $this->getSupplyOrderCounts();
  982. $supplyOrders = SupplyOrder
  983. ::where('is_cancelled', false)
  984. ->whereNotNull('shipment_id')
  985. ->orderBy('client_id', 'ASC')
  986. ->orderBy('mailing_address_full', 'ASC')
  987. ->orderBy('created_at', 'ASC')
  988. ->paginate();
  989. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  990. }
  991. public function supplyOrdersHanging(Request $request)
  992. {
  993. $counts = $this->getSupplyOrderCounts();
  994. $supplyOrders = SupplyOrder
  995. ::select('supply_order.*')
  996. ->leftJoin('shipment', function($join) {
  997. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  998. })
  999. ->where('shipment.status', 'CANCELLED')
  1000. ->where('supply_order.is_cancelled', false)
  1001. ->orderBy('supply_order.client_id', 'ASC')
  1002. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1003. ->orderBy('supply_order.created_at', 'ASC')
  1004. ->paginate();
  1005. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  1006. }
  1007. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  1008. {
  1009. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  1010. ->where('is_cancelled', true)
  1011. ->where('is_cancellation_acknowledged', false)
  1012. ->orderBy('created_at', 'desc')
  1013. ->paginate();
  1014. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  1015. }
  1016. public function supplyOrdersUnsigned(Request $request)
  1017. {
  1018. $supplyOrders = SupplyOrder
  1019. ::where('is_cancelled', false)
  1020. ->where('is_signed_by_pro', false)
  1021. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  1022. ->orderBy('created_at', 'desc')
  1023. ->paginate();
  1024. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  1025. }
  1026. private function getSupplyOrderCounts()
  1027. {
  1028. return [
  1029. "supplyOrders" => SupplyOrder::count(),
  1030. "supplyOrdersReadyToShip" => SupplyOrder
  1031. ::where('is_cleared_for_shipment', true)
  1032. ->where('is_cancelled', false)
  1033. ->whereNull('shipment_id')->count(),
  1034. "supplyOrdersShipmentUnderway" => SupplyOrder
  1035. ::where('is_cancelled', false)
  1036. ->whereNotNull('shipment_id')->count(),
  1037. "supplyOrdersHanging" => SupplyOrder
  1038. ::leftJoin('shipment', function($join) {
  1039. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1040. })
  1041. ->where('shipment.status', 'CANCELLED')
  1042. ->where('supply_order.is_cancelled', false)
  1043. ->count(),
  1044. ];
  1045. }
  1046. public function shipmentsReadyToPrint(Request $request)
  1047. {
  1048. $counts = $this->getShipmentCounts();
  1049. $shipments = Shipment
  1050. ::where('is_cancelled', false)
  1051. ->where('status', 'CREATED')
  1052. ->orderBy('created_at', 'ASC')
  1053. ->paginate();
  1054. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  1055. }
  1056. public function shipmentsShipmentUnderway(Request $request)
  1057. {
  1058. $counts = $this->getShipmentCounts();
  1059. $shipments = Shipment
  1060. ::where('is_cancelled', false)
  1061. ->where('status', 'PRINTED')
  1062. ->orderBy('created_at', 'ASC')
  1063. ->paginate();
  1064. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  1065. }
  1066. private function getShipmentCounts()
  1067. {
  1068. return [
  1069. "shipments" => Shipment::count(),
  1070. "shipmentsReadyToPrint" => Shipment
  1071. ::where('is_cancelled', false)
  1072. ->where('status', 'CREATED')
  1073. ->count(),
  1074. "shipmentsWaitingForPicker" => Shipment
  1075. ::where('is_cancelled', false)
  1076. ->where('status', 'PRINTED')
  1077. ->count()
  1078. ];
  1079. }
  1080. public function shipment(Request $request, Shipment $shipment)
  1081. {
  1082. return view('app.practice-management.shipment', compact('shipment'));
  1083. }
  1084. public function shipmentsMultiPrint(Request $request, $ids)
  1085. {
  1086. $ids = array_map(function ($_x) {
  1087. return intval($_x);
  1088. }, explode("|", $ids));
  1089. $shipments = Shipment::whereIn('id', $ids)->get();
  1090. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  1091. }
  1092. public function patientClaimSummary(Request $request, $proUid = null)
  1093. {
  1094. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  1095. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  1096. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  1097. $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;
  1098. $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;
  1099. $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;
  1100. $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;
  1101. $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;
  1102. $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;
  1103. $performerPro = $this->performer->pro;
  1104. $allPros = [];
  1105. if ($performerPro->pro_type == 'ADMIN') {
  1106. $allPros = Pro::all();
  1107. } else {
  1108. $allPros = [$performerPro];
  1109. }
  1110. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  1111. $patientsQuery = Client::where('is_dummy', '=', false)
  1112. ->whereNull('shadow_pro_id')
  1113. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  1114. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  1115. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  1116. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  1117. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  1118. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  1119. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  1120. if ($proUid) {
  1121. $mcpPro = Pro::where('uid', $proUid)->first();
  1122. if ($mcpPro) {
  1123. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  1124. }
  1125. }
  1126. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  1127. $patientsQuery->orderBy('notes_total', 'DESC');
  1128. $patients = $patientsQuery->paginate(50);
  1129. $data = [
  1130. 'patients' => $patients,
  1131. 'proUid' => $proUid,
  1132. 'allPros' => $allPros,
  1133. 'notesTotal' => $notesTotal,
  1134. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  1135. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  1136. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  1137. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  1138. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  1139. 'patientsTotal' => $patientsTotal,
  1140. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  1141. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  1142. ];
  1143. return view('app.practice-management.patient-claim-summary', $data);
  1144. }
  1145. public function claims(Request $request){
  1146. $status = $request->get('status');
  1147. $claims = [];
  1148. if(!$status){
  1149. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  1150. }else{
  1151. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  1152. }
  1153. return view('app.practice-management.claims', compact('claims', 'status'));
  1154. }
  1155. public function processClaims(Request $request) {
  1156. $status = '';
  1157. $q = $request->input('q') ? $request->input('q') : '';
  1158. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  1159. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  1160. $params = [
  1161. 'q' => '%' . $q . '%',
  1162. 'from' => $from,
  1163. 'to' => $to
  1164. ];
  1165. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  1166. if($hcpPro) {
  1167. $params['hcp'] = $hcpPro->id;
  1168. }
  1169. $claims = DB::select(DB::raw("
  1170. SELECT claim.uid AS uid,
  1171. DATE(claim.created_at) AS created,
  1172. claim.status,
  1173. client.uid AS client_uid,
  1174. client.cell_number AS client_phone,
  1175. (client.name_last || ' ' || client.name_first) AS client ,
  1176. client.chart_number AS client_chart_number,
  1177. cp.id AS claim_pro_id,
  1178. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  1179. sp.id AS status_pro_id,
  1180. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  1181. note.uid AS note_uid,
  1182. note.method,
  1183. note.new_or_fu_or_na,
  1184. -- claim.status_updated_at,
  1185. (DATE(claim.status_updated_at) || ' ' ||
  1186. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  1187. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  1188. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  1189. (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,
  1190. ROUND(claim.expected_total, 3) as expected_total
  1191. FROM claim
  1192. join client on claim.client_id = client.id
  1193. join pro cp on claim.pro_id = cp.id
  1194. left join note on claim.note_id = note.id
  1195. left join app_session on claim.status_updated_by_session_id = app_session.id
  1196. left join pro sp on app_session.pro_id = sp.id
  1197. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  1198. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'))
  1199. -- AND claim.current_version_id IS NOT NULL
  1200. AND (client.name_first ILIKE :q OR
  1201. client.name_last ILIKE :q OR
  1202. client.chart_number ILIKE :q OR
  1203. client.mcn ILIKE :q)
  1204. AND (claim.created_at >= :from AND claim.created_at <= :to)
  1205. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  1206. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  1207. ORDER BY claim.created_at ASC
  1208. --OFFSET 0 LIMIT 15
  1209. "), $params);
  1210. if($request->input('json')) {
  1211. return json_encode($claims);
  1212. }
  1213. return view('app.practice-management.process-claims', compact('claims', 'status'));
  1214. }
  1215. public function processNotes(Request $request) {
  1216. $mode = $request->input('mode') ? $request->input('mode') : '1';
  1217. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  1218. DB::enableQueryLog(); // Enable query log
  1219. $test = Note::where('is_cancelled', false)
  1220. ->where('is_signed_by_hcp', true)
  1221. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1222. ->whereNull('current_note_pickup_for_processing_id')
  1223. ->where('is_billing_marked_done', true)
  1224. ->where('is_bill_closed', true)
  1225. ->where('is_claim_closed', true)
  1226. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1227. ->count();
  1228. // var_dump($test);
  1229. // dd(DB::getQueryLog()); // Show results of log
  1230. // exit;
  1231. $counts = [
  1232. "picked" => Note::where('is_cancelled', false)
  1233. ->whereNotNull('current_note_pickup_for_processing_id')
  1234. ->count(),
  1235. "bad" => Note::where('is_cancelled', false)
  1236. ->whereRaw("((detail_json)::json->>'isBad' = 'true')")
  1237. ->count(),
  1238. // not yet signed
  1239. "mode-1" => Note::where('is_cancelled', false)
  1240. ->where('is_signed_by_hcp', false)
  1241. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1242. // ->whereNull('current_note_pickup_for_processing_id')
  1243. ->count(),
  1244. // billing not marked done
  1245. "mode-2" => Note::where('is_cancelled', false)
  1246. ->where('is_signed_by_hcp', true)
  1247. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1248. // ->whereNull('current_note_pickup_for_processing_id')
  1249. ->where('is_billing_marked_done', false)
  1250. ->count(),
  1251. // billing not closed
  1252. "mode-3" => Note::where('is_cancelled', false)
  1253. ->where('is_signed_by_hcp', true)
  1254. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1255. // ->whereNull('current_note_pickup_for_processing_id')
  1256. ->where('is_billing_marked_done', true)
  1257. ->where('is_bill_closed', false)
  1258. ->count(),
  1259. // claiming not closed
  1260. "mode-4" => Note::where('is_cancelled', false)
  1261. ->where('is_signed_by_hcp', true)
  1262. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1263. // ->whereNull('current_note_pickup_for_processing_id')
  1264. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1265. ->where('is_billing_marked_done', true)
  1266. ->where('is_bill_closed', true)
  1267. ->where('is_claim_closed', false)
  1268. ->count(),
  1269. // has unsubmitted claims
  1270. "mode-5" => Note::where('is_cancelled', false)
  1271. /*->where('is_signed_by_hcp', true)
  1272. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1273. ->whereNull('current_note_pickup_for_processing_id')
  1274. ->where('is_billing_marked_done', true)
  1275. ->where('is_bill_closed', true)
  1276. ->where('is_claim_closed', true)
  1277. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1278. */
  1279. ->where('is_signed_by_hcp', true)
  1280. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1281. // ->whereNull('current_note_pickup_for_processing_id')
  1282. ->where('is_billing_marked_done', true)
  1283. ->where('is_bill_closed', true)
  1284. // ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true')")
  1285. ->where('is_claim_closed', true)
  1286. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0)")
  1287. ->count(),
  1288. // has unsubmitted claims MARKED GOOD!
  1289. "mode-8" => Note::where('is_cancelled', false)
  1290. ->where('is_signed_by_hcp', true)
  1291. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1292. ->whereNull('current_note_pickup_for_processing_id')
  1293. ->where('is_billing_marked_done', true)
  1294. ->where('is_bill_closed', true)
  1295. ->where('is_claim_closed', false)
  1296. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1297. ->count(),
  1298. // all good
  1299. "mode-6" => Note::where('is_cancelled', false)
  1300. ->where('is_signed_by_hcp', true)
  1301. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1302. // ->whereNull('current_note_pickup_for_processing_id')
  1303. ->where('is_billing_marked_done', true)
  1304. ->where('is_bill_closed', true)
  1305. ->where('is_claim_closed', true)
  1306. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id) > 0)")
  1307. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1308. ->count(),
  1309. "mode-7" => Note::where('is_cancelled', false)
  1310. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1311. ->count(),
  1312. ];
  1313. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1314. }
  1315. public function getNextNote(Request $request, $mode)
  1316. {
  1317. $note = null;
  1318. switch (+$mode) {
  1319. case 1:
  1320. $note = Note::where('is_cancelled', false)
  1321. ->where('is_signed_by_hcp', false)
  1322. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1323. ->whereNull('current_note_pickup_for_processing_id')
  1324. ->orderBy('effective_dateest', 'DESC')
  1325. ->first();
  1326. break;
  1327. case 2:
  1328. $note = Note::where('is_cancelled', false)
  1329. ->where('is_signed_by_hcp', true)
  1330. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1331. ->whereNull('current_note_pickup_for_processing_id')
  1332. ->where('is_billing_marked_done', false)
  1333. ->orderBy('effective_dateest', 'DESC')
  1334. ->first();
  1335. break;
  1336. case 3:
  1337. $note = Note::where('is_cancelled', false)
  1338. ->where('is_signed_by_hcp', true)
  1339. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1340. ->whereNull('current_note_pickup_for_processing_id')
  1341. ->where('is_billing_marked_done', true)
  1342. ->where('is_bill_closed', false)
  1343. ->orderBy('effective_dateest', 'DESC')
  1344. ->first();
  1345. break;
  1346. case 4: // claiming not closed
  1347. DB::enableQueryLog(); // Enable query log
  1348. $note = Note::where('is_cancelled', false)
  1349. ->where('is_signed_by_hcp', true)
  1350. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1351. ->whereNull('current_note_pickup_for_processing_id')
  1352. ->where('is_billing_marked_done', true)
  1353. ->where('is_bill_closed', true)
  1354. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1355. ->where('is_claim_closed', false)
  1356. ->orderBy('effective_dateest', 'DESC')
  1357. ->first();
  1358. // DB::enableQueryLog(); // Enable query log
  1359. // Your Eloquent query executed by using get()
  1360. // dd(DB::getQueryLog()); // Show results of log
  1361. break;
  1362. case 5:
  1363. $note = Note::where('is_cancelled', false)
  1364. ->where('is_signed_by_hcp', true)
  1365. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1366. ->whereNull('current_note_pickup_for_processing_id')
  1367. ->where('is_billing_marked_done', true)
  1368. ->where('is_bill_closed', true)
  1369. ->where('is_claim_closed', true)
  1370. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1371. ->orderBy('effective_dateest', 'DESC')
  1372. ->first();
  1373. break;
  1374. case 8:
  1375. $note = Note::where('is_cancelled', false)
  1376. ->where('is_signed_by_hcp', true)
  1377. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1378. ->whereNull('current_note_pickup_for_processing_id')
  1379. ->where('is_billing_marked_done', true)
  1380. ->where('is_bill_closed', true)
  1381. ->where('is_claim_closed', false)
  1382. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1383. ->orderBy('effective_dateest', 'DESC')
  1384. ->first();
  1385. break;
  1386. }
  1387. if($note) {
  1388. $note->client_uid = $note->client->uid;
  1389. }
  1390. return json_encode($note);
  1391. }
  1392. public function pickedNotes(Request $request) {
  1393. $counts = [
  1394. "unpicked" => Note::where('is_cancelled', false)
  1395. ->whereNull('current_note_pickup_for_processing_id')
  1396. ->count(),
  1397. ];
  1398. $notes = Note::where('is_cancelled', false)
  1399. ->whereNotNull('current_note_pickup_for_processing_id')
  1400. ->orderBy('effective_dateest', 'ASC')
  1401. ->paginate();
  1402. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1403. }
  1404. public function badNotes(Request $request) {
  1405. $counts = [
  1406. "unpicked" => Note::where('is_cancelled', false)
  1407. ->whereNull('current_note_pickup_for_processing_id')
  1408. ->count(),
  1409. ];
  1410. $notes = Note::where('is_cancelled', false)
  1411. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1412. ->orderBy('effective_dateest', 'ASC')
  1413. ->paginate();
  1414. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1415. }
  1416. public function doneNotes(Request $request) {
  1417. $counts = [
  1418. "unpicked" => Note::where('is_cancelled', false)
  1419. ->whereNull('current_note_pickup_for_processing_id')
  1420. ->count(),
  1421. ];
  1422. $notes = Note::where('is_cancelled', false)
  1423. ->where('is_signed_by_hcp', true)
  1424. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1425. ->whereNull('current_note_pickup_for_processing_id')
  1426. ->where('is_billing_marked_done', true)
  1427. ->where('is_bill_closed', true)
  1428. ->where('is_claim_closed', true)
  1429. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1430. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1431. ->orderBy('effective_dateest', 'ASC')
  1432. ->paginate();
  1433. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1434. }
  1435. public function currentMbClaim(Request $request, $claimUid) {
  1436. $claim = Claim::where('uid', $claimUid)->first();
  1437. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1438. }
  1439. public function currentClaimLines(Request $request, $claimUid) {
  1440. $claim = Claim::where('uid', $claimUid)->first();
  1441. if($request->input('json')) {
  1442. foreach ($claim->lines as $line) {
  1443. $line->expected_total = round($line->expected_total, 3);
  1444. $x = $line->claimLineIcds;
  1445. }
  1446. return json_encode($claim->lines);
  1447. }
  1448. return view('app.practice-management._claim-lines', compact('claim'));
  1449. }
  1450. public function packsMultiPrint(Request $request) {
  1451. $packs = Pack
  1452. ::select('pack.*')
  1453. ->leftJoin('shipment', function($join) {
  1454. $join->on('pack.shipment_id', '=', 'shipment.id');
  1455. })
  1456. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1457. ->where(function ($query) {
  1458. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1459. })
  1460. ->whereNotNull('pack.label_system_file_id')
  1461. ->orderBy('pack.created_at', 'ASC')
  1462. ->get();
  1463. return view('app.practice-management.packs-multi-print', compact('packs'));
  1464. }
  1465. public function packsMultiPDF(Request $request, $ids) {
  1466. $ids = array_map(function ($_x) {
  1467. return intval($_x);
  1468. }, explode("|", $ids));
  1469. $packs = Pack::whereIn('id', $ids)->get();
  1470. }
  1471. public function handouts(Request $request) {
  1472. $handouts = Handout::orderBy('display_name')->get();
  1473. return view('app.practice-management.handouts', compact('handouts'));
  1474. }
  1475. private function callJava($request, $endPoint, $data)
  1476. {
  1477. $url = config('stag.backendUrl') . $endPoint;
  1478. $response = Http::asForm()
  1479. ->withHeaders([
  1480. 'sessionKey' => $request->cookie('sessionKey')
  1481. ])
  1482. ->post($url, $data)
  1483. ->body();
  1484. dd($response);
  1485. return $response;
  1486. }
  1487. public function genericBills(Request $request)
  1488. {
  1489. return view('app.practice-management.generic-bills');
  1490. }
  1491. }