PracticeManagementController.php 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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\McCodeCheck;
  16. use App\Models\Note;
  17. use App\Models\Pack;
  18. use App\Models\Pro;
  19. use App\Models\Product;
  20. use App\Models\ProFavorite;
  21. use App\Models\ProGeneralAvailability;
  22. use App\Models\ProProAccess;
  23. use App\Models\ProRate;
  24. use App\Models\ProSpecificAvailability;
  25. use App\Models\ProSpecificUnavailability;
  26. use App\Models\ProTeam;
  27. use App\Models\ProTextShortcut;
  28. use App\Models\ProTransaction;
  29. use App\Models\Shipment;
  30. use App\Models\SupplyOrder;
  31. use App\Models\Team;
  32. use App\Models\Ticket;
  33. use App\Models\AccountInvite;
  34. use App\Models\ClientMeasurementDaysPerMonth;
  35. use App\Models\ClientBDTDevice;
  36. use App\Models\ClientMemo;
  37. use Carbon\Carbon;
  38. use Illuminate\Support\Facades\DB;
  39. use Illuminate\Support\Facades\Http;
  40. use PDF;
  41. use DateTime;
  42. use DateTimeZone;
  43. use Illuminate\Http\Request;
  44. class PracticeManagementController extends Controller
  45. {
  46. public function rpmMatrix(Request $request)
  47. {
  48. $proID = $this->performer()->pro->id;
  49. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  50. $query = Client::whereNull('shadow_pro_id');
  51. if(!$isAdmin) $query->where('mcp_pro_id', '=', $proID);
  52. $clients = $query->orderByRaw('most_recent_cellular_measurement_at desc nulls last')
  53. ->paginate(50);
  54. return view ('app.practice-management.rpm-matrix', compact('clients'));
  55. }
  56. public function mcCodeChecks(Request $request)
  57. {
  58. $checks = McCodeCheck::orderBy('next_eligible_date_professional', 'asc')->get();
  59. return view ('app.practice-management.mc-code-checks', compact('checks'));
  60. }
  61. public function remoteMonitoringReport(Request $request)
  62. {
  63. $rows = null;
  64. $proID = $this->performer()->pro->id;
  65. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  66. //$rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  67. $query = CareMonth::whereNotNull('mcp_pro_id')
  68. ->where('number_of_days_with_remote_measurements', '>=', 16)
  69. ->where('days_between_most_recent_mcp_note_date_and_end_of_care_month', '<=', 90);
  70. if(!$isAdmin) $query->where('mcp_pro_id', '=', $proID);
  71. if($request->get('show_billing_not_closed_only')){
  72. $query->where(function($q){
  73. return $q->where('is_bill_closed', false)->orWhereNull('is_bill_closed');
  74. });
  75. }
  76. $rows = $query->orderByRaw(DB::raw('start_date DESC'))->paginate(100) ;
  77. return view ('app.practice-management.remote-monitoring-report', compact('rows', 'isAdmin'));
  78. }
  79. public function billingReport(Request $request)
  80. {
  81. $claimStatus = $request->get('status');
  82. $rows = BillingReport::paginate(50);
  83. if($claimStatus){
  84. if($claimStatus == 'NO_CLAIMS'){
  85. $rows = BillingReport::whereHas('note', function($noteQuery) use ($claimStatus){
  86. return $noteQuery->has('claims', '=', 0);
  87. })->whereHas('client', function($clientQuery){
  88. return $clientQuery->where('client_engagement_status_category','<>' ,'DUMMY');
  89. })->paginate(50);
  90. }else{
  91. $rows = BillingReport::whereHas('note', function($noteQuery) use ($claimStatus){
  92. return $noteQuery->whereHas('claims', function($claimQuery) use ($claimStatus) {
  93. return $claimQuery->where('status', $claimStatus);
  94. });
  95. })->whereHas('client', function($clientQuery){
  96. return $clientQuery->where('client_engagement_status_category','<>' ,'DUMMY');
  97. })->paginate(50);
  98. }
  99. }else {
  100. $rows = BillingReport::whereHas('client', function($clientQuery){
  101. return $clientQuery->where('client_engagement_status_category','<>' ,'DUMMY');
  102. })->paginate(50);
  103. }
  104. $claimStatuses = DB::select('SELECT distinct status FROM claim ORDER BY status DESC');
  105. return view('app.practice-management.billing-report', compact('rows', 'claimStatuses'));
  106. }
  107. public function dashboard(Request $request)
  108. {
  109. return view('app.practice-management.dashboard');
  110. }
  111. public function rates(Request $request, $selectedProUid = 'all')
  112. {
  113. $proUid = $selectedProUid ? $selectedProUid : 'all';
  114. $rates = ProRate::where('is_active', true);
  115. if ($proUid !== 'all') {
  116. $selectedPro = Pro::where('uid', $proUid)->first();
  117. $rates = $rates->where('pro_id', $selectedPro->id);
  118. }
  119. $rates = $rates->orderBy('pro_id', 'asc')->get();
  120. $pros = $this->pros;
  121. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  122. }
  123. public function previousBills(Request $request)
  124. {
  125. return view('app.practice-management.previous-bills');
  126. }
  127. public function financialTransactions(Request $request)
  128. {
  129. $pro = $this->performer()->pro;
  130. $transactions = null;
  131. if($pro->pro_type === 'ADMIN') {
  132. $transactions = ProTransaction::whereNotNull('id');
  133. }
  134. else {
  135. $transactions = ProTransaction::where('pro_id', $pro->id);
  136. }
  137. $filter = $request->input('p');
  138. if ($filter) {
  139. $filterPro = Pro::where('uid', $filter)->first();
  140. if($filterPro) {
  141. $transactions = $transactions->where('pro_id', '=', $filterPro->id);
  142. }
  143. }
  144. $filter = $request->input('t');
  145. if ($filter) {
  146. $transactions = $transactions->where('plus_or_minus', '=', $filter);
  147. }
  148. $filter = $request->input('c');
  149. if ($filter) {
  150. $transactions = $transactions->where('company_id', '=', $filter);
  151. }
  152. $filter = $request->input('bs');
  153. if ($filter) {
  154. $transactions = $transactions->where('created_at', '>=', $filter);
  155. }
  156. $filter = $request->input('be');
  157. if ($filter) {
  158. $transactions = $transactions->where('created_at', '<=', $filter);
  159. }
  160. $transactions = $transactions->orderBy('created_at', 'desc')->paginate();
  161. $companies = Company::where('is_active', true)->orderBy('name')->get();
  162. return view('app.practice-management.financial-transactions', compact('transactions', 'companies'));
  163. }
  164. public function pendingBillsToSign(Request $request)
  165. {
  166. return view('app.practice-management.pending-bills-to-sign');
  167. }
  168. public function HR(Request $request)
  169. {
  170. return view('app.practice-management.hr');
  171. }
  172. public function directDepositSettings(Request $request)
  173. {
  174. return view('app.practice-management.direct-deposit-settings');
  175. }
  176. public function w9(Request $request)
  177. {
  178. return view('app.practice-management.w9');
  179. }
  180. public function contract(Request $request)
  181. {
  182. return view('app.practice-management.contract');
  183. }
  184. public function notes(Request $request, $filter = '')
  185. {
  186. $proID = $this->performer()->pro->id;
  187. $query = Note::where('hcp_pro_id', $proID);
  188. switch ($filter) {
  189. case 'not-yet-signed':
  190. $query = $query->where('is_signed_by_hcp', false);
  191. break;
  192. case 'not-yet-signed-but-ally-signed':
  193. $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
  194. break;
  195. case 'without-bills':
  196. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  197. break;
  198. // more cases can be added as needed
  199. default:
  200. break;
  201. }
  202. $notes = $query->orderBy('created_at', 'desc')->get();
  203. return view('app.practice-management.notes', compact('notes', 'filter'));
  204. }
  205. public function dnaNotesPendingMcpSign(Request $request)
  206. {
  207. $proID = $this->performer()->pro->id;
  208. $notes = Note::where('ally_pro_id', $proID)
  209. ->where('is_signed_by_hcp', false)
  210. ->where('is_cancelled', false)
  211. ->orderBy('created_at', 'desc')
  212. ->get();
  213. return view('app.practice-management.dna-notes-pending-mcp-sign', compact('notes'));
  214. }
  215. public function naBillableSignedNotes(Request $request)
  216. {
  217. $notes = Note
  218. ::where('is_signed_by_hcp', TRUE)
  219. ->where('ally_pro_id', $this->performer()->pro->id)
  220. ->where('is_cancelled', FALSE)
  221. ->whereRaw("
  222. (
  223. SELECT count(bill.id)
  224. FROM bill WHERE
  225. bill.is_cancelled = FALSE AND
  226. bill.generic_pro_id = {$this->performer()->pro->id} AND
  227. bill.note_id = note.id
  228. ) = 0
  229. ");
  230. $notes = $notes->orderBy('created_at', 'desc')->get();
  231. return view('app.practice-management.na-billable-signed-notes', compact('notes'));
  232. }
  233. public function bills(Request $request, $filter = '')
  234. {
  235. $proID = $this->performer()->pro->id;
  236. $query = Bill::where('is_cancelled', false)->where('bill_service_type', '<>', 'CARE_MONTH');
  237. switch ($filter) {
  238. case 'not-yet-signed':
  239. $query = $query
  240. ->where(function ($q) use ($proID) {
  241. $q->where(function ($q2) use ($proID) {
  242. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  243. })
  244. ->orWhere(function ($q2) use ($proID) {
  245. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  246. })
  247. ->orWhere(function ($q2) use ($proID) {
  248. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  249. })
  250. ->orWhere(function ($q2) use ($proID) {
  251. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  252. })
  253. ->orWhere(function ($q2) use ($proID) {
  254. $q2->where('generic_pro_id', $proID)->where('is_signed_by_generic_pro', false);
  255. });
  256. });
  257. break;
  258. case 'previous':
  259. $query = $query
  260. ->where(function ($q) use ($proID) {
  261. $q->where(function ($q2) use ($proID) {
  262. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  263. })
  264. ->orWhere(function ($q2) use ($proID) {
  265. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  266. })
  267. ->orWhere(function ($q2) use ($proID) {
  268. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  269. })
  270. ->orWhere(function ($q2) use ($proID) {
  271. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  272. });
  273. });
  274. break;
  275. // more cases can be added as needed
  276. default:
  277. $query = $query
  278. ->where(function ($q) use ($proID) {
  279. $q->where(function ($q2) use ($proID) {
  280. $q2->where('hcp_pro_id', $proID);
  281. })
  282. ->orWhere(function ($q2) use ($proID) {
  283. $q2->where('cm_pro_id', $proID);
  284. })
  285. ->orWhere(function ($q2) use ($proID) {
  286. $q2->where('rme_pro_id', $proID);
  287. })
  288. ->orWhere(function ($q2) use ($proID) {
  289. $q2->where('rmm_pro_id', $proID);
  290. });
  291. });
  292. break;
  293. }
  294. $bills = $query->orderBy('created_at', 'desc')->get();
  295. return view('app.practice-management.bills', compact('bills', 'filter'));
  296. }
  297. public function rmBillsToSign(Request $request)
  298. {
  299. $performerProID = $this->performer()->pro->id;
  300. $bills = Bill::where('is_cancelled', false)->where('cm_or_rm', 'RM')
  301. ->where(function ($q) use ($performerProID) {
  302. $q
  303. ->where(function ($q2) use ($performerProID) {
  304. $q2->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false);
  305. })
  306. ->orWhere(function ($q2) use ($performerProID) {
  307. $q2->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false);
  308. })
  309. ->orWhere(function ($q2) use ($performerProID) {
  310. $q2->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false);
  311. })
  312. ->orWhere(function ($q2) use ($performerProID) {
  313. $q2->where('generic_pro_id', $performerProID)->where('is_signed_by_generic_pro', false);
  314. });
  315. })
  316. ->orderBy('effective_date', 'desc')
  317. ->get();
  318. return view('app.practice-management.rm-bills-to-sign', compact('bills'));
  319. }
  320. public function unacknowledgedCancelledBills(Request $request)
  321. {
  322. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  323. ->where('is_cancelled', true)
  324. ->where('is_cancellation_acknowledged', false)
  325. ->orderBy('created_at', 'desc')
  326. ->get();
  327. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  328. }
  329. public function myTickets(Request $request, $filter = 'open')
  330. {
  331. $performer = $this->performer();
  332. $myTickets = Ticket::where(function ($q) use ($performer) {
  333. $q->where('assigned_pro_id', $performer->pro_id)
  334. ->orWhere('manager_pro_id', $performer->pro_id)
  335. ->orWhere('ordering_pro_id', $performer->pro_id)
  336. ->orWhere('initiating_pro_id', $performer->pro_id);
  337. });
  338. if ($filter === 'open') {
  339. $myTickets = $myTickets->where('is_open', true);
  340. } else if ($filter === 'closed') {
  341. $myTickets = $myTickets->where('is_open', false);
  342. }
  343. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  344. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  345. }
  346. public function myTextShortcuts(Request $request)
  347. {
  348. $personalShortcuts = DB::table('pro_text_shortcut')
  349. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  350. ->select(
  351. 'pro_text_shortcut.uid',
  352. 'pro_text_shortcut.shortcut',
  353. 'pro_text_shortcut.text',
  354. 'pro.name_first',
  355. 'pro.name_last'
  356. )
  357. ->where('pro_text_shortcut.is_removed', false);
  358. if($this->performer()->pro->pro_type !== 'ADMIN') {
  359. $personalShortcuts = $personalShortcuts->where('pro_id', $this->performer()->pro_id);
  360. }
  361. $personalShortcuts = $personalShortcuts
  362. ->orderBy('pro.name_last')
  363. ->orderBy('pro.name_first')
  364. ->orderBy('pro_text_shortcut.shortcut')
  365. ->get();
  366. $globalTextShortcuts = DB::table('pro_text_shortcut')
  367. ->select(
  368. 'pro_text_shortcut.uid',
  369. 'pro_text_shortcut.shortcut',
  370. 'pro_text_shortcut.text'
  371. )
  372. ->whereNull('pro_id')
  373. ->where('pro_text_shortcut.is_removed', false)
  374. ->orderBy('pro_text_shortcut.shortcut')
  375. ->get();
  376. return view('app.practice-management.my-text-shortcuts', compact('personalShortcuts', 'globalTextShortcuts'));
  377. }
  378. public function myFavorites(Request $request, $filter = 'all')
  379. {
  380. $performer = $this->performer();
  381. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  382. ->where('is_removed', false);
  383. if ($filter !== 'all') {
  384. $myFavorites = $myFavorites->where('category', $filter);
  385. }
  386. $myFavorites = $myFavorites
  387. ->orderBy('category', 'asc')
  388. ->orderBy('position_index', 'asc')
  389. ->get();
  390. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  391. }
  392. public function patientsWithoutCoverage(Request $request, $filter = 'all')
  393. {
  394. $performer = $this->performer();
  395. $sql = "SELECT cl.uid, cl.name_first, cl.name_last FROM client cl ";
  396. $joinClause = 'LEFT JOIN client_primary_coverage cpc ON cl.latest_client_primary_coverage_id = cpc.id ';
  397. $withoutCondition = "cl.latest_client_primary_coverage_id IS NULL";
  398. $pendingCondition = "
  399. (cl.latest_client_primary_coverage_id IS NOT NULL -- coverage exists, but status is null or unknown
  400. AND (
  401. (cpc.plan_type = 'MEDICARE' AND (cpc.is_partbprimary = 'UNKNOWN' OR cpc.is_partbprimary IS NULL))
  402. OR
  403. (cpc.plan_type != 'MEDICARE' AND
  404. (cpc.manual_determination_category = 'UNKNOWN' OR cpc.manual_determination_category IS NULL))
  405. ))";
  406. switch ($filter) {
  407. case 'without-coverage-information':
  408. $sql .= 'WHERE cl.shadow_pro_id IS NULL AND ';
  409. $sql .= $withoutCondition;
  410. break;
  411. case 'pending-coverage-verification':
  412. $sql .= $joinClause . 'WHERE cl.shadow_pro_id IS NULL AND ';
  413. $sql .= $pendingCondition;
  414. break;
  415. default:
  416. $sql .= $joinClause . 'WHERE cl.shadow_pro_id IS NULL AND ';
  417. $sql .= '(' . $withoutCondition . ' OR ' . $pendingCondition . ')';
  418. break;
  419. }
  420. $sql .= ' ORDER BY cl.name_first ASC';
  421. $pendingCoverage = DB::select(DB::raw($sql));
  422. return view('app.practice-management.patients-without-coverage', compact('pendingCoverage', 'filter'));
  423. }
  424. public function proAvailability(Request $request, $proUid = null)
  425. {
  426. $performer = $this->performer();
  427. $pro = $performer->pro;
  428. if ($proUid) {
  429. $pro = Pro::where('uid', $proUid)->first();
  430. }
  431. if ($request->get('pro_uid')) {
  432. $proUid = $request->get('pro_uid');
  433. $pro = Pro::where('uid', $proUid)->first();
  434. }
  435. $selectedProUid = $pro->uid;
  436. $pros = $this->pros;
  437. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  438. $generalAvailabilities = [
  439. 'MONDAY' => [],
  440. 'TUESDAY' => [],
  441. 'WEDNESDAY' => [],
  442. 'THURSDAY' => [],
  443. 'FRIDAY' => [],
  444. 'SATURDAY' => [],
  445. 'SUNDAY' => [],
  446. ];
  447. foreach ($generalAvailabilitiesList as $ga) {
  448. if ($ga->day_of_week == 'MONDAY') {
  449. $generalAvailabilities['MONDAY'][] = $ga;
  450. }
  451. if ($ga->day_of_week == 'TUESDAY') {
  452. $generalAvailabilities['TUESDAY'][] = $ga;
  453. }
  454. if ($ga->day_of_week == 'WEDNESDAY') {
  455. $generalAvailabilities['WEDNESDAY'][] = $ga;
  456. }
  457. if ($ga->day_of_week == 'THURSDAY') {
  458. $generalAvailabilities['THURSDAY'][] = $ga;
  459. }
  460. if ($ga->day_of_week == 'FRIDAY') {
  461. $generalAvailabilities['FRIDAY'][] = $ga;
  462. }
  463. if ($ga->day_of_week == 'SATURDAY') {
  464. $generalAvailabilities['SATURDAY'][] = $ga;
  465. }
  466. if ($ga->day_of_week == 'SUNDAY') {
  467. $generalAvailabilities['SUNDAY'][] = $ga;
  468. }
  469. }
  470. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  471. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  472. //events for the calendar
  473. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  474. $endDateTime = new DateTime($startDate);
  475. $endDateTime->modify('+6 day');
  476. $endDate = $endDateTime->format("Y-m-d");
  477. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  478. $events = json_encode($eventsData);
  479. return view(
  480. 'app.practice-management.pro-availability',
  481. compact(
  482. 'pros',
  483. 'generalAvailabilities',
  484. 'specificAvailabilities',
  485. 'specificUnavailabilities',
  486. 'events',
  487. 'selectedProUid'
  488. )
  489. );
  490. }
  491. public function loadAvailability(Request $request, $proUid)
  492. {
  493. $performer = $this->performer();
  494. $pro = $performer->pro;
  495. $startDate = $request->get('start');
  496. $endDate = $request->get('end');
  497. $selectedPro = Pro::where('uid', $proUid)->first();
  498. return $selectedPro->getAvailabilityEvents($startDate, $endDate, $request->input('tz') ? $request->input('tz') : 'EASTERN');
  499. }
  500. public function proAvailabilityFilter(Request $request)
  501. {
  502. $proUid = $request->get('proUid');
  503. return ['success' => true, 'data' => $proUid];
  504. }
  505. // video call page (RHS)
  506. // generic call handle (no uid)
  507. // specific call handle (uid of client)
  508. public function meet(Request $request, $uid = false)
  509. {
  510. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  511. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  512. if (!empty($client)) {
  513. return view('app.video.call-minimal', compact('session', 'client'));
  514. }
  515. return view('app.video.call-agora-v2', compact('session', 'client'));
  516. }
  517. // check video page
  518. public function checkVideo(Request $request, $uid)
  519. {
  520. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  521. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  522. $publish = false;
  523. return view('app.video.check-video-minimal', compact('session', 'client'));
  524. }
  525. public function getParticipantInfo(Request $request)
  526. {
  527. $sid = intval($request->get('uid')) - 1000000;
  528. $session = AppSession::where('id', $sid)->first();
  529. $result = [
  530. "type" => '',
  531. "name" => ''
  532. ];
  533. if ($session) {
  534. $result["type"] = $session->session_type;
  535. switch ($session->session_type) {
  536. case 'PRO':
  537. $pro = Pro::where('id', $session->pro_id)->first();
  538. $result["name"] = $pro->displayName();
  539. break;
  540. case 'CLIENT':
  541. $client = Client::where('id', $session->client_id)->first();
  542. $result["name"] = $client->displayName();
  543. break;
  544. }
  545. }
  546. return json_encode($result);
  547. }
  548. // ajax ep used by the video page
  549. // this is needed bcoz meet() is used not
  550. // just for the client passed to the view
  551. public function getOpentokSessionKey(Request $request, $uid)
  552. {
  553. $client = Client::where('uid', $uid)->first();
  554. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  555. }
  556. // poll to check if there are patients with active mcp requests
  557. public function getPatientsInQueue(Request $request)
  558. {
  559. $myInitiatives = $this->performer->pro->initiatives;
  560. if ($myInitiatives) {
  561. $myInitiatives = strtoupper($myInitiatives);
  562. }
  563. $myInitiativesList = explode('|', $myInitiatives);
  564. $myForeignLanguages = $this->performer->pro->foreign_languages;
  565. if ($myForeignLanguages) {
  566. $myForeignLanguages = strtoupper($myForeignLanguages);
  567. }
  568. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  569. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  570. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  571. })
  572. ->where(function ($query) use ($myForeignLanguagesList) {
  573. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  574. })->limit(3)->get();
  575. $results = [];
  576. foreach ($clients as $client) {
  577. $results[] = [
  578. 'clientUid' => $client->uid,
  579. 'name' => $client->displayName(),
  580. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  581. ];
  582. }
  583. return json_encode($results);
  584. }
  585. public function currentWork(Request $request)
  586. {
  587. return view('app/current-work');
  588. }
  589. public function calendar(Request $request, $proUid = null)
  590. {
  591. $pros = Pro::all();
  592. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  593. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  594. $accessibleProIds = [];
  595. foreach ($accessiblePros as $accessiblePro) {
  596. $accessibleProIds[] = $accessiblePro->id;
  597. }
  598. $accessibleProIds[] = $this->pro->id;
  599. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  600. }
  601. return view('app.practice-management.calendar', compact('pros'));
  602. }
  603. public function cellularDeviceManager(Request $request, $proUid = null)
  604. {
  605. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  606. $performerPro = $this->performer->pro;
  607. $targetPro = null;
  608. $expectedForHcp = null;
  609. if ($performerPro->pro_type == 'ADMIN') {
  610. $targetPro = Pro::where('uid', $proUid)->first();
  611. } else {
  612. $targetPro = $performerPro;
  613. }
  614. $clients = [];
  615. if ($targetPro) {
  616. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(20);
  617. } else {
  618. $clients = Client::orderBy('created_at', 'desc')->paginate(20);
  619. }
  620. return view('app.practice-management.cellular-device-manager', compact('clients', 'targetPro', 'proUid'));
  621. }
  622. public function treatmentServiceUtil(Request $request)
  623. {
  624. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  625. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  626. $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"));
  627. return view('app.practice-management.treatment-services-util', compact(
  628. 'view_treatment_service_utilization_org',
  629. 'view_treatment_service_utilization',
  630. 'view_treatment_service_utilization_by_patient'));
  631. }
  632. public function processingBillMatrix(Request $request, $proUid = null)
  633. {
  634. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  635. $performerPro = $this->performer->pro;
  636. $targetPro = null;
  637. if ($performerPro->pro_type == 'ADMIN') {
  638. $targetPro = Pro::where('uid', $proUid)->first();
  639. } else {
  640. $targetPro = $performerPro;
  641. }
  642. $bills = Bill::where('is_cancelled', false);
  643. if(!$request->input('t') || $request->input('t') === 'hcp') {
  644. $bills = $bills
  645. ->where('has_hcp_been_paid', false)
  646. ->where('hcp_expected_payment_amount', '>', 0)
  647. ->where('is_signed_by_hcp', true);
  648. if ($targetPro) {
  649. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  650. }
  651. if($request->input('c')) {
  652. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = hcp_company_pro_id) = ' . $request->input('c'));
  653. }
  654. }
  655. else if($request->input('t') === 'na') {
  656. $bills = $bills
  657. ->where('has_generic_pro_been_paid', false)
  658. ->where('generic_pro_expected_payment_amount', '>', 0)
  659. ->where('is_signed_by_generic_pro', true);
  660. if ($targetPro) {
  661. $bills = $bills->where('generic_pro_id', $targetPro->id);
  662. }
  663. if($request->input('c')) {
  664. $bills = $bills->whereRaw('(SELECT company_id from company_pro where id = generic_company_pro_id) = ' . $request->input('c'));
  665. }
  666. }
  667. $filter = $request->input('f');
  668. switch ($filter) {
  669. case 'verified':
  670. $bills = $bills->where('is_verified', true);
  671. break;
  672. case 'not-verified':
  673. $bills = $bills->where('is_verified', false);
  674. break;
  675. }
  676. $filter = $request->input('bs');
  677. if ($filter) {
  678. $bills = $bills->where('balance_post_date', '>=', $filter);
  679. }
  680. $filter = $request->input('be');
  681. if ($filter) {
  682. $bills = $bills->where('balance_post_date', '<=', $filter);
  683. }
  684. $filter = $request->input('s');
  685. if ($filter) {
  686. $bills = $bills->where('code', '=', $filter);
  687. }
  688. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  689. $companies = Company::where('is_active', true)->orderBy('name')->get();
  690. $codes = DB::select(DB::raw("SELECT code, count(*) as count FROM bill WHERE is_cancelled IS FALSE GROUP BY code ORDER BY code"));
  691. $viewData = [
  692. 'bills' => $bills,
  693. 'targetPro' => $targetPro,
  694. 'performerPro' => $performerPro,
  695. 'proUid' => $proUid,
  696. 'companies' => $companies,
  697. 'codes' => $codes
  698. ];
  699. return view('app.practice-management.processing-bill-matrix', $viewData);
  700. }
  701. public function hcpNoteActivity(Request $request)
  702. {
  703. $filters = $request->all();
  704. $from_date = $request->get('from_date');
  705. if(!$from_date) {
  706. $from_date = '2010-01-01';
  707. }
  708. $to_date = $request->get('to_date');
  709. if(!$to_date){
  710. $to_date = '2900-12-31';
  711. }
  712. //TODO sanitize inputs
  713. $proUid = $request->get('pro-uid');
  714. $now = Carbon::now();
  715. $startOfCurrentMonth = $now->startOfMonth('Y-m-d');
  716. $endOfCurrentMonth = $now->endOfMonth()->format('Y-m-d');
  717. $rows = DB::table('pro')
  718. ->selectRaw(" *,
  719. (SELECT id FROM note WHERE note.hcp_pro_id = pro.id AND is_cancelled = false ORDER BY created_at DESC LIMIT 1) as last_note_id,
  720. (SELECT note.created_at FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false ORDER BY note.created_at DESC LIMIT 1) as last_note_created_at,
  721. (SELECT COUNT(*) FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false AND created_at >= '${startOfCurrentMonth}'::DATE AND created_at <= '${endOfCurrentMonth}'::DATE ) AS notes_this_month,
  722. (SELECT COUNT(*) FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false AND created_at >= '${from_date}'::DATE AND created_at <= '${to_date}'::DATE ) AS notes_this_period");
  723. $prosIDs = $request->get('pros_ids');
  724. if($prosIDs){
  725. $pros =
  726. $rows = $rows->whereIn('pro.id', $prosIDs);
  727. }
  728. $rows = $rows->orderBy('name_first');
  729. $rows = $rows->paginate(50);
  730. return view('app.practice-management.hcp-note-activity', compact('rows', 'filters'));
  731. }
  732. public function proFinancials(Request $request, $proUid = null)
  733. {
  734. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  735. $performerPro = $this->performer->pro;
  736. $targetPro = null;
  737. if ($performerPro->pro_type == 'ADMIN') {
  738. $targetPro = Pro::where('uid', $proUid)->first();
  739. } else {
  740. $targetPro = $performerPro;
  741. }
  742. $fPros = Pro::whereNotNull('balance');
  743. if($targetPro) {
  744. $fPros = $fPros->where('uid', $targetPro->uid);
  745. }
  746. $fPros = $fPros
  747. ->where('balance', '>', 0)
  748. ->orderBy('name_last')
  749. ->orderBy('name_first')
  750. ->paginate();
  751. return view('app.practice-management.pro-financials', compact('fPros', 'targetPro'));
  752. }
  753. public function hcpBillMatrix(Request $request, $proUid = null)
  754. {
  755. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  756. $performerPro = $this->performer->pro;
  757. $targetPro = null;
  758. $allPros = [];
  759. $expectedForHcp = null;
  760. if ($performerPro->pro_type == 'ADMIN') {
  761. $allPros = Pro::all();
  762. $targetPro = Pro::where('uid', $proUid)->first();
  763. } else {
  764. $targetPro = $performerPro;
  765. }
  766. $rows = [];
  767. if ($targetPro) {
  768. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  769. } else {
  770. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  771. }
  772. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  773. }
  774. public function billingManager(Request $request, $proUid = null)
  775. {
  776. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  777. $performerPro = $this->performer->pro;
  778. $targetPro = null;
  779. $allPros = [];
  780. $expectedForHcp = null;
  781. if ($performerPro->pro_type == 'ADMIN') {
  782. $allPros = Pro::all();
  783. $targetPro = Pro::where('uid', $proUid)->first();
  784. } else {
  785. $targetPro = $performerPro;
  786. }
  787. $notes = [];
  788. if ($targetPro) {
  789. $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;
  790. $notes = Note::where('hcp_pro_id', $targetPro->id);
  791. } else {
  792. $notes = Note::where('id', '>', 0);
  793. }
  794. if($request->input('date')) {
  795. $notes = $notes->where('effective_dateest', $request->input('date'));
  796. }
  797. $filters = [];
  798. $filters['bills_created'] = $request->input('bills_created');
  799. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  800. $filters['bills_resolved'] = $request->input('bills_resolved');
  801. $filters['bills_closed'] = $request->input('bills_closed');
  802. $filters['claims_created'] = $request->input('claims_created');
  803. $filters['claims_closed'] = $request->input('claims_closed');
  804. if ($filters['bills_created']) {
  805. $notes->where(
  806. 'bill_total_expected',
  807. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  808. 0);
  809. }
  810. if ($filters['is_billing_marked_done']) {
  811. $notes->where(
  812. 'is_billing_marked_done',
  813. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  814. true);
  815. }
  816. if ($filters['bills_resolved']) {
  817. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  818. if ($filters['bills_resolved'] === 'yes') {
  819. $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');
  820. } elseif ($filters['bills_resolved'] === 'no') {
  821. $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');
  822. }
  823. }
  824. if ($filters['bills_closed']) {
  825. $notes->where(
  826. 'is_bill_closed',
  827. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  828. true);
  829. }
  830. if ($filters['claims_created']) {
  831. $notes->where(
  832. 'claim_total_expected',
  833. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  834. 0);
  835. }
  836. if ($filters['claims_closed']) {
  837. $notes->where(
  838. 'is_claim_closed',
  839. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  840. true);
  841. }
  842. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(20);
  843. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  844. }
  845. public function remoteMonitoring(Request $request) {
  846. $performer = $this->performer();
  847. $ym = ($request->input('y') ?: 'Y') . '-' . ($request->input('m') ?: 'm');
  848. $careMonthStart = date($ym . '-01');
  849. // filters
  850. $filters = '';
  851. $fmd = $request->input('fmd');
  852. if($fmd && $fmd !== 'all') {
  853. switch($fmd) {
  854. case 'lt16':
  855. $filters .= ' AND (care_month.number_of_days_with_remote_measurements < 16 OR care_month.number_of_days_with_remote_measurements IS NULL) ';
  856. break;
  857. case 'gte16':
  858. $filters .= ' AND (care_month.number_of_days_with_remote_measurements >= 16 AND care_month.number_of_days_with_remote_measurements IS NOT NULL) ';
  859. break;
  860. }
  861. }
  862. $fcomm = $request->input('fcomm');
  863. if($fcomm && $fcomm !== 'all') {
  864. switch($fcomm) {
  865. case 'not-done':
  866. $filters .= ' AND (care_month.has_anyone_interacted_with_client_about_rm_outside_note = FALSE OR care_month.has_anyone_interacted_with_client_about_rm_outside_note IS NULL) ';
  867. break;
  868. case 'done':
  869. $filters .= ' AND (care_month.has_anyone_interacted_with_client_about_rm_outside_note = TRUE AND care_month.has_anyone_interacted_with_client_about_rm_outside_note IS NOT NULL) ';
  870. break;
  871. }
  872. }
  873. $c = $request->input('c');
  874. if($c) {
  875. $filters .= ' AND client.uid = \'' . $request->input('c') . '\' ';
  876. }
  877. $patients = DB::select(
  878. DB::raw(
  879. "
  880. SELECT client.name_first, client.name_last, client.uid as client_uid,
  881. care_month.uid as care_month_uid,
  882. care_month.id as care_month_id,
  883. care_month.start_date,
  884. care_month.number_of_days_with_remote_measurements,
  885. care_month.has_anyone_interacted_with_client_about_rm_outside_note,
  886. care_month.rm_num_measurements_not_stamped_by_mcp,
  887. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  888. care_month.rm_num_measurements_not_stamped_by_rmm,
  889. care_month.rm_num_measurements_not_stamped_by_rme,
  890. client.mcp_pro_id,
  891. client.default_na_pro_id,
  892. client.rmm_pro_id,
  893. client.rme_pro_id,
  894. client.cell_number
  895. FROM care_month join client on care_month.client_id = client.id
  896. WHERE
  897. EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
  898. AND EXTRACT(YEAR from care_month.start_date) = " . ($request->input('y') ?: 'EXTRACT(YEAR from now())') . "
  899. {$filters}
  900. ORDER BY care_month.number_of_days_with_remote_measurements DESC NULLS LAST, client.name_first ASC, client.name_last ASC
  901. "
  902. )
  903. );
  904. $timestamp = strtotime(date('Y-m-d'));
  905. $daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
  906. return view('app.practice-management.remote-monitoring', compact('patients', 'daysRemaining', 'careMonthStart'));
  907. }
  908. public function remoteMonitoringMeasurements(Request $request, CareMonth $careMonth) {
  909. $performer = $this->performer();
  910. $measurements = DB::select(
  911. DB::raw(
  912. "
  913. SELECT measurement.label,
  914. measurement.ts,
  915. measurement.effective_date,
  916. measurement.sbp_mm_hg,
  917. measurement.dbp_mm_hg,
  918. measurement.value_pulse,
  919. measurement.value_irregular,
  920. measurement.numeric_value,
  921. measurement.value,
  922. measurement.uid,
  923. client.name_first,
  924. client.name_last,
  925. client.mcp_pro_id,
  926. client.default_na_pro_id,
  927. client.rmm_pro_id,
  928. client.rme_pro_id
  929. FROM measurement RIGHT JOIN client on measurement.client_id = client.id
  930. WHERE
  931. client.id = {$careMonth->client_id}
  932. AND measurement.label IS NOT NULL
  933. AND measurement.label NOT IN ('SBP', 'DBP')
  934. AND (measurement.is_cellular_zero = FALSE or measurement.is_cellular_zero IS NULL)
  935. AND measurement.is_removed IS FALSE
  936. AND measurement.client_bdt_measurement_id IS NOT NULL
  937. AND measurement.care_month_id = {$careMonth->id}
  938. AND (
  939. (client.mcp_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_mcp IS FALSE)
  940. OR (client.rmm_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rmm IS FALSE)
  941. OR (client.rme_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_rme IS FALSE)
  942. OR (client.default_na_pro_id = {$performer->pro->id} AND measurement.has_been_stamped_by_non_hcp IS FALSE)
  943. )
  944. ORDER BY ts DESC
  945. "
  946. )
  947. );
  948. return view('app.practice-management.remote-monitoring-measurements', compact('careMonth', 'measurements'));
  949. }
  950. public function billMatrix(Request $request)
  951. {
  952. $bClients = [];
  953. $bHCPPros = [];
  954. $bNAPros = [];
  955. $filters = [];
  956. $filters['client'] = $request->input('client');
  957. $filters['service'] = $request->input('service');
  958. $filters['hcp'] = $request->input('hcp');
  959. $filters['hcp_paid'] = $request->input('hcp_paid');
  960. $filters['expected_op'] = $request->input('expected_op');
  961. $filters['expected_value'] = $request->input('expected_value');
  962. $filters['paid_op'] = $request->input('paid_op');
  963. $filters['paid_value'] = $request->input('paid_value');
  964. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  965. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  966. $filters['hcp_sign'] = $request->input('hcp_sign');
  967. $filters['verified'] = $request->input('verified');
  968. $filters['cancelled'] = $request->input('cancelled');
  969. $bills = Bill::orderBy('effective_date')->paginate();
  970. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  971. }
  972. public function medicarePartBClaims(Request $request)
  973. {
  974. $medicarePartBOnly = $request->get("medicare_part_b");
  975. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  976. //Only medicare claims
  977. $claims = [];
  978. foreach ($allClaims as $claim) {
  979. if ($claim->client != null && $claim->client->getPrimaryCoverageStatus() == 'YES' && !$claim->edi) {
  980. $claims[] = $claim;
  981. }
  982. }
  983. $claimEDIs = ClaimEDI::all();
  984. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  985. }
  986. // Generate PDF
  987. public function downloadClaims()
  988. {
  989. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  990. view()->share('claims', $claims);
  991. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  992. return $pdf->download('pdf_file.pdf');
  993. }
  994. public function tickets(Request $request, $proUid = null)
  995. {
  996. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  997. return view('app.practice-management.tickets', compact('tickets'));
  998. }
  999. public function supplyOrders(Request $request)
  1000. {
  1001. // counts
  1002. $counts = $this->getSupplyOrderCounts();
  1003. // so clients
  1004. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  1005. $soClientIDs = array_map(function ($_x) {
  1006. return $_x->client_id;
  1007. }, $soClientIDs);
  1008. $soClients = Client::whereIn('id', $soClientIDs)->get();
  1009. // so products
  1010. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  1011. $soProductIDs = array_map(function ($_x) {
  1012. return $_x->product_id;
  1013. }, $soProductIDs);
  1014. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  1015. $filters = [];
  1016. $filters['client'] = $request->input('client');
  1017. $filters['product'] = $request->input('product');
  1018. $filters['reason'] = $request->input('reason');
  1019. $filters['cu_memo'] = $request->input('cu_memo');
  1020. $filters['pro_sign'] = $request->input('pro_sign');
  1021. $filters['client_sign'] = $request->input('client_sign');
  1022. $filters['shipment'] = $request->input('shipment');
  1023. $filters['lot_number'] = $request->input('lot_number');
  1024. $filters['imei'] = $request->input('imei');
  1025. $filters['cancelled'] = $request->input('cancelled');
  1026. $supplyOrders = SupplyOrder::where('id', '>', 0);
  1027. // apply filters
  1028. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  1029. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  1030. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  1031. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  1032. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  1033. if ($filters['client_sign']) {
  1034. if ($filters['client_sign'] === 'signed')
  1035. $supplyOrders->where('is_signed_by_client', true);
  1036. elseif ($filters['client_sign'] === 'waived')
  1037. $supplyOrders->where('is_client_signature_waived', true);
  1038. else
  1039. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  1040. }
  1041. if ($filters['shipment']) {
  1042. if ($filters['shipment'] === 'not_cleared_for_shipment')
  1043. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  1044. elseif ($filters['shipment'] === 'cleared_for_shipment')
  1045. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  1046. else
  1047. $supplyOrders
  1048. ->whereNotNull('shipment_id')
  1049. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  1050. }
  1051. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  1052. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  1053. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  1054. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  1055. return view('app.practice-management.supply-orders',
  1056. compact('supplyOrders', 'filters',
  1057. 'soClients', 'soProducts', 'counts'
  1058. )
  1059. );
  1060. }
  1061. public function shipments(Request $request, $filter = null)
  1062. {
  1063. // counts
  1064. $counts = $this->getShipmentCounts();
  1065. // so clients
  1066. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  1067. $shClientIDs = array_map(function ($_x) {
  1068. return $_x->client_id;
  1069. }, $shClientIDs);
  1070. $shClients = Client::whereIn('id', $shClientIDs)->get();
  1071. $shipments = Shipment::where('id', '>', 0);
  1072. $filters = [];
  1073. $filters['client'] = $request->input('client');
  1074. $filters['courier'] = $request->input('courier');
  1075. $filters['tracking_num'] = $request->input('tracking_num');
  1076. $filters['label'] = $request->input('label');
  1077. $filters['status'] = $request->input('status');
  1078. $filters['cancelled'] = $request->input('cancelled');
  1079. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  1080. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  1081. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  1082. if ($filters['label']) {
  1083. if ($filters['label'] === 'yes')
  1084. $shipments->whereNotNull('label_system_file_id');
  1085. else
  1086. $shipments->whereNull('label_system_file_id');
  1087. }
  1088. if ($filters['status']) $shipments->where('status', $filters['status']);
  1089. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  1090. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  1091. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  1092. }
  1093. public function cellularMeasurements(Request $request)
  1094. {
  1095. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  1096. return view('app.practice-management.cellular-measurements', compact('measurements'));
  1097. }
  1098. // v2 supply-orders & shipments management (wh)
  1099. public function supplyOrdersReadyToShip(Request $request)
  1100. {
  1101. $counts = $this->getSupplyOrderCounts();
  1102. $supplyOrders = SupplyOrder
  1103. ::where('is_cleared_for_shipment', true)
  1104. ->where('is_cancelled', false)
  1105. ->whereNull('shipment_id')
  1106. ->join('client', 'client.id', '=', 'supply_order.client_id')
  1107. ->orderBy('client.name_last', 'ASC')
  1108. ->orderBy('client.name_first', 'ASC')
  1109. ->orderBy('supply_order.client_id', 'ASC')
  1110. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1111. ->orderBy('supply_order.created_at', 'ASC')
  1112. ->select('supply_order.*')
  1113. ->paginate();
  1114. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  1115. }
  1116. public function supplyOrdersShipmentUnderway(Request $request)
  1117. {
  1118. $counts = $this->getSupplyOrderCounts();
  1119. $supplyOrders = SupplyOrder
  1120. ::where('is_cancelled', false)
  1121. ->whereNotNull('shipment_id')
  1122. ->orderBy('client_id', 'ASC')
  1123. ->orderBy('mailing_address_full', 'ASC')
  1124. ->orderBy('created_at', 'ASC')
  1125. ->paginate();
  1126. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  1127. }
  1128. public function supplyOrdersHanging(Request $request)
  1129. {
  1130. $counts = $this->getSupplyOrderCounts();
  1131. $supplyOrders = SupplyOrder
  1132. ::select('supply_order.*')
  1133. ->leftJoin('shipment', function($join) {
  1134. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1135. })
  1136. ->where('shipment.status', 'CANCELLED')
  1137. ->where('supply_order.is_cancelled', false)
  1138. ->orderBy('supply_order.client_id', 'ASC')
  1139. ->orderBy('supply_order.mailing_address_full', 'ASC')
  1140. ->orderBy('supply_order.created_at', 'ASC')
  1141. ->paginate();
  1142. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  1143. }
  1144. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  1145. {
  1146. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  1147. ->where('is_cancelled', true)
  1148. ->where('is_cancellation_acknowledged', false)
  1149. ->orderBy('created_at', 'desc')
  1150. ->paginate();
  1151. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  1152. }
  1153. public function supplyOrdersUnsigned(Request $request)
  1154. {
  1155. $supplyOrders = SupplyOrder
  1156. ::where('is_cancelled', false)
  1157. ->where('is_signed_by_pro', false)
  1158. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  1159. ->orderBy('created_at', 'desc')
  1160. ->paginate();
  1161. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  1162. }
  1163. private function getSupplyOrderCounts()
  1164. {
  1165. return [
  1166. "supplyOrders" => SupplyOrder::count(),
  1167. "supplyOrdersReadyToShip" => SupplyOrder
  1168. ::where('is_cleared_for_shipment', true)
  1169. ->where('is_cancelled', false)
  1170. ->whereNull('shipment_id')->count(),
  1171. "supplyOrdersShipmentUnderway" => SupplyOrder
  1172. ::where('is_cancelled', false)
  1173. ->whereNotNull('shipment_id')->count(),
  1174. "supplyOrdersHanging" => SupplyOrder
  1175. ::leftJoin('shipment', function($join) {
  1176. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  1177. })
  1178. ->where('shipment.status', 'CANCELLED')
  1179. ->where('supply_order.is_cancelled', false)
  1180. ->count(),
  1181. ];
  1182. }
  1183. public function shipmentsReadyToPrint(Request $request)
  1184. {
  1185. $counts = $this->getShipmentCounts();
  1186. $shipments = Shipment
  1187. ::where('is_cancelled', false)
  1188. ->where('status', 'CREATED')
  1189. ->orderBy('created_at', 'ASC')
  1190. ->paginate();
  1191. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  1192. }
  1193. public function shipmentsShipmentUnderway(Request $request)
  1194. {
  1195. $counts = $this->getShipmentCounts();
  1196. $shipments = Shipment
  1197. ::where('is_cancelled', false)
  1198. ->where('status', 'PRINTED')
  1199. ->orderBy('created_at', 'ASC')
  1200. ->paginate();
  1201. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  1202. }
  1203. private function getShipmentCounts()
  1204. {
  1205. return [
  1206. "shipments" => Shipment::count(),
  1207. "shipmentsReadyToPrint" => Shipment
  1208. ::where('is_cancelled', false)
  1209. ->where('status', 'CREATED')
  1210. ->count(),
  1211. "shipmentsWaitingForPicker" => Shipment
  1212. ::where('is_cancelled', false)
  1213. ->where('status', 'PRINTED')
  1214. ->count()
  1215. ];
  1216. }
  1217. public function shipment(Request $request, Shipment $shipment)
  1218. {
  1219. return view('app.practice-management.shipment', compact('shipment'));
  1220. }
  1221. public function shipmentsMultiPrint(Request $request, $ids)
  1222. {
  1223. $ids = array_map(function ($_x) {
  1224. return intval($_x);
  1225. }, explode("|", $ids));
  1226. $shipments = Shipment::whereIn('id', $ids)->get();
  1227. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  1228. }
  1229. public function patientClaimSummary(Request $request, $proUid = null)
  1230. {
  1231. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  1232. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  1233. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  1234. $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;
  1235. $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;
  1236. $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;
  1237. $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;
  1238. $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;
  1239. $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;
  1240. $performerPro = $this->performer->pro;
  1241. $allPros = [];
  1242. if ($performerPro->pro_type == 'ADMIN') {
  1243. $allPros = Pro::all();
  1244. } else {
  1245. $allPros = [$performerPro];
  1246. }
  1247. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  1248. $patientsQuery = Client::where('is_dummy', '=', false)
  1249. ->whereNull('shadow_pro_id')
  1250. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  1251. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  1252. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  1253. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  1254. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  1255. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  1256. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  1257. if ($proUid) {
  1258. $mcpPro = Pro::where('uid', $proUid)->first();
  1259. if ($mcpPro) {
  1260. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  1261. }
  1262. }
  1263. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  1264. $patientsQuery->orderBy('notes_total', 'DESC');
  1265. $patients = $patientsQuery->paginate(50);
  1266. $data = [
  1267. 'patients' => $patients,
  1268. 'proUid' => $proUid,
  1269. 'allPros' => $allPros,
  1270. 'notesTotal' => $notesTotal,
  1271. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  1272. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  1273. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  1274. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  1275. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  1276. 'patientsTotal' => $patientsTotal,
  1277. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  1278. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  1279. ];
  1280. return view('app.practice-management.patient-claim-summary', $data);
  1281. }
  1282. public function claims(Request $request){
  1283. $status = $request->get('status');
  1284. $claims = [];
  1285. if(!$status){
  1286. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  1287. }else{
  1288. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  1289. }
  1290. return view('app.practice-management.claims', compact('claims', 'status'));
  1291. }
  1292. public function processClaims(Request $request) {
  1293. $status = '';
  1294. $q = $request->input('q') ? $request->input('q') : '';
  1295. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  1296. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  1297. $params = [
  1298. 'q' => '%' . $q . '%',
  1299. 'from' => $from,
  1300. 'to' => $to
  1301. ];
  1302. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  1303. if($hcpPro) {
  1304. $params['hcp'] = $hcpPro->id;
  1305. }
  1306. $claims = DB::select(DB::raw("
  1307. SELECT claim.uid AS uid,
  1308. DATE(claim.created_at) AS created,
  1309. claim.status,
  1310. client.uid AS client_uid,
  1311. client.cell_number AS client_phone,
  1312. (client.name_last || ' ' || client.name_first) AS client ,
  1313. client.chart_number AS client_chart_number,
  1314. cp.id AS claim_pro_id,
  1315. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  1316. sp.id AS status_pro_id,
  1317. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  1318. note.uid AS note_uid,
  1319. note.method,
  1320. note.new_or_fu_or_na,
  1321. care_month.uid AS care_month_uid,
  1322. -- claim.status_updated_at,
  1323. (DATE(claim.status_updated_at) || ' ' ||
  1324. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  1325. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  1326. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  1327. (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,
  1328. ROUND(claim.expected_total, 3) as expected_total
  1329. FROM claim
  1330. join client on claim.client_id = client.id
  1331. join pro cp on claim.pro_id = cp.id
  1332. left join note on claim.note_id = note.id
  1333. left join care_month on claim.care_month_id = care_month.id
  1334. left join app_session on claim.status_updated_by_session_id = app_session.id
  1335. left join pro sp on app_session.pro_id = sp.id
  1336. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  1337. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'" . ($request->input('show-submitted') ? "" : ", 'SUBMITTED'") . "))
  1338. -- AND claim.current_version_id IS NOT NULL
  1339. AND (client.name_first ILIKE :q OR
  1340. client.name_last ILIKE :q OR
  1341. client.chart_number ILIKE :q OR
  1342. client.mcn ILIKE :q)
  1343. AND (claim.created_at >= :from AND claim.created_at <= :to)
  1344. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  1345. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  1346. ORDER BY claim.created_at DESC
  1347. --OFFSET 0 LIMIT 15
  1348. "), $params);
  1349. if($request->input('json')) {
  1350. return json_encode($claims);
  1351. }
  1352. return view('app.practice-management.process-claims', compact('claims', 'status'));
  1353. }
  1354. public function rmLaunchAndClean(Request $request) {
  1355. $keyNumbers = [];
  1356. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_40'] = CareMonth::where('is_bill_closed', false)
  1357. ->whereNotNull('company_pro_id')
  1358. ->whereNotNull('company_pro_payer_id')
  1359. ->whereNotNull('company_location_id')
  1360. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1361. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1362. ->where('rm_total_time_in_seconds', '>=', 4200) // at 4200 (70 minutes, it becomes eligible for plus40)
  1363. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1364. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  1365. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS TRUE OR has_mcp_rm_interacted_by_note IS TRUE )')
  1366. ->count();
  1367. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_40_ifHadInteraction'] = CareMonth::where('is_bill_closed', false)
  1368. ->whereNotNull('company_pro_id')
  1369. ->whereNotNull('company_pro_payer_id')
  1370. ->whereNotNull('company_location_id')
  1371. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1372. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1373. ->where('rm_total_time_in_seconds', '>=', 4200) // at 4200 (70 minutes, it becomes eligible for plus40)
  1374. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1375. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  1376. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS NOT TRUE AND has_mcp_rm_interacted_by_note IS NOT TRUE AND is_rm_interaction_waived IS NOT TRUE )')
  1377. ->count();
  1378. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_20'] = CareMonth::where('is_bill_closed', false)
  1379. ->whereNotNull('company_pro_id')
  1380. ->whereNotNull('company_pro_payer_id')
  1381. ->whereNotNull('company_location_id')
  1382. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1383. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1384. ->where('rm_total_time_in_seconds', '<', 4200)
  1385. ->where('rm_total_time_in_seconds', '>=', 3000) // at 3000 (50 minutes, it becomes eligible for plus20)
  1386. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1387. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  1388. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS TRUE OR has_mcp_rm_interacted_by_note IS TRUE)')
  1389. ->count();
  1390. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP_PLUS_20_ifHadInteraction'] = CareMonth::where('is_bill_closed', false)
  1391. ->whereNotNull('company_pro_id')
  1392. ->whereNotNull('company_pro_payer_id')
  1393. ->whereNotNull('company_location_id')
  1394. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1395. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1396. ->where('rm_total_time_in_seconds', '<', 4200)
  1397. ->where('rm_total_time_in_seconds', '>=', 3000) // at 3000 (50 minutes, it becomes eligible for plus20)
  1398. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1399. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  1400. ->whereRaw('(has_anyone_interacted_with_client_about_rm_outside_note IS NOT TRUE AND has_mcp_rm_interacted_by_note IS NOT TRUE AND is_rm_interaction_waived IS NOT TRUE )')
  1401. ->count();
  1402. $keyNumbers['careMonthsEligibleForBillGeneration_RM30_HCP'] = CareMonth::where('is_bill_closed', false)
  1403. ->whereNotNull('company_pro_id')
  1404. ->whereNotNull('company_pro_payer_id')
  1405. ->whereNotNull('company_location_id')
  1406. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1407. ->where('rm_total_time_in_seconds_by_mcp', '>=', 1800)
  1408. ->whereRaw('is_rm_time_waived IS NOT TRUE')
  1409. ->whereRaw('(rm_total_time_in_seconds < 3000 OR (rm_total_time_in_seconds >= 3000 AND is_rm_interaction_waived IS TRUE))')
  1410. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1411. ->count();
  1412. $keyNumbers['careMonthsEligibleForBillGeneration_RMB'] = CareMonth::where('is_bill_closed', false)
  1413. ->whereNotNull('company_pro_id')
  1414. ->whereNotNull('company_pro_payer_id')
  1415. ->whereNotNull('company_location_id')
  1416. ->whereRaw('(SELECT count(id) FROM care_month_cm_rm_reason WHERE care_month_id = care_month.id) > 0')
  1417. ->whereRaw('is_rm_time_waived IS TRUE')
  1418. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1419. ->count();
  1420. $keyNumbers['careMonthsWith16OrMoreMeasurementDays'] = CareMonth::where('is_bill_closed', false)
  1421. ->where('number_of_days_with_remote_measurements', '>=', 16 )
  1422. ->count();
  1423. $careMonthsWith16PlusMeasurements = CareMonth::where('is_bill_closed', false)
  1424. ->where('number_of_days_with_remote_measurements', '>=', 16 )->get();
  1425. return view('app.practice-management.rm-launch-and-clean', compact('keyNumbers', 'careMonthsWith16PlusMeasurements'));
  1426. }
  1427. public function processNotes(Request $request) {
  1428. $mode = $request->input('mode') ? $request->input('mode') : '1';
  1429. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  1430. DB::enableQueryLog(); // Enable query log
  1431. $test = Note::where('is_cancelled', false)
  1432. ->where('is_signed_by_hcp', true)
  1433. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1434. ->whereNull('current_note_pickup_for_processing_id')
  1435. ->where('is_billing_marked_done', true)
  1436. ->where('is_bill_closed', true)
  1437. ->where('is_claim_closed', true)
  1438. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1439. ->count();
  1440. // var_dump($test);
  1441. // dd(DB::getQueryLog()); // Show results of log
  1442. // exit;
  1443. $counts = [
  1444. "picked" => Note::where('is_cancelled', false)
  1445. ->whereNotNull('current_note_pickup_for_processing_id')
  1446. ->count(),
  1447. "bad" => Note::where('is_cancelled', false)
  1448. ->whereRaw("((detail_json)::json->>'isBad' = 'true')")
  1449. ->count(),
  1450. // not yet signed
  1451. "mode-1" => Note::where('is_cancelled', false)
  1452. ->where('is_signed_by_hcp', false)
  1453. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1454. // ->whereNull('current_note_pickup_for_processing_id')
  1455. ->count(),
  1456. // billing not marked done
  1457. "mode-2" => Note::where('is_cancelled', false)
  1458. ->where('is_signed_by_hcp', true)
  1459. //->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1460. // ->whereNull('current_note_pickup_for_processing_id')
  1461. ->where('is_billing_marked_done', false)
  1462. ->count(),
  1463. // billing not closed
  1464. "mode-3" => 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', false)
  1470. ->count(),
  1471. // claiming not closed
  1472. "mode-4" => Note::where('is_cancelled', false)
  1473. ->where('is_signed_by_hcp', true)
  1474. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1475. // ->whereNull('current_note_pickup_for_processing_id')
  1476. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1477. ->where('is_billing_marked_done', true)
  1478. ->where('is_bill_closed', true)
  1479. ->where('is_claim_closed', false)
  1480. ->count(),
  1481. // has unsubmitted claims
  1482. "mode-5" => Note::where('is_cancelled', false)
  1483. /*->where('is_signed_by_hcp', true)
  1484. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1485. ->whereNull('current_note_pickup_for_processing_id')
  1486. ->where('is_billing_marked_done', true)
  1487. ->where('is_bill_closed', true)
  1488. ->where('is_claim_closed', true)
  1489. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1490. */
  1491. ->where('is_signed_by_hcp', true)
  1492. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1493. // ->whereNull('current_note_pickup_for_processing_id')
  1494. ->where('is_billing_marked_done', true)
  1495. ->where('is_bill_closed', true)
  1496. // ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true')")
  1497. ->where('is_claim_closed', true)
  1498. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0)")
  1499. ->count(),
  1500. // has unsubmitted claims MARKED GOOD!
  1501. "mode-8" => Note::where('is_cancelled', false)
  1502. ->where('is_signed_by_hcp', true)
  1503. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1504. ->whereNull('current_note_pickup_for_processing_id')
  1505. ->where('is_billing_marked_done', true)
  1506. ->where('is_bill_closed', true)
  1507. ->where('is_claim_closed', false)
  1508. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1509. ->count(),
  1510. // all good
  1511. "mode-6" => Note::where('is_cancelled', false)
  1512. ->where('is_signed_by_hcp', true)
  1513. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1514. // ->whereNull('current_note_pickup_for_processing_id')
  1515. ->where('is_billing_marked_done', true)
  1516. ->where('is_bill_closed', true)
  1517. ->where('is_claim_closed', true)
  1518. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id) > 0)")
  1519. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1520. ->count(),
  1521. "mode-7" => Note::where('is_cancelled', false)
  1522. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1523. ->count(),
  1524. ];
  1525. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1526. }
  1527. public function notesProcessingCenter(Request $request) {
  1528. $notes = Note::where('is_cancelled', false);
  1529. // filters
  1530. $proUid = $request->input('proUid');
  1531. if($proUid) {
  1532. $fPro = Pro::where('uid', $proUid)->first();
  1533. if($fPro) {
  1534. $notes = $notes->where('hcp_pro_id', $fPro->id);
  1535. }
  1536. }
  1537. $proSigned = $request->input('proSigned');
  1538. switch($proSigned) {
  1539. case 'yes': $notes = $notes->where('is_signed_by_hcp', true); break;
  1540. case 'no': $notes = $notes->where('is_signed_by_hcp', '!=', true); break;
  1541. }
  1542. $billingMarkedDone = $request->input('billingMarkedDone');
  1543. switch($billingMarkedDone) {
  1544. case 'yes': $notes = $notes->where('is_billing_marked_done', true); break;
  1545. case 'no': $notes = $notes->where('is_billing_marked_done', '!=', true); break;
  1546. }
  1547. $billingClosed = $request->input('billingClosed');
  1548. switch($billingClosed) {
  1549. case 'yes': $notes = $notes->where('is_bill_closed', true); break;
  1550. case 'no': $notes = $notes->where('is_bill_closed', '!=', true); break;
  1551. }
  1552. $claimingClosed = $request->input('claimingClosed');
  1553. switch($claimingClosed) {
  1554. case 'yes': $notes = $notes->where('is_claim_closed', true); break;
  1555. case 'no': $notes = $notes->where('is_claim_closed', '!=', true); break;
  1556. }
  1557. $allClaimsSubmitted = $request->input('allClaimsSubmitted');
  1558. if($allClaimsSubmitted) {
  1559. // TODO
  1560. }
  1561. $goodBad = $request->input('goodBad');
  1562. switch($goodBad) {
  1563. case 'good': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' = 'false'"); break;
  1564. case 'bad': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' = 'true'"); break;
  1565. case 'unclassified': $notes = $notes->whereRaw("(detail_json)::json->>'isBad' is null"); break;
  1566. }
  1567. $startDate = $request->input('startDate');
  1568. if($startDate) {
  1569. $notes = $notes->where('effective_dateest', '>=', $startDate);
  1570. }
  1571. $endDate = $request->input('endDate');
  1572. if($endDate) {
  1573. $notes = $notes->where('effective_dateest', '<=', $endDate);
  1574. }
  1575. $mcPartB = $request->input('mcPartB');
  1576. if($mcPartB) {
  1577. // TODO
  1578. }
  1579. $notes = $notes->orderBy('effective_dateest')->paginate(10);
  1580. return view('app.practice-management.notes-processing-center', compact('notes'));
  1581. }
  1582. public function getNextNote(Request $request, $mode)
  1583. {
  1584. $note = null;
  1585. switch (+$mode) {
  1586. case 1:
  1587. $note = Note::where('is_cancelled', false)
  1588. ->where('is_signed_by_hcp', false)
  1589. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1590. ->whereNull('current_note_pickup_for_processing_id')
  1591. ->orderBy('effective_dateest', 'DESC')
  1592. ->first();
  1593. break;
  1594. case 2:
  1595. $note = Note::where('is_cancelled', false)
  1596. ->where('is_signed_by_hcp', true)
  1597. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1598. ->whereNull('current_note_pickup_for_processing_id')
  1599. ->where('is_billing_marked_done', false)
  1600. ->orderBy('effective_dateest', 'DESC')
  1601. ->first();
  1602. break;
  1603. case 3:
  1604. $note = Note::where('is_cancelled', false)
  1605. ->where('is_signed_by_hcp', true)
  1606. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1607. ->whereNull('current_note_pickup_for_processing_id')
  1608. ->where('is_billing_marked_done', true)
  1609. ->where('is_bill_closed', false)
  1610. ->orderBy('effective_dateest', 'DESC')
  1611. ->first();
  1612. break;
  1613. case 4: // claiming not closed
  1614. DB::enableQueryLog(); // Enable query log
  1615. $note = Note::where('is_cancelled', false)
  1616. ->where('is_signed_by_hcp', true)
  1617. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1618. ->whereNull('current_note_pickup_for_processing_id')
  1619. ->where('is_billing_marked_done', true)
  1620. ->where('is_bill_closed', true)
  1621. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1622. ->where('is_claim_closed', false)
  1623. ->orderBy('effective_dateest', 'DESC')
  1624. ->first();
  1625. // DB::enableQueryLog(); // Enable query log
  1626. // Your Eloquent query executed by using get()
  1627. // dd(DB::getQueryLog()); // Show results of log
  1628. break;
  1629. case 5:
  1630. $note = Note::where('is_cancelled', false)
  1631. ->where('is_signed_by_hcp', true)
  1632. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1633. ->whereNull('current_note_pickup_for_processing_id')
  1634. ->where('is_billing_marked_done', true)
  1635. ->where('is_bill_closed', true)
  1636. ->where('is_claim_closed', true)
  1637. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1638. ->orderBy('effective_dateest', 'DESC')
  1639. ->first();
  1640. break;
  1641. case 8:
  1642. $note = Note::where('is_cancelled', false)
  1643. ->where('is_signed_by_hcp', true)
  1644. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1645. ->whereNull('current_note_pickup_for_processing_id')
  1646. ->where('is_billing_marked_done', true)
  1647. ->where('is_bill_closed', true)
  1648. ->where('is_claim_closed', false)
  1649. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1650. ->orderBy('effective_dateest', 'DESC')
  1651. ->first();
  1652. break;
  1653. }
  1654. if($note) {
  1655. $note->client_uid = $note->client->uid;
  1656. }
  1657. return json_encode($note);
  1658. }
  1659. public function pickedNotes(Request $request) {
  1660. $counts = [
  1661. "unpicked" => Note::where('is_cancelled', false)
  1662. ->whereNull('current_note_pickup_for_processing_id')
  1663. ->count(),
  1664. ];
  1665. $notes = Note::where('is_cancelled', false)
  1666. ->whereNotNull('current_note_pickup_for_processing_id')
  1667. ->orderBy('effective_dateest', 'ASC')
  1668. ->paginate();
  1669. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1670. }
  1671. public function badNotes(Request $request) {
  1672. $counts = [
  1673. "unpicked" => Note::where('is_cancelled', false)
  1674. ->whereNull('current_note_pickup_for_processing_id')
  1675. ->count(),
  1676. ];
  1677. $notes = Note::where('is_cancelled', false)
  1678. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1679. ->orderBy('effective_dateest', 'ASC')
  1680. ->paginate();
  1681. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1682. }
  1683. public function doneNotes(Request $request) {
  1684. $counts = [
  1685. "unpicked" => Note::where('is_cancelled', false)
  1686. ->whereNull('current_note_pickup_for_processing_id')
  1687. ->count(),
  1688. ];
  1689. $notes = Note::where('is_cancelled', false)
  1690. ->where('is_signed_by_hcp', true)
  1691. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1692. ->whereNull('current_note_pickup_for_processing_id')
  1693. ->where('is_billing_marked_done', true)
  1694. ->where('is_bill_closed', true)
  1695. ->where('is_claim_closed', true)
  1696. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1697. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1698. ->orderBy('effective_dateest', 'ASC')
  1699. ->paginate();
  1700. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1701. }
  1702. public function currentMbClaim(Request $request, $claimUid) {
  1703. $claim = Claim::where('uid', $claimUid)->first();
  1704. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1705. }
  1706. public function currentClaimLines(Request $request, $claimUid) {
  1707. $claim = Claim::where('uid', $claimUid)->first();
  1708. if($request->input('json')) {
  1709. foreach ($claim->lines as $line) {
  1710. $line->expected_total = round($line->expected_total, 3);
  1711. $x = $line->claimLineIcds;
  1712. }
  1713. return json_encode($claim->lines);
  1714. }
  1715. return view('app.practice-management._claim-lines', compact('claim'));
  1716. }
  1717. public function packsMultiPrint(Request $request) {
  1718. $packs = Pack
  1719. ::select('pack.*')
  1720. ->leftJoin('shipment', function($join) {
  1721. $join->on('pack.shipment_id', '=', 'shipment.id');
  1722. })
  1723. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1724. ->where(function ($query) {
  1725. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1726. })
  1727. ->whereNotNull('pack.label_system_file_id')
  1728. ->orderBy('pack.created_at', 'ASC')
  1729. ->get();
  1730. return view('app.practice-management.packs-multi-print', compact('packs'));
  1731. }
  1732. public function packsMultiPDF(Request $request, $ids) {
  1733. $ids = array_map(function ($_x) {
  1734. return intval($_x);
  1735. }, explode("|", $ids));
  1736. $packs = Pack::whereIn('id', $ids)->get();
  1737. }
  1738. public function handouts(Request $request) {
  1739. $handouts = Handout::orderBy('display_name')->get();
  1740. return view('app.practice-management.handouts', compact('handouts'));
  1741. }
  1742. private function callJava($request, $endPoint, $data)
  1743. {
  1744. $url = config('stag.backendUrl') . $endPoint;
  1745. $response = Http::asForm()
  1746. ->withHeaders([
  1747. 'sessionKey' => $request->cookie('sessionKey')
  1748. ])
  1749. ->post($url, $data)
  1750. ->body();
  1751. dd($response);
  1752. return $response;
  1753. }
  1754. public function genericBills(Request $request)
  1755. {
  1756. return view('app.practice-management.generic-bills');
  1757. }
  1758. public function billsUnderProcessing(Request $request)
  1759. {
  1760. $bills = Bill::where('is_cancelled', false)
  1761. ->where(function ($query) { // mcp of any client program and program OB pending
  1762. $query
  1763. ->where(function ($_query) {
  1764. $_query->where('hcp_pro_id', $this->pro->id)
  1765. ->where('hcp_expected_payment_amount', '>', 0)
  1766. ->where('has_hcp_been_paid', false)
  1767. ->where('is_signed_by_hcp', true);
  1768. })
  1769. ->orWhere(function ($_query) {
  1770. $_query->where('cm_pro_id', $this->pro->id)
  1771. ->where('cm_expected_payment_amount', '>', 0)
  1772. ->where('has_cm_been_paid', false)
  1773. ->where('is_signed_by_cm', true);
  1774. })
  1775. ->orWhere(function ($_query) {
  1776. $_query->where('rme_pro_id', $this->pro->id)
  1777. ->where('rme_expected_payment_amount', '>', 0)
  1778. ->where('has_rme_been_paid', false)
  1779. ->where('is_signed_by_rme', true);
  1780. })
  1781. ->orWhere(function ($_query) {
  1782. $_query->where('rmm_pro_id', $this->pro->id)
  1783. ->where('rmm_expected_payment_amount', '>', 0)
  1784. ->where('has_rmm_been_paid', false)
  1785. ->where('is_signed_by_rmm', true);
  1786. })
  1787. ->orWhere(function ($_query) {
  1788. $_query->where('generic_pro_id', $this->pro->id)
  1789. ->where('generic_pro_expected_payment_amount', '>', 0)
  1790. ->where('has_generic_pro_been_paid', false)
  1791. ->where('is_signed_by_generic_pro', true);
  1792. });
  1793. })
  1794. ->orderBy('created_at', 'DESC')
  1795. ->paginate();
  1796. return view('app.practice-management.bills-under-processing', compact('bills'));
  1797. }
  1798. public function careMonthReport(Request $request) {
  1799. $m = $request->input('m');
  1800. $y = $request->input('y');
  1801. if(!$m || !$y) {
  1802. $date = date('Y-m-01');
  1803. }
  1804. else {
  1805. $date = date('Y-m-d', strtotime("$m/1/$y"));
  1806. }
  1807. $pro = $this->pro;
  1808. $records = DB::select("
  1809. SELECT cm.id as care_month_id,
  1810. cm.uid as care_month_uid,
  1811. c.id as client_id,
  1812. c.uid as client_uid,
  1813. cm.number_of_days_with_remote_measurements,
  1814. cm.rm_total_time_in_seconds_by_mcp,
  1815. cm.rm_total_time_in_seconds_by_rme_pro,
  1816. cm.rm_total_time_in_seconds_by_rmm_pro,
  1817. cm.mcp_pro_id,
  1818. cm.rme_pro_id,
  1819. cm.rmm_pro_id,
  1820. (c.name_first || ' ' || c.name_last) as client_name
  1821. FROM care_month cm
  1822. JOIN client c on cm.client_id = c.id
  1823. WHERE cm.start_date = :startDate
  1824. AND (cm.mcp_pro_id = :proID OR
  1825. cm.rme_pro_id = :proID OR
  1826. cm.rmm_pro_id = :proID)
  1827. ORDER BY c.name_last, c.name_first
  1828. ",
  1829. ['startDate' => $date, 'proID' => $pro->id]
  1830. );
  1831. return view('app.practice-management.care-month-report', compact('records', 'date'));
  1832. }
  1833. public function myTeams(Request $request) {
  1834. $pro = $this->pro;
  1835. // get all teams where the authed-pro is the assistant pro
  1836. $teams = ProTeam::where('assistant_pro_id', $pro->id)
  1837. ->where('is_active', true)
  1838. ->orderBy('slug')
  1839. ->get();
  1840. return view('app.practice-management.my-teams', compact('teams'));
  1841. }
  1842. public function patientsAccountsInvites(Request $request){
  1843. $filters = $request->all();
  1844. $accountInvites = AccountInvite::select('account_invite.*')
  1845. ->join('client', 'client.id', '=', 'account_invite.for_client_id');
  1846. if($this->performer->pro->pro_type !== 'ADMIN'){
  1847. $accountInvites = $accountInvites->where('client.mcp_pro_id', $this->performer->pro->id);
  1848. }
  1849. $this->filterMultiQuery($request, $accountInvites, 'account_invite.created_at', 'date_category', 'date_value_1', 'date_value_2');
  1850. $this->filterSimpleQuery($request, $accountInvites, 'account_invite.status', 'status');
  1851. $accountInvites = $accountInvites->orderBy('created_at', 'DESC')->paginate(20);
  1852. return view('app.practice-management.patients-accounts-invites', compact('accountInvites','filters'));
  1853. }
  1854. public function clientsBdtDevices(Request $request){
  1855. $filters = $request->all();
  1856. $devices = ClientBDTDevice::select('client_bdt_device.*')
  1857. ->join('client', 'client.id', '=', 'client_bdt_device.client_id');
  1858. if($this->performer->pro->pro_type !== 'ADMIN'){
  1859. $devices = $devices->where('client.mcp_pro_id', $this->performer->pro->id);
  1860. }
  1861. $this->filterMultiQuery($request, $devices, 'client_bdt_device.created_at', 'date_category', 'date_value_1', 'date_value_2');
  1862. $status = $request->get('status');
  1863. if($status){
  1864. if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
  1865. if($status === 'DEACTIVATED') $devices = $devices->where('client_bdt_device.is_active', false);
  1866. }
  1867. $devices = $devices->orderBy('created_at', 'DESC')->paginate(20);
  1868. return view('app.practice-management.clients_bdt_devices', compact('devices','filters'));
  1869. }
  1870. public function memos(Request $request){
  1871. $filters = $request->all();
  1872. $memos = ClientMemo::select('client_memo.*')
  1873. ->join('client', 'client.id', '=', 'client_memo.client_id');
  1874. if($this->performer->pro->pro_type !== 'ADMIN'){
  1875. $memos = $memos->where('client.mcp_pro_id', $this->performer->pro->id);
  1876. }
  1877. $this->filterMultiQuery($request, $memos, 'client_memo.created_at', 'date_category', 'date_value_1', 'date_value_2');
  1878. $this->filterSimpleQuery($request, $memos, 'category', 'category');
  1879. $memos = $memos->orderBy('created_at', 'DESC')->paginate(20);
  1880. return view('app.practice-management.memos', compact('memos', 'filters'));
  1881. }
  1882. public function clientCcmRmStatus(Request $request){
  1883. $filters = $request->all();
  1884. $patients = Client::whereNull('shadow_pro_id');
  1885. // filters
  1886. /*
  1887. array:18 [▼
  1888. "age_category" => "LESS_THAN"
  1889. "age_value_1" => "34"
  1890. "age_value_2" => null
  1891. "sex" => "M"
  1892. "bmi_category" => "BETWEEN"
  1893. "bmi_value_1" => "20"
  1894. "bmi_value_2" => "25"
  1895. "last_visit_category" => "LESS_THAN"
  1896. "last_visit_value_1" => "2021-10-14"
  1897. "last_visit_value_2" => null
  1898. "next_appointment_category" => "LESS_THAN"
  1899. "next_appointment_value_1" => "2021-10-15"
  1900. "status" => "ACTIVE"
  1901. "last_weighed_in_category" => "EXACTLY"
  1902. "last_weighed_in_value_1" => "2021-10-07"
  1903. "last_bp_category" => "BETWEEN"
  1904. "last_bp_value_1" => "2021-10-01"
  1905. "last_bp_value_2" => "2021-10-31"
  1906. ]
  1907. */
  1908. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
  1909. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  1910. $this->filterMultiQuery($request, $patients, 'usual_bmi', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  1911. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  1912. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  1913. switch($request->input('status')) {
  1914. case 'ACTIVE':
  1915. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  1916. break;
  1917. case 'AWAITING_VISIT':
  1918. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  1919. break;
  1920. case 'INACTIVE':
  1921. $patients->where('is_active', '<>', true);
  1922. break;
  1923. }
  1924. if($request->input('is_eligible_for_cm')){
  1925. $patients->where('is_eligible_for_cm', '=', $request->input('is_eligible_for_cm'));
  1926. }
  1927. if($request->input('is_enrolled_in_cm')){
  1928. $patients->where('is_enrolled_in_cm', '=', $request->input('is_enrolled_in_cm'));
  1929. }
  1930. if($request->input('has_cm_setup_been_performed')){
  1931. $patients->where('has_cm_setup_been_performed', '=', $request->input('has_cm_setup_been_performed')=='YES'? true : false);
  1932. }
  1933. if($request->input('is_eligible_for_rm')){
  1934. $patients->where('is_eligible_for_rm', '=', $request->input('is_eligible_for_rm'));
  1935. }
  1936. if($request->input('is_enrolled_in_rm')){
  1937. $patients->where('is_enrolled_in_rm', '=', $request->input('is_enrolled_in_rm'));
  1938. }
  1939. if($request->input('has_rm_setup_been_performed')){
  1940. $patients->where('has_rm_setup_been_performed', '=', $request->input('has_rm_setup_been_performed') =='YES'? true : false);
  1941. }
  1942. $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
  1943. return view('app.admin.client-ccm-rm-status', compact('patients', 'filters'));
  1944. }
  1945. }