PracticeManagementController.php 88 KB

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