PracticeManagementController.php 86 KB

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