PracticeManagementController.php 88 KB

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