PracticeManagementController.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. $expectedForHcp = null;
  498. if ($performerPro->pro_type == 'ADMIN') {
  499. $targetPro = Pro::where('uid', $proUid)->first();
  500. } else {
  501. $targetPro = $performerPro;
  502. }
  503. $clients = [];
  504. if ($targetPro) {
  505. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(20);
  506. } else {
  507. $clients = Client::orderBy('created_at', 'desc')->paginate(20);
  508. }
  509. return view('app.practice-management.cellular-device-manager', compact('clients', 'targetPro', 'proUid'));
  510. }
  511. public function treatmentServiceUtil(Request $request)
  512. {
  513. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  514. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  515. $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"));
  516. return view('app.practice-management.treatment-services-util', compact(
  517. 'view_treatment_service_utilization_org',
  518. 'view_treatment_service_utilization',
  519. 'view_treatment_service_utilization_by_patient'));
  520. }
  521. public function processingBillMatrix(Request $request, $proUid = null)
  522. {
  523. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  524. $performerPro = $this->performer->pro;
  525. $targetPro = null;
  526. if ($performerPro->pro_type == 'ADMIN') {
  527. $targetPro = Pro::where('uid', $proUid)->first();
  528. } else {
  529. $targetPro = $performerPro;
  530. }
  531. $bills = Bill::where('is_cancelled', false);
  532. if(!$request->input('t') || $request->input('t') === 'hcp') {
  533. $bills = $bills
  534. ->where('has_hcp_been_paid', false)
  535. ->where('is_signed_by_hcp', true);
  536. if ($targetPro) {
  537. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  538. }
  539. if($request->input('c')) {
  540. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = hcp_company_pro_id) = ' . $request->input('c'));
  541. }
  542. }
  543. else if($request->input('t') === 'na') {
  544. $bills = $bills
  545. ->where('has_generic_pro_been_paid', false)
  546. ->where('is_signed_by_generic_pro', true);
  547. if ($targetPro) {
  548. $bills = $bills->where('generic_pro_id', $targetPro->id);
  549. }
  550. if($request->input('c')) {
  551. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = generic_company_pro_id) = ' . $request->input('c'));
  552. }
  553. }
  554. $filter = $request->input('f');
  555. switch ($filter) {
  556. case 'verified':
  557. $bills = $bills->where('is_verified', true);
  558. break;
  559. case 'not-verified':
  560. $bills = $bills->where('is_verified', false);
  561. break;
  562. }
  563. $filter = $request->input('bs');
  564. if ($filter) {
  565. $bills = $bills->where('balance_post_date', '>=', $filter);
  566. }
  567. $filter = $request->input('be');
  568. if ($filter) {
  569. $bills = $bills->where('balance_post_date', '<=', $filter);
  570. }
  571. $filter = $request->input('s');
  572. if ($filter) {
  573. $bills = $bills->where('code', '=', $filter);
  574. }
  575. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  576. $companies = Company::where('is_active', true)->orderBy('name')->get();
  577. $codes = DB::select(DB::raw("SELECT code, count(*) as count FROM bill WHERE is_cancelled IS FALSE GROUP BY code ORDER BY code"));
  578. $viewData = [
  579. 'bills' => $bills,
  580. 'targetPro' => $targetPro,
  581. 'performerPro' => $performerPro,
  582. 'proUid' => $proUid,
  583. 'companies' => $companies,
  584. 'codes' => $codes
  585. ];
  586. return view('app.practice-management.processing-bill-matrix', $viewData);
  587. }
  588. public function proFinancials(Request $request, $proUid = null)
  589. {
  590. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  591. $performerPro = $this->performer->pro;
  592. $targetPro = null;
  593. if ($performerPro->pro_type == 'ADMIN') {
  594. $targetPro = Pro::where('uid', $proUid)->first();
  595. } else {
  596. $targetPro = $performerPro;
  597. }
  598. $fPros = Pro::whereNotNull('balance');
  599. if($targetPro) {
  600. $fPros = $fPros->where('uid', $targetPro->uid);
  601. }
  602. $fPros = $fPros
  603. ->where('balance', '>', 0)
  604. ->orderBy('name_last')
  605. ->orderBy('name_first')
  606. ->paginate();
  607. return view('app.practice-management.pro-financials', compact('fPros', 'targetPro'));
  608. }
  609. public function hcpBillMatrix(Request $request, $proUid = null)
  610. {
  611. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  612. $performerPro = $this->performer->pro;
  613. $targetPro = null;
  614. $allPros = [];
  615. $expectedForHcp = null;
  616. if ($performerPro->pro_type == 'ADMIN') {
  617. $allPros = Pro::all();
  618. $targetPro = Pro::where('uid', $proUid)->first();
  619. } else {
  620. $targetPro = $performerPro;
  621. }
  622. $rows = [];
  623. if ($targetPro) {
  624. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  625. } else {
  626. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  627. }
  628. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  629. }
  630. public function billingManager(Request $request, $proUid = null)
  631. {
  632. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  633. $performerPro = $this->performer->pro;
  634. $targetPro = null;
  635. $allPros = [];
  636. $expectedForHcp = null;
  637. if ($performerPro->pro_type == 'ADMIN') {
  638. $allPros = Pro::all();
  639. $targetPro = Pro::where('uid', $proUid)->first();
  640. } else {
  641. $targetPro = $performerPro;
  642. }
  643. $notes = [];
  644. if ($targetPro) {
  645. $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;
  646. $notes = Note::where('hcp_pro_id', $targetPro->id);
  647. } else {
  648. $notes = Note::where('id', '>', 0);
  649. }
  650. if($request->input('date')) {
  651. $notes = $notes->where('effective_dateest', $request->input('date'));
  652. }
  653. $filters = [];
  654. $filters['bills_created'] = $request->input('bills_created');
  655. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  656. $filters['bills_resolved'] = $request->input('bills_resolved');
  657. $filters['bills_closed'] = $request->input('bills_closed');
  658. $filters['claims_created'] = $request->input('claims_created');
  659. $filters['claims_closed'] = $request->input('claims_closed');
  660. if ($filters['bills_created']) {
  661. $notes->where(
  662. 'bill_total_expected',
  663. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  664. 0);
  665. }
  666. if ($filters['is_billing_marked_done']) {
  667. $notes->where(
  668. 'is_billing_marked_done',
  669. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  670. true);
  671. }
  672. if ($filters['bills_resolved']) {
  673. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  674. if ($filters['bills_resolved'] === 'yes') {
  675. $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');
  676. } elseif ($filters['bills_resolved'] === 'no') {
  677. $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');
  678. }
  679. }
  680. if ($filters['bills_closed']) {
  681. $notes->where(
  682. 'is_bill_closed',
  683. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  684. true);
  685. }
  686. if ($filters['claims_created']) {
  687. $notes->where(
  688. 'claim_total_expected',
  689. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  690. 0);
  691. }
  692. if ($filters['claims_closed']) {
  693. $notes->where(
  694. 'is_claim_closed',
  695. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  696. true);
  697. }
  698. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  699. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  700. }
  701. public function remoteMonitoring(Request $request) {
  702. $performer = $this->performer();
  703. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  704. $careMonthStart = date($ym . '-01');
  705. // filters
  706. $filters = '';
  707. $fmd = $request->input('fmd');
  708. if($fmd && $fmd !== 'all') {
  709. switch($fmd) {
  710. case 'lt16':
  711. $filters .= ' AND (care_month.number_of_days_with_remote_measurements < 16 OR care_month.number_of_days_with_remote_measurements IS NULL) ';
  712. break;
  713. case 'gte16':
  714. $filters .= ' AND (care_month.number_of_days_with_remote_measurements >= 16 AND care_month.number_of_days_with_remote_measurements IS NOT NULL) ';
  715. break;
  716. }
  717. }
  718. $fcomm = $request->input('fcomm');
  719. if($fcomm && $fcomm !== 'all') {
  720. switch($fcomm) {
  721. case 'not-done':
  722. $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) ';
  723. break;
  724. case 'done':
  725. $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) ';
  726. break;
  727. }
  728. }
  729. $c = $request->input('c');
  730. if($c) {
  731. $filters .= ' AND client.uid = \'' . $request->input('c') . '\' ';
  732. }
  733. $patients = DB::select(
  734. DB::raw(
  735. "
  736. SELECT client.name_first, client.name_last, client.uid as client_uid,
  737. care_month.uid as care_month_uid,
  738. care_month.id as care_month_id,
  739. care_month.start_date,
  740. care_month.number_of_days_with_remote_measurements,
  741. care_month.has_non_hcp_communicated_to_patient_about_rm,
  742. care_month.rm_num_measurements_not_stamped_by_mcp,
  743. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  744. care_month.rm_num_measurements_not_stamped_by_rmm,
  745. care_month.rm_num_measurements_not_stamped_by_rme,
  746. client.mcp_pro_id,
  747. client.default_na_pro_id,
  748. client.rmm_pro_id,
  749. client.rme_pro_id,
  750. client.cell_number
  751. FROM care_month join client on care_month.client_id = client.id
  752. WHERE (
  753. (client.mcp_pro_id = {$performer->pro->id})
  754. OR (client.rmm_pro_id = {$performer->pro->id})
  755. OR (client.rme_pro_id = {$performer->pro->id})
  756. OR (client.default_na_pro_id = {$performer->pro->id})
  757. )
  758. AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  759. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  760. {$filters}
  761. ORDER BY care_month.number_of_days_with_remote_measurements DESC NULLS LAST, client.name_first ASC, client.name_last ASC
  762. "
  763. )
  764. );
  765. $timestamp = strtotime(date('Y-m-d'));
  766. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  767. return view('app.practice-management.remote-monitoring', compact('patients', 'daysRemaining', 'careMonthStart'));
  768. }
  769. public function remoteMonitoringMeasurements(Request $request, CareMonth $careMonth) {
  770. $performer = $this->performer();
  771. $measurements = DB::select(
  772. DB::raw(
  773. "
  774. SELECT measurement.label,
  775. measurement.ts,
  776. measurement.effective_date,
  777. measurement.sbp_mm_hg,
  778. measurement.dbp_mm_hg,
  779. measurement.value_pulse,
  780. measurement.value_irregular,
  781. measurement.numeric_value,
  782. measurement.value,
  783. measurement.uid,
  784. client.name_first,
  785. client.name_last,
  786. client.mcp_pro_id,
  787. client.default_na_pro_id,
  788. client.rmm_pro_id,
  789. client.rme_pro_id
  790. FROM measurement RIGHT JOIN client on measurement.client_id = client.id
  791. WHERE
  792. client.id = {$careMonth->client_id}
  793. AND measurement.label IS NOT NULL
  794. AND measurement.label NOT IN ('SBP', 'DBP')
  795. AND (measurement.is_cellular_zero = FALSE or measurement.is_cellular_zero IS NULL)
  796. AND measurement.is_removed IS FALSE
  797. AND measurement.client_bdt_measurement_id IS NOT NULL
  798. AND measurement.care_month_id = {$careMonth->id}
  799. AND (
  800. (client.mcp_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_mcp IS FALSE)
  801. OR (client.rmm_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rmm IS FALSE)
  802. OR (client.rme_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rme IS FALSE)
  803. OR (client.default_na_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_non_hcp IS FALSE)
  804. )
  805. ORDER BY ts DESC
  806. "
  807. )
  808. );
  809. return view('app.practice-management.remote-monitoring-measurements', compact('careMonth', 'measurements'));
  810. }
  811. public function billMatrix(Request $request)
  812. {
  813. $bClients = [];
  814. $bHCPPros = [];
  815. $bNAPros = [];
  816. $filters = [];
  817. $filters['client'] = $request->input('client');
  818. $filters['service'] = $request->input('service');
  819. $filters['hcp'] = $request->input('hcp');
  820. $filters['hcp_paid'] = $request->input('hcp_paid');
  821. $filters['expected_op'] = $request->input('expected_op');
  822. $filters['expected_value'] = $request->input('expected_value');
  823. $filters['paid_op'] = $request->input('paid_op');
  824. $filters['paid_value'] = $request->input('paid_value');
  825. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  826. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  827. $filters['hcp_sign'] = $request->input('hcp_sign');
  828. $filters['verified'] = $request->input('verified');
  829. $filters['cancelled'] = $request->input('cancelled');
  830. $bills = Bill::orderBy('effective_date')->paginate();
  831. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  832. }
  833. public function medicarePartBClaims(Request $request)
  834. {
  835. $medicarePartBOnly = $request->get("medicare_part_b");
  836. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  837. //Only medicare claims
  838. $claims = [];
  839. foreach ($allClaims as $claim) {
  840. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  841. $claims[] = $claim;
  842. }
  843. }
  844. $claimEDIs = ClaimEDI::all();
  845. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  846. }
  847. // Generate PDF
  848. public function downloadClaims()
  849. {
  850. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  851. view()->share('claims', $claims);
  852. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  853. return $pdf->download('pdf_file.pdf');
  854. }
  855. public function tickets(Request $request, $proUid = null)
  856. {
  857. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  858. return view('app.practice-management.tickets', compact('tickets'));
  859. }
  860. public function supplyOrders(Request $request)
  861. {
  862. // counts
  863. $counts = $this->getSupplyOrderCounts();
  864. // so clients
  865. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  866. $soClientIDs = array_map(function ($_x) {
  867. return $_x->client_id;
  868. }, $soClientIDs);
  869. $soClients = Client::whereIn('id', $soClientIDs)->get();
  870. // so products
  871. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  872. $soProductIDs = array_map(function ($_x) {
  873. return $_x->product_id;
  874. }, $soProductIDs);
  875. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  876. $filters = [];
  877. $filters['client'] = $request->input('client');
  878. $filters['product'] = $request->input('product');
  879. $filters['reason'] = $request->input('reason');
  880. $filters['cu_memo'] = $request->input('cu_memo');
  881. $filters['pro_sign'] = $request->input('pro_sign');
  882. $filters['client_sign'] = $request->input('client_sign');
  883. $filters['shipment'] = $request->input('shipment');
  884. $filters['lot_number'] = $request->input('lot_number');
  885. $filters['imei'] = $request->input('imei');
  886. $filters['cancelled'] = $request->input('cancelled');
  887. $supplyOrders = SupplyOrder::where('id', '>', 0);
  888. // apply filters
  889. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  890. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  891. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  892. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  893. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  894. if ($filters['client_sign']) {
  895. if ($filters['client_sign'] === 'signed')
  896. $supplyOrders->where('is_signed_by_client', true);
  897. elseif ($filters['client_sign'] === 'waived')
  898. $supplyOrders->where('is_client_signature_waived', true);
  899. else
  900. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  901. }
  902. if ($filters['shipment']) {
  903. if ($filters['shipment'] === 'not_cleared_for_shipment')
  904. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  905. elseif ($filters['shipment'] === 'cleared_for_shipment')
  906. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  907. else
  908. $supplyOrders
  909. ->whereNotNull('shipment_id')
  910. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  911. }
  912. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  913. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  914. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  915. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  916. return view('app.practice-management.supply-orders',
  917. compact('supplyOrders', 'filters',
  918. 'soClients', 'soProducts', 'counts'
  919. )
  920. );
  921. }
  922. public function shipments(Request $request, $filter = null)
  923. {
  924. // counts
  925. $counts = $this->getShipmentCounts();
  926. // so clients
  927. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  928. $shClientIDs = array_map(function ($_x) {
  929. return $_x->client_id;
  930. }, $shClientIDs);
  931. $shClients = Client::whereIn('id', $shClientIDs)->get();
  932. $shipments = Shipment::where('id', '>', 0);
  933. $filters = [];
  934. $filters['client'] = $request->input('client');
  935. $filters['courier'] = $request->input('courier');
  936. $filters['tracking_num'] = $request->input('tracking_num');
  937. $filters['label'] = $request->input('label');
  938. $filters['status'] = $request->input('status');
  939. $filters['cancelled'] = $request->input('cancelled');
  940. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  941. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  942. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  943. if ($filters['label']) {
  944. if ($filters['label'] === 'yes')
  945. $shipments->whereNotNull('label_system_file_id');
  946. else
  947. $shipments->whereNull('label_system_file_id');
  948. }
  949. if ($filters['status']) $shipments->where('status', $filters['status']);
  950. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  951. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  952. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  953. }
  954. public function cellularMeasurements(Request $request)
  955. {
  956. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  957. return view('app.practice-management.cellular-measurements', compact('measurements'));
  958. }
  959. // v2 supply-orders & shipments management (wh)
  960. public function supplyOrdersReadyToShip(Request $request)
  961. {
  962. $counts = $this->getSupplyOrderCounts();
  963. $supplyOrders = SupplyOrder
  964. ::where('is_cleared_for_shipment', true)
  965. ->where('is_cancelled', false)
  966. ->whereNull('shipment_id')
  967. ->join('client', 'client.id', '=', 'supply_order.client_id')
  968. ->orderBy('client.name_last', 'ASC')
  969. ->orderBy('client.name_first', 'ASC')
  970. ->orderBy('supply_order.client_id', 'ASC')
  971. ->orderBy('supply_order.mailing_address_full', 'ASC')
  972. ->orderBy('supply_order.created_at', 'ASC')
  973. ->select('supply_order.*')
  974. ->paginate();
  975. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  976. }
  977. public function supplyOrdersShipmentUnderway(Request $request)
  978. {
  979. $counts = $this->getSupplyOrderCounts();
  980. $supplyOrders = SupplyOrder
  981. ::where('is_cancelled', false)
  982. ->whereNotNull('shipment_id')
  983. ->orderBy('client_id', 'ASC')
  984. ->orderBy('mailing_address_full', 'ASC')
  985. ->orderBy('created_at', 'ASC')
  986. ->paginate();
  987. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  988. }
  989. public function supplyOrdersHanging(Request $request)
  990. {
  991. $counts = $this->getSupplyOrderCounts();
  992. $supplyOrders = SupplyOrder
  993. ::select('supply_order.*')
  994. ->leftJoin('shipment', function($join) {
  995. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  996. })
  997. ->where('shipment.status', 'CANCELLED')
  998. ->where('supply_order.is_cancelled', false)
  999. ->orderBy('supply_order.client_id', 'ASC')
  1000. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1001. ->orderBy('supply_order.created_at', 'ASC')
  1002. ->paginate();
  1003. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  1004. }
  1005. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  1006. {
  1007. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  1008. ->where('is_cancelled', true)
  1009. ->where('is_cancellation_acknowledged', false)
  1010. ->orderBy('created_at', 'desc')
  1011. ->paginate();
  1012. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  1013. }
  1014. public function supplyOrdersUnsigned(Request $request)
  1015. {
  1016. $supplyOrders = SupplyOrder
  1017. ::where('is_cancelled', false)
  1018. ->where('is_signed_by_pro', false)
  1019. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  1020. ->orderBy('created_at', 'desc')
  1021. ->paginate();
  1022. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  1023. }
  1024. private function getSupplyOrderCounts()
  1025. {
  1026. return [
  1027. "supplyOrders" => SupplyOrder::count(),
  1028. "supplyOrdersReadyToShip" => SupplyOrder
  1029. ::where('is_cleared_for_shipment', true)
  1030. ->where('is_cancelled', false)
  1031. ->whereNull('shipment_id')->count(),
  1032. "supplyOrdersShipmentUnderway" => SupplyOrder
  1033. ::where('is_cancelled', false)
  1034. ->whereNotNull('shipment_id')->count(),
  1035. "supplyOrdersHanging" => SupplyOrder
  1036. ::leftJoin('shipment', function($join) {
  1037. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1038. })
  1039. ->where('shipment.status', 'CANCELLED')
  1040. ->where('supply_order.is_cancelled', false)
  1041. ->count(),
  1042. ];
  1043. }
  1044. public function shipmentsReadyToPrint(Request $request)
  1045. {
  1046. $counts = $this->getShipmentCounts();
  1047. $shipments = Shipment
  1048. ::where('is_cancelled', false)
  1049. ->where('status', 'CREATED')
  1050. ->orderBy('created_at', 'ASC')
  1051. ->paginate();
  1052. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  1053. }
  1054. public function shipmentsShipmentUnderway(Request $request)
  1055. {
  1056. $counts = $this->getShipmentCounts();
  1057. $shipments = Shipment
  1058. ::where('is_cancelled', false)
  1059. ->where('status', 'PRINTED')
  1060. ->orderBy('created_at', 'ASC')
  1061. ->paginate();
  1062. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  1063. }
  1064. private function getShipmentCounts()
  1065. {
  1066. return [
  1067. "shipments" => Shipment::count(),
  1068. "shipmentsReadyToPrint" => Shipment
  1069. ::where('is_cancelled', false)
  1070. ->where('status', 'CREATED')
  1071. ->count(),
  1072. "shipmentsWaitingForPicker" => Shipment
  1073. ::where('is_cancelled', false)
  1074. ->where('status', 'PRINTED')
  1075. ->count()
  1076. ];
  1077. }
  1078. public function shipment(Request $request, Shipment $shipment)
  1079. {
  1080. return view('app.practice-management.shipment', compact('shipment'));
  1081. }
  1082. public function shipmentsMultiPrint(Request $request, $ids)
  1083. {
  1084. $ids = array_map(function ($_x) {
  1085. return intval($_x);
  1086. }, explode("|", $ids));
  1087. $shipments = Shipment::whereIn('id', $ids)->get();
  1088. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  1089. }
  1090. public function patientClaimSummary(Request $request, $proUid = null)
  1091. {
  1092. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  1093. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  1094. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  1095. $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;
  1096. $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;
  1097. $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;
  1098. $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;
  1099. $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;
  1100. $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;
  1101. $performerPro = $this->performer->pro;
  1102. $allPros = [];
  1103. if ($performerPro->pro_type == 'ADMIN') {
  1104. $allPros = Pro::all();
  1105. } else {
  1106. $allPros = [$performerPro];
  1107. }
  1108. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  1109. $patientsQuery = Client::where('is_dummy', '=', false)
  1110. ->whereNull('shadow_pro_id')
  1111. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  1112. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  1113. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  1114. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  1115. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  1116. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  1117. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  1118. if ($proUid) {
  1119. $mcpPro = Pro::where('uid', $proUid)->first();
  1120. if ($mcpPro) {
  1121. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  1122. }
  1123. }
  1124. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  1125. $patientsQuery->orderBy('notes_total', 'DESC');
  1126. $patients = $patientsQuery->paginate(50);
  1127. $data = [
  1128. 'patients' => $patients,
  1129. 'proUid' => $proUid,
  1130. 'allPros' => $allPros,
  1131. 'notesTotal' => $notesTotal,
  1132. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  1133. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  1134. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  1135. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  1136. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  1137. 'patientsTotal' => $patientsTotal,
  1138. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  1139. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  1140. ];
  1141. return view('app.practice-management.patient-claim-summary', $data);
  1142. }
  1143. public function claims(Request $request){
  1144. $status = $request->get('status');
  1145. $claims = [];
  1146. if(!$status){
  1147. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  1148. }else{
  1149. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  1150. }
  1151. return view('app.practice-management.claims', compact('claims', 'status'));
  1152. }
  1153. public function processClaims(Request $request) {
  1154. $status = '';
  1155. $q = $request->input('q') ? $request->input('q') : '';
  1156. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  1157. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  1158. $params = [
  1159. 'q' => '%' . $q . '%',
  1160. 'from' => $from,
  1161. 'to' => $to
  1162. ];
  1163. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  1164. if($hcpPro) {
  1165. $params['hcp'] = $hcpPro->id;
  1166. }
  1167. $claims = DB::select(DB::raw("
  1168. SELECT claim.uid AS uid,
  1169. DATE(claim.created_at) AS created,
  1170. claim.status,
  1171. client.uid AS client_uid,
  1172. client.cell_number AS client_phone,
  1173. (client.name_last || ' ' || client.name_first) AS client ,
  1174. client.chart_number AS client_chart_number,
  1175. cp.id AS claim_pro_id,
  1176. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  1177. sp.id AS status_pro_id,
  1178. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  1179. note.uid AS note_uid,
  1180. note.method,
  1181. note.new_or_fu_or_na,
  1182. care_month.uid AS care_month_uid,
  1183. -- claim.status_updated_at,
  1184. (DATE(claim.status_updated_at) || ' ' ||
  1185. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  1186. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  1187. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  1188. (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,
  1189. ROUND(claim.expected_total, 3) as expected_total
  1190. FROM claim
  1191. join client on claim.client_id = client.id
  1192. join pro cp on claim.pro_id = cp.id
  1193. left join note on claim.note_id = note.id
  1194. left join care_month on claim.cm_id = care_month.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 rmLaunchAndClean(Request $request) {
  1216. $keyNumbers = [];
  1217. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_NONHCP40'] = CareMonth::where('is_bill_closed', false)
  1218. ->whereNotNull('company_pro_id')
  1219. ->whereNotNull('company_pro_payer_id')
  1220. ->whereNotNull('company_location_id')
  1221. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1222. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1223. ->where('rm_total_time_in_seconds', '>=', 4200) // at 4200 (70 minutes, it becomes eligible for plus40)
  1224. ->count();
  1225. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_NONHCP20'] = CareMonth::where('is_bill_closed', false)
  1226. ->whereNotNull('company_pro_id')
  1227. ->whereNotNull('company_pro_payer_id')
  1228. ->whereNotNull('company_location_id')
  1229. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1230. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1231. ->where('rm_total_time_in_seconds', '<', 4200)
  1232. ->where('rm_total_time_in_seconds', '>=', 3000) // at 3000 (50 minutes, it becomes eligible for plus20)
  1233. ->count();
  1234. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP'] = CareMonth::where('is_bill_closed', false)
  1235. ->whereNotNull('company_pro_id')
  1236. ->whereNotNull('company_pro_payer_id')
  1237. ->whereNotNull('company_location_id')
  1238. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1239. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1240. ->where('rm_total_time_in_seconds', '<', 3000)
  1241. ->count();
  1242. $keyNumbers['careMonthsWithoutCompanyProInfo'] = CareMonth::where('is_bill_closed', false)
  1243. ->where(function ($q) {
  1244. $q->whereNull('company_pro_id')
  1245. ->orWhereNull('company_pro_payer_id')
  1246. ->orWhereNull('company_location_id');
  1247. })
  1248. ->count();
  1249. /*$keyNumbers['careMonthsWithoutRMReasons'] = CareMonth::where('is_bill_closed', false)
  1250. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) = 0')
  1251. ->count();*/
  1252. $keyNumbers['careMonthsUnder30RMMinutes'] = CareMonth::where('is_bill_closed', false)
  1253. ->where('rm_total_time_in_seconds', '<', 1800)
  1254. ->count();
  1255. return view('app.practice-management.rm-launch-and-clean', compact('keyNumbers'));
  1256. }
  1257. public function processNotes(Request $request) {
  1258. $mode = $request->input('mode') ? $request->input('mode') : '1';
  1259. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  1260. DB::enableQueryLog(); // Enable query log
  1261. $test = Note::where('is_cancelled', false)
  1262. ->where('is_signed_by_hcp', true)
  1263. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1264. ->whereNull('current_note_pickup_for_processing_id')
  1265. ->where('is_billing_marked_done', true)
  1266. ->where('is_bill_closed', true)
  1267. ->where('is_claim_closed', true)
  1268. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1269. ->count();
  1270. // var_dump($test);
  1271. // dd(DB::getQueryLog()); // Show results of log
  1272. // exit;
  1273. $counts = [
  1274. "picked" => Note::where('is_cancelled', false)
  1275. ->whereNotNull('current_note_pickup_for_processing_id')
  1276. ->count(),
  1277. "bad" => Note::where('is_cancelled', false)
  1278. ->whereRaw("((detail_json)::json->>'isBad' = 'true')")
  1279. ->count(),
  1280. // not yet signed
  1281. "mode-1" => Note::where('is_cancelled', false)
  1282. ->where('is_signed_by_hcp', false)
  1283. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1284. // ->whereNull('current_note_pickup_for_processing_id')
  1285. ->count(),
  1286. // billing not marked done
  1287. "mode-2" => Note::where('is_cancelled', false)
  1288. ->where('is_signed_by_hcp', true)
  1289. //->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1290. // ->whereNull('current_note_pickup_for_processing_id')
  1291. ->where('is_billing_marked_done', false)
  1292. ->count(),
  1293. // billing not closed
  1294. "mode-3" => Note::where('is_cancelled', false)
  1295. ->where('is_signed_by_hcp', true)
  1296. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1297. // ->whereNull('current_note_pickup_for_processing_id')
  1298. ->where('is_billing_marked_done', true)
  1299. ->where('is_bill_closed', false)
  1300. ->count(),
  1301. // claiming not closed
  1302. "mode-4" => Note::where('is_cancelled', false)
  1303. ->where('is_signed_by_hcp', true)
  1304. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1305. // ->whereNull('current_note_pickup_for_processing_id')
  1306. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1307. ->where('is_billing_marked_done', true)
  1308. ->where('is_bill_closed', true)
  1309. ->where('is_claim_closed', false)
  1310. ->count(),
  1311. // has unsubmitted claims
  1312. "mode-5" => Note::where('is_cancelled', false)
  1313. /*->where('is_signed_by_hcp', true)
  1314. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1315. ->whereNull('current_note_pickup_for_processing_id')
  1316. ->where('is_billing_marked_done', true)
  1317. ->where('is_bill_closed', true)
  1318. ->where('is_claim_closed', true)
  1319. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1320. */
  1321. ->where('is_signed_by_hcp', true)
  1322. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1323. // ->whereNull('current_note_pickup_for_processing_id')
  1324. ->where('is_billing_marked_done', true)
  1325. ->where('is_bill_closed', true)
  1326. // ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true')")
  1327. ->where('is_claim_closed', true)
  1328. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0)")
  1329. ->count(),
  1330. // has unsubmitted claims MARKED GOOD!
  1331. "mode-8" => Note::where('is_cancelled', false)
  1332. ->where('is_signed_by_hcp', true)
  1333. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1334. ->whereNull('current_note_pickup_for_processing_id')
  1335. ->where('is_billing_marked_done', true)
  1336. ->where('is_bill_closed', true)
  1337. ->where('is_claim_closed', false)
  1338. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1339. ->count(),
  1340. // all good
  1341. "mode-6" => Note::where('is_cancelled', false)
  1342. ->where('is_signed_by_hcp', true)
  1343. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1344. // ->whereNull('current_note_pickup_for_processing_id')
  1345. ->where('is_billing_marked_done', true)
  1346. ->where('is_bill_closed', true)
  1347. ->where('is_claim_closed', true)
  1348. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id) > 0)")
  1349. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1350. ->count(),
  1351. "mode-7" => Note::where('is_cancelled', false)
  1352. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1353. ->count(),
  1354. ];
  1355. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1356. }
  1357. public function getNextNote(Request $request, $mode)
  1358. {
  1359. $note = null;
  1360. switch (+$mode) {
  1361. case 1:
  1362. $note = Note::where('is_cancelled', false)
  1363. ->where('is_signed_by_hcp', false)
  1364. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1365. ->whereNull('current_note_pickup_for_processing_id')
  1366. ->orderBy('effective_dateest', 'DESC')
  1367. ->first();
  1368. break;
  1369. case 2:
  1370. $note = Note::where('is_cancelled', false)
  1371. ->where('is_signed_by_hcp', true)
  1372. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1373. ->whereNull('current_note_pickup_for_processing_id')
  1374. ->where('is_billing_marked_done', false)
  1375. ->orderBy('effective_dateest', 'DESC')
  1376. ->first();
  1377. break;
  1378. case 3:
  1379. $note = Note::where('is_cancelled', false)
  1380. ->where('is_signed_by_hcp', true)
  1381. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1382. ->whereNull('current_note_pickup_for_processing_id')
  1383. ->where('is_billing_marked_done', true)
  1384. ->where('is_bill_closed', false)
  1385. ->orderBy('effective_dateest', 'DESC')
  1386. ->first();
  1387. break;
  1388. case 4: // claiming not closed
  1389. DB::enableQueryLog(); // Enable query log
  1390. $note = Note::where('is_cancelled', false)
  1391. ->where('is_signed_by_hcp', true)
  1392. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1393. ->whereNull('current_note_pickup_for_processing_id')
  1394. ->where('is_billing_marked_done', true)
  1395. ->where('is_bill_closed', true)
  1396. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1397. ->where('is_claim_closed', false)
  1398. ->orderBy('effective_dateest', 'DESC')
  1399. ->first();
  1400. // DB::enableQueryLog(); // Enable query log
  1401. // Your Eloquent query executed by using get()
  1402. // dd(DB::getQueryLog()); // Show results of log
  1403. break;
  1404. case 5:
  1405. $note = Note::where('is_cancelled', false)
  1406. ->where('is_signed_by_hcp', true)
  1407. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1408. ->whereNull('current_note_pickup_for_processing_id')
  1409. ->where('is_billing_marked_done', true)
  1410. ->where('is_bill_closed', true)
  1411. ->where('is_claim_closed', true)
  1412. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1413. ->orderBy('effective_dateest', 'DESC')
  1414. ->first();
  1415. break;
  1416. case 8:
  1417. $note = Note::where('is_cancelled', false)
  1418. ->where('is_signed_by_hcp', true)
  1419. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1420. ->whereNull('current_note_pickup_for_processing_id')
  1421. ->where('is_billing_marked_done', true)
  1422. ->where('is_bill_closed', true)
  1423. ->where('is_claim_closed', false)
  1424. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1425. ->orderBy('effective_dateest', 'DESC')
  1426. ->first();
  1427. break;
  1428. }
  1429. if($note) {
  1430. $note->client_uid = $note->client->uid;
  1431. }
  1432. return json_encode($note);
  1433. }
  1434. public function pickedNotes(Request $request) {
  1435. $counts = [
  1436. "unpicked" => Note::where('is_cancelled', false)
  1437. ->whereNull('current_note_pickup_for_processing_id')
  1438. ->count(),
  1439. ];
  1440. $notes = Note::where('is_cancelled', false)
  1441. ->whereNotNull('current_note_pickup_for_processing_id')
  1442. ->orderBy('effective_dateest', 'ASC')
  1443. ->paginate();
  1444. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1445. }
  1446. public function badNotes(Request $request) {
  1447. $counts = [
  1448. "unpicked" => Note::where('is_cancelled', false)
  1449. ->whereNull('current_note_pickup_for_processing_id')
  1450. ->count(),
  1451. ];
  1452. $notes = Note::where('is_cancelled', false)
  1453. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1454. ->orderBy('effective_dateest', 'ASC')
  1455. ->paginate();
  1456. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1457. }
  1458. public function doneNotes(Request $request) {
  1459. $counts = [
  1460. "unpicked" => Note::where('is_cancelled', false)
  1461. ->whereNull('current_note_pickup_for_processing_id')
  1462. ->count(),
  1463. ];
  1464. $notes = Note::where('is_cancelled', false)
  1465. ->where('is_signed_by_hcp', true)
  1466. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1467. ->whereNull('current_note_pickup_for_processing_id')
  1468. ->where('is_billing_marked_done', true)
  1469. ->where('is_bill_closed', true)
  1470. ->where('is_claim_closed', true)
  1471. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1472. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1473. ->orderBy('effective_dateest', 'ASC')
  1474. ->paginate();
  1475. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1476. }
  1477. public function currentMbClaim(Request $request, $claimUid) {
  1478. $claim = Claim::where('uid', $claimUid)->first();
  1479. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1480. }
  1481. public function currentClaimLines(Request $request, $claimUid) {
  1482. $claim = Claim::where('uid', $claimUid)->first();
  1483. if($request->input('json')) {
  1484. foreach ($claim->lines as $line) {
  1485. $line->expected_total = round($line->expected_total, 3);
  1486. $x = $line->claimLineIcds;
  1487. }
  1488. return json_encode($claim->lines);
  1489. }
  1490. return view('app.practice-management._claim-lines', compact('claim'));
  1491. }
  1492. public function packsMultiPrint(Request $request) {
  1493. $packs = Pack
  1494. ::select('pack.*')
  1495. ->leftJoin('shipment', function($join) {
  1496. $join->on('pack.shipment_id', '=', 'shipment.id');
  1497. })
  1498. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1499. ->where(function ($query) {
  1500. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1501. })
  1502. ->whereNotNull('pack.label_system_file_id')
  1503. ->orderBy('pack.created_at', 'ASC')
  1504. ->get();
  1505. return view('app.practice-management.packs-multi-print', compact('packs'));
  1506. }
  1507. public function packsMultiPDF(Request $request, $ids) {
  1508. $ids = array_map(function ($_x) {
  1509. return intval($_x);
  1510. }, explode("|", $ids));
  1511. $packs = Pack::whereIn('id', $ids)->get();
  1512. }
  1513. public function handouts(Request $request) {
  1514. $handouts = Handout::orderBy('display_name')->get();
  1515. return view('app.practice-management.handouts', compact('handouts'));
  1516. }
  1517. private function callJava($request, $endPoint, $data)
  1518. {
  1519. $url = config('stag.backendUrl') . $endPoint;
  1520. $response = Http::asForm()
  1521. ->withHeaders([
  1522. 'sessionKey' => $request->cookie('sessionKey')
  1523. ])
  1524. ->post($url, $data)
  1525. ->body();
  1526. dd($response);
  1527. return $response;
  1528. }
  1529. public function genericBills(Request $request)
  1530. {
  1531. return view('app.practice-management.generic-bills');
  1532. }
  1533. }